Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add GenAutoID to Context #795

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Alignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ type AlignmentSetter struct {
func Align(at AlignmentType) *AlignmentSetter {
return &AlignmentSetter{
alignType: at,
id: GenAutoID("alignSetter"),
id: Context.GenAutoID("alignSetter"),
}
}

Expand Down Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions ClickableWidgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion CodeEditor.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func CodeEditor() *CodeEditorWidget {
panic("Code Editor is not implemented yet!")

return &CodeEditorWidget{
title: GenAutoID("##CodeEditor"),
title: Context.GenAutoID("##CodeEditor"),
}
}

Expand Down
10 changes: 10 additions & 0 deletions Context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -57,6 +66,7 @@ func CreateContext(b imgui.Backend[imgui.GLFWWindowFlags]) *context {
FontAtlas: newFontAtlas(),
textureLoadingQueue: queue.New(),
m: &sync.Mutex{},
GenAutoID: GenAutoID,
}

// Create font
Expand Down
2 changes: 1 addition & 1 deletion EventHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions ExtraWidgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion ImageWidgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down
6 changes: 3 additions & 3 deletions SliderWidgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion SplitLayout.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
}

Expand Down
6 changes: 3 additions & 3 deletions TableWidgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions TextWidgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func InputTextMultiline(text *string) *InputTextMultilineWidget {
flags: 0,
cb: nil,
onChange: nil,
label: GenAutoID("##InputTextMultiline"),
label: Context.GenAutoID("##InputTextMultiline"),
}
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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",
Expand Down
23 changes: 9 additions & 14 deletions Widgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -254,7 +249,7 @@ func ContextMenu() *ContextMenuWidget {
return &ContextMenuWidget{
mouseButton: MouseButtonRight,
layout: nil,
id: GenAutoID("ContextMenu"),
id: Context.GenAutoID("ContextMenu"),
}
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -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,
}
Expand Down
Loading