aboutsummaryrefslogtreecommitdiff
path: root/go/activities/buttonList.go
blob: b715755dd2b8ed3f0c054d1d6e4400bac1c2f302 (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
69
package activities

import (
	"log"

	"muhq.space/muhqs-game/go/ui"
)

// ButtonList is an Activity that tries to fill its vertical space with buttons
// for the specified click handlers.
// A last back button is appended calling activities.PopActivity()
type ButtonList struct {
	ui.Collection
}

const (
	// TODO: determine required with for the displayed labels
	BUTTON_LIST_BUTTON_WIDTH   = 450
	BUTTON_LIST_BUTTON_HEIGHT  = 75
	BUTTON_LIST_BUTTON_PADDING = 40
)

func NewButtonList(width, height int, labels []string, handlers []func(*ui.SimpleButton)) *ButtonList {
	bl := &ButtonList{ui.Collection{Width: width, Height: height}}

	if len(labels) != len(handlers) {
		log.Panic("labels and handler mismatch")
	}

	x := (width - BUTTON_LIST_BUTTON_WIDTH) / 2
	y := BUTTON_LIST_BUTTON_HEIGHT
	for i, handler := range handlers {
		btn := ui.NewSimpleButton(
			x,
			y,
			BUTTON_LIST_BUTTON_WIDTH,
			BUTTON_LIST_BUTTON_HEIGHT,
			labels[i],
			handler,
		)
		bl.AddWidget(btn)

		y = y + BUTTON_LIST_BUTTON_HEIGHT + BUTTON_LIST_BUTTON_PADDING
	}

	back := ui.NewSimpleButton(
		x,
		y,
		BUTTON_LIST_BUTTON_WIDTH,
		BUTTON_LIST_BUTTON_HEIGHT,
		"back",
		func(*ui.SimpleButton) { PopActivity() },
	)
	log.Println("add back button at", x, y)
	bl.AddWidget(back)

	return bl
}

func (bl *ButtonList) Layout(int, int) (int, int) {
	return bl.Width, bl.Height
}

func (bl *ButtonList) Update() error {
	if err := ui.Update(); err != nil {
		return err
	}
	return bl.Collection.Update()
}