From afcd50acb4bf6e2f71285b840e33a4aa1a82cb47 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Wed, 20 Aug 2025 13:39:41 +0200 Subject: add button list activity to simplify challenges and future activities --- go/activities/buttonList.go | 69 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 go/activities/buttonList.go (limited to 'go/activities/buttonList.go') 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() +} -- cgit v1.2.3