aboutsummaryrefslogtreecommitdiff
path: root/go/ui/choice.go
diff options
context:
space:
mode:
Diffstat (limited to 'go/ui/choice.go')
-rw-r--r--go/ui/choice.go68
1 files changed, 68 insertions, 0 deletions
diff --git a/go/ui/choice.go b/go/ui/choice.go
new file mode 100644
index 00000000..9e9b7b2f
--- /dev/null
+++ b/go/ui/choice.go
@@ -0,0 +1,68 @@
+package ui
+
+import (
+ "github.com/hajimehoshi/ebiten/v2"
+ "muhq.space/muhqs-game/go/game"
+)
+
+type Choice struct {
+ X, Y int
+ Width, choiceHeight int
+ choices []string
+ onClick func(*Choice, int, int)
+ buttons []Button
+}
+
+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,
+ choiceButtonOnClickDummy, choice)
+
+ c.buttons = append(c.buttons, b)
+ }
+ return c
+}
+
+func (c *Choice) Draw(screen *ebiten.Image) {
+ for _, b := range c.buttons {
+ b.Draw(screen)
+ }
+}
+
+func (c *Choice) GetChoosen(x, y int) int {
+ return (y - c.Y) / c.choiceHeight
+}
+
+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{} {
+ if c.Contains(x, y) {
+ return c
+ }
+ return nil
+}
+
+func NewActionChoice(x, y int, actions []game.ActionPrototype, onClick func(*Choice, int, int)) *Choice {
+ // TODO: use dynamic with width and height
+ width := 200
+ choiceHeight := 40
+ labels := make([]string, 0, len(actions))
+ for _, action := range actions {
+ labels = append(labels, action.String())
+ }
+
+ return NewChoice(x, y, width, choiceHeight, labels, onClick)
+}