Skip to content

Commit b345a26

Browse files
committedFeb 18, 2013
Touch input changes
* Add option to change button's colors * Add "checked" state for button * Make tab switch buttons on top real buttons * Fix touch issues with listview and buttons
1 parent 8230d2e commit b345a26

File tree

7 files changed

+131
-54
lines changed

7 files changed

+131
-54
lines changed
 

‎button.c

+77-24
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,28 @@ void button_init_ui(button *b, const char *text, int size)
88
{
99
b->touch_id = -1;
1010

11-
b->rect = fb_add_rect(b->x, b->y, b->w, b->h, LBLUE);
12-
13-
int text_x = center_x(b->x, b->w, size, text);
14-
int text_y = center_y(b->y, b->h, size);
15-
b->text = fb_add_text(text_x, text_y, WHITE, size, text);
11+
if(text != NULL)
12+
{
13+
b->c[CLR_NORMAL][0] = LBLUE;
14+
b->c[CLR_NORMAL][1] = WHITE;
15+
b->c[CLR_HOVER][0] = LBLUE2;
16+
b->c[CLR_HOVER][1] = WHITE;
17+
b->c[CLR_DIS][0] = GRAY;
18+
b->c[CLR_DIS][1] = WHITE;
19+
b->c[CLR_CHECK][0] = LBLUE2;
20+
b->c[CLR_CHECK][1] = WHITE;
21+
22+
b->rect = fb_add_rect(b->x, b->y, b->w, b->h, b->c[CLR_NORMAL][0]);
23+
24+
int text_x = center_x(b->x, b->w, size, text);
25+
int text_y = center_y(b->y, b->h, size);
26+
b->text = fb_add_text(text_x, text_y, b->c[CLR_NORMAL][1], size, text);
27+
}
28+
else
29+
{
30+
b->text = NULL;
31+
b->rect = NULL;
32+
}
1633

1734
add_touch_handler(&button_touch_handler, b);
1835
}
@@ -21,8 +38,11 @@ void button_destroy(button *b)
2138
{
2239
rm_touch_handler(&button_touch_handler, b);
2340

24-
fb_rm_rect(b->rect);
25-
fb_rm_text(b->text);
41+
if(b->text)
42+
{
43+
fb_rm_rect(b->rect);
44+
fb_rm_text(b->text);
45+
}
2646

2747
free(b);
2848
}
@@ -32,48 +52,44 @@ void button_move(button *b, int x, int y)
3252
b->x = x;
3353
b->y = y;
3454

35-
b->rect->head.x = x;
36-
b->rect->head.y = y;
55+
if(b->text)
56+
{
57+
b->rect->head.x = x;
58+
b->rect->head.y = y;
3759

38-
b->text->head.x = center_x(x, b->w, b->text->size, b->text->text);
39-
b->text->head.y = center_y(y, b->h, b->text->size);
60+
b->text->head.x = center_x(x, b->w, b->text->size, b->text->text);
61+
b->text->head.y = center_y(y, b->h, b->text->size);
62+
}
4063
}
4164

4265
void button_set_hover(button *b, int hover)
4366
{
44-
if(!( ((b->flags & BTN_HOVER) != 0) ^ (hover == 1) ))
67+
if((hover == 1) == ((b->flags & BTN_HOVER) != 0))
4568
return;
4669

4770
if(hover)
48-
{
4971
b->flags |= BTN_HOVER;
50-
b->rect->color = LBLUE2;
51-
}
5272
else
53-
{
5473
b->flags &= ~(BTN_HOVER);
55-
b->rect->color = LBLUE;
56-
}
5774

75+
button_update_colors(b);
5876
fb_draw();
5977
}
6078

6179
void button_enable(button *b, int enable)
6280
{
63-
if(!( ((b->flags & BTN_DISABLED) == 0) ^ (enable == 1) ))
81+
if((enable == 1) == ((b->flags & BTN_DISABLED) == 0))
6482
return;
6583

6684
if(enable)
67-
{
6885
b->flags &= ~(BTN_DISABLED);
69-
b->rect->color = LBLUE;
70-
}
7186
else
7287
{
7388
b->flags |= BTN_DISABLED;
7489
b->flags &= ~(BTN_HOVER);
75-
b->rect->color = GRAY;
7690
}
91+
92+
button_update_colors(b);
7793
fb_draw();
7894
}
7995

@@ -84,7 +100,7 @@ int button_touch_handler(touch_event *ev, void *data)
84100
if(b->flags & BTN_DISABLED)
85101
return -1;
86102

