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