scrollbar
This commit is contained in:
parent
4ba69bfe75
commit
bc00262ead
@ -106,7 +106,7 @@ func main() {
|
|||||||
SetBgColor("#ef1d494f").
|
SetBgColor("#ef1d494f").
|
||||||
SetFgColor("white").
|
SetFgColor("white").
|
||||||
SetItems([]interface{}{
|
SetItems([]interface{}{
|
||||||
"hjklyubn 12346789 or arrow keys - move",
|
"hjklyubn, NumPad 12346789, arrow keys - move",
|
||||||
"s or . - pass turn",
|
"s or . - pass turn",
|
||||||
"g or , - pick up item",
|
"g or , - pick up item",
|
||||||
"i - inventory",
|
"i - inventory",
|
||||||
@ -116,9 +116,35 @@ func main() {
|
|||||||
"z or Z - cast a spell",
|
"z or Z - cast a spell",
|
||||||
"p - pray",
|
"p - pray",
|
||||||
"Ctrl+p - message log",
|
"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")
|
screenMgr.SetScreenByName("title")
|
||||||
|
|
||||||
//fixme
|
//fixme
|
||||||
|
@ -65,6 +65,9 @@ func (ts *GameScreen) HandleInput(input string) {
|
|||||||
case "Shift+/":
|
case "Shift+/":
|
||||||
ts.scm.SetScreenByName("help")
|
ts.scm.SetScreenByName("help")
|
||||||
break
|
break
|
||||||
|
case "i":
|
||||||
|
ts.scm.SetScreenByName("inventory")
|
||||||
|
break
|
||||||
default:
|
default:
|
||||||
ts.mw.GetLayer("base").ClearArea(0, 3, 40, 1)
|
ts.mw.GetLayer("base").ClearArea(0, 3, 40, 1)
|
||||||
ts.mw.GetLayer("base").Print(1, 3, "Key: "+input)
|
ts.mw.GetLayer("base").Print(1, 3, "Key: "+input)
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package screens
|
package screens
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"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"
|
||||||
@ -16,7 +17,12 @@ type MenuScreen struct {
|
|||||||
scm *types.ScreenManager
|
scm *types.ScreenManager
|
||||||
renderParent bool
|
renderParent bool
|
||||||
|
|
||||||
items []interface{}
|
items []interface{}
|
||||||
|
offset int
|
||||||
|
|
||||||
|
drawFunc func()
|
||||||
|
inputFunc func(string)
|
||||||
|
|
||||||
title string
|
title string
|
||||||
header string
|
header string
|
||||||
footer 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 {
|
func (ms *MenuScreen) SetBgColor(color string) *MenuScreen {
|
||||||
ms.bgColor = color
|
ms.bgColor = color
|
||||||
return ms
|
return ms
|
||||||
@ -58,21 +70,11 @@ func (ms *MenuScreen) UseEcs() bool { return false }
|
|||||||
|
|
||||||
func (ms *MenuScreen) Enter() {
|
func (ms *MenuScreen) Enter() {
|
||||||
ms.redraw = true
|
ms.redraw = true
|
||||||
|
ms.offset = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ms *MenuScreen) HandleInput(input string) {
|
func (ms *MenuScreen) HandleInput(input string) {
|
||||||
//
|
ms.inputFunc(input)
|
||||||
//if input != "" {
|
|
||||||
// ms.redraw = true
|
|
||||||
//}
|
|
||||||
|
|
||||||
switch input {
|
|
||||||
case "Escape":
|
|
||||||
fallthrough
|
|
||||||
case "Space":
|
|
||||||
ms.scm.SetScreen(ms.scm.PreviousScreen)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ms *MenuScreen) Exit() {
|
func (ms *MenuScreen) Exit() {
|
||||||
@ -88,31 +90,77 @@ func (ms *MenuScreen) Render() {
|
|||||||
}
|
}
|
||||||
if (ms.redraw || ms.renderParent) {
|
if (ms.redraw || ms.renderParent) {
|
||||||
ms.redraw = false
|
ms.redraw = false
|
||||||
menuLayer := ms.mw.GetLayer("menu")
|
ms.drawFunc()
|
||||||
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)
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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, "↑")
|
||||||
|
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user