diff --git a/Alignment.go b/Alignment.go index 9e6365f9..ca681c6e 100644 --- a/Alignment.go +++ b/Alignment.go @@ -94,7 +94,7 @@ type AlignmentSetter struct { func Align(at AlignmentType) *AlignmentSetter { return &AlignmentSetter{ alignType: at, - id: Context.GenAutoID("alignSetter"), + id: 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(Context.GenAutoID("GetWidgetWidthMeasurement")) + imgui.PushIDStr(GenAutoID("GetWidgetWidthMeasurement")) defer imgui.PopID() // save cursor position before doing anything diff --git a/ClickableWidgets.go b/ClickableWidgets.go index aa1d79ff..9cf71ba5 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: Context.GenAutoID(label), + id: 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: Context.GenAutoID("ArrowButton"), + id: 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: Context.GenAutoID(id), + id: GenAutoID(id), onClick: nil, } } @@ -160,7 +160,7 @@ type InvisibleButtonWidget struct { // InvisibleButton constructs a new invisible button widget. func InvisibleButton() *InvisibleButtonWidget { return &InvisibleButtonWidget{ - id: Context.GenAutoID("InvisibleButton"), + id: 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: Context.GenAutoID("ImageButtonWithRgba"), + id: 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: Context.GenAutoID(text), + text: 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: Context.GenAutoID(text), + text: 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: Context.GenAutoID(label), + label: GenAutoID(label), selected: false, flags: 0, width: 0, diff --git a/CodeEditor.go b/CodeEditor.go index fcb03873..37924698 100644 --- a/CodeEditor.go +++ b/CodeEditor.go @@ -43,7 +43,7 @@ func CodeEditor() *CodeEditorWidget { panic("Code Editor is not implemented yet!") return &CodeEditorWidget{ - title: Context.GenAutoID("##CodeEditor"), + title: GenAutoID("##CodeEditor"), } } diff --git a/Context.go b/Context.go index f7b19cd5..21607e1a 100644 --- a/Context.go +++ b/Context.go @@ -8,12 +8,6 @@ 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 @@ -39,9 +33,6 @@ 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 @@ -66,7 +57,6 @@ 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 f514cf9e..5327f99a 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 := Context.GenAutoID("eventHandlerState") + stateID := 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 16c7614f..29ebdbf8 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: Context.GenAutoID("Splitter"), + id: 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: Context.GenAutoID("##ListBox"), + id: 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: Context.GenAutoID(id), + id: GenAutoID(id), date: date, width: 100, startOfWeek: time.Sunday, diff --git a/ImageWidgets.go b/ImageWidgets.go index 4ef7a08d..99c424f0 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: Context.GenAutoID("ImageWithRgba"), + id: GenAutoID("ImageWithRgba"), rgba: ImageToRgba(rgba), img: Image(nil), } diff --git a/SliderWidgets.go b/SliderWidgets.go index d41787f8..489f4f45 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: Context.GenAutoID("##SliderInt"), + label: 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: Context.GenAutoID("##VSliderInt"), + label: 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: Context.GenAutoID("##SliderFloat"), + label: GenAutoID("##SliderFloat"), value: value, minValue: minValue, maxValue: maxValue, diff --git a/SplitLayout.go b/SplitLayout.go index 5f3775ac..87f1d002 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: Context.GenAutoID("SplitLayout"), + id: GenAutoID("SplitLayout"), } } diff --git a/TableWidgets.go b/TableWidgets.go index 23493a70..534ba8dd 100644 --- a/TableWidgets.go +++ b/TableWidgets.go @@ -110,7 +110,7 @@ type TableWidget struct { func Table() *TableWidget { return &TableWidget{ - id: Context.GenAutoID("Table"), + id: 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: Context.GenAutoID(label), + label: GenAutoID(label), layout: widgets, } } @@ -292,7 +292,7 @@ type TreeTableWidget struct { // TreeTable creates new TreeTableWidget. func TreeTable() *TreeTableWidget { return &TreeTableWidget{ - id: Context.GenAutoID("TreeTable"), + id: GenAutoID("TreeTable"), flags: TableFlagsBordersV | TableFlagsBordersOuterH | TableFlagsResizable | TableFlagsRowBg | TableFlagsNoBordersInBody, rows: nil, columns: nil, diff --git a/TextWidgets.go b/TextWidgets.go index cd0a325f..13eff3f0 100644 --- a/TextWidgets.go +++ b/TextWidgets.go @@ -33,7 +33,7 @@ func InputTextMultiline(text *string) *InputTextMultilineWidget { flags: 0, cb: nil, onChange: nil, - label: Context.GenAutoID("##InputTextMultiline"), + label: GenAutoID("##InputTextMultiline"), } } @@ -170,7 +170,7 @@ type InputTextWidget struct { // InputText creates new input text widget. func InputText(value *string) *InputTextWidget { return &InputTextWidget{ - label: Context.GenAutoID("##InputText"), + label: 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: Context.GenAutoID("##InputInt"), + label: 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: Context.GenAutoID("##InputFloatWidget"), + label: GenAutoID("##InputFloatWidget"), width: 0, value: value, format: "%.3f", diff --git a/Widgets.go b/Widgets.go index b8d86e12..2cc572bd 100644 --- a/Widgets.go +++ b/Widgets.go @@ -7,6 +7,11 @@ 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 @@ -108,7 +113,7 @@ func (c *ChildWidget) ID(id string) *ChildWidget { func Child() *ChildWidget { return &ChildWidget{ - id: Context.GenAutoID("Child"), + id: GenAutoID("Child"), width: 0, height: 0, border: true, @@ -131,7 +136,7 @@ type ComboCustomWidget struct { // ComboCustom creates a new combo custom widget. func ComboCustom(label, previewValue string) *ComboCustomWidget { return &ComboCustomWidget{ - label: Context.GenAutoID(label), + label: GenAutoID(label), previewValue: Context.FontAtlas.RegisterString(previewValue), width: 0, flags: 0, @@ -187,7 +192,7 @@ type ComboWidget struct { // Combo creates a new ComboWidget. func Combo(label, previewValue string, items []string, selected *int32) *ComboWidget { return &ComboWidget{ - label: Context.GenAutoID(label), + label: GenAutoID(label), previewValue: Context.FontAtlas.RegisterString(previewValue), items: Context.FontAtlas.RegisterStringSlice(items), selected: selected, @@ -249,7 +254,7 @@ func ContextMenu() *ContextMenuWidget { return &ContextMenuWidget{ mouseButton: MouseButtonRight, layout: nil, - id: Context.GenAutoID("ContextMenu"), + id: GenAutoID("ContextMenu"), } } @@ -289,7 +294,7 @@ type DragIntWidget struct { func DragInt(label string, value *int32, minValue, maxValue int32) *DragIntWidget { return &DragIntWidget{ - label: Context.GenAutoID(label), + label: GenAutoID(label), value: value, speed: 1.0, minValue: minValue, @@ -398,7 +403,7 @@ type MenuItemWidget struct { func MenuItem(label string) *MenuItemWidget { return &MenuItemWidget{ - label: Context.GenAutoID(label), + label: GenAutoID(label), shortcut: "", selected: false, enabled: true, @@ -447,7 +452,7 @@ type MenuWidget struct { func Menu(label string) *MenuWidget { return &MenuWidget{ - label: Context.GenAutoID(label), + label: GenAutoID(label), enabled: true, layout: nil, } @@ -629,7 +634,7 @@ type TabBarWidget struct { // TabBar creates new TabBarWidget. func TabBar() *TabBarWidget { return &TabBarWidget{ - id: Context.GenAutoID("TabBar"), + id: GenAutoID("TabBar"), flags: 0, } } @@ -730,7 +735,7 @@ type ColorEditWidget struct { func ColorEdit(label string, c *color.RGBA) *ColorEditWidget { return &ColorEditWidget{ - label: Context.GenAutoID(label), + label: GenAutoID(label), color: c, // flags: ColorEditFlagsNone, }