From bc00262ead4a1ccb1c0b95ed399f70c09727fdf7 Mon Sep 17 00:00:00 2001 From: thefish Date: Fri, 8 Nov 2019 23:51:00 +0300 Subject: [PATCH] scrollbar --- cmd/game/main.go | 30 +++++++++- engine/screens/game.go | 3 + engine/screens/menu.go | 126 ++++++++++++++++++++++++++++------------- 3 files changed, 118 insertions(+), 41 deletions(-) diff --git a/cmd/game/main.go b/cmd/game/main.go index 7c4b5f7..afb1500 100644 --- a/cmd/game/main.go +++ b/cmd/game/main.go @@ -106,7 +106,7 @@ func main() { SetBgColor("#ef1d494f"). SetFgColor("white"). SetItems([]interface{}{ - "hjklyubn 12346789 or arrow keys - move", + "hjklyubn, NumPad 12346789, arrow keys - move", "s or . - pass turn", "g or , - pick up item", "i - inventory", @@ -116,9 +116,35 @@ func main() { "z or Z - cast a spell", "p - pray", "Ctrl+p - message log", - }), + }).MakeList(), ) + screenMgr.AddScreen("inventory", screens.NewMenuScreen( + mw, + screenMgr, + "Inventory", + "Items in your backpack:", + //"[color=yellow]Note[/color]: Many of these are not implemented yet", + "", + types.NewCenteredRect(mw.Rect, 50, 15), + true, ). + SetBgColor("#ef305c70"). + SetFgColor("white"). + SetItems([]interface{}{ + "hjklyubn, NumPad 12346789, arrow keys - move", + "s or . - pass turn", + "g or , - pick up item", + "i - inventory", + "? - this screen", + "Ctrl+q - exit", + "f or F - fire or throw weapon", + "z or Z - cast a spell", + "p - pray", + "Ctrl+p - message log", + }).MakeList(), + ) + + screenMgr.SetScreenByName("title") //fixme diff --git a/engine/screens/game.go b/engine/screens/game.go index 49e9aa2..a8d674e 100644 --- a/engine/screens/game.go +++ b/engine/screens/game.go @@ -65,6 +65,9 @@ func (ts *GameScreen) HandleInput(input string) { case "Shift+/": ts.scm.SetScreenByName("help") break + case "i": + ts.scm.SetScreenByName("inventory") + break default: ts.mw.GetLayer("base").ClearArea(0, 3, 40, 1) ts.mw.GetLayer("base").Print(1, 3, "Key: "+input) diff --git a/engine/screens/menu.go b/engine/screens/menu.go index 257f1f1..4ac7909 100644 --- a/engine/screens/menu.go +++ b/engine/screens/menu.go @@ -1,6 +1,7 @@ package screens import ( + "fmt" "lab.zaar.be/thefish/alchemyst-go/engine/types" "lab.zaar.be/thefish/alchemyst-go/ui/mainwindow" blt "lab.zaar.be/thefish/bearlibterminal" @@ -16,7 +17,12 @@ type MenuScreen struct { scm *types.ScreenManager renderParent bool - items []interface{} + items []interface{} + offset int + + drawFunc func() + inputFunc func(string) + title string header string footer string @@ -37,6 +43,12 @@ func NewMenuScreen(mw *mainwindow.MainWindow, scm *types.ScreenManager, title, h } } +func (ms *MenuScreen) MakeList() *MenuScreen { + ms.drawFunc = ms.ListRender + ms.inputFunc = ms.ListHandleInput + return ms +} + func (ms *MenuScreen) SetBgColor(color string) *MenuScreen { ms.bgColor = color return ms @@ -58,21 +70,11 @@ func (ms *MenuScreen) UseEcs() bool { return false } func (ms *MenuScreen) Enter() { ms.redraw = true + ms.offset = 0 } func (ms *MenuScreen) HandleInput(input string) { - // - //if input != "" { - // ms.redraw = true - //} - - switch input { - case "Escape": - fallthrough - case "Space": - ms.scm.SetScreen(ms.scm.PreviousScreen) - break - } + ms.inputFunc(input) } func (ms *MenuScreen) Exit() { @@ -88,31 +90,77 @@ func (ms *MenuScreen) Render() { } if (ms.redraw || ms.renderParent) { ms.redraw = false - menuLayer := ms.mw.GetLayer("menu") - menuLayer.ClearRect(ms.Rect) - bgLayer := ms.mw.GetLayer("menubg") - bgLayer.ClearRect(ms.Rect) - bgLayer.WithColor(ms.bgColor).NewWindow(ms.Rect).Splash() - menuLayer.WithColor(ms.fgColor).NewWindow(ms.Rect).DoubleBordered(ms.title) - menuLayer.Print(ms.X+(ms.W/2)-7, ms.Y+ms.H-1, "╡"+"[color=green]Space[/color] to close"+"╞") - footerHeight := 0 - if ms.footer != "" { - _, footerHeight = menuLayer.PrintInside(ms.Rect, ms.footer, 9) - footerHeight = footerHeight + 2 - } - _, headerHeight := menuLayer.PrintInside(ms.Rect, ms.header, blt.TK_ALIGN_LEFT) - itemField := types.Rect{ms.X, ms.Y + headerHeight + 1, ms.W, ms.H - headerHeight - footerHeight} - _ = itemField - if (len(ms.items) > 0) { - //fixme itemfield object, scroller, inputhandler, current selected item - menuItems := make([]string, 0) - for i, _ := range ms.items { - if string(ms.items[i].(string)) != "" { - menuItems = append(menuItems, ms.items[i].(string)) - } - } - menuLayer.PrintInside(&itemField, strings.Join(menuItems, "\n"), blt.TK_ALIGN_LEFT) - - } + ms.drawFunc() } } + +func (ms *MenuScreen) ListHandleInput(input string) { + switch input { + case "Up": + ms.offset = ms.offset - 1 + if ms.offset < 0 { + ms.offset = 0 + } + break + case "Down": + ms.offset = ms.offset + 1 + if ms.offset > len(ms.items)-1 { + ms.offset = len(ms.items) - 1 + } + break + case "Escape": + fallthrough + case "Space": + ms.scm.SetScreen(ms.scm.PreviousScreen) + break + } +} + +func (ms *MenuScreen) ListRender() { + menuLayer := ms.mw.GetLayer("menu") + menuLayer.ClearRect(ms.Rect) + bgLayer := ms.mw.GetLayer("menubg") + bgLayer.ClearRect(ms.Rect) + bgLayer.WithColor(ms.bgColor).NewWindow(ms.Rect).Splash() + menuLayer.WithColor(ms.fgColor).NewWindow(ms.Rect).DoubleBordered(ms.title) + menuLayer.Print(ms.X+(ms.W/2)-7, ms.Y+ms.H-1, "╡"+"[color=green]Space[/color] to close"+"╞") + footerHeight := 0 + if ms.footer != "" { + _, footerHeight = menuLayer.PrintInside(ms.Rect, ms.footer, 9) + footerHeight = footerHeight + 2 + } + _, headerHeight := menuLayer.PrintInside(ms.Rect, ms.header, blt.TK_ALIGN_LEFT) + itemField := types.Rect{ms.X, ms.Y + headerHeight + 1, ms.W, ms.H - headerHeight - footerHeight} + _ = itemField + var ilw, ilh int + if (len(ms.items) > 0) { + //fixme itemfield object, scroller, inputhandler, current selected item + menuItems := make([]string, 0) + for i := ms.offset; i < len(ms.items); i++ { + if string(ms.items[i].(string)) != "" { + menuItems = append(menuItems, ms.items[i].(string)) + } + } + ilw, ilh = menuLayer.PrintInside(&itemField, strings.Join(menuItems, "\n"), blt.TK_ALIGN_LEFT) + } + if ilh < len(ms.items) { + ms.drawScrollBar(menuLayer, itemField) + } + if ilw > itemField.W-4 { + fmt.Printf("Excess width of item names found! Need h-scroll of certain names") + } +} + +func (ms *MenuScreen) drawScrollBar(menuLayer *mainwindow.Layer, itemField types.Rect) { + scrollbarBg := types.NewRect(itemField.X+itemField.W-2, itemField.Y + 1, 1, itemField.H - 4) + menuLayer.WithColor("#77000000").NewWindow(scrollbarBg).Splash() + //tick + menuLayer.WithColor(ms.fgColor).Put( + scrollbarBg.X, + scrollbarBg.Y + int(float64(ms.offset) / float64(len(ms.items)) * float64(scrollbarBg.H)), + "⏹", + ) + menuLayer.WithColor(ms.fgColor).Put(itemField.X+itemField.W-2, itemField.Y+scrollbarBg.H + 1, "↓") + menuLayer.WithColor(ms.fgColor).Put(itemField.X+itemField.W-2, itemField.Y, "↑") + +}