diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2025-08-20 13:39:41 +0200 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2025-08-20 14:46:15 +0200 |
| commit | afcd50acb4bf6e2f71285b840e33a4aa1a82cb47 (patch) | |
| tree | 395bcd145161e32823a488498123346fbb2afab6 | |
| parent | af2566bad4b9d1c7c741a0891e357e58cc3de510 (diff) | |
| download | muhqs-game-afcd50acb4bf6e2f71285b840e33a4aa1a82cb47.tar.gz muhqs-game-afcd50acb4bf6e2f71285b840e33a4aa1a82cb47.zip | |
add button list activity to simplify challenges and future activities
| -rw-r--r-- | go/activities/buttonList.go | 69 | ||||
| -rw-r--r-- | go/client/challenges.go | 75 | ||||
| -rw-r--r-- | go/client/startMenu.go | 2 |
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())) })) } |
