From 02c2739184b9b7e365fb6031937b4b6aaf1911e1 Mon Sep 17 00:00:00 2001 From: gucio321 Date: Wed, 8 May 2024 09:13:55 +0200 Subject: [PATCH] add GenAutoID to state --- Alignment.go | 4 ++-- ClickableWidgets.go | 16 ++++++++-------- CodeEditor.go | 2 +- Context.go | 10 ++++++++++ EventHandler.go | 2 +- ExtraWidgets.go | 6 +++--- ImageWidgets.go | 2 +- SliderWidgets.go | 6 +++--- SplitLayout.go | 2 +- TableWidgets.go | 6 +++--- TextWidgets.go | 8 ++++---- Widgets.go | 23 +++++++++-------------- 12 files changed, 46 insertions(+), 41 deletions(-) diff --git a/Alignment.go b/Alignment.go index ca681c6e..9e6365f9 100644 --- a/Alignment.go +++ b/Alignment.go @@ -94,7 +94,7 @@ type AlignmentSetter struct { func Align(at AlignmentType) *AlignmentSetter { return &AlignmentSetter{ alignType: at, - id: GenAutoID("alignSetter"), + id: Context.GenAutoID("alignSetter"), } } @@ -178,7 +178,7 @@ func (a *AlignmentSetter) Build() { // if you find anything else, please report it on // https://github.com/AllenDang/giu Any contribution is appreciated! func GetWidgetWidth(w Widget) (result float32) { - imgui.PushIDStr(GenAutoID("GetWidgetWidthMeasurement")) + imgui.PushIDStr(Context.GenAutoID("GetWidgetWidthMeasurement")) defer imgui.PopID() // save cursor position before doing anything diff --git a/ClickableWidgets.go b/ClickableWidgets.go index 9cf71ba5..aa1d79ff 100644 --- a/ClickableWidgets.go +++ b/ClickableWidgets.go @@ -23,7 +23,7 @@ type ButtonWidget struct { // Button creates a new button widget. func Button(label string) *ButtonWidget { return &ButtonWidget{ - id: GenAutoID(label), + id: Context.GenAutoID(label), width: 0, height: 0, onClick: nil, @@ -85,7 +85,7 @@ type ArrowButtonWidget struct { // ArrowButton creates ArrowButtonWidget. func ArrowButton(dir Direction) *ArrowButtonWidget { return &ArrowButtonWidget{ - id: GenAutoID("ArrowButton"), + id: Context.GenAutoID("ArrowButton"), dir: dir, onClick: nil, } @@ -121,7 +121,7 @@ type SmallButtonWidget struct { // SmallButton constructs a new small button widget. func SmallButton(id string) *SmallButtonWidget { return &SmallButtonWidget{ - id: GenAutoID(id), + id: Context.GenAutoID(id), onClick: nil, } } @@ -160,7 +160,7 @@ type InvisibleButtonWidget struct { // InvisibleButton constructs a new invisible button widget. func InvisibleButton() *InvisibleButtonWidget { return &InvisibleButtonWidget{ - id: GenAutoID("InvisibleButton"), + id: Context.GenAutoID("InvisibleButton"), width: 0, height: 0, onClick: nil, @@ -291,7 +291,7 @@ type ImageButtonWithRgbaWidget struct { // ImageButtonWithRgba creates a new widget. func ImageButtonWithRgba(rgba image.Image) *ImageButtonWithRgbaWidget { return &ImageButtonWithRgbaWidget{ - id: GenAutoID("ImageButtonWithRgba"), + id: Context.GenAutoID("ImageButtonWithRgba"), ImageButtonWidget: ImageButton(nil), rgba: rgba, } @@ -360,7 +360,7 @@ type CheckboxWidget struct { // Checkbox creates a new CheckboxWidget. func Checkbox(text string, selected *bool) *CheckboxWidget { return &CheckboxWidget{ - text: GenAutoID(text), + text: Context.GenAutoID(text), selected: selected, onChange: nil, } @@ -393,7 +393,7 @@ type RadioButtonWidget struct { // RadioButton creates a radio button. func RadioButton(text string, active bool) *RadioButtonWidget { return &RadioButtonWidget{ - text: GenAutoID(text), + text: Context.GenAutoID(text), active: active, onChange: nil, } @@ -429,7 +429,7 @@ type SelectableWidget struct { // Selectable constructs a selectable widget. func Selectable(label string) *SelectableWidget { return &SelectableWidget{ - label: GenAutoID(label), + label: Context.GenAutoID(label), selected: false, flags: 0, width: 0, diff --git a/CodeEditor.go b/CodeEditor.go index 37924698..fcb03873 100644 --- a/CodeEditor.go +++ b/CodeEditor.go @@ -43,7 +43,7 @@ func CodeEditor() *CodeEditorWidget { panic("Code Editor is not implemented yet!") return &CodeEditorWidget{ - title: GenAutoID("##CodeEditor"), + title: Context.GenAutoID("##CodeEditor"), } } diff --git a/Context.go b/Context.go index 21607e1a..f7b19cd5 100644 --- a/Context.go +++ b/Context.go @@ -8,6 +8,12 @@ import ( "gopkg.in/eapache/queue.v1" ) +// GenAutoID automatically generates widget's ID. +// It returns an unique value each time it is called. +func GenAutoID(id string) string { + return fmt.Sprintf("%s##%d", id, Context.GetWidgetIndex()) +} + // Context represents a giu context. var Context *context @@ -33,6 +39,9 @@ type context struct { isRunning bool widgetIndexCounter int + // this function could be overwrited by user. + // Especially, do this if you want to disable auto ID generator. + GenAutoID func(baseID string) string // Indicate whether current application is running isAlive bool @@ -57,6 +66,7 @@ func CreateContext(b imgui.Backend[imgui.GLFWWindowFlags]) *context { FontAtlas: newFontAtlas(), textureLoadingQueue: queue.New(), m: &sync.Mutex{}, + GenAutoID: GenAutoID, } // Create font diff --git a/EventHandler.go b/EventHandler.go index 5327f99a..f514cf9e 100644 --- a/EventHandler.go +++ b/EventHandler.go @@ -123,7 +123,7 @@ func (eh *EventHandler) Build() { if eh.onActivate != nil || eh.onDeactivate != nil { var state *eventHandlerState - stateID := GenAutoID("eventHandlerState") + stateID := Context.GenAutoID("eventHandlerState") if state = GetState[eventHandlerState](Context, stateID); state == nil { state = &eventHandlerState{} SetState(Context, stateID, state) diff --git a/ExtraWidgets.go b/ExtraWidgets.go index 29ebdbf8..16c7614f 100644 --- a/ExtraWidgets.go +++ b/ExtraWidgets.go @@ -25,7 +25,7 @@ type SplitterWidget struct { // Splitter creates new SplitterWidget. func Splitter(direction SplitDirection, delta *float32) *SplitterWidget { return &SplitterWidget{ - id: GenAutoID("Splitter"), + id: Context.GenAutoID("Splitter"), width: 0, height: 0, delta: delta, @@ -247,7 +247,7 @@ type ListBoxWidget struct { // ListBox creates new ListBoxWidget. func ListBox(items []string) *ListBoxWidget { return &ListBoxWidget{ - id: GenAutoID("##ListBox"), + id: Context.GenAutoID("##ListBox"), width: 0, height: 0, border: true, @@ -397,7 +397,7 @@ type DatePickerWidget struct { // DatePicker creates new DatePickerWidget. func DatePicker(id string, date *time.Time) *DatePickerWidget { return &DatePickerWidget{ - id: GenAutoID(id), + id: Context.GenAutoID(id), date: date, width: 100, startOfWeek: time.Sunday, diff --git a/ImageWidgets.go b/ImageWidgets.go index 99c424f0..4ef7a08d 100644 --- a/ImageWidgets.go +++ b/ImageWidgets.go @@ -136,7 +136,7 @@ type ImageWithRgbaWidget struct { // ImageWithRgba creates ImageWithRgbaWidget. func ImageWithRgba(rgba image.Image) *ImageWithRgbaWidget { return &ImageWithRgbaWidget{ - id: GenAutoID("ImageWithRgba"), + id: Context.GenAutoID("ImageWithRgba"), rgba: ImageToRgba(rgba), img: Image(nil), } diff --git a/SliderWidgets.go b/SliderWidgets.go index 489f4f45..d41787f8 100644 --- a/SliderWidgets.go +++ b/SliderWidgets.go @@ -22,7 +22,7 @@ type SliderIntWidget struct { // SliderInt constructs new SliderIntWidget. func SliderInt(value *int32, minValue, maxValue int32) *SliderIntWidget { return &SliderIntWidget{ - label: GenAutoID("##SliderInt"), + label: Context.GenAutoID("##SliderInt"), value: value, minValue: minValue, maxValue: maxValue, @@ -95,7 +95,7 @@ type VSliderIntWidget struct { // VSliderInt creates new vslider int. func VSliderInt(value *int32, minValue, maxValue int32) *VSliderIntWidget { return &VSliderIntWidget{ - label: GenAutoID("##VSliderInt"), + label: Context.GenAutoID("##VSliderInt"), width: 18, height: 60, value: value, @@ -173,7 +173,7 @@ type SliderFloatWidget struct { // SliderFloat creates new slider float widget. func SliderFloat(value *float32, minValue, maxValue float32) *SliderFloatWidget { return &SliderFloatWidget{ - label: GenAutoID("##SliderFloat"), + label: Context.GenAutoID("##SliderFloat"), value: value, minValue: minValue, maxValue: maxValue, diff --git a/SplitLayout.go b/SplitLayout.go index 87f1d002..5f3775ac 100644 --- a/SplitLayout.go +++ b/SplitLayout.go @@ -50,7 +50,7 @@ func SplitLayout(direction SplitDirection, sashPos *float32, layout1, layout2 Wi layout1: layout1, layout2: layout2, border: true, - id: GenAutoID("SplitLayout"), + id: Context.GenAutoID("SplitLayout"), } } diff --git a/TableWidgets.go b/TableWidgets.go index 534ba8dd..23493a70 100644 --- a/TableWidgets.go +++ b/TableWidgets.go @@ -110,7 +110,7 @@ type TableWidget struct { func Table() *TableWidget { return &TableWidget{ - id: GenAutoID("Table"), + id: Context.GenAutoID("Table"), flags: TableFlagsResizable | TableFlagsBorders | TableFlagsScrollY, rows: nil, columns: nil, @@ -225,7 +225,7 @@ type TreeTableRowWidget struct { // TreeTableRow creates new TreeTableRowWidget. func TreeTableRow(label string, widgets ...Widget) *TreeTableRowWidget { return &TreeTableRowWidget{ - label: GenAutoID(label), + label: Context.GenAutoID(label), layout: widgets, } } @@ -292,7 +292,7 @@ type TreeTableWidget struct { // TreeTable creates new TreeTableWidget. func TreeTable() *TreeTableWidget { return &TreeTableWidget{ - id: GenAutoID("TreeTable"), + id: Context.GenAutoID("TreeTable"), flags: TableFlagsBordersV | TableFlagsBordersOuterH | TableFlagsResizable | TableFlagsRowBg | TableFlagsNoBordersInBody, rows: nil, columns: nil, diff --git a/TextWidgets.go b/TextWidgets.go index 13eff3f0..cd0a325f 100644 --- a/TextWidgets.go +++ b/TextWidgets.go @@ -33,7 +33,7 @@ func InputTextMultiline(text *string) *InputTextMultilineWidget { flags: 0, cb: nil, onChange: nil, - label: GenAutoID("##InputTextMultiline"), + label: Context.GenAutoID("##InputTextMultiline"), } } @@ -170,7 +170,7 @@ type InputTextWidget struct { // InputText creates new input text widget. func InputText(value *string) *InputTextWidget { return &InputTextWidget{ - label: GenAutoID("##InputText"), + label: Context.GenAutoID("##InputText"), hint: "", value: value, width: 0, @@ -329,7 +329,7 @@ type InputIntWidget struct { // with InputTextFlagsCharsDecimal and strconv.ParseInt in OnChange callback. func InputInt(value *int32) *InputIntWidget { return &InputIntWidget{ - label: GenAutoID("##InputInt"), + label: Context.GenAutoID("##InputInt"), value: value, width: 0, flags: 0, @@ -414,7 +414,7 @@ type InputFloatWidget struct { // InputFloat constructs InputFloatWidget. func InputFloat(value *float32) *InputFloatWidget { return &InputFloatWidget{ - label: GenAutoID("##InputFloatWidget"), + label: Context.GenAutoID("##InputFloatWidget"), width: 0, value: value, format: "%.3f", diff --git a/Widgets.go b/Widgets.go index 2cc572bd..b8d86e12 100644 --- a/Widgets.go +++ b/Widgets.go @@ -7,11 +7,6 @@ import ( imgui "github.com/AllenDang/cimgui-go" ) -// GenAutoID automatically generates fidget's id. -func GenAutoID(id string) string { - return fmt.Sprintf("%s##%d", id, Context.GetWidgetIndex()) -} - var _ Widget = &RowWidget{} // RowWidget joins a layout into one line @@ -113,7 +108,7 @@ func (c *ChildWidget) ID(id string) *ChildWidget { func Child() *ChildWidget { return &ChildWidget{ - id: GenAutoID("Child"), + id: Context.GenAutoID("Child"), width: 0, height: 0, border: true, @@ -136,7 +131,7 @@ type ComboCustomWidget struct { // ComboCustom creates a new combo custom widget. func ComboCustom(label, previewValue string) *ComboCustomWidget { return &ComboCustomWidget{ - label: GenAutoID(label), + label: Context.GenAutoID(label), previewValue: Context.FontAtlas.RegisterString(previewValue), width: 0, flags: 0, @@ -192,7 +187,7 @@ type ComboWidget struct { // Combo creates a new ComboWidget. func Combo(label, previewValue string, items []string, selected *int32) *ComboWidget { return &ComboWidget{ - label: GenAutoID(label), + label: Context.GenAutoID(label), previewValue: Context.FontAtlas.RegisterString(previewValue), items: Context.FontAtlas.RegisterStringSlice(items), selected: selected, @@ -254,7 +249,7 @@ func ContextMenu() *ContextMenuWidget { return &ContextMenuWidget{ mouseButton: MouseButtonRight, layout: nil, - id: GenAutoID("ContextMenu"), + id: Context.GenAutoID("ContextMenu"), } } @@ -294,7 +289,7 @@ type DragIntWidget struct { func DragInt(label string, value *int32, minValue, maxValue int32) *DragIntWidget { return &DragIntWidget{ - label: GenAutoID(label), + label: Context.GenAutoID(label), value: value, speed: 1.0, minValue: minValue, @@ -403,7 +398,7 @@ type MenuItemWidget struct { func MenuItem(label string) *MenuItemWidget { return &MenuItemWidget{ - label: GenAutoID(label), + label: Context.GenAutoID(label), shortcut: "", selected: false, enabled: true, @@ -452,7 +447,7 @@ type MenuWidget struct { func Menu(label string) *MenuWidget { return &MenuWidget{ - label: GenAutoID(label), + label: Context.GenAutoID(label), enabled: true, layout: nil, } @@ -634,7 +629,7 @@ type TabBarWidget struct { // TabBar creates new TabBarWidget. func TabBar() *TabBarWidget { return &TabBarWidget{ - id: GenAutoID("TabBar"), + id: Context.GenAutoID("TabBar"), flags: 0, } } @@ -735,7 +730,7 @@ type ColorEditWidget struct { func ColorEdit(label string, c *color.RGBA) *ColorEditWidget { return &ColorEditWidget{ - label: GenAutoID(label), + label: Context.GenAutoID(label), color: c, // flags: ColorEditFlagsNone, }