Updated: October 28, 2024 |
You can define gesture recognizers to detect gestures that are not already supported by the Gestures library (custom gestures). Gesture recognizers that are used to detect custom gestures can be compiled with the application code and added to a gesture set in the same way that system gesture recognizers are added to a gesture set.
If you're defining your own custom gesture recognizers, you will need to provide the following as part of the definition and implementation of your own gesture recognizer:
Definition of your custom gesture
typedef struct { gesture_base_t base; /* The gesture base data structure. */ ... /* The information specific to your custom gesture */ int timer_id; /* The ID of the timer for this custom gesture (if needed) */ } gesture_custom_t;
This structure represents information for your custom gesture recognizer; this structure must include gesture_base_t followed by additional members to capture your specific information. For example, if your custom gesture recognizer uses a timer, you need to include the ID of the timer as a specific parameter.
Definition and implementation of the alloc() function
gesture_custom_t* custom_gesture_alloc(gesture_custom_params_t* params, gesture_callback_f callback, struct gestures_set* set);
Definition and implementation of the process_event() function
gesture_state_e (*process_event)(struct contact_id_map* map, struct gesture_base* gesture, mtouch_event_t* event, int* consumed)Your process_event() function must handle state transitions and return the new, or unchanged, gesture recognizer state. If your custom gesture is time-based, you will need to adjust the timers accordingly. The Gestures library provides API functions for you to set and reset your timers.
Definition and implementation of the free() function
void (*free)(struct gesture_base* gesture)
Your free() function must release all the memory that's associated with your gesture recognizer that was allocated by your alloc() function.
Definition and implementation of the reset() function
void (*reset)(struct gesture_base* gesture);
Your reset() function must reset the gesture-specific data structures to their initial states.
Definition of parameters specific to your custom gesture (optional)
This is a structure that represents the parameters specific to your custom gesture recognizer.
Definition of states specific to your custom gesture (optional)
These are constants that represent the states specific to your custom gesture recognizer; these states are in addition to the set of states defined in gesture_state_e.
Definition and implementation of the gesture_timer_callback() function (optional)
gesture_state_e(*gesture_timer_callback_t)(struct gesture_base* gesture, void* param);
Gesture sets provide time-based notifications to gesture recognizers that use timers. A notification is implemented as a callback function to the gesture recognizer. If your custom gesture recognizer is timer-based, you need to implement this timer callback function.
A gesture recognizer can transition states on timer events. Similar to the process_event() function for mtouch events, the gesture recognizer's time-based state transitions are accomplished by its timer callback function. This function returns the new, or unchanged, state based on the timer event received.
A contact ID is an identifier that is used to identify mtouch events. The mtouch event data structure contains the contact_id element that is assigned a value from a zero-based index and corresponds to the individual fingers that touch the screen. The contact ID doesn't change until that finger is released.
User-defined gestures typically need to associate a specific mtouch event with a contact ID for the purpose of associating the streams of events with the finger that caused them. The contact ID from the mtouch event can't be used directly by the custom gesture. Instead, user-defined gestures need to invoke map_contact_id() to obtain a zero-based contact ID that is remapped from the gesture set's perspective.
The remapping is necessary because a contact ID of 1 for an mtouch event could actually correspond to a gesture set's contact ID 0. This mapping could be the case if there are multiple gesture sets in play, or if the user's finger is resting on the touch-sensitive bevel.
Helper functions are available if you are defining your own gestures. These functions are: