forked from vlang/ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
radio.v
178 lines (161 loc) · 3.74 KB
/
radio.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
// 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
/*
enum RadioState {
normal
check
}
*/
type RadioClickFn = fn ()
[heap]
pub struct Radio {
pub mut:
selected_index int
values []string
// state RadioState
title string
height int
width int
x int
y int
offset_x int
offset_y int
z_index int
parent Layout
is_focused bool
is_checked bool
ui &UI
text_cfg gx.TextCfg
text_size f64
hidden bool
// selected_value string
// onclick RadioClickFn
}
pub struct RadioConfig {
// onclick RadioClickFn
values []string
title string
width int
z_index int
ref &Radio = voidptr(0)
text_cfg gx.TextCfg
text_size f64
}
fn (mut r Radio) init(parent Layout) {
r.parent = parent
ui := parent.get_ui()
r.ui = ui
// Get max value text width
if r.width == 0 {
mut max := 0
for value in r.values {
width := text_width<Radio>(r, value)
if width > max {
max = width
}
}
r.width = max + check_mark_size + 10
}
init_text_cfg<Radio>(mut r)
mut subscriber := parent.get_subscriber()
subscriber.subscribe_method(events.on_click, radio_click, r)
}
pub fn radio(c RadioConfig) &Radio {
mut r := &Radio{
height: 20
z_index: c.z_index
values: c.values
title: c.title
width: c.width
text_cfg: c.text_cfg
text_size: c.text_size
ui: 0
// onclick: c.onclick
}
/*
if c.ref != 0 {
mut ref := c.ref
*ref = *r
return &ref
}
*/
return r
}
fn (mut r Radio) set_pos(x int, y int) {
r.x = x
r.y = y
}
fn (mut r Radio) size() (int, int) {
return r.width, r.values.len * (r.height + 5)
}
fn (mut r Radio) propose_size(w int, h int) (int, int) {
// r.width = w
// r.height = 20//default_font_size
return r.width, r.values.len * (r.height + 5)
}
fn (mut r Radio) draw() {
offset_start(mut r)
// Border
r.ui.gg.draw_empty_rect(r.x, r.y, r.width, r.values.len * (r.height + 5), gx.gray)
// Title
r.ui.gg.draw_rect(r.x + check_mark_size, r.y - 5, r.ui.gg.text_width(r.title) + 5,
10, default_window_color)
// r.ui.gg.draw_text(r.x + check_mark_size + 3, r.y - 7, r.title, r.text_cfg.as_text_cfg())
// r.draw_text(r.x + check_mark_size + 3, r.y - 7, r.title)
draw_text<Radio>(r, r.x + check_mark_size + 3, r.y - 7, r.title)
// Values
for i, val in r.values {
y := r.y + r.height * i + 15
x := r.x + 5
r.ui.gg.draw_image(x, y - 1, 16, 16, r.ui.selected_radio_image)
if i != r.selected_index {
r.ui.gg.draw_rect(x + 4, y + 3, 8, 8, gx.white) // hide the black circle
// r.ui.gg.draw_image(x, y-3, 16, 16, r.ui.circle_image)
}
// Text
// r.ui.gg.draw_text(r.x + check_mark_size + 10, y, val, r.text_cfg.as_text_cfg())
// r.draw_text(r.x + check_mark_size + 10, y, val)
draw_text<Radio>(r, r.x + check_mark_size + 10, y, val)
}
$if bb ? {
draw_bb(mut r, r.ui)
}
offset_end(mut r)
}
fn (r &Radio) point_inside(x f64, y f64) bool {
rx, ry := r.x + r.offset_x, r.y + r.offset_y
return x >= rx && x <= rx + r.width && y >= ry && y <= ry + (r.height + 5) * r.values.len
}
fn radio_click(mut r Radio, e &MouseEvent, zzz voidptr) {
if r.hidden {
return
}
if !r.point_inside(e.x, e.y) {
return
}
// println('e.y=$e.y r.y=$r.y')
y := e.y - r.y
r.selected_index = y / (r.height + 5)
if r.selected_index == r.values.len {
r.selected_index = r.values.len - 1
}
// println(r.selected_index)
}
fn (mut r Radio) set_visible(state bool) {
r.hidden = state
}
fn (mut r Radio) focus() {
r.is_focused = true
}
fn (mut r Radio) unfocus() {
r.is_focused = false
}
pub fn (r &Radio) selected_value() string {
return r.values[r.selected_index]
}
fn (r &Radio) is_focused() bool {
return r.is_focused
}