scroller smoothing stuff.
[framework/uifw/elementary.git] / src / lib / els_scroller.c
index 7174178..bf51363 100644 (file)
@@ -36,8 +36,12 @@ struct _Smart_Data
         Evas_Coord b2x, b2y;
         struct {
              Evas_Coord    x, y;
-             double        timestamp;
-        } history[20];
+             double        timestamp, localtimestamp;
+        } history[60];
+        struct {
+           double tadd, dxsum, dysum;
+           double est_timestamp_diff;
+        } hist;
       double anim_start;
       double anim_start2;
       double anim_start3;
@@ -814,6 +818,7 @@ _smart_momentum_animator(void *data)
              y = py;
           }
         elm_smart_scroller_child_pos_set(sd->smart_obj, x, y);
+        _update_wanted_coordinates(sd, x, y);
         sd->pan_func.max_get(sd->pan_obj, &maxx, &maxy);
         sd->pan_func.min_get(sd->pan_obj, &minx, &miny);
         if (!sd->bounce_horiz)
@@ -831,7 +836,6 @@ _smart_momentum_animator(void *data)
             (no_bounce_x_end && no_bounce_y_end))
           {
              _smart_anim_stop(sd->smart_obj);
-             _update_wanted_coordinates(sd, px, py);
 
              sd->down.momentum_animator = NULL;
              sd->down.bounce_x_hold = 0;
@@ -1007,18 +1011,19 @@ elm_smart_scroller_child_pos_get(Evas_Object *obj, Evas_Coord *x, Evas_Coord *y)
    sd->pan_func.get(sd->pan_obj, x, y);
 }
 
