-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
MasterWindow.go
424 lines (358 loc) · 15.6 KB
/
MasterWindow.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package giu
import (
"errors"
"image"
"image/color"
"runtime"
"github.com/AllenDang/cimgui-go/backend"
"github.com/AllenDang/cimgui-go/backend/glfwbackend"
"github.com/AllenDang/cimgui-go/imgui"
"github.com/AllenDang/cimgui-go/imguizmo"
"github.com/AllenDang/cimgui-go/imnodes"
"github.com/AllenDang/cimgui-go/implot"
"golang.org/x/image/colornames"
)
// MasterWindowFlags implements BackendWindowFlags.
type MasterWindowFlags int
// master window flags.
const (
// Specifies the window will be fixed size.
MasterWindowFlagsNotResizable MasterWindowFlags = 1 << iota
// Specifies whether the window is maximized.
MasterWindowFlagsMaximized
// Specifies whether the window will be always-on-top.
MasterWindowFlagsFloating
// Specifies whether the window will be frameless.
MasterWindowFlagsFrameless
// Specifies whether the window will be transparent.
MasterWindowFlagsTransparent
// Specifies whether the window will be hidden (for use with multiple windows).
MasterWindowFlagsHidden
)
// parseAndApply converts MasterWindowFlags to appropriate glfwbackend.GLFWWindowFlags.
func (m MasterWindowFlags) parseAndApply(b backend.Backend[glfwbackend.GLFWWindowFlags]) {
data := map[MasterWindowFlags]struct {
f glfwbackend.GLFWWindowFlags
value int // value isn't always true (sometimes false). Also WindowHint takes int not bool
}{
MasterWindowFlagsNotResizable: {glfwbackend.GLFWWindowFlagsResizable, 0},
MasterWindowFlagsMaximized: {glfwbackend.GLFWWindowFlagsMaximized, 1},
MasterWindowFlagsFloating: {glfwbackend.GLFWWindowFlagsFloating, 1},
MasterWindowFlagsFrameless: {glfwbackend.GLFWWindowFlagsDecorated, 0},
MasterWindowFlagsTransparent: {glfwbackend.GLFWWindowFlagsTransparent, 1},
MasterWindowFlagsHidden: {glfwbackend.GLFWWindowFlagsVisible, 0},
}
for flag, d := range data {
if m&flag != 0 {
b.SetWindowFlags(d.f, d.value)
}
}
}
// TODO(gucio321) implement this in cimgui-go
// 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 backend.Backend[glfwbackend.GLFWWindowFlags]
width int
height int
clearColor imgui.Vec4
title string
context *imgui.Context
io *imgui.IO
updateFunc func()
// possibility to expend InputHandler's stuff
// See SetAdditionalInputHandler
additionalInputCallback InputHandlerHandleCallback
}
// NewMasterWindow creates a new master window and initializes GLFW.
// it should be called in main function. For more details and use cases,
// see examples/helloworld/.
func NewMasterWindow(title string, width, height int, flags MasterWindowFlags) *MasterWindow {
imGuiContext := imgui.CreateContext()
implot.CreateContext()
imnodes.CreateContext()
io := imgui.CurrentIO()
// TODO: removed ConfigFlagEnablePowerSavingMode
// TODO: removed io.SetConfigFlags(imgui.BackendFlagsRendererHasVtxOffset)
io.SetBackendFlags(imgui.BackendFlagsRendererHasVtxOffset)
// Disable imgui.ini
io.SetIniFilename("")
currentBackend, err := backend.CreateBackend(glfwbackend.NewGLFWBackend())
if err != nil && !errors.Is(err, backend.CExposerError) {
panic(err)
}
// Create GIU context
Context = CreateContext(currentBackend)
mw := &MasterWindow{
clearColor: imgui.Vec4{X: 0, Y: 0, Z: 0, W: 1},
width: width,
height: height,
title: title,
io: io,
context: imGuiContext,
backend: currentBackend,
}
currentBackend.SetBeforeRenderHook(mw.beforeRender)
currentBackend.SetAfterRenderHook(mw.afterRender)
currentBackend.SetBeforeDestroyContextHook(mw.beforeDestroy)
flags.parseAndApply(currentBackend)
currentBackend.CreateWindow(title, width, height)
mw.SetInputHandler(newInputHandler())
mw.backend.SetSizeChangeCallback(mw.sizeChange)
mw.SetBgColor(colornames.Black)
// Scale DPI in windows
if runtime.GOOS == "windows" {
xScale, _ := Context.backend.ContentScale()
imgui.CurrentStyle().ScaleAllSizes(xScale)
}
return mw
}
func (w *MasterWindow) setTheme() (fin func()) {
imgui.PushStyleVarFloat(imgui.StyleVarWindowRounding, 2)
imgui.PushStyleVarFloat(imgui.StyleVarFrameRounding, 4)
imgui.PushStyleVarFloat(imgui.StyleVarGrabRounding, 4)
imgui.PushStyleVarFloat(imgui.StyleVarFrameBorderSize, 1)
imgui.PushStyleColorVec4(imgui.ColText, imgui.Vec4{X: 0.95, Y: 0.96, Z: 0.98, W: 1.00})
imgui.PushStyleColorVec4(imgui.ColTextDisabled, imgui.Vec4{X: 0.36, Y: 0.42, Z: 0.47, W: 1.00})
imgui.PushStyleColorVec4(imgui.ColWindowBg, imgui.Vec4{X: 0.11, Y: 0.15, Z: 0.17, W: 1.00})
imgui.PushStyleColorVec4(imgui.ColChildBg, imgui.Vec4{X: 0.15, Y: 0.18, Z: 0.22, W: 1.00})
imgui.PushStyleColorVec4(imgui.ColPopupBg, imgui.Vec4{X: 0.08, Y: 0.08, Z: 0.08, W: 0.94})
imgui.PushStyleColorVec4(imgui.ColBorder, imgui.Vec4{X: 0.08, Y: 0.10, Z: 0.12, W: 1.00})
imgui.PushStyleColorVec4(imgui.ColBorderShadow, imgui.Vec4{X: 0.00, Y: 0.00, Z: 0.00, W: 0.00})
imgui.PushStyleColorVec4(imgui.ColFrameBg, imgui.Vec4{X: 0.20, Y: 0.25, Z: 0.29, W: 1.00})
imgui.PushStyleColorVec4(imgui.ColFrameBgHovered, imgui.Vec4{X: 0.12, Y: 0.20, Z: 0.28, W: 1.00})
imgui.PushStyleColorVec4(imgui.ColFrameBgActive, imgui.Vec4{X: 0.09, Y: 0.12, Z: 0.14, W: 1.00})
imgui.PushStyleColorVec4(imgui.ColTitleBg, imgui.Vec4{X: 0.09, Y: 0.12, Z: 0.14, W: 0.65})
imgui.PushStyleColorVec4(imgui.ColTitleBgActive, imgui.Vec4{X: 0.08, Y: 0.10, Z: 0.12, W: 1.00})
imgui.PushStyleColorVec4(imgui.ColTitleBgCollapsed, imgui.Vec4{X: 0.00, Y: 0.00, Z: 0.00, W: 0.51})
imgui.PushStyleColorVec4(imgui.ColMenuBarBg, imgui.Vec4{X: 0.15, Y: 0.18, Z: 0.22, W: 1.00})
imgui.PushStyleColorVec4(imgui.ColScrollbarBg, imgui.Vec4{X: 0.02, Y: 0.02, Z: 0.02, W: 0.39})
imgui.PushStyleColorVec4(imgui.ColScrollbarGrab, imgui.Vec4{X: 0.20, Y: 0.25, Z: 0.29, W: 1.00})
imgui.PushStyleColorVec4(imgui.ColScrollbarGrabHovered, imgui.Vec4{X: 0.18, Y: 0.22, Z: 0.25, W: 1.00})
imgui.PushStyleColorVec4(imgui.ColScrollbarGrabActive, imgui.Vec4{X: 0.09, Y: 0.21, Z: 0.31, W: 1.00})
imgui.PushStyleColorVec4(imgui.ColCheckMark, imgui.Vec4{X: 0.28, Y: 0.56, Z: 1.00, W: 1.00})
imgui.PushStyleColorVec4(imgui.ColSliderGrab, imgui.Vec4{X: 0.28, Y: 0.56, Z: 1.00, W: 1.00})
imgui.PushStyleColorVec4(imgui.ColSliderGrabActive, imgui.Vec4{X: 0.37, Y: 0.61, Z: 1.00, W: 1.00})
imgui.PushStyleColorVec4(imgui.ColButton, imgui.Vec4{X: 0.20, Y: 0.25, Z: 0.29, W: 1.00})
imgui.PushStyleColorVec4(imgui.ColButtonHovered, imgui.Vec4{X: 0.28, Y: 0.56, Z: 1.00, W: 1.00})
imgui.PushStyleColorVec4(imgui.ColButtonActive, imgui.Vec4{X: 0.06, Y: 0.53, Z: 0.98, W: 1.00})
imgui.PushStyleColorVec4(imgui.ColHeader, imgui.Vec4{X: 0.20, Y: 0.25, Z: 0.29, W: 0.55})
imgui.PushStyleColorVec4(imgui.ColHeaderHovered, imgui.Vec4{X: 0.26, Y: 0.59, Z: 0.98, W: 0.80})
imgui.PushStyleColorVec4(imgui.ColHeaderActive, imgui.Vec4{X: 0.26, Y: 0.59, Z: 0.98, W: 1.00})
imgui.PushStyleColorVec4(imgui.ColSeparator, imgui.Vec4{X: 0.20, Y: 0.25, Z: 0.29, W: 1.00})
imgui.PushStyleColorVec4(imgui.ColSeparatorHovered, imgui.Vec4{X: 0.10, Y: 0.40, Z: 0.75, W: 0.78})
imgui.PushStyleColorVec4(imgui.ColSeparatorActive, imgui.Vec4{X: 0.10, Y: 0.40, Z: 0.75, W: 1.00})
imgui.PushStyleColorVec4(imgui.ColResizeGrip, imgui.Vec4{X: 0.26, Y: 0.59, Z: 0.98, W: 0.25})
imgui.PushStyleColorVec4(imgui.ColResizeGripHovered, imgui.Vec4{X: 0.26, Y: 0.59, Z: 0.98, W: 0.67})
imgui.PushStyleColorVec4(imgui.ColResizeGripActive, imgui.Vec4{X: 0.26, Y: 0.59, Z: 0.98, W: 0.95})
imgui.PushStyleColorVec4(imgui.ColTab, imgui.Vec4{X: 0.11, Y: 0.15, Z: 0.17, W: 1.00})
imgui.PushStyleColorVec4(imgui.ColTabHovered, imgui.Vec4{X: 0.26, Y: 0.59, Z: 0.98, W: 0.80})
imgui.PushStyleColorVec4(imgui.ColTabSelected, imgui.Vec4{X: 0.20, Y: 0.25, Z: 0.29, W: 1.00})
imgui.PushStyleColorVec4(imgui.ColTabDimmed, imgui.Vec4{X: 0.11, Y: 0.15, Z: 0.17, W: 1.00})
imgui.PushStyleColorVec4(imgui.ColTabDimmedSelected, imgui.Vec4{X: 0.11, Y: 0.15, Z: 0.17, W: 1.00})
imgui.PushStyleColorVec4(imgui.ColPlotLines, imgui.Vec4{X: 0.61, Y: 0.61, Z: 0.61, W: 1.00})
imgui.PushStyleColorVec4(imgui.ColPlotLinesHovered, imgui.Vec4{X: 1.00, Y: 0.43, Z: 0.35, W: 1.00})
imgui.PushStyleColorVec4(imgui.ColPlotHistogram, imgui.Vec4{X: 0.90, Y: 0.70, Z: 0.00, W: 1.00})
imgui.PushStyleColorVec4(imgui.ColPlotHistogramHovered, imgui.Vec4{X: 1.00, Y: 0.60, Z: 0.00, W: 1.00})
imgui.PushStyleColorVec4(imgui.ColTextSelectedBg, imgui.Vec4{X: 0.26, Y: 0.59, Z: 0.98, W: 0.35})
imgui.PushStyleColorVec4(imgui.ColDragDropTarget, imgui.Vec4{X: 1.00, Y: 1.00, Z: 0.00, W: 0.90})
imgui.PushStyleColorVec4(imgui.ColNavWindowingHighlight, imgui.Vec4{X: 0.26, Y: 0.59, Z: 0.98, W: 1.00})
imgui.PushStyleColorVec4(imgui.ColNavWindowingHighlight, imgui.Vec4{X: 1.00, Y: 1.00, Z: 1.00, W: 0.70})
imgui.PushStyleColorVec4(imgui.ColTableHeaderBg, imgui.Vec4{X: 0.12, Y: 0.20, Z: 0.28, W: 1.00})
imgui.PushStyleColorVec4(imgui.ColTableBorderStrong, imgui.Vec4{X: 0.20, Y: 0.25, Z: 0.29, W: 1.00})
imgui.PushStyleColorVec4(imgui.ColTableBorderLight, imgui.Vec4{X: 0.20, Y: 0.25, Z: 0.29, W: 0.70})
return func() {
imgui.PopStyleColorV(49)
imgui.PopStyleVarV(4)
}
}
func (w *MasterWindow) sizeChange(_, _ int) {
// noop
}
func (w *MasterWindow) beforeRender() {
Context.FontAtlas.rebuildFontAtlas()
// process texture load requests
if Context.textureLoadingQueue != nil && Context.textureLoadingQueue.Length() > 0 {
for Context.textureLoadingQueue.Length() > 0 {
request, ok := Context.textureLoadingQueue.Remove().(textureLoadRequest)
Assert(ok, "MasterWindow", "Run", "processing texture requests: wrong type of texture request")
NewTextureFromRgba(request.img, request.cb)
}
}
// process texture free requests
if Context.textureFreeingQueue != nil && Context.textureFreeingQueue.Length() > 0 {
for Context.textureFreeingQueue.Length() > 0 {
request, ok := Context.textureFreeingQueue.Remove().(textureFreeRequest)
Assert(ok, "MasterWindow", "Run", "processing texture requests: wrong type of texture request")
request.tex.tex.Release()
}
}
}
func (w *MasterWindow) afterRender() {
}
func (w *MasterWindow) beforeDestroy() {
implot.DestroyContext()
imnodes.DestroyContext()
}
func (w *MasterWindow) render() {
imguizmo.BeginFrame()
Context.cleanStates()
defer Context.SetDirty()
fin := w.setTheme()
defer fin()
mainStylesheet := Style()
if s, found := Context.cssStylesheet["main"]; found {
mainStylesheet = s
}
mainStylesheet.Push()
w.updateFunc()
mainStylesheet.Pop()
}
// Run runs the main loop.
// loopFunc will be used to construct the ui.
// Run should be called at the end of main function, after setting
// up the master window.
func (w *MasterWindow) Run(loopFunc func()) {
mainthreadCallPlatform(func() {
Context.isRunning = true
w.updateFunc = loopFunc
Context.m.Lock()
Context.isAlive = true
Context.m.Unlock()
Context.backend.Run(w.render)
Context.m.Lock()
Context.isAlive = false
Context.isRunning = false
Context.m.Unlock()
})
}
// GetSize return size of master window.
func (w *MasterWindow) GetSize() (width, height int) {
if w.backend != nil {
w, h := w.backend.DisplaySize()
return int(w), int(h)
}
return w.width, w.height
}
// SetBgColor sets background color of master window.
func (w *MasterWindow) SetBgColor(bgColor color.Color) {
const mask = 0xffff
r, g, b, a := bgColor.RGBA()
w.clearColor = imgui.Vec4{
X: float32(r) / mask,
Y: float32(g) / mask,
Z: float32(b) / mask,
W: float32(a) / mask,
}
w.backend.SetBgColor(w.clearColor)
}
// SetTargetFPS sets target FPS of master window.
// Default for GLFW is 30.
func (w *MasterWindow) SetTargetFPS(fps uint) {
w.backend.SetTargetFPS(fps)
}
// GetPos return position of master window.
func (w *MasterWindow) GetPos() (x, y int) {
var xResult, yResult int32
if w.backend != nil {
xResult, yResult = w.backend.GetWindowPos()
}
return int(xResult), int(yResult)
}
// SetPos sets position of master window.
func (w *MasterWindow) SetPos(x, y int) {
if w.backend != nil {
w.backend.SetWindowPos(x, y)
}
}
// SetSize sets size of master window.
func (w *MasterWindow) SetSize(x, y int) {
if w.backend != nil {
w.backend.SetWindowSize(x, y)
}
}
// SetCloseCallback sets the close callback of the window, which is called when
// the user attempts to close the window, for example by clicking the close
// widget in the title bar.
//
// The close flag is set before this callback is called, but you can modify it at
// any time with returned value of callback function.
//
// 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 backend.Backend[glfwbackend.GLFWWindowFlags]) {
b.SetShouldClose(cb())
})
}
// SetDropCallback sets callback when file was dropped into the window.
func (w *MasterWindow) SetDropCallback(cb func([]string)) {
w.backend.SetDropCallback(cb)
}
// RegisterKeyboardShortcuts registers a global - master window - keyboard shortcuts.
func (w *MasterWindow) RegisterKeyboardShortcuts(s ...WindowShortcut) *MasterWindow {
for _, shortcut := range s {
Context.InputHandler.RegisterKeyboardShortcuts(Shortcut{
Key: shortcut.Key,
Modifier: shortcut.Modifier,
Callback: shortcut.Callback,
IsGlobal: GlobalShortcut,
})
}
return w
}
// SetIcon sets the icon of the specified window. If passed an array of candidate images,
// those of or closest to the sizes desired by the system are selected. If no images are
// specified, the window reverts to its default icon.
//
// The image is ideally provided in the form of *image.NRGBA.
// The pixels are 32-bit, little-endian, non-premultiplied RGBA, i.e. eight
// bits per channel with the red channel first. They are arranged canonically
// as packed sequential rows, starting from the top-left corner. If the image
// type is not *image.NRGBA, it will be converted to it.
//
// The desired image sizes varies depending on platform and system settings. The selected
// images will be rescaled as needed. Good sizes include 16x16, 32x32 and 48x48.
func (w *MasterWindow) SetIcon(icons ...image.Image) {
w.backend.SetIcons(icons...)
}
// SetSizeLimits sets the size limits of the client area of the specified window.
// If the window is full screen or not resizable, this function does nothing.
//
// The size limits are applied immediately and may cause the window to be resized.
// To specify only a minimum size or only a maximum one, set the other pair to giu.DontCare.
// To disable size limits for a window, set them all to giu.DontCare.
func (w *MasterWindow) SetSizeLimits(minw, minh, maxw, maxh int) {
w.backend.SetWindowSizeLimits(minw, minh, maxw, maxh)
}
// SetTitle updates master window's title.
func (w *MasterWindow) SetTitle(title string) {
w.backend.SetWindowTitle(title)
}
// Close will safely close the master window.
func (w *MasterWindow) Close() {
w.SetShouldClose(true)
}
// SetShouldClose sets whether master window should be closed.
func (w *MasterWindow) SetShouldClose(v bool) {
w.backend.SetShouldClose(v)
}
// SetInputHandler allows to change default input handler.
// see InputHandler.go.
func (w *MasterWindow) SetInputHandler(handler InputHandler) {
Context.InputHandler = handler
w.backend.SetKeyCallback(func(key, _, action, modifier int) {
k, m, a := keyFromGLFWKey(glfwbackend.GLFWKey(key)), Modifier(modifier), Action(action)
handler.Handle(k, m, a)
if w.additionalInputCallback != nil {
w.additionalInputCallback(k, m, a)
}
})
}
// SetAdditionalInputHandlerCallback allows to set an input callback to handle more events (not only these from giu.inputHandler).
// See examples/issue-501.
func (w *MasterWindow) SetAdditionalInputHandlerCallback(cb InputHandlerHandleCallback) {
w.additionalInputCallback = cb
}