Imported Upstream version 1.7.1
[platform/upstream/edje.git] / src / examples / edje-perspective.c
1 /**
2  * Simple Edje example illustrating drag functions.
3  *
4  * You'll need at least one Evas engine built for it (excluding the
5  * buffer one). See stdout/stderr for output.
6  *
7  * @verbatim
8  * edje_cc drag.edc && gcc -o drag-box drag-box.c `pkg-config --libs --cflags evas ecore ecore-evas edje`
9  * @endverbatim
10  */
11
12 #ifdef HAVE_CONFIG_H
13 # include "config.h"
14 #else
15 # define __UNUSED__
16 #endif
17
18 #include <Ecore.h>
19 #include <Ecore_Evas.h>
20 #include <Edje.h>
21
22 #define WIDTH  480
23 #define HEIGHT 320
24
25 static const char commands[] = \
26   "commands are:\n"
27   "\tDown - move part down\n"
28   "\tUp -  move part up\n"
29   "\tLeft - move part left\n"
30   "\tRight - move part right\n"
31   "\tPrior - move part up-left\n"
32   "\tNext - move part down-right\n"
33   "\tInsert - increase focal\n"
34   "\tSuppr - decrease focal\n"
35   "\tEsc - exit\n"
36   "\th - print help\n";
37
38 struct _App {
39     Evas_Object *edje_obj;
40     Evas_Object *bg;
41     Edje_Perspective *ps;
42     Eina_Bool animating;
43     int x, y; // relative position of part in the screen
44     int focal;
45 };
46
47 static void
48 _on_destroy(Ecore_Evas *ee __UNUSED__)
49 {
50    ecore_main_loop_quit();
51 }
52
53 /* here just to keep our example's window size and background image's
54  * size in synchrony */
55 static void
56 _on_canvas_resize(Ecore_Evas *ee)
57 {
58    int w, h;
59    struct _App *app = ecore_evas_data_get(ee, "app");
60
61    ecore_evas_geometry_get(ee, NULL, NULL, &w, &h);
62    evas_object_resize(app->bg, w, h);
63    evas_object_resize(app->edje_obj, w, h);
64 }
65
66 static void
67 _part_move(struct _App *app, int dx, int dy)
68 {
69    char emission[64];
70
71    if (app->animating)
72      return;
73
74    app->x += dx;
75    app->y += dy;
76    if (app->x > 1)
77      app->x = 1;
78    if (app->x < 0)
79      app->x = 0;
80    if (app->y > 1)
81      app->y = 1;
82    if (app->y < 0)
83      app->y = 0;
84
85    snprintf(emission, sizeof(emission), "move,%d,%d", app->x, app->y);
86    edje_object_signal_emit(app->edje_obj, emission, "");
87    app->animating = EINA_TRUE;
88 }
89
90
91 static void
92 _on_bg_key_down(void *data, Evas *e __UNUSED__, Evas_Object *o __UNUSED__, void *event_info)
93 {
94    struct _App *app = data;
95    Evas_Event_Key_Down *ev = event_info;
96
97    if (!strcmp(ev->keyname, "h"))
98      {
99         fprintf(stdout, commands);
100         return;
101      }
102    // just moving the part and text
103    else if (!strcmp(ev->keyname, "Down"))
104      {
105         _part_move(app, 0, 1);
106      }
107    else if (!strcmp(ev->keyname, "Up"))
108      {
109         _part_move(app, 0, -1);
110      }
111    else if (!strcmp(ev->keyname, "Left"))
112      {
113         _part_move(app, -1, 0);
114      }
115    else if (!strcmp(ev->keyname, "Right"))
116      {
117         _part_move(app, 1, 0);
118      }
119    else if (!strcmp(ev->keyname, "Prior"))
120      {
121         _part_move(app, -1, -1);
122      }
123    else if (!strcmp(ev->keyname, "Next"))
124      {
125         _part_move(app, 1, 1);
126      }
127    // adjusting the perspective focal point distance
128    else if (!strcmp(ev->keyname, "KP_Add"))
129      {
130         app->focal += 5;
131         edje_perspective_set(app->ps, 240, 160, 0, app->focal);
132         edje_object_calc_force(app->edje_obj);
133      }
134    else if (!strcmp(ev->keyname, "KP_Subtract"))
135      {
136         app->focal -= 5;
137         if (app->focal < 5)
138           app->focal = 5;
139
140         edje_perspective_set(app->ps, 240, 160, 0, app->focal);
141         edje_object_calc_force(app->edje_obj);
142      }
143    // exiting
144    else if (!strcmp(ev->keyname, "Escape"))
145      ecore_main_loop_quit();
146    else
147      {
148         printf("unhandled key: %s\n", ev->keyname);
149         fprintf(stdout, commands);
150      }
151 }
152
153 static void
154 _animation_end_cb(void *data, Evas_Object *o __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
155 {
156    struct _App *app = data;
157
158    app->animating = EINA_FALSE;
159 }
160
161 int
162 main(int argc __UNUSED__, char *argv[])
163 {
164    char         edje_file_path[PATH_MAX];
165    const char  *edje_file = "perspective.edj";
166    struct _App  app;
167    Ecore_Evas  *ee;
168    Evas        *evas;
169    Eina_Prefix *pfx;
170
171    if (!ecore_evas_init())
172      return EXIT_FAILURE;
173
174    if (!edje_init())
175      goto shutdown_ecore_evas;
176
177    pfx = eina_prefix_new(argv[0], main,
178                          "EDJE_EXAMPLES",
179                          "edje/examples",
180                          edje_file,
181                          PACKAGE_BIN_DIR,
182                          PACKAGE_LIB_DIR,
183                          PACKAGE_DATA_DIR,
184                          PACKAGE_DATA_DIR);
185    if (!pfx)
186      goto shutdown_edje;
187
188    edje_frametime_set(1.0 / 60.0);
189
190    /* this will give you a window with an Evas canvas under the first
191     * engine available */
192    app.animating = EINA_FALSE;
193    app.x = 0;
194    app.y = 0;
195    app.focal = 50;
196
197    ee = ecore_evas_new(NULL, 0, 0, WIDTH, HEIGHT, NULL);
198    if (!ee)
199      goto free_prefix;
200
201    ecore_evas_callback_destroy_set(ee, _on_destroy);
202    ecore_evas_callback_resize_set(ee, _on_canvas_resize);
203    ecore_evas_title_set(ee, "Edje Box Example");
204
205    ecore_evas_data_set(ee, "app", &app);
206
207    evas = ecore_evas_get(ee);
208
209    app.bg = evas_object_rectangle_add(evas);
210    evas_object_color_set(app.bg, 255, 255, 255, 255);
211    evas_object_resize(app.bg, WIDTH, HEIGHT);
212    evas_object_focus_set(app.bg, EINA_TRUE);
213    evas_object_show(app.bg);
214
215    evas_object_event_callback_add(app.bg, EVAS_CALLBACK_KEY_DOWN, _on_bg_key_down, &app);
216
217    app.edje_obj = edje_object_add(evas);
218
219    snprintf(edje_file_path, sizeof(edje_file_path),
220             "%s/examples/%s", eina_prefix_data_get(pfx), edje_file);
221    edje_object_file_set(app.edje_obj, edje_file_path, "example/group");
222    evas_object_move(app.edje_obj, 0, 0);
223    evas_object_resize(app.edje_obj, WIDTH, HEIGHT);
224    evas_object_show(app.edje_obj);
225
226    edje_object_signal_callback_add(app.edje_obj, "animation,end", "", _animation_end_cb, &app);
227
228    app.ps = edje_perspective_new(evas);
229    edje_perspective_set(app.ps, 240, 160, 0, app.focal);
230    edje_perspective_global_set(app.ps, EINA_TRUE);
231
232    fprintf(stdout, commands);
233
234    ecore_evas_show(ee);
235
236    ecore_main_loop_begin();
237
238    eina_prefix_free(pfx);
239    ecore_evas_free(ee);
240    ecore_evas_shutdown();
241    edje_shutdown();
242
243    return EXIT_SUCCESS;
244
245  free_prefix:
246    eina_prefix_free(pfx);
247  shutdown_edje:
248    edje_shutdown();
249  shutdown_ecore_evas:
250    ecore_evas_shutdown();
251
252    return EXIT_FAILURE;
253 }