aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/elementary/efl_ui_active_view_view_manager_scroll.c
blob: fff3d851e29bb62aaa6e17dd69d853a660a967ed (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
#ifdef HAVE_CONFIG_H
# include "elementary_config.h"
#endif

#include <Efl_Ui.h>
#include "elm_priv.h"

typedef struct {
   Efl_Ui_Active_View_Container * container;
   Efl_Gfx_Entity *group, *foreclip, *backclip;
   Eina_Size2D page_size;
   struct {
      Eina_Bool active;
      int to;
      double from, progress;
      double start_time;
      double max_time;
   } transition;
   struct {
      Eina_Bool active;
      int from;
      Eina_Position2D mouse_start;
   } mouse_move;
   Eina_Bool animation;
} Efl_Ui_Active_View_View_Manager_Scroll_Data;

#define MY_CLASS EFL_UI_ACTIVE_VIEW_VIEW_MANAGER_SCROLL_CLASS

static void _page_set_animation(void *data, const Efl_Event *event);

static void
_propagate_progress(Eo *obj, double pos)
{
   efl_event_callback_call(obj, EFL_UI_ACTIVE_VIEW_VIEW_MANAGER_EVENT_POS_UPDATE, &pos);
}

static void
_apply_box_properties(Eo *obj, Efl_Ui_Active_View_View_Manager_Scroll_Data *pd)
{
   Eina_Rect geometry = EINA_RECT_EMPTY();
   Eina_Rect group_pos = efl_gfx_entity_geometry_get(pd->group);
   double current_pos;

   if (pd->transition.active)
     current_pos = pd->transition.from + ((double)pd->transition.to - pd->transition.from)*pd->transition.progress;
   else
     current_pos = efl_ui_active_view_active_index_get(pd->container);

   efl_gfx_entity_geometry_set(pd->foreclip, group_pos);
   //first calculate the size
   geometry.h = pd->page_size.h;
   geometry.w = (pd->page_size.w);

   //calculate the position
   geometry.y = (group_pos.y + group_pos.h/2)-pd->page_size.h/2;

   for (int i = 0; i < efl_content_count(pd->container) ; ++i)
     {
        double diff = i - current_pos;
        Efl_Gfx_Entity *elem = efl_pack_content_get(pd->container, i);

        geometry.x = (group_pos.x + group_pos.w/2)-(pd->page_size.w/2 - diff*pd->page_size.w);
        if (!eina_rectangles_intersect(&geometry.rect, &group_pos.rect))
          {
             efl_canvas_object_clipper_set(elem, pd->backclip);
          }
        else
          {
             efl_gfx_entity_geometry_set(elem, geometry);
             efl_canvas_object_clipper_set(elem, pd->foreclip);
          }
     }

   //Propagate progress
   _propagate_progress(obj, current_pos);
}

static void
_resize_cb(void *data, const Efl_Event *ev EINA_UNUSED)
{
   _apply_box_properties(data, efl_data_scope_get(data, EFL_UI_ACTIVE_VIEW_VIEW_MANAGER_SCROLL_CLASS));
}

static void
_move_cb(void *data, const Efl_Event *ev EINA_UNUSED)
{
   _apply_box_properties(data, efl_data_scope_get(data, EFL_UI_ACTIVE_VIEW_VIEW_MANAGER_SCROLL_CLASS));
}

static void
_mouse_down_cb(void *data,
               const Efl_Event *event)
{
   Efl_Input_Pointer *ev = event->info;
   Eo *obj = data;
   Efl_Ui_Active_View_View_Manager_Scroll_Data *pd = efl_data_scope_get(obj, MY_CLASS);

   if (efl_input_pointer_button_get(ev) != 1) return;
   if (efl_input_event_flags_get(ev) & EFL_INPUT_FLAGS_PROCESSED) return;

   if (efl_content_count(pd->container) == 0) return;

   efl_event_callback_del(pd->container, EFL_CANVAS_OBJECT_EVENT_ANIMATOR_TICK, _page_set_animation, obj);

   pd->mouse_move.active = EINA_TRUE;
   pd->mouse_move.from = efl_ui_active_view_active_index_get(pd->container);
   pd->mouse_move.mouse_start = efl_input_pointer_position_get(ev);
}

static void
_mouse_move_cb(void *data,
               const Efl_Event *event)
{
   Efl_Input_Pointer *ev = event->info;
   Eo *obj = data;
   Efl_Ui_Active_View_View_Manager_Scroll_Data *pd = efl_data_scope_get(obj, MY_CLASS);
   Eina_Position2D pos;
   int pos_y_diff;

   if (efl_input_event_flags_get(ev) & EFL_INPUT_FLAGS_PROCESSED) return;
   if (!pd->mouse_move.active) return;

   if (!efl_input_processed_get(ev))
     efl_input_processed_set(ev, EINA_TRUE);

   pos = efl_input_pointer_position_get(ev);
   pos_y_diff = pd->mouse_move.mouse_start.x - pos.x;

   pd->transition.active = EINA_TRUE;
   pd->transition.from = pd->mouse_move.from;
   pd->transition.progress = (double)pos_y_diff / (double)pd->page_size.w;
   pd->transition.to = pd->transition.from + 1;

   _propagate_progress(data, pd->transition.from + pd->transition.progress);

   _apply_box_properties(obj, pd);
}

static void
_mouse_up_cb(void *data,
             const Efl_Event *event)
{
   Efl_Input_Pointer *ev = event->info;
   Eo *obj = data;
   Efl_Ui_Active_View_View_Manager_Scroll_Data *pd = efl_data_scope_get(obj, MY_CLASS);

   if (efl_input_event_flags_get(ev) & EFL_INPUT_FLAGS_PROCESSED) return;
   if (!pd->mouse_move.active) return;

   double absolut_current_position = (double)pd->transition.from + pd->transition.progress;
   int result = round(absolut_current_position);

   efl_ui_active_view_active_index_set(pd->container, MIN(MAX(result, 0), efl_content_count(pd->container) - 1));
}

EFL_CALLBACKS_ARRAY_DEFINE(mouse_listeners,
  {EFL_EVENT_POINTER_DOWN, _mouse_down_cb},
  {EFL_EVENT_POINTER_UP, _mouse_up_cb},
  {EFL_EVENT_POINTER_MOVE, _mouse_move_cb},
);

EOLIAN static void
_efl_ui_active_view_view_manager_scroll_efl_ui_active_view_view_manager_bind(Eo *obj, Efl_Ui_Active_View_View_Manager_Scroll_Data *pd, Efl_Ui_Active_View_Container *active_view, Efl_Canvas_Group *group)
{
   if (active_view && group)
     {
        pd->container = active_view;
        pd->group = group;
        efl_event_callback_add(pd->group, EFL_GFX_ENTITY_EVENT_SIZE_CHANGED, _resize_cb, obj);
        efl_event_callback_add(pd->group, EFL_GFX_ENTITY_EVENT_POSITION_CHANGED, _move_cb, obj);

        pd->foreclip = efl_add(EFL_CANVAS_RECTANGLE_CLASS,
                               evas_object_evas_get(group));
        evas_object_static_clip_set(pd->foreclip, EINA_TRUE);

        pd->backclip = efl_add(EFL_CANVAS_RECTANGLE_CLASS,
                               evas_object_evas_get(group));
        evas_object_static_clip_set(pd->backclip, EINA_TRUE);
        efl_gfx_entity_visible_set(pd->backclip, EINA_FALSE);

        for (int i = 0; i < efl_content_count(active_view) ; ++i) {
           Efl_Gfx_Entity *elem = efl_pack_content_get(active_view, i);
           efl_canvas_object_clipper_set(elem, pd->backclip);
           efl_canvas_group_member_add(pd->group, elem);
           efl_gfx_entity_visible_set(elem, EINA_TRUE);
        }
        _apply_box_properties(obj, pd);

        efl_event_callback_array_add(efl_content_get(efl_part(pd->container, "efl.event")), mouse_listeners(), obj);
     }

}

EOLIAN static void
_efl_ui_active_view_view_manager_scroll_efl_ui_active_view_view_manager_content_add(Eo *obj EINA_UNUSED, Efl_Ui_Active_View_View_Manager_Scroll_Data *pd, Efl_Gfx_Entity *subobj, int index EINA_UNUSED)
{
   efl_gfx_entity_visible_set(subobj, EINA_TRUE);
   efl_canvas_object_clipper_set(subobj, pd->backclip);
   efl_canvas_group_member_add(pd->group, subobj);

   if (!pd->transition.active)
     _apply_box_properties(obj, pd);
}


EOLIAN static void
_efl_ui_active_view_view_manager_scroll_efl_ui_active_view_view_manager_content_del(Eo *obj EINA_UNUSED, Efl_Ui_Active_View_View_Manager_Scroll_Data *pd, Efl_Gfx_Entity *subobj, int index EINA_UNUSED)
{
   efl_canvas_object_clipper_set(subobj, NULL);
   efl_canvas_group_member_remove(pd->group, subobj);

   if (!pd->transition.active)
     _apply_box_properties(obj, pd);
}

static void
_page_set_animation(void *data, const Efl_Event *event EINA_UNUSED)
{
   Efl_Ui_Active_View_View_Manager_Scroll_Data *pd = efl_data_scope_get(data, MY_CLASS);
   double p = (ecore_loop_time_get() - pd->transition.start_time) / pd->transition.max_time;

   if (p >= 1.0) p = 1.0;
   p = ecore_animator_pos_map(p, ECORE_POS_MAP_ACCELERATE, 0.0, 0.0);

   pd->transition.progress = p;

   if (EINA_DBL_EQ(p, 1.0))
     {
        efl_event_callback_del(pd->container, EFL_CANVAS_OBJECT_EVENT_ANIMATOR_TICK,
                               _page_set_animation, data);
        pd->transition.active = EINA_FALSE;
        pd->transition.progress = 0.0;
     }
   _apply_box_properties(data, pd);
}

static void
_animation_request_switch(Eo *obj, Efl_Ui_Active_View_View_Manager_Scroll_Data *pd, int from, int to)
{
   //if there is already a transition ongoing, which is no mouse transition, but goes to the same position, then do nothing
   if (pd->transition.active && !pd->mouse_move.active && pd->transition.to == to)
    return;

   //In case there is no transition but from and to are the same, ensure that we reset the mouse_move state, and return.
   if (!pd->transition.active && from == to)
     {
        pd->mouse_move.active = EINA_FALSE;
        return;
     }

   efl_event_callback_del(pd->container, EFL_CANVAS_OBJECT_EVENT_ANIMATOR_TICK, _page_set_animation, obj);
   //if there is a ongoing transition, try to guess a better time, and try copy over the position where we are right now
   if (pd->transition.active || pd->mouse_move.active)
     {
        pd->transition.from = MIN(pd->transition.from, pd->transition.to) + pd->transition.progress;
        pd->transition.max_time = MIN(MAX(fabs(pd->transition.progress), 0.2), 0.5f);
        pd->mouse_move.active = EINA_FALSE;
     }
   else
     {
        pd->transition.from = from;
        pd->transition.max_time = 0.5;
        pd->transition.progress = 0.0;
     }

   pd->transition.start_time = ecore_loop_time_get();
   pd->transition.active = EINA_TRUE;
   pd->transition.to = to;
   efl_event_callback_add(pd->container, EFL_CANVAS_OBJECT_EVENT_ANIMATOR_TICK, _page_set_animation, obj);
}

EOLIAN static void
_efl_ui_active_view_view_manager_scroll_efl_ui_active_view_view_manager_switch_to(Eo *obj, Efl_Ui_Active_View_View_Manager_Scroll_Data *pd, int from, int to)
{
   if (pd->animation)
     {
        _animation_request_switch(obj, pd, from, to);
     }
   else
     {
        pd->mouse_move.active = EINA_FALSE;
        pd->transition.active = EINA_FALSE;
        pd->transition.progress = 0.0;
        _apply_box_properties(obj, pd);
     }
}

EOLIAN static void
_efl_ui_active_view_view_manager_scroll_efl_ui_active_view_view_manager_view_size_set(Eo *obj EINA_UNUSED, Efl_Ui_Active_View_View_Manager_Scroll_Data *pd, Eina_Size2D size)
{
   pd->page_size = size;
   if (!pd->transition.active)
     _apply_box_properties(obj, pd);
}

EOLIAN static void
_efl_ui_active_view_view_manager_scroll_efl_ui_active_view_view_manager_animation_enabled_set(Eo *obj EINA_UNUSED, Efl_Ui_Active_View_View_Manager_Scroll_Data *pd, Eina_Bool animation)
{
   pd->animation = animation;
   if (pd->transition.active && !animation)
     {
        efl_event_callback_del(pd->container, EFL_CANVAS_OBJECT_EVENT_ANIMATOR_TICK, _page_set_animation, obj);
        _apply_box_properties(obj, pd);
        pd->transition.active = EINA_FALSE;
     }
}

EOLIAN static Eina_Bool
_efl_ui_active_view_view_manager_scroll_efl_ui_active_view_view_manager_animation_enabled_get(const Eo *obj EINA_UNUSED, Efl_Ui_Active_View_View_Manager_Scroll_Data *pd)
{
   return pd->animation;
}


EOLIAN static void
_efl_ui_active_view_view_manager_scroll_efl_object_destructor(Eo *obj, Efl_Ui_Active_View_View_Manager_Scroll_Data *pd EINA_UNUSED)
{
   efl_event_callback_del(pd->group, EFL_GFX_ENTITY_EVENT_SIZE_CHANGED, _resize_cb, obj);
   efl_event_callback_del(pd->group, EFL_GFX_ENTITY_EVENT_POSITION_CHANGED, _move_cb, obj);
   efl_event_callback_array_del(efl_content_get(efl_part(pd->container, "efl.event")), mouse_listeners(), obj);
   efl_del(pd->backclip);
   efl_del(pd->foreclip);

   for (int i = 0; i < efl_content_count(pd->container); ++i)
     {
        efl_canvas_object_clipper_set(efl_pack_content_get(pd->container, i), NULL);
     }

   efl_destructor(efl_super(obj, MY_CLASS));
}


#include "efl_ui_active_view_view_manager_scroll.eo.c"