forked from Tasssadar/multirom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkbox.c
130 lines (104 loc) · 3.04 KB
/
checkbox.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
#include <stdlib.h>
#include "checkbox.h"
#include "input.h"
#include "util.h"
#include "multirom_ui.h"
#define BORDER_SIZE 2
#define BORDER_PADDING 2
#define SELECTED_SIZE (CHECKBOX_SIZE-(BORDER_SIZE+BORDER_PADDING)*2)
#define SELECTED_PADDING (BORDER_SIZE + BORDER_PADDING)
#define TOUCH 15
checkbox *checkbox_create(int x, int y, void (*clicked)(int))
{
checkbox *c = malloc(sizeof(checkbox));
memset(c, 0, sizeof(checkbox));
c->touch_id = -1;
c->clicked = clicked;
c->borders[BORDER_L] = fb_add_rect(0, 0, BORDER_SIZE, CHECKBOX_SIZE, WHITE);
c->borders[BORDER_R] = fb_add_rect(0, 0, BORDER_SIZE, CHECKBOX_SIZE, WHITE);
c->borders[BORDER_T] = fb_add_rect(0, 0, CHECKBOX_SIZE, BORDER_SIZE, WHITE);
c->borders[BORDER_B] = fb_add_rect(0, 0, CHECKBOX_SIZE, BORDER_SIZE, WHITE);
checkbox_set_pos(c, x, y);
if(c->clicked)
add_touch_handler(&checkbox_touch_handler, c);
return c;
}
void checkbox_destroy(checkbox *c)
{
int i;
for(i = 0; i < BORDER_MAX; ++i)
fb_rm_rect(c->borders[i]);
fb_rm_rect(c->selected);
if(c->clicked)
rm_touch_handler(&checkbox_touch_handler, c);
free(c);
}
void checkbox_set_pos(checkbox *c, int x, int y)
{
c->x = x;
c->y = y;
int pos[][2] =
{
// BORDER_L
{ x, y, },
// BORDER_R
{ x + CHECKBOX_SIZE - BORDER_SIZE, y },
// BORDER_T
{ x, y },
// BORDER_B
{ x, y + CHECKBOX_SIZE - BORDER_SIZE }
};
int i;
for(i = 0; i < BORDER_MAX; ++i)
{
c->borders[i]->head.x = pos[i][0];
c->borders[i]->head.y = pos[i][1];
}
if(c->selected)
{
c->selected->head.x = x + SELECTED_PADDING;
c->selected->head.y = y + SELECTED_PADDING;
}
}
void checkbox_select(checkbox *c, int select)
{
if(!((c->selected != NULL) ^ (select != 0)))
return;
if(select)
{
c->selected = fb_add_rect(c->x + SELECTED_PADDING, c->y + SELECTED_PADDING,
SELECTED_SIZE, SELECTED_SIZE, CLR_PRIMARY);
}
else
{
fb_rm_rect(c->selected);
c->selected = NULL;
}
}
int checkbox_touch_handler(touch_event *ev, void *data)
{
checkbox *box = (checkbox*)data;
if(box->touch_id == -1 && (ev->changed & TCHNG_ADDED))
{
if(!in_rect(ev->x, ev->y, box->x-TOUCH, box->y-TOUCH, CHECKBOX_SIZE+TOUCH*2, CHECKBOX_SIZE+TOUCH*2))
return -1;
box->touch_id = ev->id;
box->hover = fb_add_rect(box->x-TOUCH, box->y-TOUCH, CHECKBOX_SIZE+TOUCH*2, CHECKBOX_SIZE+TOUCH*2, CLR_SECONDARY);
fb_draw();
}
if(box->touch_id != ev->id)
return -1;
if(ev->changed & TCHNG_REMOVED)
{
if(in_rect(ev->x, ev->y, box->x-TOUCH, box->y-TOUCH, CHECKBOX_SIZE+TOUCH*2, CHECKBOX_SIZE+TOUCH*2))
{
(*box->clicked)(box->selected == NULL);
checkbox_select(box, (box->selected == NULL));
}
fb_rm_rect(box->hover);
box->hover = NULL;
box->touch_id = -1;
fb_draw();
}
return 0;
}