-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathlabel.v
More file actions
214 lines (196 loc) · 4.69 KB
/
label.v
File metadata and controls
214 lines (196 loc) · 4.69 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
// 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
@[heap]
pub struct Label {
pub mut:
id string
text string
parent Layout = empty_stack
x int
y int
offset_x int
offset_y int
width int
height int
z_index int
adj_width int
adj_height int
// Adjustable
justify []f64
ax int
ay int
ui &UI = unsafe { nil }
// Style
theme_style string
style LabelStyle
style_params LabelStyleParams
// text styles
text_styles TextStyles
// text_size f64
hidden bool
clipping bool
// component state for composable widget
component voidptr
// native widget handle (when native_widgets is enabled)
native_w NativeWidget
}
@[params]
pub struct LabelParams {
LabelStyleParams
pub:
id string
width int
height int
z_index int
clipping bool
justify []f64 = [0.0, 0.0]
text string
// text_size f64
theme string = no_style
}
pub fn label(c LabelParams) &Label {
mut lbl := &Label{
id: c.id
text: c.text
width: c.width
height: c.height
ui: unsafe { nil }
z_index: c.z_index
clipping: c.clipping
// text_size: c.text_size
justify: c.justify
style_params: c.LabelStyleParams
}
lbl.style_params.style = c.theme
return lbl
}
fn (mut l Label) init(parent Layout) {
u := parent.get_ui()
l.ui = u
l.load_style()
// l.init_style()
l.init_size()
// Create native widget if native_widgets is enabled
if l.ui.window.native_widgets.is_enabled() {
l.native_w = l.ui.window.native_widgets.create_label(l.x, l.y, l.width, l.height, l.text)
}
}
@[manualfree]
pub fn (mut l Label) cleanup() {
unsafe { l.free() }
}
@[unsafe]
pub fn (l &Label) free() {
$if free ? {
print('label ${l.id}')
}
unsafe {
l.id.free()
l.text.free()
free(l)
}
$if free ? {
println(' -> freed')
}
}
// fn (mut l Label) init_style() {
// mut dtw := DrawTextWidget(l)
// dtw.init_style()
// dtw.update_text_size(l.text_size)
// }
pub fn (mut l Label) set_pos(x int, y int) {
$if lab_sp ? {
println('label set pos (${l.id}): (${l.x}, ${l.y}, ${l.width}, ${l.height}) -> (${x}, ${y}) ')
}
l.x = x
l.y = y
}
fn (mut l Label) adj_size() (int, int) {
if l.adj_width == 0 || l.adj_height == 0 {
mut dtw := DrawTextWidget(l)
// println(dtw.current_style().size)
dtw.load_style()
mut w, mut h := 0, 0
if !l.text.contains('\n') {
w, h = dtw.text_width(l.text), dtw.current_style().size
// println("$w, $h, $l.text ${dtw.text_height(l.text)}")
} else {
for line in l.text.split('\n') {
wi, he := dtw.text_size(line)
if wi > w {
w = wi
}
h += he
}
}
// println("label size: $w, $h ${l.text.split('\n').len}")
l.adj_width, l.adj_height = w, h
}
return l.adj_width, l.adj_height
}
fn (mut l Label) init_size() {
if l.width == 0 {
l.width, _ = l.adj_size()
}
if l.height == 0 {
_, l.height = l.adj_size()
}
}
fn (l &Label) size() (int, int) {
return l.width, l.height
}
fn (mut l Label) propose_size(w int, h int) (int, int) {
l.width, l.height = w, h
return l.size()
}
fn (mut l Label) draw() {
l.draw_device(mut l.ui.dd)
}
fn (mut l Label) draw_device(mut d DrawDevice) {
// Native widget: update position/text and skip custom drawing
if l.ui.window.native_widgets.is_enabled() && l.native_w.handle != unsafe { nil } {
l.ui.window.native_widgets.update_label(&l.native_w, l.x, l.y, l.width, l.height, l.text)
return
}
offset_start(mut l)
defer {
offset_end(mut l)
}
$if layout ? {
if l.ui.layout_print {
println('Label(${l.id}): (${l.x}, ${l.y}, ${l.width}, ${l.height})')
}
}
cstate := clipping_start(l, mut d) or { return }
defer {
clipping_end(l, mut d, cstate)
}
adj_pos_x, adj_pos_y := AdjustableWidget(l).get_adjusted_pos()
splits := l.text.split('\n') // Split the text into an array of lines.
height := l.ui.dd.text_height('W') // Get the height of the current font.
mut dtw := DrawTextWidget(l)
dtw.draw_device_load_style(d)
for i, split in splits {
dtw.draw_device_text(d, adj_pos_x, adj_pos_y + (height * i), split)
$if tbb ? {
w, h := l.ui.dd.text_size(split)
println('label: w, h := l.ui.dd.text_size(split)')
println('debug_draw_bb_text(l.x(${l.x}), l.y(${l.y}) + (height(${height}) * i(${i})), w(${w}), h(${h}), l.ui)')
debug_draw_bb_text(adj_pos_x, adj_pos_y + (height * i), w, h, l.ui)
}
}
$if bb ? {
debug_draw_bb_widget(mut l, l.ui)
}
}
fn (mut l Label) set_visible(state bool) {
l.hidden = !state
}
pub fn (l &Label) point_inside(x f64, y f64) bool {
return x >= l.x && x <= l.x + l.width && y >= l.y && y <= l.y + l.height
}
pub fn (mut l Label) set_text(s string) {
l.text = s
}