From 5ae391ec8ba9b5a25fda9cc9df70a3924f10d4bb Mon Sep 17 00:00:00 2001 From: gucio321 Date: Wed, 20 Oct 2021 19:19:30 +0200 Subject: [PATCH] add api for imgui.MemoryEditor (close #393) --- MemoryEditor.go | 60 +++++++++++++++++++++++++++ examples/memoryeditor/memoryeditor.go | 12 ++---- 2 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 MemoryEditor.go diff --git a/MemoryEditor.go b/MemoryEditor.go new file mode 100644 index 00000000..eef68cf1 --- /dev/null +++ b/MemoryEditor.go @@ -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 +} diff --git a/examples/memoryeditor/memoryeditor.go b/examples/memoryeditor/memoryeditor.go index 20585af9..e92462e6 100644 --- a/examples/memoryeditor/memoryeditor.go +++ b/examples/memoryeditor/memoryeditor.go @@ -4,13 +4,11 @@ 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() { @@ -18,16 +16,12 @@ func loop() { 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) }