-/* "internal_call" actually toggles whether we should save the coords and do
- * extra "speedup" checks, or not. */
-static void
-_elm_smart_scroller_child_region_show_internal(Evas_Object *obj, Evas_Coord x, Evas_Coord y, Evas_Coord w, Evas_Coord h, Eina_Bool internal_call)
+/* returns TRUE when we need to move the scroller, FALSE otherwise.
+ * Updates w and h either way, so save them if you need them. */
+static Eina_Bool
+_elm_smart_scroller_child_region_show_internal(Evas_Object *obj, Evas_Coord *_x, Evas_Coord *_y, Evas_Coord w, Evas_Coord h)
 {
-   Evas_Coord mx = 0, my = 0, cw = 0, ch = 0, px = 0, py = 0, nx, ny, minx = 0, miny = 0;
+   Evas_Coord mx = 0, my = 0, cw = 0, ch = 0, px = 0, py = 0, nx, ny, minx = 0, miny = 0, pw = 0, ph = 0, x = *_x, y = *_y;
 
-   API_ENTRY return;
+   API_ENTRY return EINA_FALSE;
    sd->pan_func.max_get(sd->pan_obj, &mx, &my);
    sd->pan_func.min_get(sd->pan_obj, &minx, &miny);
    sd->pan_func.child_size_get(sd->pan_obj, &cw, &ch);
    sd->pan_func.get(sd->pan_obj, &px, &py);
+   evas_object_geometry_get(sd->pan_obj, NULL, NULL, &pw, &ph);
 
    nx = px;
    if ((x < px) && ((x + w) < (px + (cw - mx)))) nx = x;
@@ -1026,14 +1031,6 @@ _elm_smart_scroller_child_region_show_internal(Evas_Object *obj, Evas_Coord x, E
    ny = py;
    if ((y < py) && ((y + h) < (py + (ch - my)))) ny = y;
    else if ((y > py) && ((y + h) > (py + (ch - my)))) ny = y + h - (ch - my);
-   if (!internal_call)
-     {
-        sd->wx = x;
-        sd->wy = y;
-        sd->ww = w;
-        sd->wh = h;
-        if ((nx == px) && (ny == py)) return;
-     }
 
    if ((sd->down.bounce_x_animator) || (sd->down.bounce_y_animator) ||
        (sd->scrollto.x.animator) || (sd->scrollto.y.animator))
@@ -1079,13 +1076,18 @@ _elm_smart_scroller_child_region_show_internal(Evas_Object *obj, Evas_Coord x, E
         sd->down.pdx = 0;
         sd->down.pdy = 0;
      }
+
    x = nx;
-   if ((x + w) > cw) x = cw - w;
+   if ((x + pw) > cw) x = cw - pw;
    if (x < minx) x = minx;
    y = ny;
-   if ((y + h) > ch) y = ch - h;
+   if ((y + ph) > ch) y = ch - ph;
    if (y < miny) y = miny;
-   elm_smart_scroller_child_pos_set(obj, x, y);
+
+   if ((x == px) && (y == py)) return EINA_FALSE;
+   *_x = x;
+   *_y = y;
+   return EINA_TRUE;
 }
 
 /* Set should be used for calculated positions, for example, when we move
@@ -1094,7 +1096,8 @@ _elm_smart_scroller_child_region_show_internal(Evas_Object *obj, Evas_Coord x, E
 void
 elm_smart_scroller_child_region_set(Evas_Object *obj, Evas_Coord x, Evas_Coord y, Evas_Coord w, Evas_Coord h)
 {
-   _elm_smart_scroller_child_region_show_internal(obj, x, y, w, h, EINA_TRUE);
+   if (_elm_smart_scroller_child_region_show_internal(obj, &x, &y, w, h))
+      elm_smart_scroller_child_pos_set(obj, x, y);
 }
 
 /* Set should be used for setting the wanted position, for example a user scroll
@@ -1102,7 +1105,13 @@ elm_smart_scroller_child_region_set(Evas_Object *obj, Evas_Coord x, Evas_Coord y
 void
 elm_smart_scroller_child_region_show(Evas_Object *obj, Evas_Coord x, Evas_Coord y, Evas_Coord w, Evas_Coord h)
 {
-   _elm_smart_scroller_child_region_show_internal(obj, x, y, w, h, EINA_FALSE);
+   API_ENTRY return;
+   sd->wx = x;
+   sd->wy = y;
+   sd->ww = w;
+   sd->wh = h;
+   if (_elm_smart_scroller_child_region_show_internal(obj, &x, &y, w, h))
+      elm_smart_scroller_child_pos_set(obj, x, y);
 }
 
 void
@@ -1296,77 +1305,12 @@ elm_smart_scroller_paging_get(Evas_Object *obj, double *pagerel_h, double *pager
 void
 elm_smart_scroller_region_bring_in(Evas_Object *obj, Evas_Coord x, Evas_Coord y, Evas_Coord w, Evas_Coord h)
 {
-   Evas_Coord mx = 0, my = 0, cw = 0, ch = 0, px = 0, py = 0, nx, ny, minx = 0, miny = 0;
-
    API_ENTRY return;
-   sd->pan_func.max_get(sd->pan_obj, &mx, &my);
-   sd->pan_func.min_get(sd->pan_obj, &minx, &miny);
-   sd->pan_func.child_size_get(sd->pan_obj, &cw, &ch);
-   sd->pan_func.get(sd->pan_obj, &px, &py);
-
-   nx = px;
-   if ((x < px) && ((x + w) < (px + (cw - mx)))) nx = x;
-   else if ((x > px) && ((x + w) > (px + (cw - mx)))) nx = x + w - (cw - mx);
-   ny = py;
-   if ((y < py) && ((y + h) < (py + (ch - my)))) ny = y;
-   else if ((y > py) && ((y + h) > (py + (ch - my)))) ny = y + h - (ch - my);
-   sd->wx = x;
-   sd->wy = y;
-   sd->ww = w;
-   sd->wh = h;
-   if ((nx == px) && (ny == py)) return;
-   if ((sd->down.bounce_x_animator) || (sd->down.bounce_y_animator) ||
-       (sd->scrollto.x.animator) || (sd->scrollto.y.animator))
-     {
-        _smart_anim_stop(sd->smart_obj);
-     }
-   if (sd->scrollto.x.animator)
-     {
-        ecore_animator_del(sd->scrollto.x.animator);
-        sd->scrollto.x.animator = NULL;
-     }
-   if (sd->scrollto.y.animator)
-     {
-        ecore_animator_del(sd->scrollto.y.animator);
-        sd->scrollto.y.animator = NULL;
-     }
-   if (sd->down.bounce_x_animator)
-     {
-        ecore_animator_del(sd->down.bounce_x_animator);
-        sd->down.bounce_x_animator = NULL;
-        sd->bouncemex = 0;
-     }
-   if (sd->down.bounce_y_animator)
-     {
-        ecore_animator_del(sd->down.bounce_y_animator);
-        sd->down.bounce_y_animator = NULL;
-        sd->bouncemey = 0;
-     }
-   if (sd->down.hold_animator)
+   if (_elm_smart_scroller_child_region_show_internal(obj, &x, &y, w, h))
      {
-        ecore_animator_del(sd->down.hold_animator);
-        sd->down.hold_animator = NULL;
-        _smart_drag_stop(sd->smart_obj);
+        _smart_scrollto_x(sd, _elm_config->bring_in_scroll_friction, x);
+        _smart_scrollto_y(sd, _elm_config->bring_in_scroll_friction, y);
      }
-   if (sd->down.momentum_animator)
-     {
-        ecore_animator_del(sd->down.momentum_animator);
-        sd->down.momentum_animator = NULL;
-        sd->down.bounce_x_hold = 0;
-        sd->down.bounce_y_hold = 0;
-        sd->down.ax = 0;
-        sd->down.ay = 0;
-        sd->down.pdx = 0;
-        sd->down.pdy = 0;
-     }
-   x = nx;
-   if ((x + w) > cw) x = cw - w;
-   if (x < minx) x = minx;
-   _smart_scrollto_x(sd, _elm_config->bring_in_scroll_friction, x);
-   y = ny;
-   if ((y + h) > ch) y = ch - h;
-   if (y < miny) y = miny;
-   _smart_scrollto_y(sd, _elm_config->bring_in_scroll_friction, y);
 }
 
 void
@@ -1382,6 +1326,10 @@ _elm_smart_scroller_wanted_region_set(Evas_Object *obj)
    INTERNAL_ENTRY;
    Evas_Coord ww, wh, wx = sd->wx;
 
+   if (sd->down.now || sd->down.momentum_animator ||
+       sd->down.bounce_x_animator || sd->down.bounce_y_animator ||
+       sd->down.hold_animator || sd->down.onhold_animator) return;
+
    /* Flip to RTL cords only if init in RTL mode */
    if(sd->is_mirrored)
      wx = _elm_smart_scroller_x_mirrored_get(obj, sd->wx);
@@ -1645,6 +1593,8 @@ _smart_event_mouse_down(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSE
           }
         if (ev->button == 1)
           {
+             sd->down.hist.est_timestamp_diff =
+                ecore_loop_time_get() - ((double)ev->timestamp / 1000.0);
              sd->down.now = 1;
              sd->down.dragged = 0;
              sd->down.dir_x = 0;
@@ -1655,9 +1605,10 @@ _smart_event_mouse_down(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSE
              sd->down.sx = x;
              sd->down.sy = y;
              sd->down.locked = 0;
-             memset(&(sd->down.history[0]), 0, sizeof(sd->down.history[0]) * 20);
+             memset(&(sd->down.history[0]), 0, sizeof(sd->down.history[0]) * 60);
 #ifdef EVTIME
              sd->down.history[0].timestamp = ev->timestamp / 1000.0;
+             sd->down.history[0].localtimestamp = ecore_loop_time_get();
 #else
              sd->down.history[0].timestamp = ecore_loop_time_get();
 #endif
@@ -1670,12 +1621,131 @@ _smart_event_mouse_down(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSE
      }
 }
 
+static void
+_down_coord_eval(Smart_Data *sd, Evas_Coord *x, Evas_Coord *y)
+{
+   Evas_Coord minx, miny;
+   
+   if (sd->down.dir_x) *x = sd->down.sx - (*x - sd->down.x);
+   else *x = sd->down.sx;
+   if (sd->down.dir_y) *y = sd->down.sy - (*y - sd->down.y);
+   else *y = sd->down.sy;
+   
+   if ((sd->down.dir_x) || (sd->down.dir_y))
+     {
+        if (!((sd->down.dir_x) && (sd->down.dir_y)))
+          {
+             if (sd->down.dir_x) *y = sd->down.locked_y;
+             else *x = sd->down.locked_x;
+          }
+     }
+
+   sd->pan_func.min_get(sd->pan_obj, &minx, &miny);
+   
+   if (*x < minx)
+      *x += (minx - *x) * _elm_config->thumbscroll_border_friction;
+   else if (sd->child.w <= sd->w)
+      *x += (sd->down.sx - *x) * _elm_config->thumbscroll_border_friction;
+   else if ((sd->child.w - sd->w + minx) < *x)
+      *x += (sd->child.w - sd->w + minx - *x) * 
+      _elm_config->thumbscroll_border_friction;
+   
+   if (*y < miny)
+      *y += (miny - *y) * _elm_config->thumbscroll_border_friction;
+   else if (sd->child.h <= sd->h)
+      *y += (sd->down.sy - *y) * _elm_config->thumbscroll_border_friction;
+   else if ((sd->child.h - sd->h + miny) < *y)
+      *y += (sd->child.h - sd->h + miny - *y) *
+      _elm_config->thumbscroll_border_friction;
+}
+
 static Eina_Bool
 _smart_hold_animator(void *data)
 {
    Smart_Data *sd = data;
-   Evas_Coord ox, oy;
+   Evas_Coord ox = 0, oy = 0, fx, fy;
+
+   fx = sd->down.hold_x;
+   fy = sd->down.hold_y;
+   if (_elm_config->scroll_smooth_amount > 0.0)
+     {
+        int i, count = 0;
+        Evas_Coord basex, basey, x, y;
+        double dt, t, tdiff, tnow;
+        struct {
+           Evas_Coord x, y, dx, dy;
+           double t, dt;
+        } pos[60];
+        
+        // FIXME: assume server and client have the same "timezone"
+        // (0 timepoint) for now. this needs to be figured out in advance
+        // though.
+        tdiff = sd->down.hist.est_timestamp_diff;
+        tnow = ecore_time_get() - tdiff;
+        t = tnow;
+        for (i = 0; i < 60; i++)
+          {
+             // oldest point is sd->down.history[i]
+             // newset is sd->down.history[0]
+             dt = t - sd->down.history[i].timestamp;
+             if (dt > 0.1)
+               {
+                  i--;
+                  break;
+               }
+             x = sd->down.history[i].x;
+             y = sd->down.history[i].y;
+             _down_coord_eval(sd, &x, &y);
+             if (i == 0)
+               {
+                  basex = x;
+                  basey = y;
+               }
+             pos[i].x = x - basex;
+             pos[i].y = y - basey;
+             pos[i].t = 
+                sd->down.history[i].timestamp - sd->down.history[0].timestamp;
+             count++;
+          }
+        count = i;
+        if (count >= 2)
+          {
+             double dtsum = 0.0, tadd, maxdt;
+             double dxsum = 0.0, dysum = 0.0, xsum = 0.0, ysum = 0.0;
 
+             for (i = 0; i < (count - 1); i++)
+               {
+                  pos[i].dx = pos[i].x - pos[i + 1].x;
+                  pos[i].dy = pos[i].y - pos[i + 1].y;
+                  pos[i].dt = pos[i].t - pos[i + 1].t;
+                  dxsum += pos[i].dx;
+                  dysum += pos[i].dy;
+                  dtsum += pos[i].dt;
+                  xsum += pos[i].x;
+                  ysum += pos[i].y;
+               }
+             maxdt = pos[i].t;
+             dxsum /= (double)i;
+             dysum /= (double)i;
+             dtsum /= (double)i;
+             xsum /= (double)i;
+             ysum /= (double)i;
+             tadd = tnow - sd->down.history[0].timestamp + _elm_config->scroll_smooth_future_time;
+             tadd = tadd - (maxdt / 2);
+#define WEIGHT(n, o, v) n = (o * (v - 1.0)) + (n * v)
+             WEIGHT(tadd, sd->down.hist.tadd, _elm_config->scroll_smooth_history_weight);
+             WEIGHT(dxsum, sd->down.hist.dxsum, _elm_config->scroll_smooth_history_weight);
+             WEIGHT(dysum, sd->down.hist.dysum, _elm_config->scroll_smooth_history_weight);
+             fx = basex + xsum + ((dxsum * tadd) / dtsum);
+             fy = basey + ysum + ((dysum * tadd) / dtsum);
+             sd->down.hist.tadd = tadd;
+             sd->down.hist.dxsum = dxsum;
+             sd->down.hist.dysum = dysum;
+             fx = WEIGHT(fx, sd->down.hold_x, _elm_config->scroll_smooth_amount);
+             fy = WEIGHT(fy, sd->down.hold_y, _elm_config->scroll_smooth_amount);
+          }
+     }
+   
    elm_smart_scroller_child_pos_get(sd->smart_obj, &ox, &oy);
    if (sd->down.dir_x)
      {
@@ -1683,6 +1753,7 @@ _smart_hold_animator(void *data)
             (!elm_widget_drag_child_locked_x_get(sd->widget)))
           {
              ox = sd->down.hold_x;
+             ox = fx;
           }
      }
    if (sd->down.dir_y)
@@ -1691,8 +1762,10 @@ _smart_hold_animator(void *data)
             (!elm_widget_drag_child_locked_y_get(sd->widget)))
           {
              oy = sd->down.hold_y;
+             oy = fy;
           }
      }
