Skip to content

Commit

Permalink
Merge pull request #394 from gucio321/memoryeditor-api
Browse files Browse the repository at this point in the history
add api for imgui.MemoryEditor
  • Loading branch information
AllenDang authored Oct 21, 2021
2 parents 7d858ce + 5ae391e commit 1a18181
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 9 deletions.
60 changes: 60 additions & 0 deletions MemoryEditor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package giu

import (
"github.com/AllenDang/imgui-go"
)

type memoryEditorState struct {
editor imgui.MemoryEditor
}

// Dispose implements Disposable interface.
func (s *memoryEditorState) Dispose() {
// noop
}

// MemoryEditorWidget - Mini memory editor for Dear ImGui
// (to embed in your game/tools)
//
// Right-click anywhere to access the Options menu!
// You can adjust the keyboard repeat delay/rate in ImGuiIO.
// The code assume a mono-space font for simplicity!
// If you don't use the default font, use ImGui::PushFont()/PopFont() to switch to a mono-space font before calling this.
type MemoryEditorWidget struct {
id string
contents []byte
}

// MemoryEditor creates nwe memory editor widget.
func MemoryEditor() *MemoryEditorWidget {
return &MemoryEditorWidget{
id: GenAutoID("memoryEditor"),
}
}

// Contents sets editor's conents.
func (me *MemoryEditorWidget) Contents(contents []byte) *MemoryEditorWidget {
me.contents = contents
return me
}

// Build implements widget inetrface.
func (me *MemoryEditorWidget) Build() {
me.getState().editor.DrawContents(me.contents)
}

func (me *MemoryEditorWidget) getState() (state *memoryEditorState) {
if s := Context.GetState(me.id); s == nil {
state = &memoryEditorState{
editor: imgui.NewMemoryEditor(),
}

Context.SetState(me.id, state)
} else {
var ok bool
state, ok = s.(*memoryEditorState)
Assert(ok, "MemoryEditorWidget", "getState", "incorrect state type recovered.")
}

return state
}
12 changes: 3 additions & 9 deletions examples/memoryeditor/memoryeditor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,24 @@ import (
"fmt"

g "github.com/AllenDang/giu"
"github.com/AllenDang/imgui-go"
)

var (
buf []uint8
memEditor imgui.MemoryEditor
content string
buf []uint8
content string
)

func loop() {
g.SingleWindow().Layout(
g.Button("Print data value").OnClick(func() {
fmt.Println(buf)
}),
g.Custom(func() {
memEditor.DrawContents(buf)
}),
g.MemoryEditor().Contents(buf),
)
}

func main() {
buf = []uint8{1, 2, 3, 4, 5, 6, 7}
memEditor = imgui.NewMemoryEditor()

wnd := g.NewMasterWindow("Memory Editor", 800, 600, 0)
wnd.Run(loop)
}

0 comments on commit 1a18181

Please sign in to comment.