Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 246 lines (197 sloc) 5.767 kb
fcb26ae @billiob add eenvaders_evas_smart.{c,h}
authored
1 #include <stdlib.h>
2
3 #include "eenvaders_evas_smart.h"
4
5 #define BG 0xff002b36
6 #define FG 0xff839496
7
8 #define MIN(a,b) (((a)<(b))?(a):(b))
9
10 /* Prototypes -{{{-*/
11 typedef struct Eenvaders_Object {
16c3bee @billiob use early work on smart object
authored
12 Eina_List *datas;
fcb26ae @billiob add eenvaders_evas_smart.{c,h}
authored
13 } Eenvaders_Object;
14
15 static Evas_Smart *_eenvaders_object_smart_get(void);
16 static Evas_Object *eenvaders_object_new(Evas *evas);
17 static void _eenvaders_object_add(Evas_Object *o);
18 static void _eenvaders_object_del(Evas_Object *o);
19 static void _eenvaders_object_move(Evas_Object *o, Evas_Coord x, Evas_Coord y);
20 static void _eenvaders_object_resize(Evas_Object *o, Evas_Coord w, Evas_Coord h);
21 static void _eenvaders_object_show(Evas_Object *o);
22 static void _eenvaders_object_hide(Evas_Object *o);
23 static void _eenvaders_object_color_set(Evas_Object *o, int r, int g, int b, int a);
24 static void _eenvaders_object_clip_set(Evas_Object *o, Evas_Object *clip);
25 static void _eenvaders_object_clip_unset(Evas_Object *o);
26
27 /* }}} */
28 /* Globals -{{{-*/
29
30 static struct {
31 Evas_Smart_Class klass;
32 } eenvaders_evas_smart_g = {
33 .klass = {
34 .name = "eenvaders_object",
35 .version = EVAS_SMART_CLASS_VERSION,
36 .add = _eenvaders_object_add,
37 .del = _eenvaders_object_del,
38 .move = _eenvaders_object_move,
39 .resize = _eenvaders_object_resize,
16c3bee @billiob use early work on smart object
authored
40 .show = NULL,
41 .hide = NULL,
42 .color_set = NULL,
43 .clip_set = NULL,
44 .clip_unset = NULL,
fcb26ae @billiob add eenvaders_evas_smart.{c,h}
authored
45 .calculate = NULL,
46 .member_add = NULL,
47 .member_del = NULL,
48 .parent = NULL,
49 .callbacks = NULL,
50 .interfaces = NULL,
51 .data = NULL,
16c3bee @billiob use early work on smart object
authored
52 },
fcb26ae @billiob add eenvaders_evas_smart.{c,h}
authored
53 #define _G eenvaders_evas_smart_g
54 };
55
56 /* }}} */
57
58 Evas_Object *
16c3bee @billiob use early work on smart object
authored
59 eenvaders_smart_new(Evas *e)
fcb26ae @billiob add eenvaders_evas_smart.{c,h}
authored
60 {
61 Evas_Object *result = NULL;
62
63 if ((result = eenvaders_object_new(e))) {
64 Eenvaders_Object *data = NULL;
65
66 if ((data = evas_object_smart_data_get(result)))
67 return result;
68 else
69 evas_object_del(result);
70 }
71 return NULL;
72 }
73
74 static Evas_Object *
75 eenvaders_object_new(Evas *evas)
76 {
77 Evas_Object *eenvaders_object;
78
79 eenvaders_object = evas_object_smart_add(evas,
80 _eenvaders_object_smart_get());
81 return eenvaders_object;
82 }
83
84 static Evas_Smart *
85 _eenvaders_object_smart_get(void)
86 {
87 static Evas_Smart *smart = NULL;
88
89 if (smart)
90 return smart;
91
92 smart = evas_smart_class_new(&_G.klass);
93 return smart;
94 }
16c3bee @billiob use early work on smart object
authored
95
fcb26ae @billiob add eenvaders_evas_smart.{c,h}
authored
96 static void
97 _eenvaders_object_add(Evas_Object *o)
98 {
99 Eenvaders_Object *data = NULL;
100 Evas *evas = NULL;
101
102 evas = evas_object_evas_get(o);
16c3bee @billiob use early work on smart object
authored
103 data = (Eenvaders_Object *)calloc(1, sizeof(Eenvaders_Object));
104
fcb26ae @billiob add eenvaders_evas_smart.{c,h}
authored
105 evas_object_smart_data_set(o, data);
106 }
107
108 static void
109 _eenvaders_object_del(Evas_Object *o)
110 {
111 Eenvaders_Object *data;
112
113 if ((data = evas_object_smart_data_get(o))) {
16c3bee @billiob use early work on smart object
authored
114 void *mem;
115
116 EINA_LIST_FREE(data->datas, mem)
117 free(mem);
fcb26ae @billiob add eenvaders_evas_smart.{c,h}
authored
118 free(data);
119 }
120 }
121
122 static void
123 _eenvaders_object_move(Evas_Object *o, Evas_Coord x, Evas_Coord y)
124 {
125 Eenvaders_Object *data;
126
127 if ((data = evas_object_smart_data_get(o))) {
16c3bee @billiob use early work on smart object
authored
128 /* TODO */
fcb26ae @billiob add eenvaders_evas_smart.{c,h}
authored
129 }
130 }
131
16c3bee @billiob use early work on smart object
authored
132 static Evas_Object*
133 new_eenvader(Evas *evas, Eenvaders_Object *eo)
fcb26ae @billiob add eenvaders_evas_smart.{c,h}
authored
134 {
16c3bee @billiob use early work on smart object
authored
135 Evas_Object *o = NULL;
136 uint16_t u = lrand48();
137 int *mem = malloc(7 * 7 * sizeof(int));
fcb26ae @billiob add eenvaders_evas_smart.{c,h}
authored
138
16c3bee @billiob use early work on smart object
authored
139 if (!mem) {
140 perror(NULL);
141 exit(1);
142 }
143 for (int i = 0; i < 7 * 7; i++) {
144 mem[i] = BG;
fcb26ae @billiob add eenvaders_evas_smart.{c,h}
authored
145 }
146
16c3bee @billiob use early work on smart object
authored
147 for (int i = 0; i < 15; i++) {
148 if (u & (1 << i)) {
149 mem[7 + 7*(i/3) + 1 + i%3] = FG;
150 mem[7 + 7*(i/3) + 5 - i%3] = FG;
151 }
152 }
fcb26ae @billiob add eenvaders_evas_smart.{c,h}
authored
153
16c3bee @billiob use early work on smart object
authored
154 o = evas_object_image_filled_add(evas);
155 evas_object_image_fill_set(o, 0, 0, 7, 7);
156 evas_object_image_smooth_scale_set(o, EINA_FALSE);
157 evas_object_image_size_set (o, 7, 7);
158 evas_object_image_data_set(o, (void *) mem);
fcb26ae @billiob add eenvaders_evas_smart.{c,h}
authored
159
16c3bee @billiob use early work on smart object
authored
160 eo->datas = eina_list_append(eo->datas, mem);
fcb26ae @billiob add eenvaders_evas_smart.{c,h}
authored
161
16c3bee @billiob use early work on smart object
authored
162 return o;
fcb26ae @billiob add eenvaders_evas_smart.{c,h}
authored
163 }
164
16c3bee @billiob use early work on smart object
authored
165 static int
166 square_ceil_7(int n)
fcb26ae @billiob add eenvaders_evas_smart.{c,h}
authored
167 {
16c3bee @billiob use early work on smart object
authored
168 /* XXX: considering n >= 7 */
169 int r = 1;
170
171 n /= 7;
172
173 while (n >>= 1) {
174 r <<= 1;
175 }
176 return r * 7;
fcb26ae @billiob add eenvaders_evas_smart.{c,h}
authored
177 }
178
16c3bee @billiob use early work on smart object
authored
179 void
180 draw_eenvaders(Evas_Object *smart_obj, Eenvaders_Object *eo,
181 int x, int y, int w, int h)
fcb26ae @billiob add eenvaders_evas_smart.{c,h}
authored
182 {
16c3bee @billiob use early work on smart object
authored
183 Evas_Object *o;
184 int d;
185
186 if (w < 7 || h < 7)
187 return;
188
189 d = square_ceil_7(MIN(w,h));
190
191 o = new_eenvader(evas_object_evas_get(smart_obj), eo);
192 evas_object_resize(o, d, d);
193 evas_object_smart_member_add(o, smart_obj);
194
195 switch(lrand48() & 3) {
196 case 0:
197 /* top-left */
198 evas_object_move(o, x, y);
199 evas_object_show(o);
200
201 draw_eenvaders(smart_obj, eo, x+d, y, w-d, h); /* right */
202 draw_eenvaders(smart_obj, eo, x, y+d, d, h-d); /* bottom */
203 break;
204 case 1:
205 /* top-right */
206 evas_object_move(o, x+w-d, y);
207 evas_object_show(o);
208
209 draw_eenvaders(smart_obj, eo, x, y+d, w, h-d); /* bottom */
210 draw_eenvaders(smart_obj, eo, x, y, w-d, d); /* left */
211 break;
212 case 2:
213 /* bottom-right */
214 evas_object_move(o, x+w-d, y+h-d);
215 evas_object_show(o);
216
217 draw_eenvaders(smart_obj, eo, x, y, w-d, h); /* left */
218 draw_eenvaders(smart_obj, eo, x+w-d, y, d, h-d); /* top */
219 break;
220 case 3:
221 /* bottom-left */
222 evas_object_move(o, x, y+h-d);
223 evas_object_show(o);
224
225 draw_eenvaders(smart_obj, eo, x, y, w, h-d); /* top */
226 draw_eenvaders(smart_obj, eo, x+d, y+h-d, w-d, d); /* right */
227 break;
228 }
fcb26ae @billiob add eenvaders_evas_smart.{c,h}
authored
229 }
230
231 static void
16c3bee @billiob use early work on smart object
authored
232 _eenvaders_object_resize(Evas_Object *o, Evas_Coord w, Evas_Coord h)
fcb26ae @billiob add eenvaders_evas_smart.{c,h}
authored
233 {
234 Eenvaders_Object *data;
235
16c3bee @billiob use early work on smart object
authored
236 if ((data = evas_object_smart_data_get(o))) {
237 Evas_Coord x, y;
238
239 evas_object_geometry_get(o, &x, &y, NULL, NULL);
240 draw_eenvaders(o, data, x, y, w, h);
241 }
fcb26ae @billiob add eenvaders_evas_smart.{c,h}
authored
242 }
243
244
245
Something went wrong with that request. Please try again.