-
Notifications
You must be signed in to change notification settings - Fork 1
/
menu.v
100 lines (90 loc) · 2.24 KB
/
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
module main
import gg
import gx
const menu_line = TextMenuItem{''}
// Menu is list of labels with a callback function that is
// invoked when a label is selected.
struct Menu {
padding int = 10
width int = 125
height int = 20
focused_color gx.Color = gx.white
color gx.Color = gx.Color{0xaa, 0xaa, 0xaa, 0xff}
text_size int = 46
center_text bool = true
mut:
items []MenuItem
focused int
pos Pos
}
// draw draws the menu to the screen
fn (m &Menu) draw(g &gg.Context) {
g.draw_text(-100, -100, '', size: m.text_size)
for i, item in m.items {
textx := if m.center_text {
int((m.pos.x + (m.width / 2)) - g.text_width(item.name) / 2)
} else {
int(m.pos.x)
}
texty := int((m.pos.y + (m.height / 2) - (g.text_height(item.name) / 2) + (i * (m.height +
m.padding))))
match item {
ButtonMenuItem {
g.draw_text(textx, texty, item.name,
bold: true
size: m.text_size
color: if i == m.focused { m.focused_color } else { m.color }
)
}
ToggleMenuItem {
g.draw_text(textx, texty, '$item.name: $item.value',
bold: true
size: m.text_size
color: if i == m.focused { m.focused_color } else { m.color }
)
}
NumberMenuItem {
g.draw_text(textx, texty, '$item.name: < $item.value >',
bold: true
size: m.text_size
color: if i == m.focused { m.focused_color } else { m.color }
)
}
TextMenuItem {
g.draw_text(textx, texty, '$item.name',
bold: true
size: m.text_size
color: if i == m.focused { m.focused_color } else { m.color }
)
}
}
}
}
// MenuItem is a selectable item in a menu.
type MenuItem = ButtonMenuItem | TextMenuItem | NumberMenuItem | ToggleMenuItem
// ButtonMenuItem is a MenuItem which you can press.
struct ButtonMenuItem {
name string
mut:
cb fn (mut App) = fn (mut app App) {}
}
// ToggleMenuItem is a MenuItem which you can toggle.
struct ToggleMenuItem {
name string
mut:
value string
cb fn (mut App) = fn (mut app App) {}
}
// NumberMenuItem is a MenuItem which you can set a number.
struct NumberMenuItem {
name string
step int = 1
mut:
value int
min int
max int
}
// TextMenuItem is a MenuItem in which text is displayed.
struct TextMenuItem {
name string
}