The gesture callback function defines what the application does when a gesture is
recognized or updated:
If you have gestures that are transitioning based on timer events,
this callback function could be invoked as a result of either a timer event (from
the context of the timer thread) or a mtouch event. Your application code must
implement the synchronization mechanism between mtouch callback functions and
timer-event callback functions. It is also necessary that your application checks
the async parameter and implement
synchronization accordingly.
This function's main component is a
switch statement that defines
the application's actions based on the gesture received. Typically, your gesture
application copies information from the incoming gesture to a local structure and
uses that information accordingly. The type of local structure depends on the
incoming gesture. Usually, you're interested only if a certain gesture has been
detected (i.e., the state of the gesture recognizer is
GESTURE_STATE_COMPLETE). However, your callback function may look
for other states and behave accordingly for your application. For an example of such
switch statement, see the following code:
switch (gesture->type) {
case GESTURE_TWO_FINGER_PAN: {
gesture_tfpan_t* tfpan = (gesture_tfpan_t*)gesture;
if (tfpan->base.state == GESTURE_STATE_COMPLETE)
{
printf("Two-finger pan gesture detected: %d, %d",
tfpan->centroid.x, tfpan->centroid.y);
}
break;
}
case GESTURE_ROTATE: {
gesture_rotate_t* rotate = (gesture_rotate_t*)gesture;
if (rotate->base.state == GESTURE_STATE_COMPLETE) {
if (rotate->angle != rotate->last_angle) {
printf("Rotate: %d degs", rotate->angle - rotate->last_angle);
}
}
break;
}
case GESTURE_SWIPE: {
gesture_swipe_t* swipe = (gesture_swipe_t*)gesture;
if (swipe->base.state == GESTURE_STATE_COMPLETE) {
if (swipe->direction & GESTURE_DIRECTION_UP) {
printf("up %d", swipe->last_coords.y - swipe->coords.y);
} else if (swipe->direction & GESTURE_DIRECTION_DOWN) {
printf("down %d", swipe->coords.y - swipe->last_coords.y);
} else if (swipe->direction & GESTURE_DIRECTION_LEFT) {
printf("left %d", swipe->last_coords.x - swipe->coords.x);
} else if (swipe->direction & GESTURE_DIRECTION_RIGHT) {
printf("right %d", swipe->coords.x - swipe->last_coords.x);
}
}
break;
}
case GESTURE_PINCH: {
gesture_pinch_t* pinch = (gesture_pinch_t*)gesture;
if (pinch->base.state == GESTURE_STATE_COMPLETE) {
printf("Pinch %d, %d", (pinch->last_distance.x - pinch->distance.x),
(pinch->last_distance.y - pinch->distance.y));
}
break;
}
case GESTURE_TAP: {
gesture_tap_t* tap = (gesture_tap_t*)gesture;
if (tap->base.state == GESTURE_STATE_COMPLETE) {
printf("Tap x:%d y:%d",tap->touch_coords.x, tap->touch_coords.y);
}
break;
}
case GESTURE_DOUBLE_TAP: {
gesture_double_tap_t* d_tap = (gesture_double_tap_t*)gesture;
if (d_tap->base.state == GESTURE_STATE_COMPLETE) {
printf("Double tap first_touch x:%d y:%d", d_tap->first_touch.x,
d_tap->first_touch.y);
printf("Double tap first_release x:%d y:%d", d_tap->first_release.x,
d_tap->first_release.y);
printf("Double tap second_touch x:%d y:%d", d_tap->second_touch.x,
d_tap->second_touch.y);
printf("Double tap second_release x:%d y:%d", d_tap->second_touch.x,
d_tap->second_release.y);
}
break;
}
case GESTURE_TRIPLE_TAP: {
gesture_triple_tap_t* t_tap = (gesture_triple_tap_t*)gesture;
if (t_tap->base.state == GESTURE_STATE_COMPLETE) {
printf("Triple tap first_touch x:%d y:%d", t_tap->first_touch.x,
t_tap->first_touch.y);
printf("Triple tap first_release x:%d y:%d", t_tap->first_release.x,
t_tap->first_release.y);
printf("Triple tap second_touch x:%d y:%d", t_tap->second_touch.x,
t_tap->second_touch.y);
printf("Triple tap second_release x:%d y:%d", t_tap->second_touch.x,
t_tap->second_release.y);
printf("Triple tap third_touch x:%d y:%d", t_tap->third_touch.x,
t_tap->second_touch.y);
printf("Triple tap third_release x:%d y:%d", t_tap->third_touch.x,
t_tap->second_release.y);
}
break;
}
case GESTURE_PRESS_AND_TAP: {
gesture_pt_t* pt_t = (gesture_pt_t*)gesture;
if (pt_t->base.state == GESTURE_STATE_COMPLETE) {
printf("Initial press x:%d y:%d", pt_t->initial_coords[0].x,
pt_t->initial_coords[0].y);
printf("Initial tap x:%d y:%d", pt_t->initial_coords[1].x,
pt_t->initial_coords[1].y);
printf("Press x:%d y:%d", pt_t->coords[0].x, pt_t->coords[0].y);
printf("Tap x:%d y:%d", pt_t->coords[1].x, pt_t->coords[1].y);
}
break;
}
case GESTURE_TWO_FINGER_TAP: {
gesture_tft_t* tft_t = (gesture_tft_t*)gesture;
if (tft_t->base.state == GESTURE_STATE_COMPLETE) {
printf("Coordinates of touch event (finger 1) x:%d y:%d",
tft_t->touch_coords[0].x,
tft_t->touch_coords[0].y);
printf("Coordinates of touch event (finger 2) x:%d y:%d",
tft_t->touch_coords[1].x,
tft_t->touch_coords[1].y);
printf("Coordinates of release event (finger 1) x:%d y:%d",
tft_t->release_coords[0].x,
tft_t->release_coords[0].y);
printf("Coordinates of release event (finger 2) x:%d y:%d",
tft_t->release_coords[1].x,
tft_t->release_coords[1].y);
printf("Midpoint between two touches x:%d y:%d", tft_t->centroid.x,
tft_t->centroid.y);
}
break;
}
case GESTURE_LONG_PRESS: {
gesture_long_press_t* lp_t = (gesture_long_press_t*)gesture;
if (lp_t->base.state == GESTURE_STATE_COMPLETE) {
printf("Long press x:%d y:%d",lp_t->coords.x, lp_t->coords.y);
printf("Timer ID:%d",lp_t->success_timer);
}
break;
}
case GESTURE_USER: {
printf("User-defined gesture detected.");
break;
}
default:
printf("Unknown");
break;
}