-
Notifications
You must be signed in to change notification settings - Fork 149
/
button.c
150 lines (121 loc) · 3.61 KB
/
button.c
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
#include "main.h"
static void calculate_pos_and_width(BUTTON *b, int *x, int *w) {
int old_w = *w;
// Increase width if needed, so that button text fits.
if(maybe_i18nal_string_is_valid(&b->button_text)) {
STRING* s = maybe_i18nal_string_get(&b->button_text);
int needed_w = textwidth(s->str, s->length) + 6 * SCALE;
if(*w < needed_w) {
*w = needed_w;
}
}
// Push away from the right border to fit,
// if our panel is right-adjusted.
if(b->panel.x < 0) {
*x -= *w - old_w;
}
}
void button_draw(BUTTON *b, int x, int y, int width, int height)
{
// Button is hidden
if (b->nodraw)
return;
// If `update` function is defined, call it on each draw
if (b->update) {
b->update(b);
}
// Ensure that font is set before calculating position and width.
setfont(FONT_SELF_NAME);
// Button contents color
uint32_t color_text = b->mousedown ? b->ct2 : (b->mouseover ? b->ct2 : b->ct1);
setcolor(color_text);
int w = width;
calculate_pos_and_width(b, &x, &w);
// Button background color
uint32_t color_background = b->mousedown ? b->c3 : (b->mouseover ? b->c2 : b->c1);
if(b->bm) {
drawalpha(b->bm, x, y, width, height, color_background);
} else {
drawrectw(x, y, w, height, b->disabled ? (b->cd ? b->cd : b->cd) : color_background);
//setfont(FONT_TEXT_LARGE);
//setcolor(b->mouseover ? 0x222222 : 0x555555);
//drawtext(x + 5, y, b->text, b->text_length);
}
if(b->bm2) {
int bx = w / 2 - b->bw * SCALE / 2, by = height / 2 - b->bh * SCALE / 2;
drawalpha(b->bm2, x + bx, y + by, b->bw * SCALE, b->bh * SCALE, color_text);
}
if(maybe_i18nal_string_is_valid(&b->button_text)) {
if(b->bm) {
while(w > width) {
// The text didn't fit into the original width.
// Fill the rest of the new width with the image
// and hope for the best.
drawalpha(b->bm, x - width + w, y, width, height, color_background);
w -= width / 2 + 1;
}
}
STRING* s = maybe_i18nal_string_get(&b->button_text);
drawtext(x + 3 * SCALE, y + SCALE, s->str, s->length);
}
}
_Bool button_mmove(BUTTON *b, int UNUSED(x), int UNUSED(y), int width, int height, int mx, int my, int UNUSED(dx), int UNUSED(dy))
{
// Ensure that font is set before calculating position and width.
setfont(FONT_SELF_NAME);
int real_x = 0, real_w = width;
calculate_pos_and_width(b, &real_x, &real_w);
_Bool mouseover = inrect(mx, my, real_x, 0, real_w, height);
if(mouseover) {
if(!b->disabled) {
cursor = CURSOR_HAND;
}
if(maybe_i18nal_string_is_valid(&b->tooltip_text)) {
tooltip_new(&b->tooltip_text);
}
}
if(mouseover != b->mouseover) {
b->mouseover = mouseover;
return 1;
}
return 0;
}
_Bool button_mdown(BUTTON *b)
{
if(!b->mousedown && b->mouseover) {
b->mousedown = 1;
return 1;
}
return 0;
}
_Bool button_mright(BUTTON *b)
{
if(b->mouseover && b->onright) {
b->onright();
return 1;
}
return 0;
}
_Bool button_mwheel(BUTTON *UNUSED(b), int UNUSED(height), double UNUSED(d))
{
return 0;
}
_Bool button_mup(BUTTON *b)
{
if(b->mousedown) {
if(b->mouseover) {
b->onpress();
}
b->mousedown = 0;
return 1;
}
return 0;
}
_Bool button_mleave(BUTTON *b)
{
if(b->mouseover) {
b->mouseover = 0;
return 1;
}
return 0;
}