Version : 0813
[apps/native/sample/adventure.git] / src / map.c
1 #include <Elementary.h>
2 #include <app.h>
3
4 #include "map.h"
5 #include "log.h"
6 #include "util.h"
7 #include "conf.h"
8
9 #define WORLD_W 443
10 #define WORLD_H 259
11 #define ORIGIN_W 443
12 #define ORIGIN_H 259
13
14 static Evas_Object *main_layout = NULL;
15 static Evas_Object *map_layout = NULL;
16 static city_data_s city[MAXIMUM_SELECTED_CITY] = {{{0, 0, 0, 0, 0, 0}, NULL},
17                                      {{0, 0, 0, 0, 0, 0}, NULL},
18                                      {{0, 0, 0, 0, 0, 0}, NULL},
19                                      {{0, 0, 0, 0, 0, 0}, NULL},
20                                      {{0, 0, 0, 0, 0, 0}, NULL}};
21
22
23 static void
24 map_geom_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
25 {
26         Evas_Coord x, y, w, h, x2, y2, w2, h2;
27         evas_object_geometry_get(obj, &x, &y, &w, &h);
28         edje_object_part_geometry_get(elm_layout_edje_get(obj), "map", &x2, &y2, &w, &h);
29         double scale_w = (double) w / ORIGIN_W;
30         double scale_h = (double) h / ORIGIN_H;
31
32    int idx;
33    for (idx = 0; MAXIMUM_SELECTED_CITY > idx; idx++)
34      {
35         if (!city[idx].obj) continue;
36         evas_object_move(city[idx].obj,
37                          (x + city[idx].ci.x) - (city[idx].w / 2),
38                          (y + city[idx].ci.y) - (city[idx].h / 2));
39         evas_object_move(city[idx].obj,
40                          (int)((double)city[idx].ci.x * scale_w) + x + x2 - (city[idx].w / 2),
41                          (int)((double)city[idx].ci.y * scale_h) + y + y2 - (city[idx].h / 2));
42      }
43 }
44
45 static void
46 map_del_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
47 {
48    int idx;
49    for (idx = 0; MAXIMUM_SELECTED_CITY > idx; idx++)
50      {
51         evas_object_del(city[idx].obj);
52         city[idx].obj = NULL;
53      }
54 }
55
56 Evas_Object *
57 create_momentic_layout(Evas_Object *parent, const char *full_path)
58 {
59    //Main Layout
60    main_layout = elm_layout_add(parent);
61    elm_layout_file_set(main_layout, full_path, "main");
62    evas_object_size_hint_weight_set(main_layout, EVAS_HINT_EXPAND,
63                                     EVAS_HINT_EXPAND);
64
65    //Map
66    map_layout = elm_layout_add(main_layout);
67    elm_layout_file_set(map_layout, full_path, "map");
68    evas_object_event_callback_add(map_layout, EVAS_CALLBACK_RESIZE, map_geom_cb, NULL);
69    evas_object_event_callback_add(map_layout, EVAS_CALLBACK_MOVE, map_geom_cb, NULL);
70    evas_object_event_callback_add(map_layout, EVAS_CALLBACK_DEL, map_del_cb, NULL);
71    elm_object_part_content_set(main_layout, "map", map_layout);
72
73    return main_layout;
74 }
75
76 Eina_Bool
77 add_city_to_map(int idx, city_s *ci)
78 {
79         char *path = NULL;
80         char full_path[PATH_LEN] = {0, };
81
82    if (idx >= MAXIMUM_SELECTED_CITY) return EINA_FALSE;
83    if (0 > idx) return EINA_FALSE;
84    if (city[idx].obj) return EINA_FALSE;
85
86    int i;
87    for (i = 0; i < MAXIMUM_SELECTED_CITY; i++)
88    {
89            if (city[i].ci.id == ci->id) return EINA_FALSE;
90    }
91
92         path = app_get_resource_path();
93         retv_if(!path, EINA_FALSE);
94
95         snprintf(full_path, sizeof(full_path), "%s/edje/city.edj", path);
96         free(path);
97
98    Evas_Coord x, y, w, h, x2, y2, w2, h2;
99    evas_object_geometry_get(map_layout, &x, &y, &w, &h);
100    edje_object_part_geometry_get(elm_layout_edje_get(map_layout), "map", &x2, &y2, &w, &h);
101    double scale_w = (double) w / ORIGIN_W;
102    double scale_h = (double) h / ORIGIN_H;
103
104    char buf[256];
105    snprintf(buf, sizeof(buf), "city%d", idx);
106    city[idx].obj = elm_layout_add(map_layout);
107    elm_layout_file_set(city[idx].obj, full_path, buf);
108    evas_object_smart_member_add(city[idx].obj, map_layout);
109    evas_object_show(city[idx].obj);
110
111    Evas_Object *edje = elm_layout_edje_get(city[idx].obj);
112    city[idx].w = atoi(edje_object_data_get(edje, "width"));
113    city[idx].h = atoi(edje_object_data_get(edje, "height"));
114    memcpy(&city[idx].ci, ci, sizeof(city_s));
115
116    evas_object_resize(city[idx].obj, city[idx].w, city[idx].h);
117
118    evas_object_move(city[idx].obj,
119                     (int)((double)ci->x * scale_w) + x + x2 - (city[idx].w / 2),
120                     (int)((double)ci->y * scale_h) + y + y2 - (city[idx].h / 2));
121
122    Elm_Transit *trans;
123
124    //Effect 1
125    trans = elm_transit_add();
126    elm_transit_object_add(trans, city[idx].obj);
127    elm_transit_effect_zoom_add(trans, 3.0, 1.0);
128    elm_transit_effect_color_add(trans, 0, 0, 0, 0, 255, 255, 255, 255);
129    elm_transit_tween_mode_set(trans, ELM_TRANSIT_TWEEN_MODE_DECELERATE);
130    elm_transit_duration_set(trans, 0.35);
131    elm_transit_go(trans);
132
133    //Effect 2 
134    Evas_Object *effect =
135       (Evas_Object *) edje_object_part_object_get(edje, "effect");
136    evas_object_render_op_set(effect, EVAS_RENDER_ADD);
137    trans = elm_transit_add();
138    elm_transit_object_add(trans, effect);
139    elm_transit_effect_color_add(trans, 0, 0, 0, 0, 100, 100, 100, 100);
140    elm_transit_duration_set(trans, 1.25);
141    elm_transit_repeat_times_set(trans, -1);
142    elm_transit_auto_reverse_set(trans, EINA_TRUE);
143    elm_transit_go(trans);
144
145    return EINA_TRUE;
146 }
147
148 static void
149 trans_del_cb(void *data, Elm_Transit *trans)
150 {
151    int idx = (int) data;
152    evas_object_del(city[idx].obj);
153    city[idx].obj = NULL;
154 }
155
156 Eina_Bool
157 remove_city_from_map(int idx)
158 {
159    if (idx >= MAXIMUM_SELECTED_CITY) return EINA_FALSE;
160    if (0 > idx) return EINA_FALSE;
161    if (!city[idx].obj) return EINA_FALSE;
162
163    city[idx].ci.id = -1;
164    Elm_Transit *trans = elm_transit_add();
165    elm_transit_object_add(trans, city[idx].obj);
166    elm_transit_effect_zoom_add(trans, 1.0, 2.0);
167    elm_transit_effect_color_add(trans, 255, 255, 255, 255, 0, 0, 0, 0);
168    elm_transit_tween_mode_set(trans, ELM_TRANSIT_TWEEN_MODE_DECELERATE);
169    elm_transit_duration_set(trans, 0.35);
170    elm_transit_del_cb_set(trans, trans_del_cb, (void *)idx);
171    elm_transit_go(trans);
172
173    return EINA_TRUE;
174 }
175
176 void
177 button_effect(Evas_Object *btn)
178 {
179    Elm_Transit *trans = elm_transit_add();
180    elm_transit_object_add(trans, btn);
181    elm_transit_effect_zoom_add(trans, 1.0, 0.9);
182    elm_transit_duration_set(trans, 0.4);
183    elm_transit_event_enabled_set(trans, EINA_TRUE);
184    elm_transit_auto_reverse_set(trans, EINA_TRUE);
185    elm_transit_repeat_times_set(trans, -1);
186    elm_transit_go(trans);
187 }
188
189 typedef struct part_info
190 {
191         double p1, p2;
192
193 } part_info_s;
194
195 static void
196 item_geom_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
197 {
198         Evas_Object *rect1 = evas_object_data_get(obj, "part1");
199         Evas_Object *rect2 = evas_object_data_get(obj, "part2");
200
201         ret_if(!rect1);
202
203         Evas_Coord x, y, w, h, x2, y2, w2, h2;
204         double xx;
205         part_info_s *pi;
206
207         evas_object_geometry_get(obj, &x, &y, &w, &h);
208         edje_object_part_geometry_get(elm_layout_edje_get(obj), "graph", &x2, &y2, &w2, &h2);
209
210         pi = evas_object_data_get(rect1, "part_info");
211         if (!pi) return;
212
213         xx = (double)(x + x2) + (((double)w2) * pi->p1);
214         evas_object_move(rect1, (int) xx, (y + y2));
215         evas_object_resize(rect1, (int) (((double)w2) * (pi->p2 - pi->p1)), h2);
216
217     if (!rect2) return;
218
219         pi = evas_object_data_get(rect2, "part_info");
220         ret_if(!pi);
221
222         xx = (double)(x + x2) + (((double)w2) * pi->p1);
223         evas_object_move(rect2, (int) xx, (y + y2));
224         evas_object_resize(rect2, (int) (((double)w2) * (pi->p2 - pi->p1)), h2);
225 }
226
227 static void
228 item_del_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
229 {
230         Evas_Object *rect1 = evas_object_data_get(obj, "part1");
231         Evas_Object *rect2 = evas_object_data_get(obj, "part2");
232         part_info_s *p1 = evas_object_data_get(rect1, "p1");
233         part_info_s *p2 = evas_object_data_get(rect1, "p2");
234         free(p1);
235         free(p2);
236         evas_object_del(rect1);
237         evas_object_del(rect2);
238 }
239
240 Eina_Bool
241 set_time_range(Evas_Object *item, int idx, double p1, double p2, double p3, double p4)
242 {
243         if (!item) return EINA_FALSE;
244         if (idx < 0 || idx >= MAXIMUM_SELECTED_CITY) return EINA_FALSE;
245
246         if (0.0f != p1)
247                 p1 /= 24.0f;
248
249         if (0.0f != p2)
250                 p2 /= 24.0f;
251
252         if (0.0f != p3)
253                 p3 /= 24.0f;
254         if (0.0f != p4)
255                 p4 /= 24.0f;
256
257         evas_object_event_callback_del(item, EVAS_CALLBACK_MOVE, item_geom_cb);
258         evas_object_event_callback_del(item, EVAS_CALLBACK_RESIZE, item_geom_cb);
259         evas_object_event_callback_del(item, EVAS_CALLBACK_DEL, item_del_cb);
260         evas_object_event_callback_add(item, EVAS_CALLBACK_MOVE, item_geom_cb, NULL);
261         evas_object_event_callback_add(item, EVAS_CALLBACK_RESIZE, item_geom_cb, NULL);
262         evas_object_event_callback_add(item, EVAS_CALLBACK_DEL, item_del_cb, NULL);
263
264         Evas_Object *rect;
265         rect = evas_object_data_get(item, "part1");
266         evas_object_del(rect);
267         rect = evas_object_data_get(item, "part2");
268         evas_object_del(rect);
269
270         Evas_Coord x, y, w, h, x2, y2, w2, h2;
271
272         double xx;
273         part_info_s *pi;
274
275         //Part 1
276
277         rect = evas_object_rectangle_add(evas_object_evas_get(item));
278         switch (idx)
279         {
280             case 0:
281                 evas_object_color_set(rect, 239, 81, 39, 255);
282                 break;
283             case 1:
284                 evas_object_color_set(rect, 63, 170, 150, 255);
285         break;
286             case 2:
287                 evas_object_color_set(rect, 34, 138, 255, 255);
288         break;
289             case 3:
290                 evas_object_color_set(rect, 255, 47, 213, 255);
291         break;
292             case 4:
293                 evas_object_color_set(rect, 248, 176, 67, 255);
294         break;
295                 case 5:
296                 evas_object_color_set(rect, 255, 200, 0, 255);
297                 break;
298         }
299         evas_object_geometry_get(item, &x, &y, &w, &h);
300         edje_object_part_geometry_get(elm_layout_edje_get(item), "graph", &x2, &y2, &w2, &h2);
301
302         xx = x + x2 + (w2 * p1);
303         evas_object_show(rect);
304
305         evas_object_data_set(item, "part1", rect);
306
307         pi = calloc(1, sizeof(part_info_s));
308         if (pi)
309         {
310                 pi->p1 = p1;
311                 pi->p2 = p2;
312                 evas_object_data_set(rect, "part_info", pi);
313         }
314
315         //Part 2
316         if ((p3 == 0 && p4 == 0)) return EINA_TRUE;
317
318         rect = evas_object_rectangle_add(evas_object_evas_get(item));
319         switch (idx)
320         {
321                 case 0:
322                         evas_object_color_set(rect, 239, 81, 39, 255);
323                         break;
324             case 1:
325                 evas_object_color_set(rect, 63, 170, 150, 255);
326                 break;
327             case 2:
328                 evas_object_color_set(rect, 34, 138, 255, 255);
329                 break;
330             case 3:
331                 evas_object_color_set(rect, 255, 47, 213, 255);
332                 break;
333             case 4:
334                 evas_object_color_set(rect, 248, 176, 67, 255);
335                 break;
336                 case 5:
337                 evas_object_color_set(rect, 255, 200, 0, 255);
338                 break;
339         }
340         evas_object_geometry_get(item, &x, &y, &w, &h);
341         edje_object_part_geometry_get(elm_layout_edje_get(item), "graph", &x2, &y2, &w2, &h2);
342     xx = x + x2 + (w2 * p1);
343         evas_object_show(rect);
344
345         evas_object_data_set(item, "part2", rect);
346
347         pi = calloc(1, sizeof(part_info_s));
348         if (pi)
349         {
350                 pi->p1 = p3;
351                 pi->p2 = p4;
352                 evas_object_data_set(rect, "part_info", pi);
353         }
354
355         return EINA_TRUE;
356 }
357
358
359 Eina_Bool
360 set_marker(Evas_Object *item, int idx)
361 {
362         if (idx < 0 || idx >= MAXIMUM_SELECTED_CITY) return EINA_FALSE;
363         if (!item) return EINA_FALSE;
364
365         char *path = NULL;
366         char full_path[PATH_LEN] = {0, };
367         path = app_get_resource_path();
368         retv_if(!path, EINA_FALSE);
369
370         snprintf(full_path, sizeof(full_path), "%s/edje/item2.edj", path);
371         free(path);
372
373         Evas_Object *img = elm_image_add(item);
374         retv_if(!img, EINA_FALSE);
375
376         switch (idx)
377         {
378             case 0:
379                 elm_image_file_set(img, full_path, "my_status");
380                 break;
381             case 1:
382                 elm_image_file_set(img, full_path, "location_mint");
383         break;
384             case 2:
385                 elm_image_file_set(img, full_path, "location_blue");
386         break;
387             case 3:
388                 elm_image_file_set(img, full_path, "location_pink");
389         break;
390             case 4:
391                 elm_image_file_set(img, full_path, "location_orange");
392         break;
393         }
394         elm_object_part_content_set(item, "mark", img);
395
396         return EINA_TRUE;
397 }