aboutsummaryrefslogtreecommitdiff
path: root/go/ui/choice.go
blob: 28f03f726c62342e287c86d6a4c81830ecdbed17 (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
package ui

import (
	"fmt"

	"github.com/hajimehoshi/ebiten/v2"
	"muhq.space/muhqs-game/go/game"
	"muhq.space/muhqs-game/go/utils"
)

type Choice struct {
	TextBox
	choices []interface{}
	onClick func(*Choice, int, int)
}

func NewChoice(x, y int, choices []interface{}, onClick func(*Choice, int, int)) *Choice {
	c := &Choice{
		TextBox: *(NewAutoTextBox(x, y, "")),
		choices: choices,
		onClick: onClick,
	}

	c.renderImpl = func() *ebiten.Image {
		c.setText()
		return c.render()
	}
	return c
}

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 {
	i := (y - c.Y) / (c.Height / len(c.choices))
	return i
}

func (c *Choice) Click(x, y int) {
	c.onClick(c, x, y)
}

func NewActionChoice(x, y int, actions []game.Action, onClick func(*Choice, int, int)) *Choice {
	return NewChoice(x, y, utils.TypedSliceToInterfaceSlice(actions), onClick)
}

func NewPermChoice(x, y int, perms []game.Permanent, onClick func(*Choice, int, int)) *Choice {
	return NewChoice(x, y, utils.TypedSliceToInterfaceSlice(perms), onClick)
}