forked from vlang/ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkbox.v
161 lines (143 loc) · 3.75 KB
/
checkbox.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
// Copyright (c) 2020-2021 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
const (
check_mark_size = 14
cb_border_color = gx.rgb(76, 145, 244)
cb_image = u32(0)
)
/*
enum CheckBoxState {
normal
check
}
*/
type CheckChangedFn = fn (voidptr, bool)
[heap]
pub struct CheckBox {
pub mut:
// state CheckBoxState
height int
width int
x int
y int
offset_x int
offset_y int
z_index int
parent Layout
is_focused bool
checked bool
ui &UI
on_check_changed CheckChangedFn
text string
disabled bool
text_cfg gx.TextCfg
text_size f64
hidden bool
}
pub struct CheckBoxConfig {
x int
y int
z_index int
parent Layout
text string
on_check_changed CheckChangedFn
checked bool
disabled bool
text_cfg gx.TextCfg
text_size f64
}
fn (mut cb CheckBox) init(parent Layout) {
cb.parent = parent
cb.ui = parent.get_ui()
cb.width = text_width<CheckBox>(cb, cb.text) + 5 + ui.check_mark_size
init_text_cfg<CheckBox>(mut cb)
mut subscriber := parent.get_subscriber()
subscriber.subscribe_method(events.on_click, cb_click, cb)
}
pub fn checkbox(c CheckBoxConfig) &CheckBox {
mut cb := &CheckBox{
height: 20 // TODO
z_index: c.z_index
ui: 0
text: c.text
on_check_changed: c.on_check_changed
checked: c.checked
disabled: c.disabled
text_cfg: c.text_cfg
text_size: c.text_size
}
return cb
}
fn cb_click(mut cb CheckBox, e &MouseEvent, window &Window) {
if cb.hidden {
return
}
if cb.point_inside(e.x, e.y) { // && e.action == 0 {
cb.checked = !cb.checked
// println("checked: $cb.checked")
if cb.on_check_changed != voidptr(0) {
cb.on_check_changed(window.state, cb.checked)
}
}
}
fn (mut cb CheckBox) set_pos(x int, y int) {
cb.x = x
cb.y = y
}
fn (mut cb CheckBox) size() (int, int) {
return cb.width, cb.height
}
fn (mut cb CheckBox) propose_size(w int, h int) (int, int) {
// cb.width = w
// cb.height = h
// width := check_mark_size + 5 + cb.ui.ft.text_width(cb.text)
return cb.width, ui.check_mark_size
}
fn (mut cb CheckBox) draw() {
offset_start(mut cb)
cb.ui.gg.draw_rect(cb.x, cb.y, ui.check_mark_size, ui.check_mark_size, gx.white) // progress_bar_color)
// cb.ui.gg.draw_empty_rect(cb.x, cb.y, check_mark_size, check_mark_size, cb_border_color)
draw_inner_border(false, cb.ui.gg, cb.x, cb.y, ui.check_mark_size, ui.check_mark_size,
false)
// Draw X (TODO draw a check mark instead)
if cb.checked {
cb.ui.gg.draw_rect(cb.x + 3, cb.y + 3, 2, 2, gx.black)
/*
x0 := cb.x +2
y0 := cb.y +2
cb.ui.gg.draw_line_c(x0, y0, x0+check_mark_size -4, y0 + check_mark_size-4, gx.black)
cb.ui.gg.draw_line_c(0.5+x0, y0, -3.5 +x0+check_mark_size , y0 + check_mark_size-4, gx.black)
//
y1 := cb.y + check_mark_size - 2
cb.ui.gg.draw_line_c(x0, y1, x0+check_mark_size -4, y0, gx.black)
cb.ui.gg.draw_line_c(0.5+x0, y1, -3.5+x0+check_mark_size, y0, gx.black)
*/
cb.ui.gg.draw_image(cb.x + 3, cb.y + 3, 8, 8, cb.ui.cb_image)
}
// Text
cb.ui.gg.draw_text(cb.x + ui.check_mark_size + 5, cb.y, cb.text, cb.text_cfg)
$if bb ? {
draw_bb(mut cb, cb.ui)
}
offset_end(mut cb)
}
fn (cb &CheckBox) point_inside(x f64, y f64) bool {
return point_inside<CheckBox>(cb, x, y)
}
fn (mut cb CheckBox) mouse_move(e MouseEvent) {
}
fn (mut cb CheckBox) set_visible(state bool) {
cb.hidden = state
}
fn (mut cb CheckBox) focus() {
cb.is_focused = true
}
fn (mut cb CheckBox) unfocus() {
cb.is_focused = false
}
fn (cb &CheckBox) is_focused() bool {
return cb.is_focused
}