-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathbutton.v
More file actions
539 lines (501 loc) · 13.6 KB
/
button.v
File metadata and controls
539 lines (501 loc) · 13.6 KB
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
// Copyright (c) 2020-2022 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by a MIT license
// that can be found in the LICENSE file.
module ui
import gg
import os
import math
const button_bg_color = gg.rgb(28, 28, 28)
const button_border_color = gg.rgb(200, 200, 200)
const button_focus_border_color = gg.rgb(50, 50, 50)
const button_horizontal_padding = 26
const button_vertical_padding = 8
enum ButtonState {
normal = 1 // synchronized with .button_normal
pressed
hovering
}
pub type ButtonFn = fn (&Button)
pub type ButtonU32Fn = fn (&Button, u32)
pub type ButtonMouseFn = fn (&Button, &MouseEvent)
pub type ButtonMouseMoveFn = fn (&Button, &MouseMoveEvent)
@[heap]
pub struct Button {
// init size read-only
pub:
width_ int
height_ int
pub mut:
id string
state ButtonState = .normal
height int
width int
z_index int
x int
y int
offset_x int
offset_y int
text_width int
text_height int
parent Layout = empty_stack
is_focused bool
ui &UI = unsafe { nil }
on_click ButtonFn = unsafe { nil }
// TODO: same convention for all callback
on_key_down ButtonU32Fn = unsafe { nil }
on_mouse_down ButtonMouseFn = unsafe { nil }
on_mouse_up ButtonMouseFn = unsafe { nil }
on_mouse_move ButtonMouseMoveFn = unsafe { nil }
on_mouse_enter ButtonMouseMoveFn = unsafe { nil }
on_mouse_leave ButtonMouseMoveFn = unsafe { nil }
text string
icon_path string
image gg.Image
use_icon bool
alpha_mode bool
padding f32
hidden bool
disabled bool
movable bool // drag, transition or anything allowing offset yo be updated
just_dragged bool
drag_type string = 'btn'
hoverable bool
tooltip TooltipMessage
// style
// radius f32
bg_color &gg.Color = unsafe { nil }
// Style
theme_style string
style ButtonShapeStyle
style_params ButtonStyleParams
// text styles
text_styles TextStyles
// text_size f64
// // theme
// theme_cfg ColorThemeCfg
// theme map[int]gg.Color = map[int]gg.Color{}
// component state for composable widget
component voidptr
// native widget handle (when native_widgets is enabled)
native_w NativeWidget
}
@[params]
pub struct ButtonParams {
ButtonStyleParams
pub:
id string
text string
icon_path string
on_click ButtonFn = unsafe { nil }
on_key_down ButtonU32Fn = unsafe { nil }
on_mouse_down ButtonMouseFn = unsafe { nil }
on_mouse_up ButtonMouseFn = unsafe { nil }
on_mouse_move ButtonMouseMoveFn = unsafe { nil }
on_mouse_enter ButtonMouseMoveFn = unsafe { nil }
on_mouse_leave ButtonMouseMoveFn = unsafe { nil }
height int
width int
z_index int
movable bool
hoverable bool
tooltip string
tooltip_side Side = .top
padding f64
// text_size f64
theme string = no_style
}
pub fn button(c ButtonParams) &Button {
mut b := &Button{
id: c.id
width_: c.width
height_: c.height
z_index: c.z_index
movable: c.movable
hoverable: c.hoverable
text: c.text
icon_path: c.icon_path
use_icon: c.icon_path != ''
tooltip: TooltipMessage{c.tooltip, c.tooltip_side}
style_params: c.ButtonStyleParams
on_click: c.on_click
on_key_down: c.on_key_down
on_mouse_down: c.on_mouse_down
on_mouse_up: c.on_mouse_up
on_mouse_move: c.on_mouse_move
on_mouse_enter: c.on_mouse_enter
on_mouse_leave: c.on_mouse_leave
// text_size: c.text_size
// radius: f32(c.radius)
padding: f32(c.padding)
ui: unsafe { nil }
}
b.style_params.style = c.theme
if b.use_icon && !os.exists(c.icon_path) {
println('Invalid icon path "${c.icon_path}". The alternate text will be used.')
b.use_icon = false
}
return b
}
fn (mut b Button) init(parent Layout) {
b.parent = parent
u := parent.get_ui()
b.ui = u
if b.use_icon {
if mut b.ui.dd is DrawDeviceContext {
if img := b.ui.dd.create_image(b.icon_path) {
b.image = img
}
}
}
b.load_style()
b.set_text_size()
if b.tooltip.text != '' {
mut win := u.window
win.tooltip.append(b, b.tooltip)
}
// Create native widget if native_widgets is enabled
if b.ui.window.native_widgets.is_enabled() {
b.native_w = b.ui.window.native_widgets.create_button(b.x, b.y, b.width, b.height, b.text)
if b.on_click != unsafe { ButtonFn(0) } {
b.ui.window.native_widgets.button_set_callback(&b.native_w, native_button_clicked, b)
}
}
mut subscriber := parent.get_subscriber()
subscriber.subscribe_method(events.on_key_down, btn_key_down, b)
subscriber.subscribe_method(events.on_mouse_down, btn_mouse_down, b)
subscriber.subscribe_method(events.on_click, btn_click, b)
subscriber.subscribe_method(events.on_touch_down, btn_mouse_down, b)
subscriber.subscribe_method(events.on_mouse_move, btn_mouse_move, b)
subscriber.subscribe_method(events.on_mouse_up, btn_mouse_up, b)
subscriber.subscribe_method(events.on_touch_up, btn_mouse_up, b)
b.ui.window.evt_mngr.add_receiver(b, [events.on_mouse_down, events.on_mouse_move])
}
@[manualfree]
fn (mut b Button) cleanup() {
mut subscriber := b.parent.get_subscriber()
subscriber.unsubscribe_method(events.on_key_down, b)
subscriber.unsubscribe_method(events.on_mouse_down, b)
subscriber.unsubscribe_method(events.on_click, b)
subscriber.unsubscribe_method(events.on_touch_down, b)
subscriber.unsubscribe_method(events.on_mouse_move, b)
b.ui.window.evt_mngr.rm_receiver(b, [events.on_mouse_down, events.on_mouse_move])
unsafe { b.free() }
}
@[unsafe]
pub fn (b &Button) free() {
$if free ? {
print('button ${b.id}')
}
unsafe {
b.id.free()
b.text.free()
b.icon_path.free()
// s.on_click ButtonClickFn
b.tooltip.free()
// s.theme ColorThemeCfg = 'classic'
// if b.component != voidptr(0) {
// free(b.component)
// }
free(b)
}
$if free ? {
println(' -> freed')
}
}
fn native_button_clicked(v_button voidptr) {
mut b := unsafe { &Button(v_button) }
if b.on_click != unsafe { ButtonFn(0) } {
b.on_click(b)
}
}
fn btn_key_down(mut b Button, e &KeyEvent, _ &Window) {
// println('key down $e <$e.key> <$e.codepoint> <$e.mods>')
// println('key down key=<$e.key> code=<$e.codepoint> mods=<$e.mods>')
$if btn_keydown ? {
println('btn_keydown: ${b.id} -> ${b.hidden} ${b.is_focused}')
}
if b.hidden {
return
}
if !b.is_focused {
return
}
if b.on_key_down != unsafe { ButtonU32Fn(0) } {
b.on_key_down(b, e.codepoint)
} else {
// default behavior like click for space and enter
if e.key in [.enter, .space] {
// println("btn key as a click")
if b.on_click != unsafe { ButtonFn(0) } {
b.on_click(b)
}
}
}
}
fn btn_click(mut b Button, e &MouseEvent, _ &Window) {
$if btn_click ? {
println('btn_click ${b.id} movable ${b.movable} focused ${b.is_focused} top_widget ${b.ui.window.is_top_widget(b,
events.on_mouse_down)}')
}
if b.hidden || b.disabled {
return
}
$if wpir ? {
println('btn click: ${b.id} ${b.ui.window.point_inside_receivers(events.on_mouse_down)}')
}
if !b.ui.window.is_top_widget(b, events.on_mouse_down) {
return
}
if !b.is_focused {
return
}
// unclickable if dragged
if b.just_dragged {
b.just_dragged = false
return
}
if b.point_inside(e.x, e.y) {
if e.action == .down {
b.state = .pressed
} else if e.action == .up {
b.state = .normal
if b.on_click != unsafe { ButtonFn(0) } && b.is_focused {
$if btn_onclick ? {
println('onclick ${b.id}')
}
b.on_click(b)
}
}
}
}
fn btn_mouse_down(mut b Button, e &MouseEvent, _ &Window) {
$if btn_md ? {
println('btn_mouse_down ${b.id} movable ${b.movable} top_widget ${b.ui.window.is_top_widget(b,
events.on_mouse_down)}')
}
if b.hidden {
return
}
if !b.ui.window.is_top_widget(b, events.on_mouse_down) {
return
}
if b.point_inside(e.x, e.y) {
b.focus() // IMPORTANT to not propagate event at the same position of removed widget
if b.movable {
b.just_dragged = drag_register(b, e)
}
if !b.just_dragged {
b.state = .pressed
}
if b.on_mouse_down != unsafe { ButtonMouseFn(0) } {
b.on_mouse_down(b, e)
}
}
}
fn btn_mouse_up(mut b Button, e &MouseEvent, _ &Window) {
$if btn_mu ? {
println('btn_mu ${b.id}')
}
if b.hidden {
return
}
b.state = .normal
if b.on_mouse_up != unsafe { ButtonMouseFn(0) } {
b.on_mouse_up(b, e)
}
}
fn btn_mouse_move(mut b Button, e &MouseMoveEvent, _ &Window) {
// println('btn_click for window=$window.title')
if b.hidden {
return
}
if e.mouse_button == 256 {
if b.point_inside(e.x, e.y) {
if b.hoverable && b.state != .hovering {
if b.state != .pressed {
b.state = .hovering
}
}
} else {
b.state = .normal
}
} else {
// to use button as a splitter (no test point_inside)
if b.on_mouse_move != unsafe { ButtonMouseMoveFn(0) } {
b.on_mouse_move(b, e)
}
}
}
pub fn (mut b Button) mouse_enter(e &MouseMoveEvent) {
if b.on_mouse_enter != unsafe { ButtonMouseMoveFn(0) } {
b.on_mouse_enter(b, e)
}
}
pub fn (mut b Button) mouse_leave(e &MouseMoveEvent) {
if b.on_mouse_leave != unsafe { ButtonMouseMoveFn(0) } {
b.on_mouse_leave(b, e)
}
}
pub fn (mut b Button) set_pos(x int, y int) {
b.x = x
b.y = y
}
pub fn (b &Button) size() (int, int) {
return b.width, b.height
}
pub fn (mut b Button) propose_size(w int, h int) (int, int) {
// println('prop size $w $h')
if w != 0 {
b.width = w
}
if h != 0 {
b.height = h
}
// b.height = h
// b.width = b.ui.ft.text_width(b.text) + ui.button_horizontal_padding
// b.height = 20 // vertical padding
// println("but $b.id prop size: $w, $h => $b.width, $b.height")
// update_text_size(mut b)
return b.width, b.height
}
fn (mut b Button) draw() {
b.draw_device(mut b.ui.dd)
}
fn (mut b Button) draw_device(mut d DrawDevice) {
// Native widget: update position/text and skip custom drawing
if b.ui.window.native_widgets.is_enabled() && b.native_w.handle != unsafe { nil } {
b.ui.window.native_widgets.update_button(&b.native_w, b.x, b.y, b.width, b.height, b.text)
return
}
offset_start(mut b)
defer {
offset_end(mut b)
}
$if layout ? {
if b.ui.layout_print {
println('Button(${b.id}): (${b.x}, ${b.y}, ${b.width}, ${b.height})')
}
}
bcenter_x := b.x + b.width / 2
bcenter_y := b.y + b.height / 2
padding := relative_size(b.padding, b.width, b.height)
x, y, width, height := b.x + padding, b.y + padding, b.width - 2 * padding, b.height - 2 * padding
mut state := b.state
if b.disabled {
state = .normal
}
mut bg_color := match state {
.normal {
if b.bg_color != unsafe { nil } { *b.bg_color } else { b.style.bg_color }
}
.hovering {
b.style.bg_color_hover
}
.pressed {
b.style.bg_color_pressed
}
}
if b.disabled {
bg_color.a = 50
}
if b.style.radius > 0 {
radius := relative_size(b.style.radius, int(width), int(height))
// println("draw $b.id ${bg_color}")
d.draw_rounded_rect_filled(x, y, width, height, radius, bg_color) // gg.white)
d.draw_rounded_rect_empty(x, y, width, height, radius, if b.is_focused {
button_focus_border_color
} else {
b.style.border_color
})
} else {
if b.alpha_mode && bg_color.a < 255 { // draw a background to see alpha color
n := 5
dx, dy := width / n, height / n
for k1 in 0 .. n {
for k2 in 0 .. n {
if math.mod(k1 + k2, 2) < 0.1 {
d.draw_rect_filled(x + k1 * dx, y + k2 * dy, width / n, height / n,
gg.light_gray)
}
}
}
}
d.draw_rect_filled(x, y, width, height, bg_color) // gg.white)
d.draw_rect_empty(x, y, width, height, if b.is_focused {
button_focus_border_color
} else {
b.style.border_color
})
}
if b.use_icon {
d.draw_image(x, y, width, height, b.image)
} else {
mut dtw := DrawTextWidget(b)
dtw.draw_device_load_style(d)
dtw.draw_device_text(d, bcenter_x, bcenter_y, b.text)
}
$if tbb ? {
println('bcenter_x(${bcenter_x}) = b.x(${b.x}) + b.width(${b.width}) / 2')
println('bcenter_y(${bcenter_y}) = b.y(${b.y}) + b.height(${b.height}) / 2')
println('draw_text(b, bcenter_x(${bcenter_x}), bcenter_y(${bcenter_y}), b.text(${b.text}))')
println('draw_rect(b.x(${b.x}), b.y(${b.y}), b.width(${b.width}), b.height(${b.height}), bg_color)')
debug_draw_bb_text(bcenter_x, y, b.text_width, b.text_height, b.ui)
}
$if bb ? {
debug_draw_bb_widget(mut b, b.ui)
}
}
pub fn (mut b Button) set_text(text string) {
b.text = text
b.set_text_size()
}
pub fn (mut b Button) set_text_size() {
if b.use_icon {
b.width = b.image.width
b.height = b.image.height
} else {
mut dtw := DrawTextWidget(b)
dtw.load_style()
b.text_width, b.text_height = dtw.text_size(b.text)
if b.id == 'hi' {
println('${b.text_width}')
}
// b.text_width, b.text_height = text_size(b, b.text)
// b.text_width = int(f32(b.text_width))
// b.text_height = int(f32(b.text_height))
b.width = b.text_width + button_horizontal_padding
if b.width_ > b.width {
b.width = b.width_
}
b.height = b.text_height + button_vertical_padding
if b.height_ > b.height {
b.height = b.height_
}
}
}
fn (b &Button) point_inside(x f64, y f64) bool {
// println("point_inside button: ($b.x $b.offset_x, $b.y $b.offset_y) ($x, $y) ($b.width, $b.height)")
return point_inside(b, x, y)
}
fn (mut b Button) set_visible(state bool) {
b.hidden = !state
}
fn (mut b Button) focus() {
mut f := Focusable(b)
f.force_focus()
}
fn (mut b Button) unfocus() {
b.is_focused = false
b.state = .normal
}
// method implemented in Draggable
fn (b &Button) get_window() &Window {
return b.ui.window
}
fn (b &Button) drag_type() string {
return b.drag_type
}
fn (b &Button) drag_bounds() gg.Rect {
w, h := b.size()
return gg.Rect{b.x + b.offset_x, b.y + b.offset_y, w, h}
}