forked from Tasssadar/multirom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton.c
169 lines (138 loc) · 3.4 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <stdlib.h>
#include "button.h"
#include "input.h"
#include "util.h"
#include "multirom_ui.h"
void button_init_ui(button *b, const char *text, int size)
{
b->touch_id = -1;
if(text != NULL)
{
b->c[CLR_NORMAL][0] = CLR_PRIMARY;
b->c[CLR_NORMAL][1] = WHITE;
b->c[CLR_HOVER][0] = CLR_SECONDARY;
b->c[CLR_HOVER][1] = WHITE;
b->c[CLR_DIS][0] = GRAY;
b->c[CLR_DIS][1] = WHITE;
b->c[CLR_CHECK][0] = CLR_SECONDARY;
b->c[CLR_CHECK][1] = WHITE;
b->rect = fb_add_rect(b->x, b->y, b->w, b->h, b->c[CLR_NORMAL][0]);
int text_x = center_x(b->x, b->w, size, text);
int text_y = center_y(b->y, b->h, size);
b->text = fb_add_text(text_x, text_y, b->c[CLR_NORMAL][1], size, text);
}
else
{
b->text = NULL;
b->rect = NULL;
}
add_touch_handler(&button_touch_handler, b);
}
void button_destroy(button *b)
{
rm_touch_handler(&button_touch_handler, b);
if(b->text)
{
fb_rm_rect(b->rect);
fb_rm_text(b->text);
}
free(b);
}
void button_move(button *b, int x, int y)
{
b->x = x;
b->y = y;
if(b->text)
{
b->rect->head.x = x;
b->rect->head.y = y;
b->text->head.x = center_x(x, b->w, b->text->size, b->text->text);
b->text->head.y = center_y(y, b->h, b->text->size);
}
}
void button_set_hover(button *b, int hover)
{
if((hover == 1) == ((b->flags & BTN_HOVER) != 0))
return;
if(hover)
b->flags |= BTN_HOVER;
else
b->flags &= ~(BTN_HOVER);
if(b->text)
{
button_update_colors(b);
fb_draw();
}
}
void button_enable(button *b, int enable)
{
if((enable == 1) == ((b->flags & BTN_DISABLED) == 0))
return;
if(enable)
b->flags &= ~(BTN_DISABLED);
else
{
b->flags |= BTN_DISABLED;
b->flags &= ~(BTN_HOVER);
}
if(b->text)
{
button_update_colors(b);
fb_draw();
}
}
int button_touch_handler(touch_event *ev, void *data)
{
button *b = (button*)data;
if(b->flags & BTN_DISABLED)
return -1;
if(b->touch_id == -1 && (ev->changed & TCHNG_ADDED))
{
if(!in_rect(ev->x, ev->y, b->x, b->y, b->w, b->h))
return -1;
b->touch_id = ev->id;
}
if(b->touch_id != ev->id)
return -1;
if(ev->changed & TCHNG_REMOVED)
{
if((b->flags & BTN_HOVER) && b->clicked)
(*b->clicked)(b->action);
button_set_hover(b, 0);
b->touch_id = -1;
}
else if(ev->changed & TCHNG_POS)
button_set_hover(b, in_rect(ev->x, ev->y, b->x, b->y, b->w, b->h));
return 0;
}
void button_set_color(button *b, int idx, int text, uint32_t color)
{
b->c[idx][text] = color;
button_update_colors(b);
}
void button_update_colors(button *b)
{
int state = CLR_NORMAL;
if(b->flags & BTN_DISABLED)
state = CLR_DIS;
else if(b->flags & BTN_HOVER)
state = CLR_HOVER;
else if(b->flags & BTN_CHECKED)
state = CLR_CHECK;
if(b->text)
{
b->rect->color = b->c[state][0];
b->text->color = b->c[state][1];
}
}
void button_set_checked(button *b, int checked)
{
if((checked == 1) == ((b->flags & BTN_CHECKED) != 0))
return;
if(checked)
b->flags |= BTN_CHECKED;
else
b->flags &= ~(BTN_CHECKED);
button_update_colors(b);
fb_draw();
}