forked from vlang/ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slider.v
396 lines (368 loc) · 10.6 KB
/
slider.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
module ui
import gx
const (
thumb_color = gx.rgb(87, 153, 245)
slider_background_color = gx.rgb(219, 219, 219)
slider_background_border_color = gx.rgb(191, 191, 191)
slider_focused_background_border_color = gx.rgb(255, 0, 0)
)
type SliderValueChangedFn = fn (arg_1 voidptr, arg_2 voidptr)
pub enum Orientation {
vertical = 0
horizontal = 1
}
[heap]
pub struct Slider {
pub mut:
height int // track width
width int // track height
thumb_width int
thumb_height int
orientation Orientation = Orientation.horizontal
x int
y int
offset_x int
offset_y int
z_index int
parent Layout
ui &UI
val f32
min int
max int = 100
is_focused bool
dragging bool
on_value_changed SliderValueChangedFn
focus_on_thumb_only bool
rev_min_max_pos bool
thumb_in_track bool
track_line_displayed bool
entering bool
hidden bool
}
pub struct SliderConfig {
width int
height int
z_index int
min int
max int
val f32
orientation Orientation
on_value_changed SliderValueChangedFn
focus_on_thumb_only bool = true
rev_min_max_pos bool
thumb_in_track bool
track_line_displayed bool = true
entering bool
}
fn (mut s Slider) init(parent Layout) {
s.parent = parent
ui := parent.get_ui()
s.ui = ui
mut subscriber := parent.get_subscriber()
subscriber.subscribe_method(events.on_click, slider_click, s)
subscriber.subscribe_method(events.on_key_down, slider_key_down, s)
subscriber.subscribe_method(events.on_mouse_down, slider_mouse_down, s)
subscriber.subscribe_method(events.on_mouse_up, slider_mouse_up, s)
subscriber.subscribe_method(events.on_mouse_move, slider_mouse_move, s)
$if android {
subscriber.subscribe_method(events.on_touch_down, slider_mouse_down, s)
subscriber.subscribe_method(events.on_touch_up, slider_mouse_up, s)
subscriber.subscribe_method(events.on_touch_move, slider_touch_move, s)
}
}
pub fn slider(c SliderConfig) &Slider {
mut s := &Slider{
height: c.height
width: c.width
min: c.min
max: c.max
val: c.val
orientation: c.orientation
on_value_changed: c.on_value_changed
focus_on_thumb_only: c.focus_on_thumb_only
rev_min_max_pos: c.rev_min_max_pos
thumb_in_track: c.thumb_in_track
track_line_displayed: c.track_line_displayed
ui: 0
z_index: c.z_index
entering: c.entering
}
s.set_thumb_size()
// if !c.thumb_in_track {
// s.thumb_height = if s.orientation == .horizontal { s.height + 10 } else { 10 }
// s.thumb_width = if s.orientation == .horizontal { 10 } else { s.width + 10 }
// } else {
// s.thumb_height = if s.orientation == .horizontal { s.height - 3 } else { 10 }
// s.thumb_width = if s.orientation == .horizontal { 10 } else { s.width - 3 }
// }
if s.min > s.max {
tmp := s.max
s.max = s.min
s.min = tmp
}
return s
}
fn (s &Slider) draw_thumb() {
axis := if s.orientation == .horizontal { s.x } else { s.y }
rev_axis := if s.orientation == .horizontal { s.y } else { s.x }
rev_dim := if s.orientation == .horizontal { s.height } else { s.width }
rev_thumb_dim := if s.orientation == .horizontal { s.thumb_height } else { s.thumb_width }
dim := if s.orientation == .horizontal { s.width } else { s.height }
mut pos := f32(dim) * ((s.val - f32(s.min)) / f32(s.max - s.min))
if s.rev_min_max_pos {
pos = -pos + f32(dim)
}
pos += f32(axis)
if pos > axis + dim {
pos = f32(dim) + f32(axis)
}
if pos < axis {
pos = f32(axis)
}
middle := f32(rev_axis) - (f32(rev_thumb_dim - rev_dim) / 2)
if s.orientation == .horizontal {
s.ui.gg.draw_rect(pos - f32(s.thumb_width) / 2, middle, s.thumb_width, s.thumb_height,
ui.thumb_color)
} else {
s.ui.gg.draw_rect(middle, pos - f32(s.thumb_height) / 2, s.thumb_width, s.thumb_height,
ui.thumb_color)
}
}
fn (mut s Slider) set_pos(x int, y int) {
s.x = x
s.y = y
}
fn (mut s Slider) set_thumb_size() {
if !s.thumb_in_track {
s.thumb_height = if s.orientation == .horizontal { s.height + 10 } else { 10 }
s.thumb_width = if s.orientation == .horizontal { 10 } else { s.width + 10 }
} else {
s.thumb_height = if s.orientation == .horizontal { s.height - 3 } else { 10 }
s.thumb_width = if s.orientation == .horizontal { 10 } else { s.width - 3 }
}
}
fn (mut s Slider) size() (int, int) {
// if s.orientation == .horizontal {
// return s.width, s.thumb_height
// } else {
// return s.thumb_width, s.height
// }
return s.width, s.height
}
fn (mut s Slider) propose_size(w int, h int) (int, int) {
// TODO: fix
$if debug_slider ? {
println('slider propose_size: ($s.width,$s.height) -> ($w, $h) | s.orientation: $s.orientation')
}
// if s.orientation == .horizontal {
s.width = w
// } else {
s.height = h
// }
s.set_thumb_size()
return s.size()
}
fn (mut s Slider) draw() {
offset_start(mut s)
// Draw the track
s.ui.gg.draw_rect(s.x, s.y, s.width, s.height, ui.slider_background_color)
if s.track_line_displayed {
if s.orientation == .horizontal {
s.ui.gg.draw_line(s.x + 2, s.y + s.height / 2, s.x + s.width - 4, s.y + s.height / 2,
gx.rgb(0, 0, 0))
} else {
s.ui.gg.draw_line(s.x + s.width / 2, s.y + 2, s.x + s.width / 2, s.y + s.height - 4,
gx.rgb(0, 0, 0))
}
}
if !s.is_focused {
s.ui.gg.draw_empty_rect(s.x, s.y, s.width, s.height, ui.slider_background_border_color)
} else {
s.ui.gg.draw_empty_rect(s.x, s.y, s.width, s.height, ui.slider_focused_background_border_color)
}
// Draw the thumb
s.draw_thumb()
$if bb ? {
draw_bb(mut s, s.ui)
}
offset_end(mut s)
}
fn slider_key_down(mut s Slider, e &KeyEvent, zzz voidptr) {
if s.hidden {
return
}
if !s.is_focused {
return
}
match e.key {
.up, .left {
if !s.rev_min_max_pos {
if int(s.val) > s.min {
s.val--
}
} else {
if int(s.val) < s.max {
s.val++
}
}
}
.down, .right {
if !s.rev_min_max_pos {
if int(s.val) < s.max {
s.val++
}
} else {
if int(s.val) > s.min {
s.val--
}
}
}
else {}
}
if s.on_value_changed != voidptr(0) {
parent := s.parent
state := parent.get_state()
s.on_value_changed(state, s)
}
}
fn (s &Slider) point_inside(x f64, y f64) bool {
return point_inside<Slider>(s, x, y) // x >= s.x && x <= s.x + s.width && y >= s.y && y <= s.y + s.height
}
fn slider_click(mut s Slider, e &MouseEvent, zzz voidptr) {
if s.hidden {
return
}
if !s.point_inside_thumb(e.x, e.y) && (!s.point_inside(e.x, e.y) || s.focus_on_thumb_only) {
s.is_focused = false
return
}
if !s.focus_on_thumb_only {
s.change_value(e.x, e.y)
}
s.is_focused = true
}
fn slider_touch_move(mut s Slider, e &MouseMoveEvent, zzz voidptr) {
if s.hidden {
return
}
if s.entering && s.point_inside_thumb(e.x, e.y) {
// println('slider touch move DRAGGING')
s.dragging = true
}
if s.dragging {
s.change_value(int(e.x), int(e.y))
}
}
fn slider_mouse_down(mut s Slider, e &MouseEvent, zzz voidptr) {
if s.hidden {
return
}
// println('slider touchup NO MORE DRAGGING')
if int(e.button) == 0 && s.point_inside_thumb(e.x, e.y) {
// println('slider touch move DRAGGING ${e.button}')
s.dragging = true
}
}
fn slider_mouse_up(mut s Slider, e &MouseEvent, zzz voidptr) {
// println('slider touchup NO MORE DRAGGING')
s.dragging = false
}
fn slider_mouse_move(mut s Slider, e &MouseMoveEvent, zzz voidptr) {
// println("slider: $s.dragging ${e.mouse_button} ${int(e.mouse_button)}")
if s.ui.btn_down[0] { // int(e.mouse_button) == 0 {
// left: 0, right: 1, middle: 2
if s.entering && s.point_inside_thumb(e.x, e.y) {
// println("slider DRAGGING")
s.dragging = true
}
} else {
s.dragging = false
}
if s.dragging {
s.change_value(int(e.x), int(e.y))
}
}
fn (mut s Slider) change_value(x int, y int) {
dim := if s.orientation == .horizontal { s.width } else { s.height }
axis := if s.orientation == .horizontal { s.x } else { s.y }
// TODO parser bug ` - axis`
mut pos := if s.orientation == .horizontal { x } else { y }
pos -= axis
if s.rev_min_max_pos {
pos = -pos + dim
}
s.val = f32(s.min) + (f32(pos) * f32(s.max - s.min)) / f32(dim)
if int(s.val) < s.min {
s.val = f32(s.min)
} else if int(s.val) > s.max {
s.val = f32(s.max)
}
if s.on_value_changed != voidptr(0) {
parent := s.parent
state := parent.get_state()
s.on_value_changed(state, s)
}
}
fn (mut s Slider) set_visible(state bool) {
s.hidden = state
}
fn (mut s Slider) focus() {
parent := s.parent
parent.unfocus_all()
s.is_focused = true
}
fn (s &Slider) is_focused() bool {
return s.is_focused
}
fn (mut s Slider) unfocus() {
s.is_focused = false
}
fn (s &Slider) point_inside_thumb(x f64, y f64) bool {
sx, sy := s.x + s.offset_x, s.y + s.offset_y
axis := if s.orientation == .horizontal { sx } else { sy }
rev_axis := if s.orientation == .horizontal { sy } else { sx }
rev_dim := if s.orientation == .horizontal { s.height } else { s.width }
rev_thumb_dim := if s.orientation == .horizontal { s.thumb_height } else { s.thumb_width }
dim := if s.orientation == .horizontal { s.width } else { s.height }
mut pos := f32(dim) * ((s.val - f32(s.min)) / f32(s.max - s.min))
if s.rev_min_max_pos {
pos = -pos + f32(dim)
}
pos += f32(axis)
if pos > axis + dim {
pos = f32(dim) + f32(axis)
}
if pos < axis {
pos = f32(axis)
}
middle := f32(rev_axis) - (f32(rev_thumb_dim - rev_dim) / 2)
$if android {
tol := 20.
if s.orientation == .horizontal {
t_x := pos - f32(s.thumb_width) / 2 - tol
t_y := middle - tol
return x >= t_x && x <= t_x + f32(s.thumb_width) + tol * 2 && y >= t_y
&& y <= t_y + f32(s.thumb_height) + tol * 2
} else {
t_x := middle - tol
t_y := pos - f32(s.thumb_height) / 2 - tol
// println('slider inside: $x >= $t_x && $x <= ${t_x + f32(s.thumb_width)} && $y >= $t_y && $y <= ${
// t_y + f32(s.thumb_height)}')
return x >= t_x && x <= t_x + f32(s.thumb_width) + tol * 2 && y >= t_y
&& y <= t_y + f32(s.thumb_height) + tol * 2
}
} $else {
if s.orientation == .horizontal {
t_x := pos - f32(s.thumb_width) / 2
t_y := middle
return x >= t_x && x <= t_x + f32(s.thumb_width) && y >= t_y
&& y <= t_y + f32(s.thumb_height)
} else {
t_x := middle
t_y := pos - f32(s.thumb_height) / 2
// println("slider inside: $x >= $t_x && $x <= ${t_x + f32(s.thumb_width)} && $y >= $t_y && $y <= ${t_y + f32(s.thumb_height)}")
return x >= t_x && x <= t_x + f32(s.thumb_width) && y >= t_y
&& y <= t_y + f32(s.thumb_height)
}
}
}