87-
if(b->touch_id == -1)
103+
if(b->touch_id == -1 && (ev->changed & TCHNG_ADDED))
88104
{
89105
if(!in_rect(ev->x, ev->y, b->x, b->y, b->w, b->h))
90106
return -1;
@@ -107,3 +123,40 @@ int button_touch_handler(touch_event *ev, void *data)
107123

108124
return 0;
109125
}
126+
127+
void button_set_color(button *b, int idx, int text, uint32_t color)
128+
{
129+
b->c[idx][text] = color;
130+
button_update_colors(b);
131+
}
132+
133+
void button_update_colors(button *b)
134+
{
135+
int state = CLR_NORMAL;
136+
if(b->flags & BTN_DISABLED)
137+
state = CLR_DIS;
138+
else if(b->flags & BTN_HOVER)
139+
state = CLR_HOVER;
140+
else if(b->flags & BTN_CHECKED)
141+
state = CLR_CHECK;
142+
143+
if(b->text)
144+
{
145+
b->rect->color = b->c[state][0];
146+
b->text->color = b->c[state][1];
147+
}
148+
}
149+
150+
void button_set_checked(button *b, int checked)
151+
{
152+
if((checked == 1) == ((b->flags & BTN_CHECKED) != 0))
153+
return;
154+
155+
if(checked)
156+
b->flags |= BTN_CHECKED;
157+
else
158+
b->flags &= ~(BTN_CHECKED);
159+
160+
button_update_colors(b);
161+
fb_draw();
162+
}

‎button.h

+17-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,18 @@
77
enum
88
{
99
BTN_HOVER = 0x01,
10-
BTN_DISABLED = 0x02
10+
BTN_DISABLED = 0x02,
11+
BTN_CHECKED = 0x04,
12+
};
13+
14+
enum
15+
{
16+
CLR_NORMAL = 0,
17+
CLR_HOVER,
18+
CLR_DIS,
19+
CLR_CHECK,
20+
21+
CLR_MAX
1122
};
1223

1324
typedef struct
@@ -16,6 +27,8 @@ typedef struct
1627
int w, h;
1728
fb_text *text;
1829
fb_rect *rect;
30+
31+
uint32_t c[CLR_MAX][2];
1932

2033
int flags;
2134
int touch_id;
@@ -29,6 +42,9 @@ void button_destroy(button *b);
2942
void button_move(button *b, int x, int y);
3043
void button_set_hover(button *b, int hover);
3144
void button_enable(button *b, int enable);
45+
void button_set_checked(button *b, int checked);
46+
void button_set_color(button *b, int idx, int text, uint32_t color);
47+
void button_update_colors(button *b);
3248
int button_touch_handler(touch_event *ev, void *data);
3349

3450
#endif

