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 15:57:41 +0200 |
| commit | 2f1b1f831c97124f0ec7ed7055338f929e9fd984 (patch) | |
| tree | dce2b613f2c7fdd9e5a8750c1034eed6ddd8e046 /go/activities | |
| parent | 815d9936d2b75b59a0ff13bb502558809c418f93 (diff) | |
| download | muhqs-game-2f1b1f831c97124f0ec7ed7055338f929e9fd984.tar.gz muhqs-game-2f1b1f831c97124f0ec7ed7055338f929e9fd984.zip | |
add button list activity to simplify challenges and future activities
Diffstat (limited to 'go/activities')
| -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() +} |
