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 a special type for IDs #798

Merged
merged 5 commits into from
May 22, 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
8 changes: 4 additions & 4 deletions Alignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ var _ Widget = &AlignmentSetter{}
type AlignmentSetter struct {
alignType AlignmentType
layout Layout
id string
id ID
}

// Align sets widgets alignment.
Expand All @@ -107,8 +107,8 @@ func (a *AlignmentSetter) To(widgets ...Widget) *AlignmentSetter {
// ID allows to manually set AlignmentSetter ID
// NOTE: there isn't any known reason to use this method, however
// it is here for some random cases. YOU DON'T NEED TO USE IT
// in normal conditions.
func (a *AlignmentSetter) ID(id string) *AlignmentSetter {
// in normal cases.
func (a *AlignmentSetter) ID(id ID) *AlignmentSetter {
a.id = id
return a
}
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(string(GenAutoID("GetWidgetWidthMeasurement")))
defer imgui.PopID()

// save cursor position before doing anything
Expand Down
46 changes: 23 additions & 23 deletions ClickableWidgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var _ Widget = &ButtonWidget{}

// ButtonWidget represents a ImGui button widget.
type ButtonWidget struct {
id string
id ID
width float32
height float32
disabled bool
Expand Down Expand Up @@ -56,7 +56,7 @@ func (b *ButtonWidget) Size(width, height float32) *ButtonWidget {
return b
}

func (b *ButtonWidget) ID(id string) *ButtonWidget {
func (b *ButtonWidget) ID(id ID) *ButtonWidget {
b.id = id
return b
}
Expand All @@ -68,7 +68,7 @@ func (b *ButtonWidget) Build() {
defer imgui.EndDisabled()
}

if imgui.ButtonV(Context.FontAtlas.RegisterString(b.id), imgui.Vec2{X: b.width, Y: b.height}) && b.onClick != nil {
if imgui.ButtonV(Context.FontAtlas.RegisterString(b.id.String()), imgui.Vec2{X: b.width, Y: b.height}) && b.onClick != nil {
b.onClick()
}
}
Expand All @@ -77,7 +77,7 @@ var _ Widget = &ArrowButtonWidget{}

// ArrowButtonWidget represents a square button with an arrow.
type ArrowButtonWidget struct {
id string
id ID
dir Direction
onClick func()
}
Expand All @@ -98,14 +98,14 @@ func (b *ArrowButtonWidget) OnClick(onClick func()) *ArrowButtonWidget {
}

// ID allows to manually set widget's id.
func (b *ArrowButtonWidget) ID(id string) *ArrowButtonWidget {
func (b *ArrowButtonWidget) ID(id ID) *ArrowButtonWidget {
b.id = id
return b
}

// Build implements Widget interface.
func (b *ArrowButtonWidget) Build() {
if imgui.ArrowButton(b.id, imgui.Dir(b.dir)) && b.onClick != nil {
if imgui.ArrowButton(b.id.String(), imgui.Dir(b.dir)) && b.onClick != nil {
b.onClick()
}
}
Expand All @@ -114,7 +114,7 @@ var _ Widget = &SmallButtonWidget{}

// SmallButtonWidget is like a button but without frame padding.
type SmallButtonWidget struct {
id string
id ID
onClick func()
}

Expand All @@ -140,7 +140,7 @@ func (b *SmallButtonWidget) OnClick(onClick func()) *SmallButtonWidget {

// Build implements Widget interface.
func (b *SmallButtonWidget) Build() {
if imgui.SmallButton(Context.FontAtlas.RegisterString(b.id)) && b.onClick != nil {
if imgui.SmallButton(Context.FontAtlas.RegisterString(b.id.String())) && b.onClick != nil {
b.onClick()
}
}
Expand All @@ -151,7 +151,7 @@ var _ Widget = &InvisibleButtonWidget{}
// NOTE: you may want to display other widgets on this button.
// to do so, you may move drawing cursor back by Get/SetCursor(Screen)Pos.
type InvisibleButtonWidget struct {
id string
id ID
width float32
height float32
onClick func()
Expand Down Expand Up @@ -180,14 +180,14 @@ func (b *InvisibleButtonWidget) OnClick(onClick func()) *InvisibleButtonWidget {
}

// ID allows to manually set widget's id (no need to use in normal conditions).
func (b *InvisibleButtonWidget) ID(id string) *InvisibleButtonWidget {
func (b *InvisibleButtonWidget) ID(id ID) *InvisibleButtonWidget {
b.id = id
return b
}

// Build implements Widget interface.
func (b *InvisibleButtonWidget) Build() {
if imgui.InvisibleButton(Context.FontAtlas.RegisterString(b.id), imgui.Vec2{X: b.width, Y: b.height}) && b.onClick != nil {
if imgui.InvisibleButton(Context.FontAtlas.RegisterString(b.id.String()), imgui.Vec2{X: b.width, Y: b.height}) && b.onClick != nil {
b.onClick()
}
}
Expand Down Expand Up @@ -279,13 +279,13 @@ func (b *ImageButtonWidget) FramePadding(padding int) *ImageButtonWidget {

var _ Widget = &ImageButtonWithRgbaWidget{}

// ImageButtonWithRgbaWidget does similar to ImageButtonWIdget,
// ImageButtonWithRgbaWidget does similar to ImageButtonWidget,
// but implements image.Image instead of giu.Texture. It is probably
// more useful than the original ImageButtonWIdget.
// more useful than the original ImageButtonWidget.
type ImageButtonWithRgbaWidget struct {
*ImageButtonWidget
rgba image.Image
id string
id ID
}

// ImageButtonWithRgba creates a new widget.
Expand Down Expand Up @@ -335,11 +335,11 @@ func (b *ImageButtonWithRgbaWidget) FramePadding(padding int) *ImageButtonWithRg

// Build implements Widget interface.
func (b *ImageButtonWithRgbaWidget) Build() {
if state := GetState[imageState](Context, b.id); state == nil {
SetState(Context, b.id, &imageState{})
if state := GetState[imageState](Context, b.id.String()); state == nil {
SetState(Context, b.id.String(), &imageState{})

NewTextureFromRgba(b.rgba, func(tex *Texture) {
SetState(Context, b.id, &imageState{texture: tex})
SetState(Context, b.id.String(), &imageState{texture: tex})
})
} else {
b.ImageButtonWidget.texture = state.texture
Expand All @@ -352,7 +352,7 @@ var _ Widget = &CheckboxWidget{}

// CheckboxWidget adds a checkbox.
type CheckboxWidget struct {
text string
text ID
selected *bool
onChange func()
}
Expand All @@ -374,7 +374,7 @@ func (c *CheckboxWidget) OnChange(onChange func()) *CheckboxWidget {

// Build implements Widget interface.
func (c *CheckboxWidget) Build() {
if imgui.Checkbox(Context.FontAtlas.RegisterString(c.text), c.selected) && c.onChange != nil {
if imgui.Checkbox(Context.FontAtlas.RegisterString(c.text.String()), c.selected) && c.onChange != nil {
c.onChange()
}
}
Expand All @@ -385,7 +385,7 @@ var _ Widget = &RadioButtonWidget{}
// It is common to use it for single-choice questions.
// see examples/widgets.
type RadioButtonWidget struct {
text string
text ID
active bool
onChange func()
}
Expand All @@ -407,7 +407,7 @@ func (r *RadioButtonWidget) OnChange(onChange func()) *RadioButtonWidget {

// Build implements Widget interface.
func (r *RadioButtonWidget) Build() {
if imgui.RadioButtonBool(Context.FontAtlas.RegisterString(r.text), r.active) && r.onChange != nil {
if imgui.RadioButtonBool(Context.FontAtlas.RegisterString(r.text.String()), r.active) && r.onChange != nil {
r.onChange()
}
}
Expand All @@ -417,7 +417,7 @@ var _ Widget = &SelectableWidget{}
// SelectableWidget is a window-width button with a label which can get selected (highlighted).
// useful for certain lists.
type SelectableWidget struct {
label string
label ID
selected bool
flags SelectableFlags
width float32
Expand Down Expand Up @@ -482,7 +482,7 @@ func (s *SelectableWidget) Build() {
s.flags |= SelectableFlagsAllowDoubleClick
}

if imgui.SelectableBoolV(Context.FontAtlas.RegisterString(s.label), s.selected, imgui.SelectableFlags(s.flags), imgui.Vec2{X: s.width, Y: s.height}) && s.onClick != nil {
if imgui.SelectableBoolV(Context.FontAtlas.RegisterString(s.label.String()), s.selected, imgui.SelectableFlags(s.flags), imgui.Vec2{X: s.width, Y: s.height}) && s.onClick != nil {
s.onClick()
}

Expand Down
8 changes: 4 additions & 4 deletions CodeEditor.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var _ Widget = &CodeEditorWidget{}

// CodeEditorWidget represents imgui.TextEditor.
type CodeEditorWidget struct {
title string
title ID
width,
height float32
border bool
Expand All @@ -50,7 +50,7 @@ func CodeEditor() *CodeEditorWidget {
// ID allows to manually set editor's ID.
// It isn't necessary to use it in a normal conditions.

func (ce *CodeEditorWidget) ID(id string) *CodeEditorWidget {
func (ce *CodeEditorWidget) ID(id ID) *CodeEditorWidget {
ce.title = id
return ce
}
Expand Down Expand Up @@ -216,12 +216,12 @@ func (ce *CodeEditorWidget) Build() {
}

func (ce *CodeEditorWidget) getState() (state *codeEditorState) {
if state = GetState[codeEditorState](Context, ce.title); state == nil {
if state = GetState[codeEditorState](Context, ce.title.String()); state == nil {
state = &codeEditorState{
// editor: imgui.NewTextEditor(),
}

SetState(Context, ce.title, state)
SetState(Context, ce.title.String(), state)
}

return state
Expand Down
29 changes: 18 additions & 11 deletions Context.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ 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) ID {
idx, ok := Context.widgetIndex[id]
if !ok {
Context.widgetIndex[id] = 0
return ID(id)
}

Context.widgetIndex[id]++

return ID(fmt.Sprintf("%s##%d", id, idx))
}

// Context represents a giu context.
var Context *context

Expand All @@ -32,7 +46,7 @@ type context struct {

isRunning bool

widgetIndexCounter int
widgetIndex map[string]int

// Indicate whether current application is running
isAlive bool
Expand All @@ -57,6 +71,7 @@ func CreateContext(b imgui.Backend[imgui.GLFWWindowFlags]) *context {
FontAtlas: newFontAtlas(),
textureLoadingQueue: queue.New(),
m: &sync.Mutex{},
widgetIndex: make(map[string]int),
}

// Create font
Expand Down Expand Up @@ -106,8 +121,8 @@ func (c *context) cleanState() {
return true
})

// Reset widgetIndexCounter
c.widgetIndexCounter = 0
// Reset widgetIndex
c.widgetIndex = make(map[string]int)
}

func SetState[T any, PT genericDisposable[T]](c *context, id string, data PT) {
Expand Down Expand Up @@ -154,11 +169,3 @@ func (c *context) load(id any) (*state, bool) {

return nil, false
}

// Get widget index for current layout.
func (c *context) GetWidgetIndex() int {
i := c.widgetIndexCounter
c.widgetIndexCounter++

return i
}
7 changes: 0 additions & 7 deletions Context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,3 @@ func Test_invalidState(t *testing.T) {
assert.Nil(t, GetState[teststate](ctx, state1ID),
"although state hasn't been accessed during the frame, it hasn't ben deleted by invalidAllState/cleanState")
}

func Test_GetWidgetIndex(t *testing.T) {
ctx := CreateContext(nil)
for i := 0; i <= 3; i++ {
assert.Equal(t, i, ctx.GetWidgetIndex(), "widget index wasn't increased")
}
}
4 changes: 2 additions & 2 deletions EventHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ func (eh *EventHandler) Build() {
var state *eventHandlerState

stateID := GenAutoID("eventHandlerState")
if state = GetState[eventHandlerState](Context, stateID); state == nil {
if state = GetState[eventHandlerState](Context, stateID.String()); state == nil {
state = &eventHandlerState{}
SetState(Context, stateID, state)
SetState(Context, stateID.String(), state)
}

if eh.onActivate != nil && isActive && !state.isActive {
Expand Down
Loading
Loading