anton.gurov 56f3447666 diverge
2019-10-25 11:27:47 +03:00

65 lines
1.3 KiB
Go

package mainwindow
import blt "lab.zaar.be/thefish/bearlibterminal"
type LayerInterface interface {
Render()
Put(x, y int, symbol rune, color string)
}
type Layer struct {
idx int
defaultColor uint32
}
func AddLayer(idx int, colorName string) Layer {
c := blt.ColorFromName(colorName)
return Layer{idx: idx, defaultColor: c}
}
func (layer *Layer) before() *Layer {
blt.Layer(layer.idx)
return layer
}
func (layer *Layer) WithColor(colorName string) *Layer {
layer.before()
c := blt.ColorFromName(colorName)
blt.Color(c)
return layer
}
func (layer *Layer) after() *Layer {
blt.Color(layer.defaultColor)
blt.Layer(0)
return layer
}
func (layer Layer) Put(x,y int, symbol string) {
rnes := []rune(symbol)
if (len(rnes)) > 0 {
blt.Put(x, y, int(rnes[0]))
}
}
func (layer Layer) Print(x,y int, txt string) (w,h int) {
return blt.Print(x,y, txt)
}
func (layer Layer) DrawWindow (title string, x, y, w, h int) {
if len(title) > (w -2) {
title = title[:(w-2)]
}
layer.NewRect(x,y,w,h).DrawBorder()
centerX := x + (w / 2)
layer.Print(centerX - (len(title) / 2) - 1, y, "╡" + title + "╞")
};
func (layer *Layer) Decorate (f func (args ...interface{})) func (args ...interface{}) {
return func (args ...interface{}) {
layer.before()
f(args)
layer.after()
}
}