-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow.v
731 lines (676 loc) · 15.7 KB
/
window.v
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
// Copyright (c) 2020 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by a GPL license that can be found in the LICENSE file.
module ui
import gx
import gg
import clipboard
import eventbus
import sokol.sapp
const (
default_window_color = gx.rgb(236, 236, 236)
default_font_size = 13
)
pub type DrawFn = fn (ctx &gg.Context, state voidptr)
pub type ClickFn = fn (e MouseEvent, window &Window)
pub type KeyFn = fn (e KeyEvent, func voidptr)
pub type ScrollFn = fn (e ScrollEvent, window &Window)
pub type MouseMoveFn = fn (e MouseMoveEvent, window &Window)
[ref_only]
pub struct Window {
pub mut:
// pub:
ui &UI = voidptr(0)
// glfw_obj &glfw.Window = voidptr(0)
children []Widget
child_window &Window = voidptr(0)
parent_window &Window = voidptr(0)
has_textbox bool // for initial focus
tab_index int
just_tabbed bool
state voidptr
draw_fn DrawFn
title string
mx f64
my f64
width int
height int
bg_color gx.Color
click_fn ClickFn
mouse_down_fn ClickFn
mouse_up_fn ClickFn
scroll_fn ScrollFn
key_down_fn KeyFn
char_fn KeyFn
mouse_move_fn MouseMoveFn
eventbus &eventbus.EventBus = eventbus.new()
// resizable has limitation https://github.com/vlang/ui/issues/231
resizable bool // currently only for events.on_resized not modify children
}
pub struct WindowConfig {
pub:
width int
height int
resizable bool
title string
always_on_top bool
state voidptr
draw_fn DrawFn
bg_color gx.Color = default_window_color
on_click ClickFn
on_mouse_down ClickFn
on_mouse_up ClickFn
on_key_down KeyFn
on_scroll ScrollFn
on_mouse_move MouseMoveFn
children []Widget
font_path string
custom_bold_font_path string
native_rendering bool
// pub mut:
// parent_window &Window
}
/*
pub fn window2(cfg WindowConfig) &Window {
return window(cfg, cfg.children)
}
*/
fn C.sapp_mouse_locked() bool
fn on_event(e &sapp.Event, mut window Window) {
/*
if false && e.typ != .mouse_move {
print('window.on_event() $e.typ ') // code=$e.char_code')
if C.sapp_mouse_locked() {
println('locked')
} else {
println('unlocked')
}
}
*/
window.ui.needs_refresh = true
// window.refresh()
$if macos {
if window.ui.gg.native_rendering {
if e.typ in [.key_down, .mouse_scroll, .mouse_up] {
C.darwin_window_refresh()
} else {
C.darwin_window_refresh()
}
}
}
window.ui.ticks = 0
// window.ui.ticks_since_refresh = 0
match e.typ {
.mouse_up {
// println('click')
window_mouse_up(e, window.ui)
window_click(e, window.ui)
}
.mouse_down {
window_mouse_down(e, window.ui)
}
.key_down {
// println('key down')
window_key_down(e, window.ui)
}
.char {
// println('char')
window_char(e, window.ui)
}
.mouse_scroll {
window_scroll(e, window.ui)
}
.mouse_move {
// println('mod=$e.modifiers $e.num_touches $e.key_repeat $e.mouse_button')
window_mouse_move(e, window.ui)
}
.resized {
window_resize(e, window.ui)
}
else {}
}
/*
if e.typ == .key_down {
game.key_down(e.key_code)
}
*/
}
fn gg_init(mut window Window) {
for _, child in window.children {
// if child is Stack {
// }
/*
match child {
Stack {
println('column')
}
TextBox {
println('textbox')
}
else{}
}
*/
child.init(window)
}
}
pub fn window(cfg WindowConfig, children []Widget) &Window {
/*
println('window()')
defer {
println('end of window()')
}
*/
C.printf('window() state =%p \n', cfg.state)
mut window := &Window{
state: cfg.state
draw_fn: cfg.draw_fn
title: cfg.title
bg_color: cfg.bg_color
width: cfg.width
height: cfg.height
children: children
click_fn: cfg.on_click
key_down_fn: cfg.on_key_down
scroll_fn: cfg.on_scroll
mouse_move_fn: cfg.on_mouse_move
mouse_down_fn: cfg.on_mouse_down
mouse_up_fn: cfg.on_mouse_up
resizable: cfg.resizable
}
gcontext := gg.new_context(
width: cfg.width
height: cfg.height
use_ortho: true // This is needed for 2D drawing
create_window: true
window_title: cfg.title
resizable: cfg.resizable
frame_fn: if cfg.native_rendering { native_frame } else { frame }
// native_frame_fn: native_frame
event_fn: on_event
user_data: window
font_path: if cfg.font_path == '' { gg.system_font_path() } else { cfg.font_path }
custom_bold_font_path: cfg.custom_bold_font_path
init_fn: gg_init
// keydown_fn: window_key_down
// char_fn: window_char
bg_color: cfg.bg_color // gx.rgb(230,230,230)
// window_state: ui
native_rendering: cfg.native_rendering
)
// wsize := gcontext.window.get_window_size()
// fsize := gcontext.window.get_framebuffer_size()
// scale := 2 //if wsize.width == fsize.width { 1 } else { 2 } // detect high dpi displays
mut ui_ctx := &UI{
gg: gcontext
clipboard: clipboard.new()
}
ui_ctx.load_icos()
/*
ui_ctx.gg.window.set_user_ptr(ui_ctx)
ui_ctx.gg.window.onkeydown(window_key_down)
ui_ctx.gg.window.onchar(window_char)
ui_ctx.gg.window.onmousemove(window_mouse_move)
ui_ctx.gg.window.on_click(window_click)
ui_ctx.gg.window.on_resize(window_resize)
ui_ctx.gg.window.on_scroll(window_scroll)
*/
window.ui = ui_ctx
/*
mut window := &Window{
state: cfg.state
ui: ui_ctx
//glfw_obj: ui_ctx.gg.window
draw_fn: cfg.draw_fn
title: cfg.title
bg_color: cfg.bg_color
width: cfg.width
height: cfg.height
children: children
click_fn: cfg.on_click
key_down_fn: cfg.on_key_down
scroll_fn: cfg.on_scroll
}
*/
// q := int(window)
// println('created window $q.hex()')
return window
}
pub fn child_window(cfg WindowConfig, mut parent_window Window, children []Widget) &Window {
// q := int(parent_window)
// println('child_window() parent=$q.hex()')
mut window := &Window{
parent_window: parent_window
// state: parent_window.state
state: cfg.state
ui: parent_window.ui
// glfw_obj: parent_window.ui.gg.window
draw_fn: cfg.draw_fn
title: cfg.title
bg_color: cfg.bg_color
width: cfg.width
height: cfg.height
children: children
click_fn: cfg.on_click
}
parent_window.child_window = window
for _, child in window.children {
// using `parent_window` here so that all events handled by the main window are redirected
// to parent_window.child_window.child
child.init(parent_window)
}
// window.set_cursor()
return window
}
/*
fn window_mouse_move(glfw_wnd voidptr, x, y f64) {
ui := &UI(glfw.get_window_user_pointer(glfw_wnd))
mut window := ui.window
x0,y0 := glfw.get_cursor_pos(glfw_wnd)
window.mx = int(x0)
window.my = int(y0)
e := MouseEvent{
x: int(x0)
y: int(y0)
}
/* if window.mouse_move_fn != 0 {
window.mouse_move_fn(e, &ui.window)
}
for child in window.children {
inside := child.point_inside(x, y) // TODO if ... doesn't work with interface calls
if inside {
child.mouse_move(e)
}
} */
window.eventbus.publish(events.on_mouse_move, &window, e)
}
fn window_resize(glfw_wnd voidptr, width int, height int) {
/*
ui := &UI(glfw.get_window_user_pointer(glfw_wnd))
window := ui.window
window.resize(width, height)
*/
}
*/
fn window_mouse_move(event sapp.Event, ui &UI) {
window := ui.window
e := MouseMoveEvent{
x: event.mouse_x / ui.gg.scale
y: event.mouse_y / ui.gg.scale
mouse_button: int(event.mouse_button)
}
if window.mouse_move_fn != voidptr(0) {
window.mouse_move_fn(e, window)
}
window.eventbus.publish(events.on_mouse_move, window, e)
}
fn window_scroll(event sapp.Event, ui &UI) {
window := ui.window
// println('title =$window.title')
e := ScrollEvent{
x: event.scroll_x
y: event.scroll_y
}
if window.scroll_fn != voidptr(0) {
window.scroll_fn(e, window)
}
window.eventbus.publish(events.on_scroll, window, e)
}
fn window_mouse_down(event sapp.Event, ui &UI) {
window := ui.window
e := MouseEvent{
action: .down
x: int(event.mouse_x / ui.gg.scale)
y: int(event.mouse_y / ui.gg.scale)
button: MouseButton(event.mouse_button)
mods: KeyMod(event.modifiers)
}
if window.mouse_down_fn != voidptr(0) { // && action == voidptr(0) {
window.mouse_down_fn(e, window)
}
/*
for child in window.children {
inside := child.point_inside(x, y) // TODO if ... doesn't work with interface calls
if inside {
child.click(e)
}
}
*/
if window.child_window != 0 {
// If there's a child window, use it, so that the widget receives correct user pointer
window.eventbus.publish(events.on_mouse_down, window.child_window, e)
} else {
window.eventbus.publish(events.on_mouse_down, window, e)
}
}
fn window_mouse_up(event sapp.Event, ui &UI) {
window := ui.window
e := MouseEvent{
action: .up
x: int(event.mouse_x / ui.gg.scale)
y: int(event.mouse_y / ui.gg.scale)
button: MouseButton(event.mouse_button)
mods: KeyMod(event.modifiers)
}
if window.mouse_up_fn != voidptr(0) { // && action == voidptr(0) {
window.mouse_up_fn(e, window)
}
/*
for child in window.children {
inside := child.point_inside(x, y) // TODO if ... doesn't work with interface calls
if inside {
child.click(e)
}
}
*/
if window.child_window != 0 {
// If there's a child window, use it, so that the widget receives correct user pointer
window.eventbus.publish(events.on_mouse_up, window.child_window, e)
} else {
window.eventbus.publish(events.on_mouse_up, window, e)
}
}
fn window_click(event sapp.Event, ui &UI) {
window := ui.window
e := MouseEvent{
action: if event.typ == .mouse_up { MouseAction.up } else { MouseAction.down }
x: int(event.mouse_x / ui.gg.scale)
y: int(event.mouse_y / ui.gg.scale)
button: MouseButton(event.mouse_button)
mods: KeyMod(event.modifiers)
}
if window.click_fn != voidptr(0) { // && action == voidptr(0) {
window.click_fn(e, window)
}
/*
for child in window.children {
inside := child.point_inside(x, y) // TODO if ... doesn't work with interface calls
if inside {
child.click(e)
}
}
*/
if window.child_window != 0 {
// If there's a child window, use it, so that the widget receives correct user pointer
window.eventbus.publish(events.on_click, window.child_window, e)
} else {
window.eventbus.publish(events.on_click, window, e)
}
}
fn window_key_down(event sapp.Event, ui &UI) {
// println('keydown char=$event.char_code')
mut window := ui.window
// C.printf('g child=%p\n', child)
e := KeyEvent{
key: Key(event.key_code)
mods: KeyMod(event.modifiers)
codepoint: 0 // event.char_code
// code: code
// action: action
// mods: mod
}
if e.key == .escape {
println('escape')
}
if e.key == .escape && window.child_window != 0 {
// Close the child window on Escape
window.child_window = &Window(0)
}
if window.key_down_fn != voidptr(0) {
window.key_down_fn(e, window.state)
}
// TODO
if true { // action == 2 || action == 1 {
window.eventbus.publish(events.on_key_down, window, e)
} else {
window.eventbus.publish(events.on_key_up, window, e)
}
/*
for child in window.children {
is_focused := child.is_focused()
if !is_focused {
continue
}
child.key_down()
}
*/
}
// fn window_char(glfw_wnd voidptr, codepoint u32) {
fn window_char(event sapp.Event, ui &UI) {
// println('keychar char=$event.char_code')
window := ui.window
e := KeyEvent{
codepoint: event.char_code
mods: KeyMod(event.modifiers)
}
if window.key_down_fn != voidptr(0) {
window.key_down_fn(e, window.state)
}
window.eventbus.publish(events.on_key_down, window, e)
/*
for child in window.children {
is_focused := child.is_focused()
if !is_focused {
continue
}
child.key_down()
}
*/
}
fn window_resize(event sapp.Event, ui &UI) {
mut window := ui.window
if window.resizable {
window.resize(event.window_width, event.window_height)
window.eventbus.publish(events.on_resize, window, voidptr(0))
}
}
fn (mut w Window) focus_next() {
mut doit := false
for child in w.children {
// Focus on the next widget
if doit {
child.focus()
break
}
is_focused := child.is_focused()
if is_focused {
doit = true
}
}
w.just_tabbed = true
}
fn (w &Window) focus_previous() {
for i, child in w.children {
is_focused := child.is_focused()
if is_focused && i > 0 {
prev := w.children[i - 1]
prev.focus()
// w.children[i - 1].focus()
}
}
}
pub fn (w &Window) set_cursor(cursor Cursor) {
// glfw.set_cursor(.ibeam)
// w.glfw_obj.set_cursor(.ibeam)
}
pub fn (w &Window) close() {
}
pub fn (mut w Window) refresh() {
// println('ui: window.refres()')
w.ui.needs_refresh = true
$if macos {
C.darwin_window_refresh()
}
}
pub fn (w &Window) onmousedown(cb voidptr) {
}
pub fn (w &Window) onkeydown(cb voidptr) {
}
pub fn (mut w Window) on_click(func ClickFn) {
w.click_fn = func
}
pub fn (mut w Window) on_mousemove(func MouseMoveFn) {
w.mouse_move_fn = func
}
pub fn (mut w Window) on_scroll(func ScrollFn) {
w.scroll_fn = func
}
pub fn (w &Window) mouse_inside(x int, y int, width int, height int) bool {
return false
}
pub fn (w &Window) focus() {
}
pub fn (w &Window) always_on_top(val bool) {
// w.glfw_obj.window_hint(
}
// TODO remove this once interfaces are smarter
fn foo(w Widget) {
}
fn foo2(l Layout) {
}
fn bar() {
foo(&TextBox{
ui: 0
})
foo(&Button{
ui: 0
})
foo(&ProgressBar{
ui: 0
})
foo(&Slider{
ui: 0
})
foo(&CheckBox{
ui: 0
})
foo(&Label{
ui: 0
})
foo(&Radio{
ui: 0
})
foo(&Picture{
ui: 0
})
foo(&Canvas{})
foo(&Menu{
ui: 0
})
foo(&Dropdown{
ui: 0
})
foo(&Transition{
ui: 0
animated_value: 0
})
foo(&Stack{
ui: 0
})
foo(&Switch{
ui: 0
})
foo(&Rectangle{
ui: 0
})
foo(&Group{
ui: 0
})
}
fn (w &Window) draw() {
}
fn frame(mut w Window) {
if !w.ui.needs_refresh {
// Draw 3 more frames after the "stop refresh" command
w.ui.ticks++
if w.ui.ticks > 3 {
return
}
}
// println('frame() needs_refresh=$w.ui.needs_refresh $w.ui.ticks nr children=$w.children.len')
// game.frame_sw.restart()
// game.ft.flush()
w.ui.gg.begin()
// draw_scene()
if w.child_window == 0 {
// Render all widgets, including Canvas
for child in w.children {
child.draw()
}
}
// w.showfps()
else if w.child_window != 0 {
for child in w.child_window.children {
child.draw()
}
}
w.ui.gg.end()
w.ui.needs_refresh = false
}
fn native_frame(mut w Window) {
// println('naative_frame()')
if !w.ui.needs_refresh {
// Draw 3 more frames after the "stop refresh" command
w.ui.ticks++
if w.ui.ticks > 3 {
return
}
}
if w.child_window == 0 {
// Render all widgets, including Canvas
for child in w.children {
child.draw()
}
}
// w.showfps()
else if w.child_window != 0 {
for child in w.child_window.children {
child.draw()
}
}
w.ui.needs_refresh = false
}
// fn C.sapp_macos_get_window() voidptr
fn C.sapp_set_window_title(charptr)
// #define cls objc_getClass
// #define sel sel_getUid
#define objc_msg ((id (*)(id, SEL, ...))objc_msgSend)
#define objc_cls_msg ((id (*)(Class, SEL, ...))objc_msgSend)
fn C.objc_msg()
fn C.objc_cls_msg()
fn C.sel_getUid()
fn C.objc_getClass()
pub fn (mut w Window) set_title(title string) {
w.title = title
/*
$if macos {
x := C.sapp_macos_get_window()
C.objc_msg(x, C.sel_getUid("setTitle:"), C.objc_cls_msg(C.objc_getClass("NSString"),
C.sel_getUid("stringWithUTF8String:"),"Pure C App"))
println('SETTING')
#[nsw setTitlee:"test string"];
}
*/
C.sapp_set_window_title(title.str)
}
// Layout Interface Methods
fn (w &Window) get_ui() &UI {
return w.ui
}
fn (w &Window) get_state() voidptr {
return w.state
}
pub fn (w &Window) get_subscriber() &eventbus.Subscriber {
return w.eventbus.subscriber
}
fn (w &Window) size() (int, int) {
return w.width, w.height
}
fn (mut window Window) resize(width int, height int) {
window.width = width
window.height = height
}
fn (window &Window) unfocus_all() {
println('window.unfocus_all()')
for child in window.children {
child.unfocus()
}
}