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 /go/activities/buttonList.go | |
| parent | af2566bad4b9d1c7c741a0891e357e58cc3de510 (diff) | |
| download | muhqs-game-afcd50acb4bf6e2f71285b840e33a4aa1a82cb47.tar.gz muhqs-game-afcd50acb4bf6e2f71285b840e33a4aa1a82cb47.zip | |
add button list activity to simplify challenges and future activities
Diffstat (limited to 'go/activities/buttonList.go')
| -rw-r--r-- | go/activities/buttonList.go | 69 |
1 files changed, 69 insertions, 0 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() +} |
