aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/evas/gesture/efl_canvas_gesture_recognizer_flick.c
blob: 4968c61b34b849128a29eb0942c191ff65d3a9ba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include "efl_canvas_gesture_private.h"

#define MY_CLASS EFL_CANVAS_GESTURE_RECOGNIZER_FLICK_CLASS

#define MOMENTUM_TIMEOUT 50
#define THUMBSCROLL_FRICTION 0.95
#define THUMBSCROLL_MOMENTUM_THRESHOLD 100.0
#define EFL_GESTURE_MINIMUM_MOMENTUM 0.001

#define RAD2DEG(x) ((x) * 57.295779513)
#define DEG2RAD(x) ((x) / 57.295779513)

EOLIAN static Efl_Canvas_Gesture *
_efl_canvas_gesture_recognizer_flick_efl_canvas_gesture_recognizer_add(Eo *obj, Efl_Canvas_Gesture_Recognizer_Flick_Data *pd EINA_UNUSED, Efl_Object *target EINA_UNUSED)
{
   return efl_add(EFL_CANVAS_GESTURE_FLICK_CLASS, obj);
}

static void
_momentum_set(Eo *obj,
              Efl_Canvas_Gesture_Flick_Data *fd,
              Eina_Position2D v1,
              Eina_Position2D v2,
              unsigned int t1,
              unsigned int t2)
{
   Evas_Coord velx = 0, vely = 0, vel;
   Evas_Coord dx = v2.x - v1.x;
   Evas_Coord dy = v2.y - v1.y;
   int dt = t2 - t1;
   Eina_Value *tf, *tmt;
   double thumbscroll_friction, thumbscroll_momentum_threshold;

   if (dt > 0)
     {
        velx = (dx * 1000) / dt;
        vely = (dy * 1000) / dt;
     }

   vel = sqrt((velx * velx) + (vely * vely));

   tf = efl_gesture_recognizer_config_get(obj, "thumbscroll_friction");
   if (tf) eina_value_get(tf, &thumbscroll_friction);
   else thumbscroll_friction = THUMBSCROLL_FRICTION;

   tmt = efl_gesture_recognizer_config_get(obj, "thumbscroll_momentum_threshold");
   if (tmt) eina_value_get(tmt, &thumbscroll_momentum_threshold);
   else thumbscroll_momentum_threshold = THUMBSCROLL_MOMENTUM_THRESHOLD;

   if ((thumbscroll_friction > 0.0) &&
       (vel > thumbscroll_momentum_threshold)) /* report
                                                * momentum */
     {
        fd->momentum.x = velx;
        fd->momentum.y = vely;
     }
   else
     {
        fd->momentum.x = 0;
        fd->momentum.y = 0;
     }
}

static void
_single_line_process(Eo *obj,
                     Efl_Canvas_Gesture_Recognizer_Flick_Data *pd,
                     Efl_Canvas_Gesture *gesture,
                     Efl_Canvas_Gesture_Flick_Data *fd,
                     Efl_Canvas_Gesture_Touch *event)
{
   switch (efl_gesture_touch_state_get(event))
     {
       case EFL_GESTURE_TOUCH_BEGIN:
       case EFL_GESTURE_TOUCH_UPDATE:
         if (!pd->t_st)
           {
              pd->st_line = efl_gesture_touch_cur_point_get(event);
              pd->t_st = efl_gesture_touch_cur_timestamp_get(event);

              efl_gesture_hotspot_set(gesture, pd->st_line);

              return;
           }

         break;

       case EFL_GESTURE_TOUCH_END:
         {
            if (!pd->t_st) return;

            pd->t_end = efl_gesture_touch_cur_timestamp_get(event);

            break;
         }

       default:

         return;
     }

   _momentum_set(obj, fd, pd->st_line, efl_gesture_touch_cur_point_get(event),
                 pd->t_st, efl_gesture_touch_cur_timestamp_get(event));
}

