Gesture Library
drag.c
Go to the documentation of this file.
1 #include "drag.h"
2 
3 #include "math.h"
4 #include "stroke.h"
5 #include "utils.h"
6 
8 void (*on_drag)(const drag_t*) = 0;
9 
10 void init_drag() {
11  for (int i = 0; i < MAX_TOUCHES; i++) {
13  drag_d[i].x0 = 0;
14  drag_d[i].y0 = 0;
15  drag_d[i].x = 0;
16  drag_d[i].y = 0;
17  }
18  on_drag = 0;
19 }
20 
21 static void update_drag(drag_t* drag, const stroke_t* stroke, char down);
22 
23 void recognize_drag(const touch_event_t* event) {
24  const stroke_t* strokes = get_stroke();
25  for (int index = 0; index < MAX_TOUCHES; index++) {
26  update_drag(drag_d + index, strokes + index, event->type == TOUCH_EVENT_DOWN);
27  }
28 }
29 
30 const drag_t* get_drag() {
31  return drag_d;
32 }
33 
34 int set_on_drag(void (*listener)(const drag_t*)) {
35  if (on_drag) {
36  on_drag = listener;
37  return 0;
38  } else {
39  on_drag = listener;
40  return 1;
41  }
42 }
43 
44 static void update_drag(drag_t* drag, const stroke_t* stroke, char down) {
45  drag->group = stroke->group;
46  drag->x0 = stroke->x0;
47  drag->y0 = stroke->y0;
48  drag->x = stroke->x;
49  drag->y = stroke->y;
50  drag->vx = stroke->vx;
51  drag->vy = stroke->vy;
52 
53  switch (drag->state) {
57  if (on_drag) {
58  on_drag(drag);
59  }
60  }
61  break;
64  float dx = stroke->x - stroke->x0;
65  float dy = stroke->y - stroke->y0;
66  if (SQUARE_SUM(dx, dy) > SQUARE(DRAG_DIST_MIN)) {
68  } else {
70  }
71  }
72  if (on_drag) {
73  on_drag(drag);
74  }
75  break;
78  if (down && stroke->state == RECOGNIZER_STATE_IN_PROGRESS) {
80  if (on_drag) {
81  on_drag(drag);
82  }
83  }
84  break;
85  default:
86  return;
87  }
88 }
void init_drag()
Initialize drag data structures.
Definition: drag.c:10
drag_t drag_d[MAX_TOUCHES]
Definition: drag.c:7
void(* on_drag)(const drag_t *)=0
Definition: drag.c:8
int set_on_drag(void(*listener)(const drag_t *))
Subscribe listener to drag gesture updates.
Definition: drag.c:34
const drag_t * get_drag()
Access drag data array of size MAX_TOUCHES.
Definition: drag.c:30
void recognize_drag(const touch_event_t *event)
Recognize drag gesture.
Definition: drag.c:23
@ TOUCH_EVENT_DOWN
Definition: gesturelib.h:9
#define MAX_TOUCHES
Definition: gestureparams.h:5
#define DRAG_DIST_MIN
Definition: gestureparams.h:35
@ RECOGNIZER_STATE_IN_PROGRESS
Definition: recognizer.h:10
@ RECOGNIZER_STATE_FAILED
Definition: recognizer.h:12
@ RECOGNIZER_STATE_NULL
Definition: recognizer.h:6
@ RECOGNIZER_STATE_COMPLETED
Definition: recognizer.h:14
const stroke_t * get_stroke()
Access array of stroke_t of size MAX_TOUCHES.
Definition: stroke.c:47
Data structure for drag gesture data.
Definition: drag.h:7
int group
Definition: drag.h:12
float x0
Definition: drag.h:15
state_t state
Definition: drag.h:9
float vy
Definition: drag.h:24
float vx
Definition: drag.h:23
float y
Definition: drag.h:20
float x
Definition: drag.h:19
float y0
Definition: drag.h:16
Data structure for stroke gesture data.
Definition: stroke.h:12
float x0
Definition: stroke.h:22
state_t state
Definition: stroke.h:14
float vy
Definition: stroke.h:33
unsigned int group
Definition: stroke.h:17
float vx
Definition: stroke.h:32
float y
Definition: stroke.h:28
float x
Definition: stroke.h:27
float y0
Definition: stroke.h:23
To use the gesture library, users create touch events and fill in the appropriate fields.
Definition: gesturelib.h:17
event_type_t type
Definition: gesturelib.h:19
#define SQUARE_SUM(x, y)
Definition: utils.h:4
#define SQUARE(x)
Definition: utils.h:3