aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--go/activities/buttonList.go69
-rw-r--r--go/client/challenges.go75
-rw-r--r--go/client/startMenu.go2
3 files changed, 97 insertions, 49 deletions
diff --git a/go/activities/buttonList.go b/go/activities/buttonList.go
new file mode 100644
index 00000000..55e5d0de
--- /dev/null
+++ b/go/activities/buttonList.go
@@ -0,0 +1,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 (
+ BUTTON_LIST_BUTTON_WIDTH = 250
+ BUTTON_LIST_BUTTON_HEIGHT = 75
+ BUTTON_LIST_BUTTON_PADDING = 40
+)
+
+func NewButtonList(width, height int, labels []string, handlers []func()) *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],
+ func(*ui.SimpleButton) { handler() },
+ )
+ bl.AddWidget(btn)
+
+ y = y + BUTTON_LIST_BUTTON_HEIGHT + BUTTON_LIST_BUTTON_PADDING
+ log.Println("add button at", x, y)
+ }
+
+ 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()
+}
diff --git a/go/client/challenges.go b/go/client/challenges.go
index 7a9e1ace..ca6e3563 100644
--- a/go/client/challenges.go
+++ b/go/client/challenges.go
@@ -1,67 +1,42 @@
package main
import (
+ "github.com/hajimehoshi/ebiten/v2"
"muhq.space/muhqs-game/go/activities"
"muhq.space/muhqs-game/go/game"
- "muhq.space/muhqs-game/go/ui"
)
+var availChallenges = map[string]func(string) *game.LocalState{
+ "Missionary": game.NewMissionaryChallenge,
+ "Cav. Archer": game.NewCavArcherChallenge,
+ "Random": game.NewRandomChallenge,
+}
+
type challenges struct {
- ui.Collection
+ bl *activities.ButtonList
app *app
playerName string
}
-func NewChallengesMenu(app *app, playerName string) *challenges {
- m := &challenges{app: app, playerName: playerName}
- m.Width = app.windowWidth
- m.Height = app.windowHeight
-
- return m
-}
-
-var availChallenges map[string]func(string) *game.LocalState
-
-func init() {
- availChallenges = make(map[string]func(string) *game.LocalState)
- availChallenges["Missionary"] = game.NewMissionaryChallenge
- availChallenges["Cav. Archer"] = game.NewCavArcherChallenge
- availChallenges["Random"] = game.NewRandomChallenge
-}
-
-func (m *challenges) build() {
- i := 0
- for ch, f := range availChallenges {
-
- m.AddWidget(ui.NewSimpleButton(
- (m.Width-START_BUTTON_WIDTH)/2,
- i*START_BUTTON_HEIGHT+50+i*25,
- START_BUTTON_WIDTH,
- START_BUTTON_HEIGHT,
- ch,
- func(*ui.SimpleButton) {
- s := f(m.playerName)
- m.startChallenge(s)
- }))
- i = i + 1
+func newChallengesMenu(app *app, playerName string) *challenges {
+ var m *challenges
+ labels := []string{}
+ handlers := []func(){}
+ for k, start := range availChallenges {
+ labels = append(labels, k)
+ handlers = append(handlers, func() {
+ s := start(m.playerName)
+ m.startChallenge(s)
+ })
}
-}
-
-func (m *challenges) Update() error {
- if m.Widgets() == nil {
- m.build()
- }
-
- if err := ui.Update(); err != nil {
- return err
+ m = &challenges{
+ bl: activities.NewButtonList(app.windowWidth, app.windowHeight, labels, handlers),
+ app: app,
+ playerName: playerName,
}
- return m.Collection.Update()
-}
-
-func (m *challenges) Layout(width, height int) (int, int) {
- return m.Width, m.Height
+ return m
}
func (m *challenges) startChallenge(state game.State) {
@@ -74,3 +49,7 @@ func (m *challenges) startChallenge(state game.State) {
activities.PushActivity(g.Start())
}
+
+func (c *challenges) Draw(screen *ebiten.Image) { c.bl.Draw(screen) }
+func (c *challenges) Layout(w, h int) (int, int) { return c.bl.Layout(w, h) }
+func (c *challenges) Update() error { return c.bl.Update() }
diff --git a/go/client/startMenu.go b/go/client/startMenu.go
index ad3219bf..aaf51de4 100644
--- a/go/client/startMenu.go
+++ b/go/client/startMenu.go
@@ -139,7 +139,7 @@ func (m *startMenu) build() {
START_BUTTON_HEIGHT,
"Challenges",
func(*ui.SimpleButton) {
- activities.PushActivity(NewChallengesMenu(m.app, playerInput.Text()))
+ activities.PushActivity(newChallengesMenu(m.app, playerInput.Text()))
}))
}