db8fac510d3e9f157159b69aa79363afa43540d8
[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
8 #define CITY_MAX 5
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[CITY_MAX] = {{{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 static void
23 map_geom_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
24 {
25         Evas_Coord x, y, w, h, x2, y2, w2, h2;
26         evas_object_geometry_get(obj, &x, &y, &w, &h);
27         edje_object_part_geometry_get(elm_layout_edje_get(obj), "map", &x2, &y2, &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; CITY_MAX > idx; idx++)
33      {
34         if (!city[idx].obj) continue;
35         evas_object_move(city[idx].obj,
36                          (x + city[idx].ci.x) - (city[idx].w / 2),
37                          (y + city[idx].ci.y) - (city[idx].h / 2));
38         evas_object_move(city[idx].obj,
39                          (int)((double)city[idx].ci.x * scale_w) + x + x2 - (city[idx].w / 2),
40                          (int)((double)city[idx].ci.y * scale_h) + y + y2 - (city[idx].h / 2));
41      }
42 }
43
44 static void
45 map_del_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
46 {
47    int idx;
48    for (idx = 0; CITY_MAX > idx; idx++)
49      {
50         evas_object_del(city[idx].obj);
51         city[idx].obj = NULL;
52      }
53 }
54
55 Evas_Object *
56 create_momentic_layout(Evas_Object *parent, const char *full_path)
57 {
58    //Main Layout
59    main_layout = elm_layout_add(parent);
60    elm_layout_file_set(main_layout, full_path, "main");
61    evas_object_size_hint_weight_set(main_layout, EVAS_HINT_EXPAND,
62                                     EVAS_HINT_EXPAND);
63
64    //Map
65    map_layout = elm_layout_add(main_layout);
66    elm_layout_file_set(map_layout, full_path, "map");
67    evas_object_event_callback_add(map_layout, EVAS_CALLBACK_RESIZE, map_geom_cb, NULL);
68    evas_object_event_callback_add(map_layout, EVAS_CALLBACK_MOVE, map_geom_cb, NULL);
69    evas_object_event_callback_add(map_layout, EVAS_CALLBACK_DEL, map_del_cb, NULL);
70    elm_object_part_content_set(main_layout, "map", map_layout);
71
72    return main_layout;
73 }
74
75 Eina_Bool
76 add_city_to_map(int idx, city_s *ci)
77 {
78         char *path = NULL;
79         char full_path[PATH_LEN] = {0, };
80
81    if (idx >= CITY_MAX) return EINA_FALSE;
82    if (0 > idx) return EINA_FALSE;
83    if (city[idx].obj) return EINA_FALSE;
84
85    int i;
86    for (i = 0; i < CITY_MAX; i++)
87    {
88            if (city[i].ci.id == ci->id) return EINA_FALSE;
89    }
90
91    path = app_get_resource_path();
92    retv_if(!path, EINA_FALSE);
93
94    snprintf(full_path, sizeof(full_path), "%s/edje/city.edj", path);
95    free(path);
96
97    Evas_Coord x, y, w, h, x2, y2, w2, h2;
98    evas_object_geometry_get(map_layout, &x, &y, &w, &h);
99    edje_object_part_geometry_get(elm_layout_edje_get(map_layout), "map", &x2, &y2, &w, &h);
100    double scale_w = (double) w / ORIGIN_W;
101    double scale_h = (double) h / ORIGIN_H;
102
103    char buf[256];
104    snprintf(buf, sizeof(buf), "city%d", idx);
105    city[idx].obj = elm_layout_add(map_layout);
106    elm_layout_file_set(city[idx].obj, full_path, buf);
107    evas_object_smart_member_add(city[idx].obj, map_layout);
108    evas_object_show(city[idx].obj);
109
110    Evas_Object *edje = elm_layout_edje_get(city[idx].obj);
111    city[idx].w = atoi(edje_object_data_get(edje, "width"));
112    city[idx].h = atoi(edje_object_data_get(edje, "height"));
113    memcpy(&city[idx].ci, ci, sizeof(city_s));
114
115    evas_object_resize(city[idx].obj, city[idx].w, city[idx].h);
116
117    evas_object_move(city[idx].obj,
118                     (int)((double)ci->x * scale_w) + x + x2 - (city[idx].w / 2),
119                     (int)((double)ci->y * scale_h) + y + y2 - (city[idx].h / 2));
120
121    Elm_Transit *trans;
122
123    //Effect 1
124    trans = elm_transit_add();
125    elm_transit_object_add(trans, city[idx].obj);
126    elm_transit_effect_zoom_add(trans, 3.0, 1.0);
127    elm_transit_effect_color_add(trans, 0, 0, 0, 0, 255, 255, 255, 255);
128    elm_transit_tween_mode_set(trans, ELM_TRANSIT_TWEEN_MODE_DECELERATE);
129    elm_transit_duration_set(trans, 0.35);
130    elm_transit_go(trans);
131
132    //Effect 2 
133    Evas_Object *effect =
134       (Evas_Object *) edje_object_part_object_get(edje, "effect");
135    evas_object_render_op_set(effect, EVAS_RENDER_ADD);
136    trans = elm_transit_add();
137    elm_transit_object_add(trans, effect);
138    elm_transit_effect_color_add(trans, 0, 0, 0, 0, 100, 100, 100, 100);
139    elm_transit_duration_set(trans, 1.25);
140    elm_transit_repeat_times_set(trans, -1);
141    elm_transit_auto_reverse_set(trans, EINA_TRUE);
142    elm_transit_go(trans);
143
144    return EINA_TRUE;
145 }
146
147 static void
148 trans_del_cb(void *data, Elm_Transit *trans)
149 {
150    int idx = (int) data;
151    evas_object_del(city[idx].obj);
152    city[idx].obj = NULL;
153 }
154
155 Eina_Bool
156 remove_city_from_map(int idx)
157 {
158    if (idx >= CITY_MAX) return EINA_FALSE;
159    if (0 > idx) return EINA_FALSE;
160    if (!city[idx].obj) return EINA_FALSE;
161
162    city[idx].ci.id = -1;
163
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