-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget_rectangle.v
161 lines (147 loc) · 2.94 KB
/
widget_rectangle.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-2022 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
import gg
[heap]
pub struct Rectangle {
pub mut:
id string
color gx.Color
text string
offset_x int
offset_y int
height int
width int
ui &UI
parent Layout = empty_stack
text_cfg gx.TextCfg
text_size f64
// component state for composable widget
component voidptr
mut:
x int
y int
z_index int
radius int
border bool
border_color gx.Color
hidden bool
}
[params]
pub struct RectangleParams {
id string
text string
height int
width int
z_index int
color gx.Color = gx.Color{0, 0, 0, 0}
radius int
border bool
border_color gx.Color = gx.Color{
r: 180
g: 180
b: 190
}
x int
y int
text_cfg gx.TextCfg
text_size f64
}
pub fn rectangle(c RectangleParams) &Rectangle {
rect := &Rectangle{
id: c.id
text: c.text
height: c.height
width: c.width
z_index: c.z_index
radius: c.radius
color: c.color
border: c.border
border_color: c.border_color
ui: 0
x: c.x
y: c.y
text_size: c.text_size
text_cfg: c.text_cfg
}
return rect
}
// Workaround to have a spacing notably
pub fn spacing(c RectangleParams) &Rectangle {
mut rect := &Rectangle{
color: c.color
ui: 0
}
rect.hidden = true
return rect
}
fn (mut r Rectangle) init(parent Layout) {
ui := parent.get_ui()
r.ui = ui
init_text_cfg(mut r)
}
[manualfree]
pub fn (mut r Rectangle) cleanup() {
unsafe { r.free() }
}
[unsafe]
pub fn (r &Rectangle) free() {
$if free ? {
print('rectangle $r.id')
}
unsafe {
r.text.free()
r.id.free()
free(r)
}
$if free ? {
println(' -> freed')
}
}
pub fn (mut r Rectangle) set_pos(x int, y int) {
r.x = x
r.y = y
}
pub fn (mut r Rectangle) size() (int, int) {
return r.width, r.height
}
pub fn (mut r Rectangle) propose_size(w int, h int) (int, int) {
r.width, r.height = w, h
return r.width, r.height
}
fn (mut r Rectangle) draw() {
offset_start(mut r)
if r.radius > 0 {
r.ui.gg.draw_rounded_rect_filled(r.x, r.y, r.width, r.height, r.radius, r.color)
if r.border {
r.ui.gg.draw_rounded_rect_empty(r.x, r.y, r.width, r.height, r.radius, r.border_color)
}
} else {
r.ui.gg.draw_rect_filled(r.x, r.y, r.width, r.height, r.color)
if r.border {
r.ui.gg.draw_rect_empty(r.x, r.y, r.width, r.height, r.border_color)
}
}
// Display rectangle text
if r.text != '' {
text_width, text_height := text_size(r, r.text)
mut dx := (r.width - text_width) / 2
mut dy := (r.height - text_height) / 2
if dx < 0 {
dx = 0
}
if dy < 0 {
dy = 0
}
draw_text(r, r.x + dx, r.y + dy, r.text)
}
offset_end(mut r)
}
fn (mut r Rectangle) set_visible(state bool) {
r.hidden = !state
}
fn (r &Rectangle) point_inside(x f64, y f64) bool {
return point_inside(r, x, y)
}