summaryrefslogtreecommitdiff
path: root/src/examples/evas/efl-canvas-animation.c
blob: 2cc2355956b0a9b9f29cbc0bdd5841ba5c259c58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
 * Example of animation in efl canvas
 *
 * You'll need at least one engine built for it (excluding the buffer
 * one). See stdout/stderr for output.
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <Ecore.h>
#include <Ecore_Evas.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define WIDTH  (640)
#define HEIGHT (480)

struct example_data
{
   Ecore_Evas  *ee;
   Evas        *evas;
   Evas_Object *bg, *scale;
   double current_speed;
};

static struct example_data d;

static Evas_Object * /* new rectangle to be put in the box */
_new_rectangle_add(Evas *e)
{
   Efl_Canvas_Rectangle *rect = efl_add(EFL_CANVAS_RECTANGLE_CLASS, e);

   efl_gfx_entity_size_set(rect, EINA_SIZE2D(10, 10));
   efl_gfx_color_set(rect, 0, 255, 0, 255);
   efl_gfx_entity_visible_set(rect, EINA_TRUE);

   return rect;
}

/* use the following commands to interact with this example - 'h' is
 * the key for help */
static void
_on_keydown(void *data EINA_UNUSED, const Efl_Event *ev)
{
   if (strcmp(efl_input_key_sym_get(ev->info) , "r") == 0)
     {
        Efl_Canvas_Animation *animation = efl_canvas_object_animation_get(d.scale);
        double current_pos = efl_canvas_object_animation_progress_get(d.scale);
        d.current_speed *= -1;
        efl_canvas_object_animation_start(d.scale, animation, d.current_speed, 1.0 - current_pos);
     }
   if (strcmp(efl_input_key_sym_get(ev->info), "p") == 0)
     {
        efl_canvas_object_animation_pause_set(d.scale, !efl_canvas_object_animation_pause_get(d.scale));
     }
}

static void
_on_delete(Ecore_Evas *ee EINA_UNUSED)
{
   ecore_main_loop_quit();
}

static void /* adjust canvas' contents on resizes */
_canvas_resize_cb(Ecore_Evas *ee)
{
   int w, h;

   ecore_evas_geometry_get(ee, NULL, NULL, &w, &h);

   efl_gfx_entity_geometry_set(d.bg, EINA_RECT(0, 0, w, h));
}

static void
print_help(void)
{
   printf("evas-animation example\n Press r to reverse the animation of the red rect.\n Press p to pause the animation of the red rect.\n");
}

