Skip to content

Commit

Permalink
Add Touch Support
Browse files Browse the repository at this point in the history
  • Loading branch information
Karthik Kumar Viswanathan committed Feb 26, 2012
1 parent 4d23504 commit 139f467
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 0 deletions.
2 changes: 2 additions & 0 deletions example/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ int main(int argc, char *argv[])
queue = al_create_event_queue();
al_register_event_source(queue, al_get_keyboard_event_source());
al_register_event_source(queue, al_get_mouse_event_source());
if (al_install_touch_input())
al_register_event_source(queue, al_get_touch_input_event_source());

refresh_rate = 60;
fixed_dt = 1.0f / refresh_rate;
Expand Down
9 changes: 9 additions & 0 deletions src/widgets/box.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ int wz_box_proc(WZ_WIDGET* wgt, ALLEGRO_EVENT* event)
ret = 0;
break;
}
case ALLEGRO_EVENT_TOUCH_BEGIN:
{
if (wz_widget_rect_test(wgt, event->touch.x, event->touch.y))
{
wz_ask_parent_for_focus(wgt);
}
ret = 0;
break;
}
case WZ_DRAW:
{
if (wgt->flags & WZ_STATE_HIDDEN)
Expand Down
19 changes: 19 additions & 0 deletions src/widgets/button.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,25 @@ int wz_button_proc(WZ_WIDGET* wgt, ALLEGRO_EVENT* event)
}
break;
}
case ALLEGRO_EVENT_TOUCH_BEGIN:
{
if (wgt->flags & WZ_STATE_DISABLED)
{
ret = 0;
}
else if ((event->touch.x != 0 || event->touch.y != 0) && wz_widget_rect_test(wgt, event->touch.x, event->touch.y))
{
but->down = 1;
wz_ask_parent_for_focus(wgt);
wz_trigger(wgt);
}
else
{
but->down = 0;
ret = 0;
}
break;
}
case WZ_DESTROY:
{
if(but->own)
Expand Down
20 changes: 20 additions & 0 deletions src/widgets/editbox.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,26 @@ int wz_editbox_proc(WZ_WIDGET* wgt, ALLEGRO_EVENT* event)
ret = 0;
break;
}
case ALLEGRO_EVENT_TOUCH_BEGIN:
{
if (wgt->flags & WZ_STATE_DISABLED)
{
ret = 0;
}
else if (wz_widget_rect_test(wgt, event->touch.x, event->touch.y))
{
int len = al_ustr_length(box->text);
ALLEGRO_USTR_INFO info;
ALLEGRO_USTR* text = al_ref_ustr(&info, box->text, box->scroll_pos, len - 1);

ALLEGRO_FONT* font = wgt->theme->get_font(wgt->theme, 0);
wz_ask_parent_for_focus(wgt);
box->cursor_pos = wz_get_text_pos(font, text, event->touch.x - wgt->x) + box->scroll_pos;
}
else
ret = 0;
break;
}
case WZ_HANDLE_SHORTCUT:
{
wz_ask_parent_for_focus(wgt);
Expand Down
33 changes: 33 additions & 0 deletions src/widgets/scroll.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,39 @@ int wz_scroll_proc(WZ_WIDGET* wgt, ALLEGRO_EVENT* event)
ret = 0;
break;
}
case ALLEGRO_EVENT_TOUCH_BEGIN:
{
int button_down = 1;
if (wgt->flags & WZ_STATE_DISABLED)
{
ret = 0;
}
else if(wz_widget_rect_test(wgt, event->touch.x, event->touch.y))
{
wz_ask_parent_for_focus(wgt);
if(button_down == 1)
{
float fraction;
int old_pos;

if (vertical)
fraction = ((float)(event->touch.y - wgt->y)) / ((float)wgt->h);
else
fraction = ((float)(event->touch.x - wgt->x)) / ((float)wgt->w);

old_pos = scl->cur_pos;
scl->cur_pos = (int)(((float)scl->max_pos) * fraction + 0.5f);
if (old_pos != scl->cur_pos)
{
wz_trigger(wgt);
}
}
}
else
ret = 0;
break;

}
case ALLEGRO_EVENT_KEY_CHAR:
{
int old_pos = scl->cur_pos;;
Expand Down
14 changes: 14 additions & 0 deletions src/widgets/toggle.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,20 @@ int wz_toggle_button_proc(WZ_WIDGET* wgt, ALLEGRO_EVENT* event)
return wz_widget_proc(wgt, event);
break;
}
case ALLEGRO_EVENT_TOUCH_BEGIN:
{
if (wgt->flags & WZ_STATE_DISABLED)
{
ret = 0;
}
if (wz_widget_rect_test(wgt, event->touch.x, event->touch.y))
{
wz_trigger(wgt);
wz_ask_parent_for_focus(wgt);
}
return wz_widget_proc(wgt, event);
break;
}
case ALLEGRO_EVENT_KEY_UP:
case ALLEGRO_EVENT_MOUSE_BUTTON_UP:
{
Expand Down
18 changes: 18 additions & 0 deletions src/widgets/widget.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,24 @@ int wz_widget_proc(WZ_WIDGET* wgt, ALLEGRO_EVENT* event)
}
break;
}
/* Switch through elements on Touch:
case ALLEGRO_EVENT_TOUCH_BEGIN:
{
if (wgt->flags & WZ_STATE_DISABLED)
{
ret = 0;
}
else if (wgt->first_child == 0 && wgt->parent != 0)
{
wz_ask_parent_to_focus_next(wgt);
}
else
{
ret = 0;
}
break;
}
*/
case ALLEGRO_EVENT_KEY_CHAR:
{
if(event->keyboard.keycode == wgt->shortcut.keycode && ((event->keyboard.modifiers & wgt->shortcut.modifiers) || wgt->shortcut.modifiers == 0))
Expand Down

0 comments on commit 139f467

Please sign in to comment.