static double
_angle_get(Evas_Coord xx1,
           Evas_Coord yy1,
           Evas_Coord xx2,
           Evas_Coord yy2)
{
   double a, xx, yy, rt = (-1);

   xx = abs(xx2 - xx1);
   yy = abs(yy2 - yy1);

   if (((int)xx) && ((int)yy))
     {
        rt = a = RAD2DEG(atan(yy / xx));
        if (xx1 < xx2)
          {
             if (yy1 < yy2) rt = 360 - a;
             else rt = a;
          }
        else
          {
             if (yy1 < yy2) rt = 180 + a;
             else rt = 180 - a;
          }
     }

   if (rt < 0) /* Do this only if rt is not set */
     {
        if (((int)xx)) /* Horizontal line */
          {
             if (xx2 < xx1) rt = 180;
             else rt = 0.0;
          }
        else
          {  /* Vertical line */
             if (yy2 < yy1) rt = 90;
             else rt = 270;
          }
     }

   /* Now we want to change from:
    *                      90                   0
    * original circle   180   0   We want:  270   90
    *                     270                 180
    */
   rt = 450 - rt;
   if (rt >= 360) rt -= 360;

   return rt;
}


static void
_vector_get(Eina_Position2D v1,
            Eina_Position2D v2,
            int *l,
            double *a)
{
   int xx, yy;

   xx = (int)(v2.x - v1.x);
   yy = (int)(v2.y - v1.y);
   *l = (int)sqrt((xx * xx) + (yy * yy));
   *a = _angle_get((int)v1.x, (int)v1.y, (int)v2.x, (int)v2.y);
}

