-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow.go
505 lines (424 loc) · 11.3 KB
/
window.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
package main
import (
"image"
"image/color"
"sync"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/text"
"github.com/hajimehoshi/ebiten/v2/vector"
)
var windowsLock sync.Mutex
var windows []*windowData = []*windowData{
{
idName: "game options",
title: "Options",
size: XYs{X: 250, Y: 275},
centered: true,
closeable: true,
windowDraw: drawOptionsWindow,
windowSetup: setupOptionsWindow,
movable: true,
windowMouse: handleOptions,
},
{
idName: "game help",
title: "Help & Controls",
size: XYs{X: 300, Y: 270},
centered: false,
position: XYs{X: 8, Y: 46},
closeable: true,
windowDraw: drawHelpWindow,
windowMouse: handleHelpWindow,
movable: true,
},
{
idName: "game login",
title: "Login to goMMO:",
size: XYs{X: 300, Y: 200},
movable: false,
opaque: true,
centered: true,
closeable: false,
windowDraw: drawLoginWindow,
windowMouse: handleLoginWindow,
windowKeys: handleLoginKeys,
},
}
var openWindows map[string]*windowData
type windowData struct {
idName string
title string // Window title
focused bool //Mouse is on window
movable bool // Can be dragged
opaque bool // Non-semitransparent background
centered bool // Auto-centered
borderless bool
closeable bool // Has a close-x in title bar
keepCache bool // Draw cache persists when window is closed
dragPos XYs // Position where window drag began
titleButtons titleButtonData // Window title buttons
buttons []buttonData
size XYs // Size in pixels
scaledSize XYs // Size with UI scale
position XYs // Position
bgColor *color.Color // Custom BG color
titleBGColor *color.Color // Custom title bar background color
titleColor *color.Color // Custom title text color
dirty bool // Needs to be redrawn
cache *ebiten.Image // Cache image
windowDraw func(Window *windowData)
windowMouse func(input XYs, window *windowData) bool
windowKeys func(input []rune, window *windowData, delete, enter bool) bool
windowSetup func(Window *windowData)
}
type buttonData struct {
text string
image *ebiten.Image
rect image.Rectangle
activate func()
}
type titleButtonData struct {
closePos XYs
closeSize XYs
titleBarHeight int
}
// Toggle settings window
func settingsToggle() {
defer reportPanic("settingsToggle")
if openWindows["game options"] != nil {
closeWindow("game options")
} else {
openWindow("game options")
}
}
// Toggle help window
func toggleHelp() {
defer reportPanic("toggleHelp")
if openWindows["game help"] != nil {
closeWindow("game help")
} else {
openWindow("game help")
}
}
// Allow windows to do any precalculation they need to do
func initWindows() {
defer reportPanic("initWindows")
for _, win := range windows {
if win.windowSetup != nil {
win.windowSetup(win)
win.dirty = true
}
}
}
// Draw whatever windows are currently open
func drawOpenWindows(screen *ebiten.Image) {
defer reportPanic("drawOpenWindows")
for _, win := range openWindows {
if win.focused {
drawWindow(screen, win)
}
}
for _, win := range openWindows {
if !win.focused {
drawWindow(screen, win)
}
}
}
// Open a window
// Until layering is added, close other windows if we open one
func openWindow(idName string) {
defer reportPanic("openWindow")
windowsLock.Lock()
defer windowsLock.Unlock()
//Window is already open
if openWindows[idName] != nil {
return
}
var window *windowData
for wpos, win := range windows {
if win.idName == idName {
window = windows[wpos]
break
}
}
//Window not found
if window == nil {
return
}
if window.centered && window.movable {
window.scaledSize = XYs{X: int32(float64(window.size.X) * uiScale), Y: int32(float64(window.size.Y) * uiScale)}
window.position = XYs{
X: int32(screenX/2) - (window.scaledSize.X / 2),
Y: int32(screenY/2) - (window.scaledSize.Y / 2)}
}
if windowDebugMode {
doLog(true, "Window '%v' added to open list.", window.title)
}
//Close open windows, for now
openWindows = make(map[string]*windowData)
//Add to open windows list
openWindows[window.idName] = window
}
// Close a window
func closeWindow(idName string) {
defer reportPanic("closeWindow")
windowsLock.Lock()
defer windowsLock.Unlock()
window := openWindows[idName]
//No such window
if window == nil {
return
}
//Handle window closed while dragging
if draggingWindow == window {
draggingWindow = nil
}
//Dispose window image cache if needed
if !window.keepCache && window.cache != nil {
if windowDebugMode {
doLog(true, "Window '%v' closed, disposing cache.", window.title)
}
window.cache.Dispose()
window.cache = nil
}
// Eat click event
clickCaptured = true
mouseHeld = false
//Remove from open list
delete(openWindows, window.idName)
}
const closePad = 18
const closeScale = 0.7
// Draw window title, frame, background and cached window contents
func dragWindow() {
windowsLock.Lock()
defer windowsLock.Unlock()
if draggingWindow == nil {
return
}
draggingWindow.position = XYs{X: int32(mouseX) - draggingWindow.dragPos.X, Y: int32(mouseY) - draggingWindow.dragPos.Y}
clickCaptured = true
// Clamp position
clampWindow(draggingWindow)
}
func clampUIWindows() {
windowsLock.Lock()
defer windowsLock.Unlock()
for _, window := range openWindows {
clampWindow(window)
}
}
func clampWindow(w *windowData) {
if w == nil {
return
}
//Clamp position
if w.position.X < 0 {
w.position.X = 0
} else if w.position.X > int32(screenX)-w.size.X {
w.position.X = int32(screenX) - w.size.X
}
if w.position.Y < 0 {
w.position.Y = 0
} else if w.position.Y > int32(screenY)-w.size.Y {
w.position.Y = int32(screenY) - w.size.Y
}
}
func drawWindow(screen *ebiten.Image, window *windowData) {
defer reportPanic("drawWindow")
windowsLock.Lock()
defer windowsLock.Unlock()
//Calculate some values for UI scale
pad := int(closePad * uiScale)
halfPad := int((closePad / 2.0) * uiScale)
winPos := getWindowPos(window)
window.scaledSize = XYs{X: int32(float64(window.size.X) * uiScale), Y: int32(float64(window.size.Y) * uiScale)}
//If window not dirty, and it has a cache, draw the cache
if !window.dirty {
if window.cache != nil {
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(winPos.X), float64(winPos.Y))
screen.DrawImage(window.cache, op)
return
}
}
//If there is no window cache, init it
if window.cache == nil {
window.cache = ebiten.NewImage(int(window.scaledSize.X), int(window.scaledSize.Y))
if windowDebugMode {
doLog(true, "Window '%v' cache initalized.", window.title)
}
} else {
window.cache.Clear()
}
//Custom colors
var winBG color.Color
if window.bgColor != nil {
winBG = *window.bgColor
} else if window.opaque {
winBG = ColorWindowBGO
} else {
winBG = ColorWindowBG
}
var titleBGColor color.Color
if window.titleBGColor != nil {
titleBGColor = *window.titleBGColor
} else {
titleBGColor = ColorWindowTitle
}
var titleColor color.Color
if window.titleBGColor != nil {
titleColor = *window.titleColor
} else {
titleColor = color.White
}
//Draw window BG
vector.DrawFilledRect(
window.cache,
0, 0,
float32(window.scaledSize.X), float32(window.scaledSize.Y),
winBG, false)
if window.title != "" {
fHeight := text.BoundString(generalFont, "!Aa0")
// Border
if !window.borderless {
vector.DrawFilledRect(
window.cache, 0, +float32(window.scaledSize.Y)-1,
float32(window.scaledSize.X), 2, titleBGColor, false,
)
vector.DrawFilledRect(
window.cache,
0, 0,
2, float32(window.scaledSize.Y),
titleBGColor, false)
vector.DrawFilledRect(
window.cache,
float32(window.scaledSize.X)-1, 0,
2, float32(window.scaledSize.Y),
titleBGColor, false)
}
// Title bar
vector.DrawFilledRect(
window.cache, 0, 0,
float32(window.scaledSize.X), float32(float64(fHeight.Dy()))+float32(pad), titleBGColor, false,
)
window.titleButtons.titleBarHeight = fHeight.Dy() + pad
text.Draw(window.cache, window.title, generalFont, halfPad, int(fHeight.Dy()+halfPad), titleColor)
if window.closeable {
img := closeBox
op := &ebiten.DrawImageOptions{Filter: ebiten.FilterLinear}
closePosX := float64(window.scaledSize.X - int32(float64(img.Bounds().Dx())*uiScale*closeScale))
op.GeoM.Scale(uiScale*closeScale, uiScale*closeScale)
op.GeoM.Translate(closePosX, 0)
// save button positions
window.titleButtons.closePos = XYs{X: int32(closePosX), Y: int32(0)}
window.titleButtons.closeSize = XYs{X: int32(float64(img.Bounds().Dx()) * uiScale),
Y: int32(float64(img.Bounds().Dy()) * uiScale)}
window.cache.DrawImage(img, op)
}
}
// Call custom draw function, if it exists
if window.windowDraw != nil {
window.windowDraw(window)
}
window.dirty = false
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(winPos.X), float64(winPos.Y))
screen.DrawImage(window.cache, op)
}
// Check if a click is within an open window
func collisionWindowsCheck(input XYs) bool {
defer reportPanic("collisionWindowsCheck")
if clickCaptured {
return true
}
for _, win := range openWindows {
if collisionWindow(input, win) {
return true
}
}
return false
}
// Check if a click is within a specific window
func collisionWindow(input XYs, window *windowData) bool {
defer reportPanic("collisionWindow")
winPos := getWindowPos(window)
if input.X > winPos.X && input.X < winPos.X+window.scaledSize.X &&
input.Y > winPos.Y && input.Y < winPos.Y+window.scaledSize.Y {
if !window.focused {
window.focused = true
}
// Handle X close
if handleClose(input, window) {
return true
}
if handleDrag(input, window) {
return true
}
// Handle input
if window.windowMouse != nil {
window.windowMouse(input, window)
}
return true
} else {
if window.focused {
window.focused = false
}
return false
}
}
// Check if a click was within a window's close box
func handleClose(input XYs, window *windowData) bool {
defer reportPanic("handleCLose")
if draggingWindow != nil {
return false
}
if !window.closeable {
return false
}
winPos := getWindowPos(window)
if input.X > winPos.X+window.scaledSize.X-window.titleButtons.closeSize.X &&
input.X < winPos.X+window.scaledSize.X &&
input.Y < winPos.Y+window.titleButtons.closeSize.Y &&
input.Y > winPos.Y {
closeWindow(window.idName)
return true
}
return false
}
// Handle dragging windows
func handleDrag(input XYs, window *windowData) bool {
defer reportPanic("handleDrag")
if !mouseHeld {
return false
}
if !window.movable {
return false
}
if draggingWindow != nil {
return true
}
winPos := getWindowPos(window)
winOff := XYs{X: input.X - winPos.X, Y: input.Y - winPos.Y}
if input.X > winPos.X &&
input.X < winPos.X+window.scaledSize.X &&
input.Y > winPos.Y &&
input.Y < winPos.Y+int32(window.titleButtons.titleBarHeight) {
draggingWindow = window
draggingWindow.dragPos = winOff
doLog(true, "dragging window '%v'", window.title)
return true
}
return false
}
// Get window position, assists with auto-centered windows
func getWindowPos(window *windowData) XYs {
defer reportPanic("getWindowPos")
var winPos XYs
if window.centered && !window.movable {
winPos.X, winPos.Y = int32(screenX/2)-(window.scaledSize.X/2), int32(screenY/2)-(window.scaledSize.Y/2)
} else {
winPos = window.position
}
return winPos
}