From 5813f8fab81f700e0d10092b48c387f86c93a0de Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Fri, 7 Jun 2024 18:39:14 +0200 Subject: [PATCH] added option to select theme, can be used also to select RLEs :) --- src/options.go | 18 +++++++ src/widgets.go | 136 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 154 insertions(+) diff --git a/src/options.go b/src/options.go index 7474f51..7cf0187 100644 --- a/src/options.go +++ b/src/options.go @@ -117,7 +117,19 @@ func (scene *SceneOptions) Init() { }) scene.SetInitialValue(wrap, scene.Config.Wrap) + themes := NewCombobox( + []string{"dark", "light"}, + scene.Config.Theme, + func(args *widget.ListComboButtonEntrySelectedEventArgs) { + scene.Config.SwitchTheme(args.Entry.(ListEntry).Name) + }) + themelabel := NewLabel("Themes") + combocontainer := NewColumnContainer() + combocontainer.AddChild(themes) + combocontainer.AddChild(themelabel) + separator := NewSeparator(3) + separator2 := NewSeparator(3) cancel := NewMenuButton("Close", func(args *widget.ButtonClickedEventArgs) { @@ -130,7 +142,13 @@ func (scene *SceneOptions) Init() { rowContainer.AddChild(gridlines) rowContainer.AddChild(evolution) rowContainer.AddChild(wrap) + rowContainer.AddChild(separator) + + rowContainer.AddChild(combocontainer) + + rowContainer.AddChild(separator2) + rowContainer.AddChild(cancel) scene.Ui = &ebitenui.UI{ diff --git a/src/widgets.go b/src/widgets.go index 358bd07..c6fd5a0 100644 --- a/src/widgets.go +++ b/src/widgets.go @@ -78,6 +78,122 @@ func NewSeparator(padding int) widget.PreferredSizeLocateableWidget { return c } +type ListEntry struct { + id int + Name string +} + +func NewCombobox(items []string, selected string, + action func(args *widget.ListComboButtonEntrySelectedEventArgs)) *widget.ListComboButton { + buttonImage, _ := LoadButtonImage() + + entries := make([]any, 0, len(items)) + idxselected := 0 + for i, item := range items { + entries = append(entries, ListEntry{i, item}) + if items[i] == selected { + idxselected = i + } + } + + comboBox := widget.NewListComboButton( + widget.ListComboButtonOpts.SelectComboButtonOpts( + widget.SelectComboButtonOpts.ComboButtonOpts( + //Set the max height of the dropdown list + widget.ComboButtonOpts.MaxContentHeight(150), + //Set the parameters for the primary displayed button + widget.ComboButtonOpts.ButtonOpts( + widget.ButtonOpts.Image(buttonImage), + widget.ButtonOpts.TextPadding(widget.NewInsetsSimple(5)), + widget.ButtonOpts.Text("", *FontRenderer.FontSmall, &widget.ButtonTextColor{ + Idle: color.White, + Disabled: color.White, + }), + widget.ButtonOpts.WidgetOpts( + //Set how wide the button should be + widget.WidgetOpts.MinSize(50, 0), + //Set the combobox's position + widget.WidgetOpts.LayoutData(widget.AnchorLayoutData{ + HorizontalPosition: widget.AnchorLayoutPositionCenter, + VerticalPosition: widget.AnchorLayoutPositionCenter, + })), + ), + ), + ), + widget.ListComboButtonOpts.ListOpts( + //Set how wide the dropdown list should be + widget.ListOpts.ContainerOpts(widget.ContainerOpts.WidgetOpts(widget.WidgetOpts.MinSize(50, 0))), + //Set the entries in the list + widget.ListOpts.Entries(entries), + widget.ListOpts.ScrollContainerOpts( + //Set the background images/color for the dropdown list + widget.ScrollContainerOpts.Image(&widget.ScrollContainerImage{ + Idle: image.NewNineSliceColor(color.NRGBA{100, 100, 100, 255}), + Disabled: image.NewNineSliceColor(color.NRGBA{100, 100, 100, 255}), + Mask: image.NewNineSliceColor(color.NRGBA{100, 100, 100, 255}), + }), + ), + widget.ListOpts.SliderOpts( + //Set the background images/color for the background of the slider track + widget.SliderOpts.Images(&widget.SliderTrackImage{ + Idle: image.NewNineSliceColor(color.NRGBA{100, 100, 100, 255}), + Hover: image.NewNineSliceColor(color.NRGBA{100, 100, 100, 255}), + }, buttonImage), + widget.SliderOpts.MinHandleSize(5), + //Set how wide the track should be + widget.SliderOpts.TrackPadding(widget.NewInsetsSimple(2))), + //Set the font for the list options + widget.ListOpts.EntryFontFace(*FontRenderer.FontSmall), + //Set the colors for the list + // FIXME: Change to our own color set + widget.ListOpts.EntryColor(&widget.ListEntryColor{ + Selected: color.NRGBA{254, 255, 255, 255}, //Foreground color for the unfocused selected entry + Unselected: color.NRGBA{254, 255, 255, 255}, //Foreground color for the unfocused unselected entry + SelectedBackground: color.NRGBA{R: 130, G: 130, B: 200, A: 255}, //Background color for the unfocused selected entry + SelectedFocusedBackground: color.NRGBA{R: 130, G: 130, B: 170, A: 255}, //Background color for the focused selected entry + FocusedBackground: color.NRGBA{R: 170, G: 170, B: 180, A: 255}, //Background color for the focused unselected entry + DisabledUnselected: color.NRGBA{100, 100, 100, 255}, //Foreground color for the disabled unselected entry + DisabledSelected: color.NRGBA{100, 100, 100, 255}, //Foreground color for the disabled selected entry + DisabledSelectedBackground: color.NRGBA{100, 100, 100, 255}, //Background color for the disabled selected entry + }), + //Padding for each entry + widget.ListOpts.EntryTextPadding(widget.NewInsetsSimple(5)), + ), + //Define how the entry is displayed + widget.ListComboButtonOpts.EntryLabelFunc( + func(e any) string { + //Button Label function, visible if not open + return e.(ListEntry).Name + }, + func(e any) string { + //List Label function, visible items if open + return e.(ListEntry).Name + }), + //Callback when a new entry is selected + widget.ListComboButtonOpts.EntrySelectedHandler(action), + ) + + //Select the middle entry -- optional + comboBox.SetSelectedEntry(entries[idxselected]) + + return comboBox +} + +func NewLabel(text string) *widget.Text { + return widget.NewText( + widget.TextOpts.Text(text, *FontRenderer.FontSmall, color.White), + widget.TextOpts.Position(widget.TextPositionCenter, widget.TextPositionCenter), + widget.TextOpts.WidgetOpts( + widget.WidgetOpts.LayoutData(widget.RowLayoutData{ + Position: widget.RowLayoutPositionCenter, + }), + ), + ) + +} + +/////////////// containers + type RowContainer struct { Root *widget.Container Row *widget.Container @@ -130,6 +246,19 @@ func NewRowContainer(title string) *RowContainer { } } +func NewColumnContainer() *widget.Container { + colcontainer := widget.NewContainer( + widget.ContainerOpts.Layout( + widget.NewGridLayout( + widget.GridLayoutOpts.Columns(2), + widget.GridLayoutOpts.Spacing(5, 0), + ), + ), + ) + + return colcontainer +} + func LoadButtonImage() (*widget.ButtonImage, error) { idle := image.NewNineSlice(Assets["button-9slice2"], [3]int{3, 3, 3}, [3]int{3, 3, 3}) hover := image.NewNineSlice(Assets["button-9slice3"], [3]int{3, 3, 3}, [3]int{3, 3, 3}) @@ -142,6 +271,13 @@ func LoadButtonImage() (*widget.ButtonImage, error) { }, nil } +func LoadComboLabelImage() *widget.ButtonImageImage { + return &widget.ButtonImageImage{ + Idle: Assets["checkbox-9slice2"], + Disabled: Assets["checkbox-9slice2"], + } +} + func LoadCheckboxImage() (*widget.CheckboxGraphicImage, error) { unchecked := &widget.ButtonImageImage{ Idle: Assets["checkbox-9slice2"],