summaryrefslogtreecommitdiff
path: root/src/lib/efl/interfaces/efl_gfx_shape.c
blob: 922f6b92262ac6d24d3eef72e6f9894658710966 (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
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <math.h>
#include <float.h>
#include <ctype.h>

#include <Efl.h>

typedef struct _Efl_Gfx_Shape_Data
{
   Efl_Gfx_Shape_Public public;
   Efl_Gfx_Fill_Rule fill_rule;
} Efl_Gfx_Shape_Data;

static inline double
interpolate(double from, double to, double pos_map)
{
   return (from * (1.0 - pos_map)) + (to * pos_map);
}

static inline int
interpolatei(int from, int to, double pos_map)
{
   return (from * (1.0 - pos_map)) + (to * pos_map);
}

typedef struct _Efl_Gfx_Property Efl_Gfx_Property;
struct _Efl_Gfx_Property
{
   double scale;
   double w;
   double centered;

   Efl_Gfx_Cap c;
   Efl_Gfx_Join j;

   const Efl_Gfx_Dash *dash;
   unsigned int dash_length;

   int r, g, b, a;
   int fr, fg, fb, fa;
};

static inline void
_efl_gfx_property_get(const Eo *obj, Efl_Gfx_Property *property)
{
   property->scale = efl_gfx_shape_stroke_scale_get(obj);
   efl_gfx_shape_stroke_color_get(obj, &property->r, &property->g,
                                  &property->b, &property->a);
   efl_gfx_color_get(obj, &property->fr, &property->fg,
                     &property->fb, &property->fa);
   property->w = efl_gfx_shape_stroke_width_get(obj);
   property->centered = efl_gfx_shape_stroke_location_get(obj);
   efl_gfx_shape_stroke_dash_get(obj, &property->dash, &property->dash_length);
   property->c = efl_gfx_shape_stroke_cap_get(obj);
   property->j = efl_gfx_shape_stroke_join_get(obj);
}

static Eina_Bool
_efl_gfx_shape_interpolate(Eo *obj, Efl_Gfx_Shape_Data *pd,
                           const Eo *from, const Eo *to, double pos_map)
{
   Efl_Gfx_Shape_Data *from_pd, *to_pd;
   Efl_Gfx_Property property_from, property_to;
   Efl_Gfx_Dash *dash = NULL;
   unsigned int i;

   from_pd = efl_data_scope_get(from, EFL_GFX_SHAPE_MIXIN);
   to_pd = efl_data_scope_get(to, EFL_GFX_SHAPE_MIXIN);
   if (!efl_isa(from, EFL_GFX_SHAPE_MIXIN) || !efl_isa(to, EFL_GFX_SHAPE_MIXIN))
     return EINA_FALSE;
   if ((pd == from_pd) || (pd == to_pd)) return EINA_FALSE;

   _efl_gfx_property_get(from, &property_from);
   _efl_gfx_property_get(to, &property_to);

   if (property_from.dash_length != property_to.dash_length) return EINA_FALSE;

   if (property_to.dash_length)
     {
        dash = malloc(sizeof (Efl_Gfx_Dash) * property_to.dash_length);
        if (!dash) return EINA_FALSE;

        for (i = 0; i < property_to.dash_length; i++)
          {
             dash[i].length = interpolate(property_from.dash[i].length,
                                          property_to.dash[i].length, pos_map);
             dash[i].gap = interpolate(property_from.dash[i].gap,
                                       property_to.dash[i].gap, pos_map);
          }
     }

   efl_gfx_shape_stroke_scale_set(obj, interpolate(property_from.scale,
                                                   property_to.scale, pos_map));
   efl_gfx_shape_stroke_color_set(obj,
                                  interpolatei(property_from.r,
                                               property_to.r, pos_map),
                                  interpolatei(property_from.g,
                                               property_to.g, pos_map),
                                  interpolatei(property_from.b,
                                               property_to.b, pos_map),
                                  interpolatei(property_from.a,
                                               property_to.a, pos_map));
   efl_gfx_color_set(obj,
                     interpolatei(property_from.fr, property_to.fr, pos_map),
                     interpolatei(property_from.fg, property_to.fg, pos_map),
                     interpolatei(property_from.fb, property_to.fb, pos_map),
                     interpolatei(property_from.fa, property_to.fa, pos_map));

   efl_gfx_shape_stroke_width_set(obj, interpolate(property_from.w,
                                                   property_to.w, pos_map));
   efl_gfx_shape_stroke_location_set(obj, interpolate(property_from.centered,
                                                      property_to.centered,
                                                      pos_map));
   efl_gfx_shape_stroke_dash_set(obj, dash, property_to.dash_length);
   efl_gfx_shape_stroke_cap_set(obj, (pos_map < 0.5) ?
                                property_from.c : property_to.c);
   efl_gfx_shape_stroke_join_set(obj, (pos_map < 0.5) ?
                                 property_from.j : property_to.j);

   return efl_gfx_path_interpolate(obj, from, to, pos_map);
}

static void
_efl_gfx_shape_stroke_scale_set(Eo *obj EINA_UNUSED, Efl_Gfx_Shape_Data *pd,
                                double s)
{
   pd->public.stroke.scale = s;
}

static double
_efl_gfx_shape_stroke_scale_get(Eo *obj EINA_UNUSED, Efl_Gfx_Shape_Data *pd)
{
   return pd->public.stroke.scale;
}

static void
_efl_gfx_shape_stroke_color_set(Eo *obj EINA_UNUSED, Efl_Gfx_Shape_Data *pd,
                                int r, int g, int b, int a)
{
   pd->public.stroke.color.r = r;
   pd->public.stroke.color.g = g;
   pd->public.stroke.color.b = b;
   pd->public.stroke.color.a = a;
}

static void
_efl_gfx_shape_stroke_color_get(Eo *obj EINA_UNUSED, Efl_Gfx_Shape_Data *pd,
                                int *r, int *g, int *b, int *a)
{
   if (r) *r = pd->public.stroke.color.r;
   if (g) *g = pd->public.stroke.color.g;
   if (b) *b = pd->public.stroke.color.b;
   if (a) *a = pd->public.stroke.color.a;
}

static void
_efl_gfx_shape_stroke_width_set(Eo *obj, Efl_Gfx_Shape_Data *pd, double w)
{
   pd->public.stroke.width = w;
   efl_event_callback_legacy_call(obj, EFL_GFX_PATH_EVENT_CHANGED, NULL);
   efl_event_callback_legacy_call(obj, EFL_GFX_EVENT_CHANGED, NULL);
}

static double
_efl_gfx_shape_stroke_width_get(Eo *obj EINA_UNUSED,
                                Efl_Gfx_Shape_Data *pd)
{
   return pd->public.stroke.width;
}

static void
_efl_gfx_shape_stroke_location_set(Eo *obj EINA_UNUSED, Efl_Gfx_Shape_Data *pd,
                                   double centered)
{
   pd->public.stroke.centered = centered;
}

static double
_efl_gfx_shape_stroke_location_get(Eo *obj EINA_UNUSED, Efl_Gfx_Shape_Data *pd)
{
   return pd->public.stroke.centered;
}

static void
_efl_gfx_shape_stroke_dash_set(Eo *obj EINA_UNUSED, Efl_Gfx_Shape_Data *pd,
                               const Efl_Gfx_Dash *dash, unsigned int length)
{
   Efl_Gfx_Dash *tmp;

   if (!dash)
     {
        free(pd->public.stroke.dash);
        pd->public.stroke.dash = NULL;
        pd->public.stroke.dash_length = 0;
        return;
     }

   tmp = realloc(pd->public.stroke.dash, length * sizeof (Efl_Gfx_Dash));
   if (!tmp && length) return;
   memcpy(tmp, dash, length * sizeof (Efl_Gfx_Dash));

   pd->public.stroke.dash = tmp;
   pd->public.stroke.dash_length = length;
}

static void
_efl_gfx_shape_stroke_dash_get(Eo *obj EINA_UNUSED,
                               Efl_Gfx_Shape_Data *pd,
                               const Efl_Gfx_Dash **dash, unsigned int *length)
{
   if (dash) *dash = pd->public.stroke.dash;
   if (length) *length = pd->public.stroke.dash_length;
}

static void
_efl_gfx_shape_stroke_cap_set(Eo *obj EINA_UNUSED,
                              Efl_Gfx_Shape_Data *pd,
                              Efl_Gfx_Cap c)
{
   pd->public.stroke.cap = c;
}

static Efl_Gfx_Cap
_efl_gfx_shape_stroke_cap_get(Eo *obj EINA_UNUSED,
                              Efl_Gfx_Shape_Data *pd)
{
   return pd->public.stroke.cap;
}

static void
_efl_gfx_shape_stroke_join_set(Eo *obj EINA_UNUSED,
                               Efl_Gfx_Shape_Data *pd,
                               Efl_Gfx_Join j)
{
   pd->public.stroke.join = j;
}

static Efl_Gfx_Join
_efl_gfx_shape_stroke_join_get(Eo *obj EINA_UNUSED,
                               Efl_Gfx_Shape_Data *pd)
{
   return pd->public.stroke.join;
}

static void
_efl_gfx_shape_fill_rule_set(Eo *obj EINA_UNUSED,
                             Efl_Gfx_Shape_Data *pd,
                             Efl_Gfx_Fill_Rule fill_rule)
{
   pd->fill_rule = fill_rule;
}

static Efl_Gfx_Fill_Rule
_efl_gfx_shape_fill_rule_get(Eo *obj EINA_UNUSED,
                             Efl_Gfx_Shape_Data *pd)
{
   return pd->fill_rule;
}

static void
_efl_gfx_shape_dup(Eo *obj, Efl_Gfx_Shape_Data *pd,
                                const Eo *dup_from)
{
   Efl_Gfx_Shape_Data *from;

   if (obj == dup_from) return;

   from = efl_data_scope_get(dup_from, EFL_GFX_SHAPE_MIXIN);
   if (!from) return;

   pd->public.stroke.scale = from->public.stroke.scale;
   pd->public.stroke.width = from->public.stroke.width;
   pd->public.stroke.centered = from->public.stroke.centered;
   pd->public.stroke.cap = from->public.stroke.cap;
   pd->public.stroke.join = from->public.stroke.join;
   pd->public.stroke.color.r = from->public.stroke.color.r;
   pd->public.stroke.color.g = from->public.stroke.color.g;
   pd->public.stroke.color.b = from->public.stroke.color.b;
   pd->public.stroke.color.a = from->public.stroke.color.a;
   pd->fill_rule = from->fill_rule;

   _efl_gfx_shape_stroke_dash_set(obj, pd, from->public.stroke.dash,
                                  from->public.stroke.dash_length);

   efl_gfx_path_dup(obj, dup_from);
}

#include "interfaces/efl_gfx_shape.eo.c"