-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton.v
221 lines (202 loc) · 5 KB
/
button.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
// 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
import gg
import os
const (
button_bg_color = gx.rgb(28, 28, 28)
button_border_color = gx.rgb(200, 200, 200)
button_horizontal_padding = 26
button_vertical_padding = 8
)
enum ButtonState {
normal
pressed
}
type ButtonClickFn = fn (voidptr, voidptr) // userptr, btn
pub struct ButtonConfig {
text string
icon_path string
onclick ButtonClickFn
height int = 20
width int
z_index int
text_cfg gx.TextCfg
text_size f64
}
[heap]
pub struct Button {
mut:
text_width int
text_height int
pub mut:
state ButtonState
height int
width int
z_index int
x int
y int
parent Layout
is_focused bool
ui &UI
onclick ButtonClickFn
text string
icon_path string
image gg.Image
use_icon bool
text_cfg gx.TextCfg
text_size f64
fixed_text bool
}
fn (mut b Button) init(parent Layout) {
b.parent = parent
ui := parent.get_ui()
b.ui = ui
if b.use_icon {
b.image = b.ui.gg.create_image(b.icon_path)
}
if is_empty_text_cfg(b.text_cfg) {
b.text_cfg = b.ui.window.text_cfg
}
if b.text_size > 0 {
_, win_height := b.ui.window.size()
b.text_cfg = gx.TextCfg{
...b.text_cfg
size: text_size_as_int(b.text_size, win_height)
}
}
mut subscriber := parent.get_subscriber()
subscriber.subscribe_method(events.on_mouse_down, btn_click, b)
subscriber.subscribe_method(events.on_click, btn_click, b)
}
pub fn button(c ButtonConfig) &Button {
mut b := &Button{
width: c.width
height: c.height
z_index: c.z_index
text: c.text
icon_path: c.icon_path
use_icon: c.icon_path != ''
onclick: c.onclick
text_cfg: c.text_cfg
text_size: c.text_size
ui: 0
}
if b.use_icon && !os.exists(c.icon_path) {
println('Invalid icon path "$c.icon_path". The alternate text will be used.')
b.use_icon = false
}
return b
}
fn btn_click(mut b Button, e &MouseEvent, window &Window) {
// println('btn_click for window=$window.title')
if b.point_inside(e.x, e.y) {
if e.action == .down {
b.state = .pressed
} else if e.action == .up {
b.state = .normal
if b.onclick != voidptr(0) {
b.onclick(window.state, b)
}
}
}
}
fn (mut b Button) set_pos(x int, y int) {
b.x = x
b.y = y
}
fn (mut b Button) size() (int, int) {
if b.width == 0 || b.height == 0 {
b.set_text_size()
}
return b.width, b.height
}
fn (mut b Button) propose_size(w int, h int) (int, int) {
// println('prop size $w $h')
if w != 0 {
b.width = w
}
if h != 0 {
b.height = h
}
// b.height = h
// b.width = b.ui.ft.text_width(b.text) + ui.button_horizontal_padding
// b.height = 20 // vertical padding
return b.width, b.height
}
fn (mut b Button) draw() {
if b.use_icon {
b.width = b.image.width
b.height = b.image.height
} else if b.text_width == 0 || b.text_height == 0 {
b.text_width, b.text_height = b.ui.gg.text_size(b.text)
if b.width == 0 {
b.width = b.text_width + ui.button_horizontal_padding
}
if b.height == 0 {
b.height = b.text_height + ui.button_vertical_padding
}
}
w2 := b.text_width / 2
h2 := b.text_height / 2
bcenter_x := b.x + b.width / 2
bcenter_y := b.y + b.height / 2
bg_color := if b.state == .normal { gx.white } else { progress_bar_background_color } // gx.gray }
b.ui.gg.draw_rect(b.x, b.y, b.width, b.height, bg_color) // gx.white)
b.ui.gg.draw_empty_rect(b.x, b.y, b.width, b.height, ui.button_border_color)
mut y := bcenter_y - h2 - 1
// if b.ui.gg.scale == 2 {
// $if macos { // TODO
// y -= 2
// }
if b.use_icon {
b.ui.gg.draw_image(b.x, b.y, b.width, b.height, b.image)
} else {
// b.ui.gg.draw_text(bcenter_x - w2, y, b.text, b.text_cfg.as_text_cfg())
b.draw_text(bcenter_x - w2, y, b.text)
}
$if bb ? {
draw_bb(b, b.ui)
draw_text_bb(bcenter_x - w2, y, b.text_width, b.text_height, b.ui)
}
// b.ui.gg.draw_empty_rect(bcenter_x-w2, bcenter_y-h2, text_width, text_height, ui.button_border_color)
}
fn (mut b Button) set_text_size() {
if b.use_icon {
b.width = b.image.width
b.height = b.image.height
}
// if b.text_width == 0 || b.text_height == 0 {
else if b.ui != 0 {
b.text_width, b.text_height = b.ui.gg.text_size(b.text)
b.text_width = int(f32(b.text_width) * b.ui.gg.scale * b.ui.gg.scale)
b.text_height = int(f32(b.text_height) * b.ui.gg.scale * b.ui.gg.scale)
if b.width == 0 {
b.width = b.text_width + ui.button_horizontal_padding
}
if b.height == 0 {
b.height = b.text_height + ui.button_vertical_padding
}
}
//}
}
// fn (b &Button) key_down(e KeyEvent) {}
fn (b &Button) point_inside(x f64, y f64) bool {
return x >= b.x && x <= b.x + b.width && y >= b.y && y <= b.y + b.height
}
// fn (mut b Button) mouse_move(e MouseEvent) {}
fn (mut b Button) focus() {
b.is_focused = true
}
fn (mut b Button) unfocus() {
b.is_focused = false
b.state = .normal
}
fn (b &Button) is_focused() bool {
return b.is_focused
}
pub fn (mut b Button) set_ui(ui &UI) {
b.ui = ui
}