MemoryEditor.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package giu
  2. import (
  3. "github.com/AllenDang/imgui-go"
  4. )
  5. type memoryEditorState struct {
  6. editor imgui.MemoryEditor
  7. }
  8. // Dispose implements Disposable interface.
  9. func (s *memoryEditorState) Dispose() {
  10. // noop
  11. }
  12. // MemoryEditorWidget - Mini memory editor for Dear ImGui
  13. // (to embed in your game/tools)
  14. //
  15. // Right-click anywhere to access the Options menu!
  16. // You can adjust the keyboard repeat delay/rate in ImGuiIO.
  17. // The code assume a mono-space font for simplicity!
  18. // If you don't use the default font, use ImGui::PushFont()/PopFont() to switch to a mono-space font before calling this.
  19. type MemoryEditorWidget struct {
  20. id string
  21. contents []byte
  22. }
  23. // MemoryEditor creates nwe memory editor widget.
  24. func MemoryEditor() *MemoryEditorWidget {
  25. return &MemoryEditorWidget{
  26. id: GenAutoID("memoryEditor"),
  27. }
  28. }
  29. // Contents sets editor's conents.
  30. func (me *MemoryEditorWidget) Contents(contents []byte) *MemoryEditorWidget {
  31. me.contents = contents
  32. return me
  33. }
  34. // Build implements widget inetrface.
  35. func (me *MemoryEditorWidget) Build() {
  36. me.getState().editor.DrawContents(me.contents)
  37. }
  38. func (me *MemoryEditorWidget) getState() (state *memoryEditorState) {
  39. if s := Context.GetState(me.id); s == nil {
  40. state = &memoryEditorState{
  41. editor: imgui.NewMemoryEditor(),
  42. }
  43. Context.SetState(me.id, state)
  44. } else {
  45. var ok bool
  46. state, ok = s.(*memoryEditorState)
  47. Assert(ok, "MemoryEditorWidget", "getState", "incorrect state type recovered.")
  48. }
  49. return state
  50. }