-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabel.v
80 lines (64 loc) · 1.62 KB
/
label.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
// Copyright (c) 2020 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
pub struct Label {
mut:
text string
parent ILayouter
x int
y int
ui &UI
}
pub struct LabelConfig {
text string
ref &Label
}
fn (l mut Label)init(parent ILayouter) {
ui := parent.get_ui()
l.ui = ui
}
pub fn label(c LabelConfig) &Label {
lbl := &Label{
text: c.text
}
if c.ref != 0 {
mut ref := c.ref
*ref = *lbl
return &ref
}
return lbl
}
fn (b mut Label) set_pos(x, y int) {
b.x = x
b.y = y
}
fn (b mut Label) size() (int, int) {
size := b.ui.ft.text_size(b.text)
// First return the width, then the height multiplied by line count.
return size.var_0, size.var_1 * b.text.split('\n').len
}
fn (b mut Label) propose_size(w, h int) (int, int) {
size := b.ui.ft.text_size(b.text)
// First return the width, then the height multiplied by line count.
return size.var_0, size.var_1 * b.text.split('\n').len
}
fn (b mut Label) draw() {
splits := b.text.split('\n') // Split the text into an array of lines.
height := b.ui.ft.text_height('W') // Get the height of the current font.
for i, split in splits {
// Draw the text at b.x and b.y + line height * current line
b.ui.ft.draw_text(b.x, b.y + (height * i), split, btn_text_cfg)
}
}
fn (t &Label) focus() {}
fn (t &Label) is_focused() bool {
return false
}
fn (t &Label) unfocus() {}
fn (t &Label) point_inside(x, y f64) bool {
return false // x >= t.x && x <= t.x + t.width && y >= t.y && y <= t.y + t.height
}
pub fn (l mut Label) set_text(s string) {
l.text = s
}