forked from AllenDang/imgui-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlatformGlfw.go
511 lines (409 loc) · 14.5 KB
/
PlatformGlfw.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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
package imgui
import (
"fmt"
"image"
"math"
"runtime"
"github.com/HACKERALERT/glfw/v3.3/glfw"
)
var GlfwDontCare int = glfw.DontCare
type GLFWClipboard struct {
window *glfw.Window
}
func NewGLFWClipboard(w *glfw.Window) *GLFWClipboard {
return &GLFWClipboard{window: w}
}
func (c *GLFWClipboard) Text() (string, error) {
return c.window.GetClipboardString(), nil
}
func (c *GLFWClipboard) SetText(text string) {
c.window.SetClipboardString(text)
}
type GLFWWindowFlags uint8
const (
GLFWWindowFlagsNotResizable GLFWWindowFlags = 1 << iota
GLFWWindowFlagsMaximized
GLFWWindowFlagsFloating
GLFWWindowFlagsFrameless
GLFWWindowFlagsTransparent
)
// GLFW implements a platform based on github.com/HACKERALERT/glfw (v3.3).
type GLFW struct {
imguiIO IO
window *glfw.Window
tps int
time float64
mouseJustPressed [3]bool
mouseCursors map[int]*glfw.Cursor
posChangeCallback func(int, int)
sizeChangeCallback func(int, int)
dropCallback func([]string)
inputCallback func(key glfw.Key, mods glfw.ModifierKey, action glfw.Action)
closeCallback func() bool
}
// NewGLFW attempts to initialize a GLFW context.
func NewGLFW(io IO, title string, width, height int, flags GLFWWindowFlags) (*GLFW, error) {
runtime.LockOSThread()
err := glfw.Init()
if err != nil {
return nil, fmt.Errorf("failed to initialize glfw: %v", err)
}
glfw.WindowHint(glfw.ContextVersionMajor, 3)
glfw.WindowHint(glfw.ContextVersionMinor, 3)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
glfw.WindowHint(glfw.OpenGLForwardCompatible, 1)
glfw.WindowHint(glfw.ScaleToMonitor, glfw.True)
glfw.WindowHint(glfw.Visible, glfw.False)
if flags&GLFWWindowFlagsNotResizable != 0 {
glfw.WindowHint(glfw.Resizable, glfw.False)
}
if flags&GLFWWindowFlagsMaximized != 0 {
glfw.WindowHint(glfw.Maximized, glfw.True)
}
if flags&GLFWWindowFlagsFloating != 0 {
glfw.WindowHint(glfw.Floating, glfw.True)
}
if flags&GLFWWindowFlagsFrameless != 0 {
glfw.WindowHint(glfw.Decorated, glfw.False)
}
if flags&GLFWWindowFlagsTransparent != 0 {
glfw.WindowHint(glfw.TransparentFramebuffer, glfw.True)
}
window, err := glfw.CreateWindow(width, height, title, nil, nil)
if err != nil {
glfw.Terminate()
return nil, fmt.Errorf("failed to create window: %v", err)
}
window.MakeContextCurrent()
glfw.SwapInterval(1)
platform := &GLFW{
imguiIO: io,
window: window,
tps: 60,
}
platform.setKeyMapping()
platform.installCallbacks()
// Create mosue cursors
platform.mouseCursors = make(map[int]*glfw.Cursor)
platform.mouseCursors[MouseCursorArrow] = glfw.CreateStandardCursor(glfw.ArrowCursor)
platform.mouseCursors[MouseCursorTextInput] = glfw.CreateStandardCursor(glfw.IBeamCursor)
platform.mouseCursors[MouseCursorResizeAll] = glfw.CreateStandardCursor(glfw.CrosshairCursor)
platform.mouseCursors[MouseCursorHand] = glfw.CreateStandardCursor(glfw.HandCursor)
platform.mouseCursors[MouseCursorResizeEW] = glfw.CreateStandardCursor(glfw.HResizeCursor)
platform.mouseCursors[MouseCursorResizeNS] = glfw.CreateStandardCursor(glfw.VResizeCursor)
io.SetClipboard(NewGLFWClipboard(window))
if flags&GLFWWindowFlagsMaximized == 0 {
// Center window to monitor
platform.centerWindow()
}
platform.window.Show()
return platform, nil
}
// Dispose cleans up the resources.
func (platform *GLFW) Dispose() {
platform.window.Destroy()
glfw.Terminate()
}
func (platform *GLFW) GetContentScale() float32 {
x, _ := platform.window.GetContentScale()
// Do not scale on MacOS
if runtime.GOOS == "darwin" {
x = 1
}
return x
}
func (platform *GLFW) GetWindow() *glfw.Window {
return platform.window
}
func (platform *GLFW) GetPos() (x, y int) {
return platform.window.GetPos()
}
func (platform *GLFW) centerWindow() {
monitor := platform.getBestMonitor()
if monitor == nil {
return
}
mode := monitor.GetVideoMode()
if mode == nil {
return
}
monitorX, monitorY := monitor.GetPos()
windowWidth, windowHeight := platform.window.GetSize()
platform.window.SetPos(monitorX+(mode.Width-windowWidth)/2, monitorY+(mode.Height-windowHeight)/2)
}
func (platform *GLFW) getBestMonitor() *glfw.Monitor {
monitors := glfw.GetMonitors()
if len(monitors) == 0 {
return nil
}
width, height := platform.window.GetSize()
x, y := platform.window.GetPos()
var bestMonitor *glfw.Monitor
var bestArea int
for _, m := range monitors {
monitorX, monitorY := m.GetPos()
mode := m.GetVideoMode()
if mode == nil {
continue
}
areaMinX := int(math.Max(float64(x), float64(monitorX)))
areaMinY := int(math.Max(float64(y), float64(monitorY)))
areaMaxX := int(math.Min(float64(x+width), float64(monitorX+mode.Width)))
areaMaxY := int(math.Min(float64(y+height), float64(monitorY+mode.Height)))
area := (areaMaxX - areaMinX) * (areaMaxY - areaMinY)
if area > bestArea {
bestArea = area
bestMonitor = m
}
}
return bestMonitor
}
// ShouldStop returns true if the window is to be closed.
func (platform *GLFW) ShouldStop() bool {
return platform.window.ShouldClose()
}
// SetShouldStop sets whether window should be closed
func (platform *GLFW) SetShouldStop(v bool) {
platform.window.SetShouldClose(v)
}
func (platform *GLFW) WaitForEvent() {
if platform.imguiIO.GetConfigFlags()&ConfigFlagEnablePowerSavingMode == 0 {
return
}
windowIsHidden := platform.window.GetAttrib(glfw.Visible) == glfw.False || platform.window.GetAttrib(glfw.Iconified) == glfw.True
waitingTime := math.Inf(0)
if !windowIsHidden {
waitingTime = GetEventWaitingTime()
}
if waitingTime > 0 {
if math.IsInf(waitingTime, 0) {
glfw.WaitEvents()
} else {
glfw.WaitEventsTimeout(waitingTime)
}
}
}
// ProcessEvents handles all pending window events.
func (platform *GLFW) ProcessEvents() {
platform.WaitForEvent()
glfw.PollEvents()
}
// DisplaySize returns the dimension of the display.
func (platform *GLFW) DisplaySize() [2]float32 {
w, h := platform.window.GetSize()
return [2]float32{float32(w), float32(h)}
}
// FramebufferSize returns the dimension of the framebuffer.
func (platform *GLFW) FramebufferSize() [2]float32 {
w, h := platform.window.GetFramebufferSize()
return [2]float32{float32(w), float32(h)}
}
// NewFrame marks the begin of a render pass. It forwards all current state to imgui IO.
func (platform *GLFW) NewFrame() {
// Setup display size (every frame to accommodate for window resizing)
displaySize := platform.DisplaySize()
platform.imguiIO.SetDisplaySize(Vec2{X: displaySize[0], Y: displaySize[1]})
// Setup time step
currentTime := glfw.GetTime()
if platform.time > 0 {
platform.imguiIO.SetDeltaTime(float32(currentTime - platform.time))
}
platform.time = currentTime
// Setup inputs
if platform.window.GetAttrib(glfw.Focused) != 0 {
x, y := platform.window.GetCursorPos()
platform.imguiIO.SetMousePosition(Vec2{X: float32(x), Y: float32(y)})
} else {
platform.imguiIO.SetMousePosition(Vec2{X: -math.MaxFloat32, Y: -math.MaxFloat32})
}
for i := 0; i < len(platform.mouseJustPressed); i++ {
down := platform.mouseJustPressed[i] || (platform.window.GetMouseButton(glfwButtonIDByIndex[i]) == glfw.Press)
platform.imguiIO.SetMouseButtonDown(i, down)
platform.mouseJustPressed[i] = false
}
platform.updateMouseCursor()
}
// PostRender performs a buffer swap.
func (platform *GLFW) PostRender() {
platform.window.SwapBuffers()
}
func (platform *GLFW) SetPosChangeCallback(cb func(int, int)) {
platform.posChangeCallback = cb
}
func (platform *GLFW) SetSizeChangeCallback(cb func(int, int)) {
platform.sizeChangeCallback = cb
}
func (platform *GLFW) Update() {
glfw.PostEmptyEvent()
}
func (platform *GLFW) SetDropCallback(cb func(names []string)) {
platform.dropCallback = cb
}
func (platform *GLFW) SetInputCallback(cb func(key glfw.Key, mods glfw.ModifierKey, action glfw.Action)) {
platform.inputCallback = cb
}
func (platform *GLFW) SetCloseCallback(cb func() bool) {
platform.closeCallback = cb
}
func (platform *GLFW) updateMouseCursor() {
io := platform.imguiIO
if (io.GetConfigFlags()&ConfigFlagNoMouseCursorChange) == 1 || platform.window.GetInputMode(glfw.CursorMode) == glfw.CursorDisabled {
return
}
cursor := MouseCursor()
if cursor == MouseCursorNone || io.GetMouseDrawCursor() {
platform.window.SetInputMode(glfw.CursorMode, glfw.CursorHidden)
} else {
gCursor := platform.mouseCursors[MouseCursorArrow]
if c, ok := platform.mouseCursors[cursor]; ok {
gCursor = c
}
platform.window.SetCursor(gCursor)
platform.window.SetInputMode(glfw.CursorMode, glfw.CursorNormal)
}
}
func (platform *GLFW) setKeyMapping() {
// Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array.
platform.imguiIO.KeyMap(KeyTab, int(glfw.KeyTab))
platform.imguiIO.KeyMap(KeyLeftArrow, int(glfw.KeyLeft))
platform.imguiIO.KeyMap(KeyRightArrow, int(glfw.KeyRight))
platform.imguiIO.KeyMap(KeyUpArrow, int(glfw.KeyUp))
platform.imguiIO.KeyMap(KeyDownArrow, int(glfw.KeyDown))
platform.imguiIO.KeyMap(KeyPageUp, int(glfw.KeyPageUp))
platform.imguiIO.KeyMap(KeyPageDown, int(glfw.KeyPageDown))
platform.imguiIO.KeyMap(KeyHome, int(glfw.KeyHome))
platform.imguiIO.KeyMap(KeyEnd, int(glfw.KeyEnd))
platform.imguiIO.KeyMap(KeyInsert, int(glfw.KeyInsert))
platform.imguiIO.KeyMap(KeyDelete, int(glfw.KeyDelete))
platform.imguiIO.KeyMap(KeyBackspace, int(glfw.KeyBackspace))
platform.imguiIO.KeyMap(KeySpace, int(glfw.KeySpace))
platform.imguiIO.KeyMap(KeyEnter, int(glfw.KeyEnter))
platform.imguiIO.KeyMap(KeyEscape, int(glfw.KeyEscape))
platform.imguiIO.KeyMap(KeyA, int(glfw.KeyA))
platform.imguiIO.KeyMap(KeyC, int(glfw.KeyC))
platform.imguiIO.KeyMap(KeyV, int(glfw.KeyV))
platform.imguiIO.KeyMap(KeyX, int(glfw.KeyX))
platform.imguiIO.KeyMap(KeyY, int(glfw.KeyY))
platform.imguiIO.KeyMap(KeyZ, int(glfw.KeyZ))
}
func (platform *GLFW) installCallbacks() {
platform.window.SetMouseButtonCallback(platform.mouseButtonChange)
platform.window.SetScrollCallback(platform.mouseScrollChange)
platform.window.SetKeyCallback(platform.keyChange)
platform.window.SetCharCallback(platform.charChange)
platform.window.SetSizeCallback(platform.sizeChange)
platform.window.SetDropCallback(platform.onDrop)
platform.window.SetPosCallback(platform.posChange)
platform.window.SetCloseCallback(platform.onClose)
platform.window.SetFocusCallback(platform.onFocus)
}
var glfwButtonIndexByID = map[glfw.MouseButton]int{
glfw.MouseButton1: 0,
glfw.MouseButton2: 1,
glfw.MouseButton3: 2,
}
var glfwButtonIDByIndex = map[int]glfw.MouseButton{
0: glfw.MouseButton1,
1: glfw.MouseButton2,
2: glfw.MouseButton3,
}
func (platform *GLFW) onFocus(window *glfw.Window, focused bool) {
platform.imguiIO.AddFocusEvent(focused)
}
func (platform *GLFW) onClose(window *glfw.Window) {
if platform.closeCallback != nil {
shouldClose := platform.closeCallback()
window.SetShouldClose(shouldClose)
}
}
func (platform *GLFW) onDrop(window *glfw.Window, names []string) {
window.Focus()
if platform.dropCallback != nil {
platform.dropCallback(names)
}
}
func (platform *GLFW) posChange(window *glfw.Window, x, y int) {
platform.imguiIO.SetFrameCountSinceLastInput(0)
// Notfy pos changed and redraw.
if platform.posChangeCallback != nil {
platform.posChangeCallback(x, y)
}
}
func (platform *GLFW) sizeChange(window *glfw.Window, width, height int) {
platform.imguiIO.SetFrameCountSinceLastInput(0)
// Notify size changed and redraw.
if platform.sizeChangeCallback != nil {
platform.sizeChangeCallback(width, height)
}
}
func (platform *GLFW) mouseButtonChange(window *glfw.Window, rawButton glfw.MouseButton, action glfw.Action, mods glfw.ModifierKey) {
platform.imguiIO.SetFrameCountSinceLastInput(0)
buttonIndex, known := glfwButtonIndexByID[rawButton]
if known && (action == glfw.Press) {
platform.mouseJustPressed[buttonIndex] = true
}
}
func (platform *GLFW) mouseScrollChange(window *glfw.Window, x, y float64) {
platform.imguiIO.SetFrameCountSinceLastInput(0)
platform.imguiIO.AddMouseWheelDelta(float32(x), float32(y))
}
func (platform *GLFW) keyChange(window *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
platform.imguiIO.SetFrameCountSinceLastInput(0)
if action == glfw.Press {
platform.imguiIO.KeyPress(int(key))
}
if action == glfw.Release {
platform.imguiIO.KeyRelease(int(key))
}
// Modifiers are not reliable across systems
platform.imguiIO.KeyCtrl(int(glfw.KeyLeftControl), int(glfw.KeyRightControl))
platform.imguiIO.KeyShift(int(glfw.KeyLeftShift), int(glfw.KeyRightShift))
platform.imguiIO.KeyAlt(int(glfw.KeyLeftAlt), int(glfw.KeyRightAlt))
platform.imguiIO.KeySuper(int(glfw.KeyLeftSuper), int(glfw.KeyRightSuper))
if platform.inputCallback != nil {
platform.inputCallback(key, mods, action)
}
}
func (platform *GLFW) charChange(window *glfw.Window, char rune) {
platform.imguiIO.SetFrameCountSinceLastInput(0)
platform.imguiIO.AddInputCharacters(string(char))
}
func (platform *GLFW) GetClipboard() string {
return platform.window.GetClipboardString()
}
func (platform *GLFW) SetClipboard(content string) {
platform.window.SetClipboardString(content)
}
func (platform *GLFW) GetTPS() int {
return platform.tps
}
func (platform *GLFW) SetTPS(tps int) {
platform.tps = tps
}
// 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 (platform *GLFW) SetIcon(icons []image.Image) {
platform.window.SetIcon(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 GLFW_DONT_CARE.
// To disable size limits for a window, set them all to GLFW_DONT_CARE.
func (platform *GLFW) SetSizeLimits(minw, minh, maxw, maxh int) {
platform.window.SetSizeLimits(minw, minh, maxw, maxh)
}
func (platform *GLFW) SetTitle(title string) {
platform.window.SetTitle(title)
}