int
main(void)
{
   if (!ecore_evas_init())
     return EXIT_FAILURE;

   /* this will give you a window with an Evas canvas under the first
    * engine available */
   d.ee = ecore_evas_new(NULL, 0, 0, WIDTH, HEIGHT, NULL);
   if (!d.ee)
     goto panic;

   print_help();

   ecore_evas_name_class_set(d.ee, "Evas Animation example", "Evas Animation Example");
   ecore_evas_callback_delete_request_set(d.ee, _on_delete);
   ecore_evas_callback_resize_set(d.ee, _canvas_resize_cb);
   ecore_evas_show(d.ee);

   d.evas = ecore_evas_get(d.ee);

   d.bg = _new_rectangle_add(d.evas);
   efl_gfx_color_set(d.bg, 255, 255, 255, 255);
   efl_gfx_entity_visible_set(d.bg, EINA_TRUE);
   efl_canvas_object_key_focus_set(d.bg, EINA_TRUE);
   efl_event_callback_add(d.bg, EFL_EVENT_KEY_DOWN, _on_keydown, NULL);

   _canvas_resize_cb(d.ee);

   Evas_Object *scale_rect = _new_rectangle_add(d.evas);
   efl_gfx_entity_geometry_set(scale_rect, EINA_RECT(50, 50, 50, 50));
   efl_canvas_object_animation_start(scale_rect,
    efl_new(EFL_CANVAS_ANIMATION_SCALE_CLASS,
      efl_animation_scale_set(efl_added, EINA_VECTOR2(1.0, 1.0), EINA_VECTOR2(3.0, 3.0), scale_rect, EINA_VECTOR2(0.5, 0.5)),
      efl_animation_start_delay_set(efl_added, 5.0),
      efl_animation_duration_set(efl_added, 2.0),
      efl_animation_repeat_count_set(efl_added, EFL_ANIMATION_REPEAT_INFINITE)
    ),
   1.0, 0.0);

   Evas_Object *scale_rect2 = _new_rectangle_add(d.evas);
   efl_gfx_entity_geometry_set(scale_rect2, EINA_RECT(50, 200, 50, 50));
   efl_canvas_object_animation_start(scale_rect2,
    efl_new(EFL_CANVAS_ANIMATION_SCALE_CLASS,
      efl_animation_scale_set(efl_added, EINA_VECTOR2(1.0, 1.0), EINA_VECTOR2(3.0, 3.0), scale_rect2, EINA_VECTOR2(0.5, 0.5)),
      efl_animation_duration_set(efl_added, 2.0),
      efl_animation_repeat_count_set(efl_added, EFL_ANIMATION_REPEAT_INFINITE),
      efl_animation_repeat_mode_set(efl_added, EFL_CANVAS_ANIMATION_REPEAT_MODE_REVERSE)
    ),
   1.0, 0.0);

   Evas_Object *scale_rect3 = _new_rectangle_add(d.evas);
   efl_gfx_entity_geometry_set(scale_rect3, EINA_RECT(50, 350, 50, 50));
   efl_canvas_object_animation_start(scale_rect3,
    efl_new(EFL_CANVAS_ANIMATION_SCALE_CLASS,
      efl_animation_scale_set(efl_added, EINA_VECTOR2(1.0, 1.0), EINA_VECTOR2(3.0, 3.0), scale_rect3, EINA_VECTOR2(0.5, 0.5)),
      efl_animation_duration_set(efl_added, 2.0),
      efl_animation_repeat_count_set(efl_added, 3),
      efl_animation_repeat_mode_set(efl_added, EFL_CANVAS_ANIMATION_REPEAT_MODE_REVERSE)
    ),
   1.0, 0.0);

   Evas_Object *scale_rect4 = _new_rectangle_add(d.evas);
   efl_gfx_entity_geometry_set(scale_rect4, EINA_RECT(200, 50, 50, 50));
   efl_canvas_object_animation_start(scale_rect4,
    efl_new(EFL_CANVAS_ANIMATION_SCALE_CLASS,
      efl_animation_scale_set(efl_added, EINA_VECTOR2(1.0, 1.0), EINA_VECTOR2(3.0, 3.0), scale_rect4, EINA_VECTOR2(0.5, 0.5)),
      efl_animation_duration_set(efl_added, 2.0),
      efl_animation_final_state_keep_set(efl_added, EINA_TRUE)
    ),
   1.0, 0.0);


   Evas_Object *scale_rect5 = d.scale = _new_rectangle_add(d.evas);
   efl_gfx_color_set(scale_rect5, 255, 0, 0, 255);
   efl_gfx_entity_geometry_set(scale_rect5, EINA_RECT(200, 200, 50, 50));
   efl_canvas_object_animation_start(scale_rect5,
    efl_new(EFL_CANVAS_ANIMATION_SCALE_CLASS,
      efl_animation_scale_set(efl_added, EINA_VECTOR2(1.0, 1.0), EINA_VECTOR2(5.0, 5.0), scale_rect5, EINA_VECTOR2(0.5, 0.5)),
      efl_animation_duration_set(efl_added, 5.0),
      efl_animation_repeat_count_set(efl_added, EFL_ANIMATION_REPEAT_INFINITE)
    ),
   1.0, 0.0);
   d.current_speed = 1.0;

   ecore_main_loop_begin();
   ecore_evas_shutdown();
   return 0;

panic:
   fprintf(stderr, "error: Requires at least one Evas engine built and linked"
                   " to ecore-evas for this example to run properly.\n");
   return -2;
}