‎checkbox.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ int checkbox_touch_handler(touch_event *ev, void *data)
9898
{
9999
checkbox *box = (checkbox*)data;
100100

101-
if(box->touch_id == -1)
101+
if(box->touch_id == -1 && (ev->changed & TCHNG_ADDED))
102102
{
103103
if(!in_rect(ev->x, ev->y, box->x-TOUCH, box->y-TOUCH, CHECKBOX_SIZE+TOUCH*2, CHECKBOX_SIZE+TOUCH*2))
104104
return -1;

‎input.c

+1
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ static void handle_touch_event(struct input_event *ev)
203203
if(!mt_events[i].changed)
204204
continue;
205205

206+
ERROR("Ev %d 0x%X\n", mt_events[i].id, mt_events[i].changed);
206207
it = mt_handlers;
207208
while(it)
208209
{

‎listview.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ void listview_update_scroll_mark(listview *view)
119119
int listview_touch_handler(touch_event *ev, void *data)
120120
{
121121
listview *view = (listview*)data;
122-
if(view->touch.id == -1)
122+
if(view->touch.id == -1 && (ev->changed & TCHNG_ADDED))
123123
{
124124
if (ev->x < view->x || ev->y < view->y ||
125125
ev->x > view->x+view->w || ev->y > view->y+view->h)

‎multirom_ui.c

+33-27
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
static fb_text *tab_texts[TAB_COUNT] = { 0 };
2525
static fb_rect *selected_tab_rect = NULL;
26+
static button *tab_btns[TAB_COUNT] = { NULL };
2627
static int selected_tab = -1;
2728
static void *tab_data = NULL;
2829
static struct multirom_status *mrom_status = NULL;
@@ -31,8 +32,7 @@ static volatile int exit_ui_code = -1;
3132
static fb_msgbox *active_msgbox = NULL;
3233
static volatile int update_usb_roms = 0;
3334
static volatile int start_pong = 0;
34-
static int pong_btn_w = 0;
35-
static int pong_btn_h = 0;
35+
static button *pong_btn = NULL;
3636

3737
static pthread_mutex_t exit_code_mutex = PTHREAD_MUTEX_INITIALIZER;
3838

@@ -148,6 +148,16 @@ int multirom_ui(struct multirom_status *s, struct multirom_rom **to_boot)
148148
fb_draw();
149149
fb_freeze(1);
150150

151+
button_destroy(pong_btn);
152+
free(pong_btn);
153+
154+
int i;
155+
for(i = 0; i < TAB_COUNT; ++i)
156+
{
157+
button_destroy(tab_btns[i]);
158+
free(tab_btns[i]);
159+
}
160+
151161
stop_input_thread();
152162

153163
multirom_ui_destroy_tab(selected_tab);
@@ -167,8 +177,12 @@ void multirom_ui_init_header(void)
167177
text_x = center_x(0, x, SIZE_EXTRA, str[3]);
168178
fb_add_text(text_x, 5, WHITE, SIZE_EXTRA, str[3]);
169179

170-
pong_btn_w = x;
171-
pong_btn_h = HEADER_HEIGHT;
180+
pong_btn = malloc(sizeof(button));
181+
memset(pong_btn, 0, sizeof(button));
182+
pong_btn->w = x;
183+
pong_btn->h = HEADER_HEIGHT;
184+
pong_btn->clicked = &multirom_ui_start_pong;
185+
button_init_ui(pong_btn, NULL, 0);
172186

173187
for(i = 0; i < TAB_COUNT; ++i)
174188
{
@@ -178,6 +192,15 @@ void multirom_ui_init_header(void)
178192

179193
fb_add_rect(x, 0, 2, HEADER_HEIGHT, WHITE);
180194

195+
tab_btns[i] = malloc(sizeof(button));
196+
memset(tab_btns[i], 0, sizeof(button));
197+
tab_btns[i]->x = x;
198+
tab_btns[i]->w = TAB_BTN_WIDTH;
199+
tab_btns[i]->h = HEADER_HEIGHT;
200+
tab_btns[i]->action = i;
201+
tab_btns[i]->clicked = &multirom_ui_switch;
202+
button_init_ui(tab_btns[i], NULL, 0);
203+
181204
x += TAB_BTN_WIDTH;
182205
}
183206

@@ -298,31 +321,9 @@ int multirom_ui_touch_handler(touch_event *ev, void *data)
298321
}
299322
}
300323

301-
if(!(ev->changed & TCHNG_REMOVED))
302-
return -1;
303-
304-
if(ev->x < pong_btn_w && ev->y < pong_btn_h)
305-
start_pong = 1;
306-
307-
if(touch_count > 0)
324+
if((ev->changed & TCHNG_REMOVED) && touch_count > 0)
308325
--touch_count;
309326

310-
int x = fb_width - (TAB_BTN_WIDTH*TAB_COUNT);
311-
if(ev->y > HEADER_HEIGHT || ev->x < x)
312-
return -1;
313-
314-
int i, nextx;
315-
for(i = 0; i < TAB_COUNT; ++i)
316-
{
317-
nextx = x + TAB_BTN_WIDTH;
318-
if(ev->x > x && ev->x < nextx)
319-
{
320-
multirom_ui_switch(i);
321-
return 0;
322-
}
323-
x = nextx;
324-
}
325-
326327
return -1;
327328
}
328329

@@ -382,6 +383,11 @@ void multirom_ui_refresh_usb_handler(void)
382383
pthread_mutex_unlock(&exit_code_mutex);
383384
}
384385

386+
void multirom_ui_start_pong(int action)
387+
{
388+
start_pong = 1;
389+
}
390+
385391
#define ROMS_FOOTER_H 130
386392
#define ROMS_HEADER_H 90
387393

‎multirom_ui.h

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ void multirom_ui_switch(int tab);
3232
void multirom_ui_fill_rom_list(listview *view, int mask);
3333
void multirom_ui_auto_boot(void);
3434
void multirom_ui_refresh_usb_handler(void);
35+
void multirom_ui_start_pong(int action);
3536

3637
void *multirom_ui_tab_rom_init(int tab_type);
3738
void multirom_ui_tab_rom_destroy(void *data);

0 commit comments

Comments
 (0)
Please sign in to comment.