From 288088814dce806b367fef5834c1e9dbc064cbe3 Mon Sep 17 00:00:00 2001 From: gucio321 Date: Thu, 21 Oct 2021 20:24:59 +0200 Subject: [PATCH] Context: put context initialization in context.go (not NewMasterWindow) --- Context.go | 22 ++++++++++++++++++++++ MasterWindow.go | 18 +----------------- examples/bug/main.go | 35 +++++++++++++++++++++++++++++++++++ examples/child/main.go | 25 +++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 17 deletions(-) create mode 100644 examples/bug/main.go create mode 100644 examples/child/main.go diff --git a/Context.go b/Context.go index 6498084c..5936af8b 100644 --- a/Context.go +++ b/Context.go @@ -34,6 +34,28 @@ type context struct { FontAtlas FontAtlas } +func CreateContext(p imgui.Platform, r imgui.Renderer) context { + result := context{ + platform: p, + renderer: r, + } + + result.FontAtlas = newFontAtlas() + + // Create font + if len(result.FontAtlas.defaultFonts) == 0 { + io := result.IO() + io.Fonts().AddFontDefault() + fontAtlas := io.Fonts().TextureDataRGBA32() + r.SetFontTexture(fontAtlas) + } else { + result.FontAtlas.shouldRebuildFontAtlas = true + // result.FontAtlas.rebuildFontAtlas() + } + + return result +} + func (c *context) GetRenderer() imgui.Renderer { return c.renderer } diff --git a/MasterWindow.go b/MasterWindow.go index 5dd9d0c1..6f91bb87 100644 --- a/MasterWindow.go +++ b/MasterWindow.go @@ -64,28 +64,12 @@ func NewMasterWindow(title string, width, height int, flags MasterWindowFlags) * panic(err) } - // Assign platform to contex - Context.platform = p - r, err := imgui.NewOpenGL3(io, 1.0) if err != nil { panic(err) } - // Create context - Context.renderer = r - - Context.FontAtlas = newFontAtlas() - - // Create font - if len(Context.FontAtlas.defaultFonts) == 0 { - io.Fonts().AddFontDefault() - fontAtlas := io.Fonts().TextureDataRGBA32() - r.SetFontTexture(fontAtlas) - } else { - Context.FontAtlas.shouldRebuildFontAtlas = true - Context.FontAtlas.rebuildFontAtlas() - } + Context = CreateContext(p, r) mw := &MasterWindow{ clearColor: [4]float32{0, 0, 0, 1}, diff --git a/examples/bug/main.go b/examples/bug/main.go new file mode 100644 index 00000000..358ff2f1 --- /dev/null +++ b/examples/bug/main.go @@ -0,0 +1,35 @@ +package main + +import ( + "fmt" + + "github.com/AllenDang/giu" +) + +const popupID = "somepopupmodal" + +func loop() { + items := make([]string, 200) + for i := range items { + items[i] = fmt.Sprintf("Item %d", i) + } + + giu.SingleWindow().Layout( + giu.Row( + giu.ListBox("listbox", items).Size(300, 400), + giu.ListBox("listbox2", items).Size(300, 400), + ), + giu.Popup(popupID).Layout( + giu.Row( + giu.Button("Do something").OnClick(func() { fmt.Println("Something done") }), + giu.Button("close me").OnClick(func() { giu.CloseCurrentPopup() }), + ), + ), + giu.Button("Open popup").OnClick(func() { giu.OpenPopup(popupID) }), + ) +} + +func main() { + wnd := giu.NewMasterWindow("#390 [bug]", 640, 480, 0) + wnd.Run(loop) +} diff --git a/examples/child/main.go b/examples/child/main.go new file mode 100644 index 00000000..73eaaaf0 --- /dev/null +++ b/examples/child/main.go @@ -0,0 +1,25 @@ +package main + +import ( + "github.com/AllenDang/giu" + "github.com/AllenDang/imgui-go" +) + +func loop() { + giu.SingleWindow().Layout( + giu.Custom(func() { + imgui.PushStyleColor(imgui.StyleColorChildBg, imgui.CurrentStyle().GetColor(imgui.StyleColorWindowBg)) + if imgui.BeginChild("child") { + giu.Button("button").Build() + } + imgui.EndChild() + imgui.PopStyleColor() + }), + giu.Label("label"), + ) +} + +func main() { + wnd := giu.NewMasterWindow("native child layout [demo]", 640, 480, 0) + wnd.Run(loop) +}