This commit is contained in:
thefish 2022-10-12 16:25:50 +03:00
parent 3886a05ce2
commit 31d5be5d07
2 changed files with 113 additions and 113 deletions

View File

@ -69,7 +69,7 @@ func (devm *DevmenuScreen) HandleInput(input string) {
level.Tiles[idx].Visible = true level.Tiles[idx].Visible = true
level.Tiles[idx].Explored = true level.Tiles[idx].Explored = true
} }
appctx.Logger(devm.ctx).Warn().Msg("making everything visible!") appctx.Logger().Warn().Msg("making everything visible!")
devm.scm.SetScreen(devm.scm.PreviousScreen) devm.scm.SetScreen(devm.scm.PreviousScreen)
break break
case "p": case "p":

View File

@ -1,166 +1,166 @@
package screens package screens
import ( import (
"fmt" "fmt"
"lab.zaar.be/thefish/alchemyst-go/engine/types" "lab.zaar.be/thefish/alchemyst-go/engine/types"
"lab.zaar.be/thefish/alchemyst-go/ui/mainwindow" "lab.zaar.be/thefish/alchemyst-go/ui/mainwindow"
blt "lab.zaar.be/thefish/bearlibterminal" blt "lab.zaar.be/thefish/bearlibterminal"
"strings" "strings"
) )
const runeIndex = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" const runeIndex = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
type MenuScreen struct { type MenuScreen struct {
types.Rect types.Rect
mw *mainwindow.MainWindow mw *mainwindow.MainWindow
scm *types.ScreenManager scm *types.ScreenManager
renderParent bool renderParent bool
items []interface{} items []interface{}
offset int offset int
drawFunc func() drawFunc func()
inputFunc func(string) inputFunc func(string)
title string title string
header string header string
footer string footer string
redraw bool redraw bool
bgColor string bgColor string
fgColor string fgColor string
} }
func NewMenuScreen(mw *mainwindow.MainWindow, scm *types.ScreenManager, title, header, footer string, rect types.Rect, renderParent bool) *MenuScreen { func NewMenuScreen(mw *mainwindow.MainWindow, scm *types.ScreenManager, title, header, footer string, rect types.Rect, renderParent bool) *MenuScreen {
return &MenuScreen{ return &MenuScreen{
title: title, title: title,
header: header, header: header,
footer: footer, footer: footer,
Rect: rect, Rect: rect,
mw: mw, mw: mw,
scm: scm, scm: scm,
renderParent: renderParent, renderParent: renderParent,
} }
} }
func (ms *MenuScreen) MakeList() *MenuScreen { func (ms *MenuScreen) MakeList() *MenuScreen {
ms.drawFunc = ms.ListRender ms.drawFunc = ms.ListRender
ms.inputFunc = ms.ListHandleInput ms.inputFunc = ms.ListHandleInput
return ms return ms
} }
func (ms *MenuScreen) SetBgColor(color string) *MenuScreen { func (ms *MenuScreen) SetBgColor(color string) *MenuScreen {
ms.bgColor = color ms.bgColor = color
return ms return ms
} }
func (ms *MenuScreen) SetFgColor(color string) *MenuScreen { func (ms *MenuScreen) SetFgColor(color string) *MenuScreen {
ms.fgColor = color ms.fgColor = color
return ms return ms
} }
//fixme!! //fixme!!
func (ms *MenuScreen) SetItems(items []interface{}) *MenuScreen { func (ms *MenuScreen) SetItems(items []interface{}) *MenuScreen {
ms.items = items ms.items = items
return ms return ms
} }
func (ms *MenuScreen) UseEcs() bool { return false } func (ms *MenuScreen) UseEcs() bool { return false }
func (ms *MenuScreen) Enter() { func (ms *MenuScreen) Enter() {
ms.redraw = true ms.redraw = true
ms.offset = 0 ms.offset = 0
} }
func (ms *MenuScreen) HandleInput(input string) { func (ms *MenuScreen) HandleInput(input string) {
ms.inputFunc(input) ms.inputFunc(input)
} }
func (ms *MenuScreen) Exit() { func (ms *MenuScreen) Exit() {
menuLayer := ms.mw.GetLayer("menu") menuLayer := ms.mw.GetLayer("menu")
menuLayer.ClearRect(ms.Rect) menuLayer.ClearRect(ms.Rect)
bgLayer := ms.mw.GetLayer("menubg") bgLayer := ms.mw.GetLayer("menubg")
bgLayer.ClearRect(ms.Rect) bgLayer.ClearRect(ms.Rect)
} }
func (ms *MenuScreen) Render() { func (ms *MenuScreen) Render() {
if ms.renderParent { if ms.renderParent {
ms.scm.PreviousScreen.Render() ms.scm.PreviousScreen.Render()
} }
if (ms.redraw || ms.renderParent) { if ms.redraw || ms.renderParent {
ms.redraw = false ms.redraw = false
ms.drawFunc() ms.drawFunc()
} }
} }
func (ms *MenuScreen) ListHandleInput(input string) { func (ms *MenuScreen) ListHandleInput(input string) {
switch input { switch input {
case "Up": case "Up", "k":
ms.offset = ms.offset - 1 ms.offset = ms.offset - 1
if ms.offset < 0 { if ms.offset < 0 {
ms.offset = 0 ms.offset = 0
} }
break break
case "Down": case "Down", "j":
ms.offset = ms.offset + 1 ms.offset = ms.offset + 1
if ms.offset > len(ms.items)-1 { if ms.offset > len(ms.items)-1 {
ms.offset = len(ms.items) - 1 ms.offset = len(ms.items) - 1
} }
break break
case "Escape": case "Escape":
fallthrough fallthrough
case "Space": case "Space":
ms.scm.SetScreen(ms.scm.PreviousScreen) ms.scm.SetScreen(ms.scm.PreviousScreen)
break break
} }
} }
func (ms *MenuScreen) ListRender() { func (ms *MenuScreen) ListRender() {
menuLayer := ms.mw.GetLayer("menu") menuLayer := ms.mw.GetLayer("menu")
menuLayer.ClearRect(ms.Rect) menuLayer.ClearRect(ms.Rect)
bgLayer := ms.mw.GetLayer("menubg") bgLayer := ms.mw.GetLayer("menubg")
bgLayer.ClearRect(ms.Rect) bgLayer.ClearRect(ms.Rect)
bgLayer.WithColor(ms.bgColor).NewWindow(ms.Rect).Splash() bgLayer.WithColor(ms.bgColor).NewWindow(ms.Rect).Splash()
menuLayer.WithColor(ms.fgColor).NewWindow(ms.Rect).DoubleBordered(ms.title) 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"+"╞") menuLayer.Print(ms.X+(ms.W/2)-7, ms.Y+ms.H-1, "╡"+"[color=green]Space[/color] to close"+"╞")
footerHeight := 0 footerHeight := 0
if ms.footer != "" { if ms.footer != "" {
_, footerHeight = menuLayer.PrintInside(ms.Rect, ms.footer, 9) _, footerHeight = menuLayer.PrintInside(ms.Rect, ms.footer, 9)
footerHeight = footerHeight + 2 footerHeight = footerHeight + 2
} }
_, headerHeight := menuLayer.PrintInside(ms.Rect, ms.header, blt.TK_ALIGN_LEFT) _, 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 := types.Rect{ms.X, ms.Y + headerHeight + 1, ms.W, ms.H - headerHeight - footerHeight}
_ = itemField _ = itemField
var ilw, ilh int var ilw, ilh int
if (len(ms.items) > 0) { if len(ms.items) > 0 {
//fixme itemfield object, scroller, inputhandler, current selected item //fixme itemfield object, scroller, inputhandler, current selected item
menuItems := make([]string, 0) menuItems := make([]string, 0)
for i := ms.offset; i < len(ms.items); i++ { for i := ms.offset; i < len(ms.items); i++ {
if string(ms.items[i].(string)) != "" { if string(ms.items[i].(string)) != "" {
menuItems = append(menuItems, ms.items[i].(string)) menuItems = append(menuItems, ms.items[i].(string))
} }
} }
ilw, ilh = menuLayer.PrintInside(itemField, strings.Join(menuItems, "\n"), blt.TK_ALIGN_LEFT) ilw, ilh = menuLayer.PrintInside(itemField, strings.Join(menuItems, "\n"), blt.TK_ALIGN_LEFT)
} }
if ilh < len(ms.items) { if ilh < len(ms.items) {
ms.drawScrollBar(menuLayer, itemField) ms.drawScrollBar(menuLayer, itemField)
} }
if ilw > itemField.W-4 { if ilw > itemField.W-4 {
fmt.Printf("Excess width of item names found! Need h-scroll of certain names") fmt.Printf("Excess width of item names found! Need h-scroll of certain names")
} }
} }
func (ms *MenuScreen) drawScrollBar(menuLayer *mainwindow.Layer, itemField types.Rect) { 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) scrollbarBg := types.NewRect(itemField.X+itemField.W-2, itemField.Y+1, 1, itemField.H-4)
menuLayer.WithColor("#77000000").NewWindow(scrollbarBg).Splash() menuLayer.WithColor("#77000000").NewWindow(scrollbarBg).Splash()
//tick //tick
menuLayer.WithColor(ms.fgColor).Put( menuLayer.WithColor(ms.fgColor).Put(
scrollbarBg.X, scrollbarBg.X,
scrollbarBg.Y + int(float64(ms.offset) / float64(len(ms.items)) * float64(scrollbarBg.H)), 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+scrollbarBg.H+1, "↓")
menuLayer.WithColor(ms.fgColor).Put(itemField.X+itemField.W-2, itemField.Y, "↑") menuLayer.WithColor(ms.fgColor).Put(itemField.X+itemField.W-2, itemField.Y, "↑")
} }