bfa63ae01d222c4754ae32fb2df77a704c11dc85
[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 Eina_Bool
238 set_time_range(Evas_Object *item, int idx, double p1, double p2, double p3, double p4)
239 {
240         retv_if(!item, EINA_FALSE);
241         retv_if(idx < 0 || idx >= 6, EINA_FALSE); /* FIXME */
242
243         if (0.0f != p1)
244                 p1 /= 24.0f;
245
246         if (0.0f != p2)
247                 p2 /= 24.0f;
248
249         if (0.0f != p3)
250                 p3 /= 24.0f;
251         if (0.0f != p4)
252                 p4 /= 24.0f;
253
254         evas_object_event_callback_del(item, EVAS_CALLBACK_MOVE, item_geom_cb);
255         evas_object_event_callback_del(item, EVAS_CALLBACK_RESIZE, item_geom_cb);
256         evas_object_event_callback_del(item, EVAS_CALLBACK_DEL, item_del_cb);
257         evas_object_event_callback_add(item, EVAS_CALLBACK_MOVE, item_geom_cb, NULL);
258         evas_object_event_callback_add(item, EVAS_CALLBACK_RESIZE, item_geom_cb, NULL);
259         evas_object_event_callback_add(item, EVAS_CALLBACK_DEL, item_del_cb, NULL);
260
261         Evas_Object *rect;
262         rect = evas_object_data_get(item, "part1");
263         evas_object_del(rect);
264         rect = evas_object_data_get(item, "part2");
265         evas_object_del(rect);
266
267         Evas_Coord x, y, w, h, x2, y2, w2, h2;
268
269         double xx;
270         part_info_s *pi;
271
272         //Part 1
273
274         rect = evas_object_rectangle_add(evas_object_evas_get(item));
275         evas_object_smart_member_add(rect, item);
276         switch (idx)
277         {
278             case 0:
279                 evas_object_color_set(rect, 239, 81, 39, 255);
280                 break;
281             case 1:
282                 evas_object_color_set(rect, 63, 170, 150, 255);
283         break;
284             case 2:
285                 evas_object_color_set(rect, 34, 138, 255, 255);
286         break;
287             case 3:
288                 evas_object_color_set(rect, 255, 47, 213, 255);
289         break;
290             case 4:
291                 evas_object_color_set(rect, 248, 176, 67, 255);
292         break;
293                 case 5:
294                 evas_object_color_set(rect, 255, 200, 0, 255);
295                 break;
296         }
297         evas_object_geometry_get(item, &x, &y, &w, &h);
298         edje_object_part_geometry_get(elm_layout_edje_get(item), "result_bar", &x2, &y2, &w2, &h2);
299
300         xx = x + x2 + (w2 * p1);
301         evas_object_show(rect);
302
303         evas_object_data_set(item, "part1", rect);
304
305         pi = calloc(1, sizeof(part_info_s));
306         if (pi)
307         {
308                 pi->p1 = p1;
309                 pi->p2 = p2;
310                 evas_object_data_set(rect, "part_info", pi);
311         }
312
313         //Part 2
314         if ((p3 == 0 && p4 == 0)) return EINA_TRUE;
315
316         rect = evas_object_rectangle_add(evas_object_evas_get(item));
317         evas_object_smart_member_add(rect, item);
318
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), "result_bar", &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 }