+   
    elm_smart_scroller_child_pos_set(sd->smart_obj, ox, oy);
    return ECORE_CALLBACK_RENEW;
 }
@@ -1757,7 +1830,7 @@ _smart_event_mouse_up(void *data, Evas *e, Evas_Object *obj __UNUSED__, void *ev
 #ifdef SCROLLDBG
                        printf("------ %i %i\n", ev->canvas.x, ev->canvas.y);
 #endif
-                       for (i = 0; i < 20; i++)
+                       for (i = 0; i < 60; i++)
                          {
                             dt = t - sd->down.history[i].timestamp;
                             if (dt > 0.2) break;
@@ -1773,7 +1846,7 @@ _smart_event_mouse_up(void *data, Evas *e, Evas_Object *obj __UNUSED__, void *ev
                        ax /= (i + 1);
                        ay /= (i + 1);
                        at /= (i + 1);
-                       at *= 4.0;
+                       at *= 4.0; // magic number! just trial and error shows this makes it behave "nicer" and not run off at high speed all the time
                        dx = ev->canvas.x - ax;
                        dy = ev->canvas.y - ay;
                        if (at > 0)
@@ -2050,9 +2123,10 @@ _smart_event_mouse_move(void *data, Evas *e, Evas_Object *obj __UNUSED__, void *
              printf("::: %i %i\n", ev->cur.canvas.x, ev->cur.canvas.y);
 #endif
              memmove(&(sd->down.history[1]), &(sd->down.history[0]),
-                     sizeof(sd->down.history[0]) * 19);
+                     sizeof(sd->down.history[0]) * (60 - 1));
 #ifdef EVTIME
              sd->down.history[0].timestamp = ev->timestamp / 1000.0;
+             sd->down.history[0].localtimestamp = ecore_loop_time_get();
 #else
              sd->down.history[0].timestamp = ecore_loop_time_get();
 #endif
@@ -2155,7 +2229,6 @@ _smart_event_mouse_move(void *data, Evas *e, Evas_Object *obj __UNUSED__, void *
                                  else x = sd->down.locked_x;
                               }
                          }
-                       if (_elm_config->thumbscroll_border_friction > 0.0)
                          {
                             Evas_Coord minx, miny;
                             sd->pan_func.min_get(sd->pan_obj, &minx, &miny);
@@ -2178,7 +2251,7 @@ _smart_event_mouse_move(void *data, Evas *e, Evas_Object *obj __UNUSED__, void *
                               x += (sd->child.w - sd->w + minx - x) *
                                  _elm_config->thumbscroll_border_friction;
                          }
-
+                       
                        sd->down.hold_x = x;
                        sd->down.hold_y = y;
                        if (!sd->down.hold_animator)