-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuttons.cc
59 lines (53 loc) · 1.3 KB
/
buttons.cc
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
#include "main.hh"
#include "utils.hh"
using button = button_group::button;
static inline bool in_button(float x, float y, const button &btn)
{
return (x >= btn.pos.x && x < btn.pos.x + btn.size.x &&
y >= btn.pos.y && y < btn.pos.y + btn.size.y);
}
bool button_group::pton(float x, float y)
{
for (auto &btn : buttons) {
if (in_button(x, y, btn)) {
sel_button = &btn;
cur_in = true;
return true;
}
}
sel_button = nullptr;
return false;
}
bool button_group::ptmove(float x, float y)
{
if (sel_button == nullptr) return false;
cur_in = in_button(x, y, *sel_button);
return true;
}
bool button_group::ptoff(float x, float y)
{
if (sel_button == nullptr) return false;
if (cur_in) sel_button->fn();
sel_button = nullptr;
return true;
}
void button_group::draw(tint4 tint, int text_size, vec2 text_anchor)
{
for (const auto &btn : buttons) {
tint4 cur_tint = tint;
if (&btn == sel_button && cur_in)
cur_tint = tint4(tint.r * 0.8f, tint.g * 0.8f, tint.b * 0.8f, 1);
if (btn.content[0] == '#') {
painter::image(
btn.content + 1, btn.pos, btn.size,
cur_tint);
} else {
painter::text(
btn.content,
text_size,
btn.pos + btn.size * text_anchor,
text_anchor,
cur_tint);
}
}
}