EOLIAN static Efl_Canvas_Gesture_Recognizer_Result
_efl_canvas_gesture_recognizer_flick_efl_canvas_gesture_recognizer_recognize(Eo *obj,
                                                                             Efl_Canvas_Gesture_Recognizer_Flick_Data *pd,
                                                                             Efl_Canvas_Gesture *gesture, Efl_Object *watched,
                                                                             Efl_Canvas_Gesture_Touch *event)
{
   double angle;
   Eina_Value *val;
   unsigned char glayer_continues_enable;
   Efl_Canvas_Gesture_Recognizer_Result result = EFL_GESTURE_CANCEL;
   Eina_Bool touch_up = EINA_FALSE;
   Efl_Canvas_Gesture_Flick_Data *fd = efl_data_scope_get(gesture, EFL_CANVAS_GESTURE_FLICK_CLASS);
   Efl_Canvas_Gesture_Recognizer_Data *rd = efl_data_scope_get(obj, EFL_CANVAS_GESTURE_RECOGNIZER_CLASS);

   val = efl_gesture_recognizer_config_get(obj, "glayer_continues_enable");
   if (val) eina_value_get(val, &glayer_continues_enable);
   else glayer_continues_enable = 1;

   //We need to cover events that occur continuously in the mouse down state
   //without mouse up.
   //Recognizing the gesture again, even though it was canceled during gesture
   //recognition.
   if (efl_gesture_state_get(gesture) == EFL_GESTURE_CANCELED)
     efl_gesture_state_set(gesture, EFL_GESTURE_NONE);

   if (efl_gesture_touch_state_get(event) == EFL_GESTURE_TOUCH_END)
     touch_up = EINA_TRUE;

   //This is to handle a case with a mouse click on the target object.
   if (efl_gesture_touch_state_get(event) == EFL_GESTURE_TOUCH_END && !pd->touched)
     efl_gesture_manager_gesture_clean_up(rd->manager, watched, EFL_EVENT_GESTURE_FLICK);

   if (glayer_continues_enable && !pd->touched)
     {
       pd->touched = EINA_TRUE;
       pd->line_angle = -1.0;
       rd->continues = EINA_TRUE;

       return EFL_GESTURE_IGNORE;
     }

   _single_line_process(obj, pd, gesture, fd, event);
   _vector_get(pd->st_line, efl_gesture_touch_cur_point_get(event),
               &pd->line_length, &angle);

   if (pd->t_st)
     {
        if (pd->line_angle >= 0.0)
          {
             double line_distance_tolerance, line_angular_tolerance;
             double a = fabs(angle - pd->line_angle);
             double d = (tan(DEG2RAD(a))) * pd->line_length;

             val = efl_gesture_recognizer_config_get(obj, "glayer_line_distance_tolerance");
             if (val) eina_value_get(val, &line_distance_tolerance);
             else line_distance_tolerance = 3.0;

             line_distance_tolerance *= rd->finger_size;

             val = efl_gesture_recognizer_config_get(obj, "glayer_line_angular_tolerance");
             if (val) eina_value_get(val, &line_angular_tolerance);
             else line_angular_tolerance = 20.0;

             if ((d > line_distance_tolerance) ||
                 (a > line_angular_tolerance))
               {
                  memset(pd, 0, sizeof(Efl_Canvas_Gesture_Recognizer_Flick_Data));

                  if (touch_up) rd->continues = EINA_FALSE;

                  return EFL_GESTURE_CANCEL;
               }

             /* We may finish line if momentum is zero */
             if (glayer_continues_enable)
               {
                  /* This is for continues-gesture */
                  /* Finish line on zero momentum for continues gesture */
                  if ((!fd->momentum.x) && (!fd->momentum.y))
                    pd->t_end = efl_gesture_touch_cur_timestamp_get(event);
               }
          }
        else
          {
             double line_min_length;

             val = efl_gesture_recognizer_config_get(obj, "glayer_line_min_length");
             if (val) eina_value_get(val, &line_min_length);
             else line_min_length = 1.0;

             line_min_length *= rd->finger_size;

             if (pd->line_length >= line_min_length)
               fd->angle = pd->line_angle = angle;

          }

        if (pd->t_end)
          {
             if (pd->line_angle < 0.0)
               {
                  memset(pd, 0, sizeof(Efl_Canvas_Gesture_Recognizer_Flick_Data));

                  if (touch_up) rd->continues = EINA_FALSE;

                  return EFL_GESTURE_CANCEL;
               }
          }
     }

   unsigned int tm_end = efl_gesture_touch_cur_timestamp_get(event);
   if (pd->t_end)
     {
        if (pd->t_end < tm_end)
          tm_end = pd->t_end;
     }

   unsigned int time_limit_ms;
   val = efl_gesture_recognizer_config_get(obj, "glayer_flick_time_limit_ms");
   if (val) eina_value_get(val, &time_limit_ms);
   else time_limit_ms = 120;

   if ((tm_end - pd->t_st) > time_limit_ms)
   {
     memset(pd, 0, sizeof(Efl_Canvas_Gesture_Recognizer_Flick_Data));

     if (touch_up) rd->continues = EINA_FALSE;

     return EFL_GESTURE_CANCEL;
   }

   switch (efl_gesture_touch_state_get(event))
     {
      case EFL_GESTURE_TOUCH_BEGIN:
      case EFL_GESTURE_TOUCH_UPDATE:
        {
           if (pd->t_st)
             {
                if (glayer_continues_enable && pd->t_end)
                  {
                     result = EFL_GESTURE_FINISH;
                  }
                else
                  {
                     result = EFL_GESTURE_TRIGGER;
                  }
             }
           break;
        }

      case EFL_GESTURE_TOUCH_END:
        {
           if (!pd->t_st)
             {
                pd->touched = EINA_FALSE;
                rd->continues = EINA_FALSE;

                return EFL_GESTURE_CANCEL;
             }
           if (pd->t_st && pd->t_end)
             {
                rd->continues = EINA_FALSE;

                result = EFL_GESTURE_FINISH;
             }

           efl_gesture_hotspot_set(gesture, efl_gesture_touch_cur_point_get(event));

           memset(pd, 0, sizeof(Efl_Canvas_Gesture_Recognizer_Flick_Data));

		   rd->continues = EINA_FALSE;

           break;
        }

      default:

        break;
     }

   return result;
}

#include "efl_canvas_gesture_recognizer_flick.eo.c"