Skip to content

Commit

Permalink
Context: put context initialization in context.go (not NewMasterWindow)
Browse files Browse the repository at this point in the history
  • Loading branch information
gucio321 committed Oct 21, 2021
1 parent cd8e82c commit 2880888
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 17 deletions.
22 changes: 22 additions & 0 deletions Context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
18 changes: 1 addition & 17 deletions MasterWindow.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
35 changes: 35 additions & 0 deletions examples/bug/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
25 changes: 25 additions & 0 deletions examples/child/main.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 2880888

Please sign in to comment.