summaryrefslogtreecommitdiff
path: root/src/lib/efx_resize.c
blob: d48990150f5d1116ac0756162050aec1342d6c07 (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
#include "efx_private.h"

typedef enum
{
   NONE,
   TOP_RIGHT,
   BOTTOM_LEFT,
   BOTTOM_RIGHT
} Anchor;

typedef struct Efx_Resize_Data
{
   EFX *e;
   Efx_Effect_Speed speed;
   Ecore_Animator *anim;
   int w, h;
   int start_w, start_h;
   Efx_End_Cb cb;
   void *data;
   Anchor anchor_type;
   Evas_Point anchor;
   Eina_Bool moving : 1;
} Efx_Resize_Data;

static void
_obj_del(void *data, Evas *evas __UNUSED__, Evas_Object *obj EINA_UNUSED, void *event_info __UNUSED__)
{
   Efx_Resize_Data *erd = data;

   if (erd->anim) ecore_animator_del(erd->anim);
   erd->e->resize_data = NULL;
   if ((!erd->e->owner) && (!erd->e->followers)) efx_free(erd->e);
   free(erd);
}

static void
_resize_anchor(Efx_Resize_Data *erd)
{
   int x = 0, y = 0;
   int cx, cy;

   if (!erd->anchor_type) return;

   _efx_resize_adjust(erd->e, &x, &y);
   if ((!x) && (!y)) return;

   if (erd->e->move_data)
     cx = erd->e->x, cy = erd->e->y;
   else
     evas_object_geometry_get(erd->e->obj, &cx, &cy, NULL, NULL);
   x += cx, y += cy;
   evas_object_move(erd->e->obj, x, y);
}

static Eina_Bool
_resize_cb(Efx_Resize_Data *erd, double pos)
{
   double factor;

   if (pos < 1.0)
     {
        int w, h;

        factor = ecore_animator_pos_map(pos, erd->speed, 0, 0);
        w = lround(factor * (erd->w - erd->start_w)) + erd->start_w;
        h = lround(factor * (erd->h - erd->start_h)) + erd->start_h;
        //DBG("%p to (%dx%d)", erd->e->obj, w, h);
        erd->e->w = w, erd->e->h = h;
        evas_object_resize(erd->e->obj, w, h);
        _resize_anchor(erd);
        return EINA_TRUE;
     }
   /* lround will usually be off by 1 at the end, so we manually set this here */
   erd->e->w = erd->w, erd->e->h = erd->h;
   evas_object_resize(erd->e->obj, erd->w, erd->h);
   _resize_anchor(erd);

   erd->anim = NULL;
   EFX_QUEUE_CHECK(erd);
   return EINA_TRUE;
}

static void
_resize_stop(Evas_Object *obj, Eina_Bool reset)
{
   EFX *e;
   Efx_Resize_Data *erd;

   e = evas_object_data_get(obj, "efx-data");
   if ((!e) || (!e->resize_data)) return;
   erd = e->resize_data;
   if (reset)
     {
        erd->e->w = erd->start_w, erd->e->h = erd->start_h;
        evas_object_resize(obj, erd->start_w, erd->start_h);
        evas_object_event_callback_del_full(obj, EVAS_CALLBACK_FREE, (Evas_Object_Event_Cb)_obj_del, erd);
        if (erd->moving)
          {
             erd->moving = 0;
             efx_move_reset(obj);
          }
        else if (efx_queue_complete(erd->e, erd))
          efx_queue_process(erd->e);
        _obj_del(erd, NULL, NULL, NULL);
        INF("reset resized object %p", obj);
     }
   else
     {
        INF("stopped resized object %p", obj);
        if (erd->anim) ecore_animator_del(erd->anim);
        if (erd->moving)
          {
             erd->moving = 0;
             efx_move_stop(obj);
          }
        if (efx_queue_complete(erd->e, erd))
          efx_queue_process(erd->e);
     }
}

void
_efx_resize_adjust(EFX *e, int *ax, int *ay)
{
   Efx_Resize_Data *erd = e->resize_data;
   int x, y, w, h;

   if (!erd) return;
   if (e->move_data)
     x = e->x, y = e->y;
   w = e->w, h = e->h;
   switch (erd->anchor_type)
     {
      case TOP_RIGHT:
        *ax = erd->anchor.x - (x + w);
        *ay = erd->anchor.y - y;
        break;
      case BOTTOM_LEFT:
        *ax = erd->anchor.x - x;
        *ay = erd->anchor.y - (y + h);
        break;
      case BOTTOM_RIGHT:
        *ax = erd->anchor.x - (x + w);
        *ay = erd->anchor.y - (y + h);
        break;
      default: break;
     }
}

EAPI Eina_Bool
efx_resize(Evas_Object *obj, Efx_Effect_Speed speed, const Evas_Point *position, int w, int h, double total_time, Efx_End_Cb cb, const void *data)
{
   EFX *e;
   Efx_Resize_Data *erd;
   int x, y;

   EINA_SAFETY_ON_NULL_RETURN_VAL(obj, EINA_FALSE);
   EINA_SAFETY_ON_TRUE_RETURN_VAL(w < 0, EINA_FALSE);
   EINA_SAFETY_ON_TRUE_RETURN_VAL(h < 0, EINA_FALSE);
   e = evas_object_data_get(obj, "efx-data");
   if (!e) e = efx_new(obj);
   EINA_SAFETY_ON_NULL_RETURN_VAL(e, EINA_FALSE);
   erd = e->resize_data;
   if (!erd)
     {
        e->resize_data = erd = calloc(1, sizeof(Efx_Resize_Data));
        EINA_SAFETY_ON_NULL_RETURN_VAL(erd, EINA_FALSE);
        evas_object_event_callback_add(obj, EVAS_CALLBACK_FREE, (Evas_Object_Event_Cb)_obj_del, e->resize_data);
     }

   erd->e = e;
   erd->anchor_type = NONE;
   erd->w = w;
   erd->h = h;
   erd->cb = cb;
   erd->data = (void*)data;
   evas_object_geometry_get(obj, &x, &y, &erd->start_w, &erd->start_h);
   e->w = erd->start_w;
   e->h = erd->start_h;
   if (e->move_data)
     x = e->x, y = e->y;
   INF("resize: %p || %dx%d => %dx%d %s over %gs", obj, erd->start_w, erd->start_h, w, h, efx_speed_str[speed], total_time);
   if (position && ((position->x != x) || (position->y != y)))
     {
        Evas_Point tr, bl, br;
        Evas_Point atr, abl, abr;

        tr = (Evas_Point){x + erd->start_w, y};
        bl = (Evas_Point){x, y + erd->start_h};
        br = (Evas_Point){x + erd->start_w, y + erd->start_h};
        atr = (Evas_Point){position->x + w, position->y};
        abl = (Evas_Point){position->x, position->y + h};
        abr = (Evas_Point){position->x + w, position->y + h};
        if (!memcmp(&tr, &atr, sizeof(Evas_Point)))
          {
             erd->anchor_type = TOP_RIGHT;
             erd->anchor = tr;
          }
        else if (!memcmp(&bl, &abl, sizeof(Evas_Point)))
          {
             erd->anchor_type = BOTTOM_LEFT;
             erd->anchor = bl;
          }
        else if (!memcmp(&br, &abr, sizeof(Evas_Point)))
          {
             erd->anchor_type = BOTTOM_RIGHT;
             erd->anchor = br;
          }

        if (!efx_move(obj, speed, position, total_time, NULL, NULL))
          {
             evas_object_event_callback_del_full(obj, EVAS_CALLBACK_FREE, (Evas_Object_Event_Cb)_obj_del, e->resize_data);
             free(erd);
             e->resize_data = NULL;
             efx_free(e);
             return EINA_FALSE;
          }
        else
          erd->moving = 1;
     }
   if (total_time)
     erd->anim = ecore_animator_timeline_add(total_time, (Ecore_Timeline_Cb)_resize_cb, erd);
   else
     _resize_cb(erd, 1.0);

   return EINA_TRUE;
}

EAPI void
efx_resize_reset(Evas_Object *obj)
{
   _resize_stop(obj, EINA_TRUE);
}

EAPI void
efx_resize_stop(Evas_Object *obj)
{
   _resize_stop(obj, EINA_FALSE);
}