aboutsummaryrefslogtreecommitdiff
path: root/go/ui
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2023-02-17 00:29:26 +0100
committerFlorian Fischer <florian.fischer@muhq.space>2025-08-20 15:50:11 +0200
commit28defc1cad1a96a2954780efa01396db8bb2fe31 (patch)
treeb6bf6e43d48a7bc7f0ccce7d6f4253e1e15e6492 /go/ui
parent8f81177d191b3e6d80212e1fa370d37db2fe38ce (diff)
downloadmuhqs-game-28defc1cad1a96a2954780efa01396db8bb2fe31.tar.gz
muhqs-game-28defc1cad1a96a2954780efa01396db8bb2fe31.zip
implement unit and artifact switch action
Simplify the Choice Widget and make it a TextBox variant. Drive-by fixes: * ui.Collection: prioritize newer widgets during active widget selection * game.FullAction: fix incorrect cost function. Full actions cost all available actions. Also fix the cost check
Diffstat (limited to 'go/ui')
-rw-r--r--go/ui/choice.go62
-rw-r--r--go/ui/collection.go5
2 files changed, 27 insertions, 40 deletions
diff --git a/go/ui/choice.go b/go/ui/choice.go
index 28878604..04b8f725 100644
--- a/go/ui/choice.go
+++ b/go/ui/choice.go
@@ -9,66 +9,50 @@ import (
)
type Choice struct {
- X, Y int
- Width, choiceHeight int
- choices []string
- onClick func(*Choice, int, int)
- buttons []Button
+ TextBox
+ choices []interface{}
+ onClick func(*Choice, int, int)
}
func choiceButtonOnClickDummy(b *SimpleButton) {}
-func NewChoice(x, y int, width, choiceHeight int, choices []string,
- onClick func(*Choice, int, int),
-) *Choice {
- c := &Choice{x, y, width, choiceHeight, choices, onClick, []Button{}}
-
- for i, choice := range c.choices {
- b := NewSimpleButton(c.X, c.Y+i*c.choiceHeight, c.Width, c.choiceHeight, choice,
- choiceButtonOnClickDummy)
+func NewChoice(x, y int, choices []interface{}, onClick func(*Choice, int, int)) *Choice {
+ c := &Choice{
+ TextBox: *(NewAutoTextBox(x, y, "")),
+ choices: choices,
+ onClick: onClick,
+ }
- c.buttons = append(c.buttons, b)
+ c.renderImpl = func() *ebiten.Image {
+ c.setText()
+ return c.render()
}
return c
}
-func (c *Choice) Draw(screen *ebiten.Image) {
- for _, b := range c.buttons {
- b.Draw(screen)
+func (c *Choice) setText() {
+ c.Width, c.Height = -1, -1
+ t := ""
+ for _, choice := range c.choices {
+ t = fmt.Sprintf("%s%v\n", t, choice)
}
+
+ c.text = t[:len(t)-1]
}
func (c *Choice) GetChoosen(x, y int) int {
- return (y - c.Y) / c.choiceHeight
+ i := (y - c.Y) / (c.Height / len(c.choices))
+ return i
}
func (c *Choice) Click(x, y int) {
c.onClick(c, x, y)
}
-func (c *Choice) Contains(x, y int) bool {
- height := len(c.choices) * c.choiceHeight
- return x >= c.X && x <= c.X+c.Width && y >= c.Y && y <= c.Y+height
-}
-
-func (c *Choice) FindObjectAt(x, y int) interface{} { return nil }
-func (c *Choice) Render() *ebiten.Image { return nil }
-
-func NewObjectChoice(x, y int, objs []interface{}, onClick func(*Choice, int, int)) *Choice {
- // TODO: use dynamic with width and height
- width := 200
- choiceHeight := 40
- labels := make([]string, 0, len(objs))
- for _, obj := range objs {
- labels = append(labels, fmt.Sprintf("%v", obj))
- }
- return NewChoice(x, y, width, choiceHeight, labels, onClick)
-}
-
func NewActionChoice(x, y int, actions []game.Action, onClick func(*Choice, int, int)) *Choice {
- return NewObjectChoice(x, y, utils.TypedSliceToInterfaceSlice(actions), onClick)
+ return NewChoice(x, y, utils.TypedSliceToInterfaceSlice(actions), onClick)
}
func NewPermChoice(x, y int, perms []game.Permanent, onClick func(*Choice, int, int)) *Choice {
- return NewObjectChoice(x, y, utils.TypedSliceToInterfaceSlice(perms), onClick)
+ return NewChoice(x, y, utils.TypedSliceToInterfaceSlice(perms), onClick)
}
diff --git a/go/ui/collection.go b/go/ui/collection.go
index d75e3267..027d0d99 100644
--- a/go/ui/collection.go
+++ b/go/ui/collection.go
@@ -69,7 +69,10 @@ func (c *Collection) Update() error {
x, y := ebiten.CursorPosition()
var activeWidget Widget
- for _, w := range c.widgets {
+ // Iterate the widget in reverse order to ensure that upper widgets are
+ // considered first
+ for i := len(c.widgets) - 1; i >= 0; i-- {
+ w := c.widgets[i]
if !w.Contains(x, y) {
continue
}