aboutsummaryrefslogtreecommitdiff
path: root/go/ui/choice.go
blob: 2020ed0a9e133d6664c53445feeee624ecde4775 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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)
}