add things

This commit is contained in:
anton.gurov
2019-10-23 19:03:09 +03:00
parent ed22f7a37e
commit 8126e68141
12 changed files with 302 additions and 74 deletions

161
ui/mainwindow/layer.go Normal file
View File

@ -0,0 +1,161 @@
package mainwindow
import blt "lab.zaar.be/thefish/bearlibterminal"
var emptyCorners = [4]uint32{0,0,0,0}
type LayerInterface interface {
Render()
Put(x, y int, symbol rune, color string)
}
type LayerInterfaceImpl struct {
idx int
defaultColor uint32
}
func AddLayer(idx int, colorName string) LayerInterfaceImpl {
c := blt.ColorFromName(colorName)
return LayerInterfaceImpl{idx: idx, defaultColor: c}
}
func (lii *LayerInterfaceImpl) before() *LayerInterfaceImpl {
blt.Layer(lii.idx)
return lii
}
func (lii *LayerInterfaceImpl) WithColor(colorName string) *LayerInterfaceImpl {
lii.before()
c := blt.ColorFromName(colorName)
blt.Color(c)
return lii
}
func (lii *LayerInterfaceImpl) after() *LayerInterfaceImpl {
blt.Color(lii.defaultColor)
blt.Layer(0)
return lii
}
func (lii LayerInterfaceImpl) Put(x,y int, symbol string) {
rnes := []rune(symbol)
if (len(rnes)) > 0 {
blt.Put(x, y, int(rnes[0]))
}
}
func (lii LayerInterfaceImpl) Print(x,y int, txt string) (w,h int) {
return blt.Print(x,y, txt)
}
type Rect struct {
x,y,w,h int
layer *LayerInterfaceImpl
}
type rectFill struct {
top, bottom, left, right, topLeft, topRight, bottomLeft, bottomRight, body string
}
func (lii *LayerInterfaceImpl) NewRect(x,y,w,h int) *Rect {
return &Rect{x,y,w,h, lii}
}
var noborder = rectFill{
top: "▄",
bottom: "▀",
left: "▐",
right: "▌",
topLeft: "▗",
topRight: "▖",
bottomLeft: "▝",
bottomRight: "▘",
body: "█",
}
var splash = rectFill{
top: "█",
bottom: "█",
left: "█",
right: "█",
topLeft: "█",
topRight: "█",
bottomLeft: "█",
bottomRight: "█",
body: "█",
}
var doubleBorder = rectFill {
top: "═",
bottom: "═",
left: "║",
right: "║",
topLeft: "╔",
topRight: "╗",
bottomLeft: "╚",
bottomRight: "╝",
}
func (r *Rect) Fill() {
r.render(noborder)
}
func (r *Rect) Splash() {
r.render(splash)
}
func (r *Rect) DrawBorder() {
r.render(doubleBorder)
}
func (r *Rect) render (fillage rectFill) {
if fillage.body != "" {
for i := r.x + 1; i < r.x+r.w; i++ {
for j := r.y + 1; j < r.y+r.h; j++ {
r.layer.Put(i, j, fillage.body);
//lii.Put(i, j, "X");
}
}
}
for i := r.x + 1; i < r.x+r.w; i++ {
r.layer.Put(i, r.y, fillage.top);
//lii.Put(i, y-1, "Q");
r.layer.Put(i, r.y+r.h, fillage.bottom);
//lii.Put(i, y+h, "H");
}
for j := r.y + 1; j < r.y+r.h; j++ {
r.layer.Put(r.x, j, fillage.left);
//lii.Put(x-1, j, "U");
r.layer.Put(r.x+r.w, j, fillage.right);
//lii.Put(x+w, j, "M");
}
r.layer.Put(r.x, r.y, fillage.topLeft);
//lii.Put(x-1, y-1, "T");
r.layer.Put(r.x, r.y+r.h, fillage.bottomLeft);
//lii.Put(x-1, y+h, "q");
r.layer.Put(r.x+r.w, r.y, fillage.topRight);
//lii.Put(x+w, y-1, "L");
r.layer.Put(r.x+r.w, r.y+r.h, fillage.bottomRight);
};
func (lii LayerInterfaceImpl) DrawWindow (title string, x, y, w, h int) {
if len(title) > (w -2) {
title = title[:(w-2)]
}
lii.NewRect(x,y,w,h).DrawBorder()
centerX := x + (w / 2)
lii.Print(centerX - (len(title) / 2) - 1, y, "╡" + title + "╞")
};
func (lii *LayerInterfaceImpl) Decorate (f func (args ...interface{})) func (args ...interface{}) {
return func (args ...interface{}) {
lii.before()
f(args)
lii.after()
}
}

View File

@ -6,22 +6,46 @@ import (
blt "lab.zaar.be/thefish/bearlibterminal"
)
func Init(ctx util.ClientCtx) {
type MainWindow struct {
ctx util.ClientCtx
layers []LayerInterface
}
func Init(ctx util.ClientCtx) *MainWindow {
ctx.Logger().Info().Msgf("Opening main window...")
config := ctx.Config()
mw := MainWindow{ctx: ctx}
mw.Open()
return &mw
}
func (mw *MainWindow) Open() {
config := mw.ctx.Config()
blt.Open()
//blt.Set("window: size=80x25, title="+config.Title+" v"+string(version)+"; font: ./fonts/Monaco-Linux.ttf, size=10")
blt.Set(
fmt.Sprintf("window: size=%dx%d, title=%s v%s; font: ./resources/fonts-bitmap/ibmnew8x12.png, size=8x12;",
fmt.Sprintf(
//"window: size=%dx%d, title='%s v%s'; font: ./resources/fonts-bitmap/ibmnew8x12.png, size=8x12;",
"window: size=%dx%d, title='%s v%s'; font: %s, size=8x16;",
config.MainWindowSizeX,
config.MainWindowSizeY,
config.Title,
config.Version,
config.Font,
),
)
}
func Shutdown(ctx util.ClientCtx) {
ctx.Logger().Info().Msg("Closing main window...")
func (mw *MainWindow) Close() {
mw.ctx.Logger().Info().Msg("Closing main window...")
blt.Close()
}
func (mw *MainWindow) Render() {
for _, l := range mw.layers {
l.Render()
}
}
func (mw *MainWindow) AddLayer(li LayerInterface) {
mw.layers = append(mw.layers, li)
}