Skip to content

Commit

Permalink
update deps; fix bg color setting
Browse files Browse the repository at this point in the history
  • Loading branch information
gucio321 committed Sep 17, 2023
1 parent 083bbe1 commit c3ab91a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 18 deletions.
4 changes: 2 additions & 2 deletions Context.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type state struct {
}

type context struct {
backend imgui.Backend
backend imgui.Backend[imgui.GLFWWindowFlags]

// TODO: should be handled by mainthread tbh
// see https://github.com/faiface/mainthread/pull/4
Expand All @@ -52,7 +52,7 @@ type context struct {
m *sync.Mutex
}

func CreateContext(b imgui.Backend) *context {
func CreateContext(b imgui.Backend[imgui.GLFWWindowFlags]) *context {
result := context{
cssStylesheet: make(cssStylesheet),
backend: b,
Expand Down
50 changes: 37 additions & 13 deletions MasterWindow.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,55 @@ import (
"image/color"
)

// MasterWindowFlags wraps imgui.GLFWWindowFlags.
type MasterWindowFlags imgui.GLFWWindowFlags
// MasterWindowFlags implements BackendWindowFlags
type MasterWindowFlags int

// master window flags.
const (
// Specifies the window will be fixed size.
MasterWindowFlagsNotResizable MasterWindowFlags = MasterWindowFlags(imgui.GLFWWindowFlagsNotResizable)
MasterWindowFlagsNotResizable MasterWindowFlags = 1 << iota
// Specifies whether the window is maximized.
MasterWindowFlagsMaximized MasterWindowFlags = MasterWindowFlags(imgui.GLFWWindowFlagsMaximized)
MasterWindowFlagsMaximized
// Specifies whether the window will be always-on-top.
MasterWindowFlagsFloating MasterWindowFlags = MasterWindowFlags(imgui.GLFWWindowFlagsFloating)
MasterWindowFlagsFloating
// Specifies whether the window will be frameless.
MasterWindowFlagsFrameless MasterWindowFlags = MasterWindowFlags(imgui.GLFWWindowFlagsFrameless)
MasterWindowFlagsFrameless
// Specifies whether the window will be transparent.
MasterWindowFlagsTransparent MasterWindowFlags = MasterWindowFlags(imgui.GLFWWindowFlagsTransparent)
MasterWindowFlagsTransparent
)

// parseAndApply converts MasterWindowFlags to appropiate imgui.GLFWWindowFlags.
func (m MasterWindowFlags) parseAndApply(b imgui.Backend[imgui.GLFWWindowFlags]) {
data := map[MasterWindowFlags]struct {
f imgui.GLFWWindowFlags
value int // value isn't always true (sometimes false). Also WindowHint takes int not bool
}{
MasterWindowFlagsNotResizable: {imgui.GLFWWindowFlagsResizable, 0},
MasterWindowFlagsMaximized: {imgui.GLFWWindowFlagsMaximized, 1},
MasterWindowFlagsFloating: {imgui.GLFWWindowFlagsFloating, 1},
MasterWindowFlagsFrameless: {imgui.GLFWWindowFlagsDecorated, 0},
MasterWindowFlagsTransparent: {imgui.GLFWWindowFlagsTransparent, 1},
}

for flag, d := range data {
if m&flag != 0 {
b.SetWindowFlags(d.f, d.value)
}
}
}

// TODO
// DontCare could be used as an argument to (*MasterWindow).SetSizeLimits.
// var DontCare int = imgui.GlfwDontCare

// MasterWindow represents a glfw master window
// It is a base for a windows (see Window.go).
type MasterWindow struct {
backend imgui.Backend
backend imgui.Backend[imgui.GLFWWindowFlags]

width int
height int
clearColor [4]float32
clearColor imgui.Vec4
title string
context *imgui.Context
io *imgui.IO
Expand Down Expand Up @@ -69,7 +90,7 @@ func NewMasterWindow(title string, width, height int, flags MasterWindowFlags) *
Context = CreateContext(backend)

mw := &MasterWindow{
clearColor: [4]float32{0, 0, 0, 1},
clearColor: imgui.Vec4{0, 0, 0, 1},
width: width,
height: height,
title: title,
Expand All @@ -81,7 +102,8 @@ func NewMasterWindow(title string, width, height int, flags MasterWindowFlags) *
backend.SetBeforeRenderHook(mw.beforeRender)
backend.SetAfterRenderHook(mw.afterRender)
backend.SetBeforeDestroyContextHook(mw.beforeDestroy)
backend.CreateWindow(title, width, height, imgui.GLFWWindowFlags(flags))
flags.parseAndApply(backend)
backend.CreateWindow(title, width, height)

mw.SetInputHandler(newInputHandler())

Expand Down Expand Up @@ -241,12 +263,14 @@ func (w *MasterWindow) SetBgColor(bgColor color.Color) {
const mask = 0xffff

r, g, b, a := bgColor.RGBA()
w.clearColor = [4]float32{
w.clearColor = imgui.Vec4{
float32(r) / mask,
float32(g) / mask,
float32(b) / mask,
float32(a) / mask,
}

w.backend.SetBgColor(w.clearColor)
}

// GetPos return position of master window.
Expand Down Expand Up @@ -283,7 +307,7 @@ func (w *MasterWindow) SetSize(x, y int) {
// Mac OS X: Selecting Quit from the application menu will trigger the close
// callback for all windows.
func (w *MasterWindow) SetCloseCallback(cb func() bool) {
w.backend.SetCloseCallback(func(b imgui.Backend) {
w.backend.SetCloseCallback(func(b imgui.Backend[imgui.GLFWWindowFlags]) {
b.SetShouldClose(cb())
})
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/AllenDang/giu
go 1.21

require (
github.com/AllenDang/cimgui-go v0.0.0-20230914121740-a353600541d2
github.com/AllenDang/cimgui-go v0.0.0-20230916161521-36bee41c6188
github.com/AllenDang/go-findfont v0.0.0-20200702051237-9f180485aeb8
github.com/faiface/mainthread v0.0.0-20171120011319-8b78f0a41ae3
github.com/mazznoer/csscolorparser v0.1.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/AllenDang/cimgui-go v0.0.0-20230914121740-a353600541d2 h1:w5r1Oc6/eUb2AXZRBFga3rae56PpIr8txoJyYxlxQRg=
github.com/AllenDang/cimgui-go v0.0.0-20230914121740-a353600541d2/go.mod h1:UH0IZCbsJKA1hAVQQU6AIDfamqTk0/d3VucaN8lp1Gs=
github.com/AllenDang/cimgui-go v0.0.0-20230916161521-36bee41c6188 h1:x5uIiCg1tgFXnn9yq8TGfCWuf25geOQTaiytkJ+c1jw=
github.com/AllenDang/cimgui-go v0.0.0-20230916161521-36bee41c6188/go.mod h1:UH0IZCbsJKA1hAVQQU6AIDfamqTk0/d3VucaN8lp1Gs=
github.com/AllenDang/go-findfont v0.0.0-20200702051237-9f180485aeb8 h1:dKZMqib/yUDoCFigmz2agG8geZ/e3iRq304/KJXqKyw=
github.com/AllenDang/go-findfont v0.0.0-20200702051237-9f180485aeb8/go.mod h1:b4uuDd0s6KRIPa84cEEchdQ9ICh7K0OryZHbSzMca9k=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down

0 comments on commit c3ab91a

Please sign in to comment.