-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget_menu.v
171 lines (151 loc) · 3.03 KB
/
widget_menu.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
// 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
const (
menu_height = 30
menu_color = gx.rgb(240, 240, 240)
menu_border_color = gx.rgb(223, 223, 223)
)
[heap]
pub struct Menu {
pub mut:
id string
offset_x int
offset_y int
hidden bool
ui &UI
text_cfg gx.TextCfg
text_size f64
component voidptr
width int
height int
mut:
text string
parent Layout = empty_stack
x int
y int
z_index int
items []MenuItem
}
pub type MenuItemFn = fn (m &Menu, item &MenuItem, state voidptr)
pub struct MenuItem {
mut:
action MenuItemFn
pub mut:
text string
}
[params]
pub struct MenuParams {
id string
width int = 150
z_index int
text_cfg gx.TextCfg
text_size f64
text string
items []MenuItem
}
pub fn menu(c MenuParams) &Menu {
return &Menu{
id: c.id
text: c.text
items: c.items
width: c.width
ui: 0
z_index: c.z_index
text_cfg: c.text_cfg
text_size: c.text_size
}
}
fn (mut m Menu) init(parent Layout) {
m.parent = parent
ui := parent.get_ui()
m.ui = ui
init_text_cfg(mut m)
m.update_height()
mut subscriber := parent.get_subscriber()
subscriber.subscribe_method(events.on_click, menu_click, m)
}
[manualfree]
pub fn (mut m Menu) cleanup() {
mut subscriber := m.parent.get_subscriber()
subscriber.unsubscribe_method(events.on_click, m)
unsafe { m.free() }
}
[unsafe]
pub fn (m &Menu) free() {
$if free ? {
print('menu $m.id')
}
unsafe {
m.id.free()
m.text.free()
for item in m.items {
item.text.free()
}
m.items.free()
free(m)
}
$if free ? {
println(' -> freed')
}
}
fn menu_click(mut m Menu, e &MouseEvent, window &Window) {
if m.hidden {
return
}
if m.point_inside(e.x, e.y) {
i := int((e.y - m.y - m.offset_y) / ui.menu_height)
item := m.items[i]
if item.action != voidptr(0) {
parent := m.parent
state := parent.get_state()
item.action(&m, &item, state)
}
}
}
pub fn (mut m Menu) set_pos(x int, y int) {
m.x = x
m.y = y
}
fn (mut m Menu) update_height() {
m.height = m.items.len * ui.menu_height
}
pub fn (mut m Menu) size() (int, int) {
m.update_height()
return m.width, m.height
}
pub fn (mut m Menu) propose_size(w int, h int) (int, int) {
m.width = w
m.height = h
return m.width, m.height
}
fn (mut m Menu) draw() {
offset_start(mut m)
if m.hidden {
return
}
gg := m.ui.gg
gg.draw_rect_filled(m.x, m.y, m.width, m.height, ui.menu_color)
gg.draw_rect_empty(m.x, m.y, m.width, m.height, ui.menu_border_color)
for i, item in m.items {
m.ui.gg.draw_text_def(m.x + 10, m.y + i * ui.menu_height + 10, item.text)
}
offset_end(mut m)
}
pub fn (mut m Menu) add_item(text string, action MenuItemFn) {
m.items << MenuItem{
text: text
action: action
}
}
fn (mut m Menu) set_visible(state bool) {
m.hidden = !state
}
fn (m &Menu) point_inside(x f64, y f64) bool {
return point_inside(m, x, y)
}
pub fn (mut m Menu) set_text(s string) {
m.text = s
}