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