evas-map-utils.c
#ifdef HAVE_CONFIG_H
#include "config.h"
#else
#define PACKAGE_EXAMPLES_DIR "."
#endif
#include <Ecore.h>
#include <Ecore_Evas.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "evas-common.h"
#define WIDTH 480
#define HEIGHT 480
typedef struct
{
Ecore_Evas *ee;
Evas *canvas;
struct {
int r, g, b, a;
} colors[6];
int colors_index;
int frame;
Eina_Bool alpha : 1;
Eina_Bool smooth : 1;
Eina_Bool backface_culling : 1;
Eina_Bool apply_perspective : 1;
Eina_Bool apply_lighting : 1;
} App_Data;
static const char *img1_path = PACKAGE_EXAMPLES_DIR EVAS_IMAGE_FOLDER "/cube1.png";
static const char *img2_path = PACKAGE_EXAMPLES_DIR EVAS_IMAGE_FOLDER "/enlightenment.png";
static const char *img3_path = PACKAGE_EXAMPLES_DIR EVAS_IMAGE_FOLDER "/im1.png";
static const char *help_string =
"Valid commands:\n"
"\ta - toggle alpha for maps\n"
"\ts - toggle smooth for maps\n"
"\tc - switch map color\n"
"\tb - toggle backface culling\n"
"\tp - toggle perspective\n"
"\tl - toggle lighting\n"
"\th - prints this help\n";
static Eina_Bool
_anim_cb(void *data)
{
App_Data *ad = data;
Evas_Map *m;
Evas_Object *o;
int r, g, b, a;
int win_w, win_h, img_w, img_h;
Evas_Coord x, y, w, h;
evas_output_size_get(ad->canvas, &win_w, &win_h);
m = evas_map_new(4);
evas_map_smooth_set(m, ad->smooth);
evas_map_alpha_set(m, ad->alpha);
r = ad->colors[ad->colors_index].r;
g = ad->colors[ad->colors_index].g;
b = ad->colors[ad->colors_index].b;
a = ad->colors[ad->colors_index].a;
evas_map_util_points_color_set(m, r, g, b, a);
o = evas_object_name_find(ad->canvas, "obj1");
evas_object_geometry_get(o, &x, &y, &w, &h);
evas_map_util_points_populate_from_object(m, o);
evas_map_util_rotate(m, 3 * ad->frame, x + (w / 2), y + (h / 2));
evas_object_map_set(o, m);
evas_object_map_enable_set(o, EINA_TRUE);
o = evas_object_name_find(ad->canvas, "obj2");
evas_object_geometry_get(o, &x, &y, &w, &h);
evas_object_image_size_get(o, &img_w, &img_h);
evas_map_util_points_populate_from_object_full(m, o, 100);
evas_map_point_image_uv_set(m, 0, 0, 0);
evas_map_point_image_uv_set(m, 1, img_w, 0);
evas_map_point_image_uv_set(m, 2, img_w, img_h);
evas_map_point_image_uv_set(m, 3, 0, img_h);
evas_map_util_3d_rotate(m, ad->frame * 6, ad->frame * 6, ad->frame * 6,
x + (w / 3), y + 10, 0);
if (ad->apply_lighting)
evas_map_util_3d_lighting(m, win_w / 2, win_h / 2, -100,
255, 255, 255, 0, 0, 0);
evas_object_map_set(o, m);
evas_object_map_enable_set(o, EINA_TRUE);
o = evas_object_name_find(ad->canvas, "obj3");
evas_object_geometry_get(o, &x, &y, &w, &h);
evas_object_image_size_get(o, &img_w, &img_h);
evas_map_util_points_populate_from_geometry(m, x, y + (h / 2), w, h, -20);
evas_map_point_image_uv_set(m, 0, 0, 0);
evas_map_point_image_uv_set(m, 1, img_w, 0);
evas_map_point_image_uv_set(m, 2, img_w, img_h);
evas_map_point_image_uv_set(m, 3, 0, img_h);
evas_map_util_3d_rotate(m, 20, ad->frame * 6, 0,
x + (w / 2), y + (w / 2), w / 2);
if (ad->apply_perspective)
evas_map_util_3d_perspective(m, x + (w / 2), y + (h / 2), 0, 256);
if (ad->apply_lighting)
{
Evas_Coord mx, my;
evas_pointer_canvas_xy_get(ad->canvas, &mx, &my);
evas_map_util_3d_lighting(m, mx, my, -256,
255, 255, 255, 0, 0, 0);
}
if (ad->backface_culling)
{
if (evas_map_util_clockwise_get(m))
evas_object_show(o);
else
evas_object_hide(o);
}
else
evas_object_show(o);
evas_object_map_set(o, m);
evas_object_map_enable_set(o, EINA_TRUE);
evas_map_free(m);
o = evas_object_name_find(ad->canvas, "obj4");
evas_object_geometry_get(o, &x, &y, &w, &h);
evas_object_image_size_get(o, &img_w, &img_h);
m = evas_map_new(4);
evas_map_point_coord_set(m, 0, x, y + h, 0);
evas_map_point_coord_set(m, 1, x + w, y + h, 0);
evas_map_point_coord_set(m, 2, win_w - 10, win_h - 30, 0);
evas_map_point_coord_set(m, 3, (win_w / 2) + 10, win_h - 30, 0);
evas_map_point_image_uv_set(m, 0, 0, img_h);
evas_map_point_image_uv_set(m, 1, img_w, img_h);
evas_map_point_image_uv_set(m, 2, img_w, 2 * (img_h / 3));
evas_map_point_image_uv_set(m, 3, 0, 2 * (img_h / 3));
evas_map_point_color_set(m, 0, 200, 200, 200, 150);
evas_map_point_color_set(m, 1, 200, 200, 200, 150);
evas_map_point_color_set(m, 2, 0, 0, 0, 0);
evas_map_point_color_set(m, 3, 0, 0, 0, 0);
evas_object_map_set(o, m);
evas_object_map_enable_set(o, EINA_TRUE);
evas_map_free(m);
ad->frame = (ad->frame + 1) % 60;
return EINA_TRUE;
}
static void
_on_keydown(void *data, Evas *e EINA_UNUSED, Evas_Object *o EINA_UNUSED, void *event)
{
App_Data *ad = data;
Evas_Event_Key_Down *ev = event;
switch (ev->key[0])
{
case 'a':
ad->alpha = !ad->alpha;
break;
case 's':
ad->smooth = !ad->smooth;
break;
case 'c':
ad->colors_index = (ad->colors_index + 1) % 6;
break;
case 'b':
ad->backface_culling = !ad->backface_culling;
break;
case 'p':
ad->apply_perspective = !ad->apply_perspective;
break;
case 'l':
ad->apply_lighting = !ad->apply_lighting;
break;
case 'h':
puts(help_string);
break;
default:
break;
}
}
static void
_objs_fit(Evas *e)
{
Evas_Object *o;
int w, h;
evas_output_size_get(e, &w, &h);
w /= 2;
h /= 2;
o = evas_object_name_find(e, "obj1");
evas_object_move(o, w / 4, h / 4);
evas_object_resize(o, w / 2, h / 2);
o = evas_object_name_find(e, "obj2");
evas_object_move(o, 5 * w / 4, h / 4);
evas_object_resize(o, w / 2, h / 2);
o = evas_object_name_find(e, "obj3");
evas_object_move(o, w / 4, 5 * h / 4);
evas_object_resize(o, w / 2, h / 2);
o = evas_object_name_find(e, "obj4_source");
evas_object_move(o, 5 * w / 4, 5 * h / 4);
evas_object_resize(o, w / 2, h / 2);
o = evas_object_name_find(e, "obj4");
evas_object_move(o, 5 * w / 4, 5 * h / 4);
evas_object_resize(o, w / 2, h / 2);
}
static void
_on_resize(void *data EINA_UNUSED, Evas *e, Evas_Object *o EINA_UNUSED, void *event EINA_UNUSED)
{
_objs_fit(e);
}
static void
_on_free(void *data EINA_UNUSED, Evas *e EINA_UNUSED, Evas_Object *o EINA_UNUSED, void *event EINA_UNUSED)
{
ecore_main_loop_quit();
}
int
main(void)
{
Evas_Object *bg, *o, *osrc;
static App_Data d = {
.ee = NULL,
.canvas = NULL,
.colors = {
{ 255, 255, 255, 255 },
{ 128, 128, 0, 128 },
{ 255, 0, 0, 255 },
{ 64, 128, 255, 255 },
{ 11, 23, 58, 132 },
{ 0, 0, 0, 255 }
},
.colors_index = 0,
.frame = 0,
.alpha = EINA_FALSE,
.smooth = EINA_FALSE,
.backface_culling = EINA_FALSE,
.apply_perspective = EINA_TRUE,
.apply_lighting = EINA_TRUE
};
if (!ecore_evas_init())
return EXIT_FAILURE;
d.ee = ecore_evas_new(NULL, 0, 0, WIDTH, HEIGHT, NULL);
if (!d.ee)
goto error;
d.canvas = ecore_evas_get(d.ee);
bg = evas_object_image_filled_add(d.canvas);
evas_object_image_file_set(bg, img1_path, NULL);
ecore_evas_object_associate(d.ee, bg, 0);
evas_object_focus_set(bg, EINA_TRUE);
evas_object_move(bg, 0, 0);
evas_object_resize(bg, WIDTH, HEIGHT);
evas_object_show(bg);
o = evas_object_rectangle_add(d.canvas);
evas_object_name_set(o, "obj1");
evas_object_color_set(o, 128, 0, 200, 200);
evas_object_show(o);
o = evas_object_image_filled_add(d.canvas);
evas_object_name_set(o, "obj2");
evas_object_image_file_set(o, img2_path, NULL);
evas_object_show(o);
o = evas_object_image_filled_add(d.canvas);
evas_object_name_set(o, "obj3");
evas_object_image_file_set(o, img2_path, NULL);
evas_object_show(o);
osrc = evas_object_image_filled_add(d.canvas);
evas_object_image_file_set(osrc, img3_path, NULL);
evas_object_name_set(osrc, "obj4_source");
evas_object_show(osrc);
o = evas_object_image_filled_add(d.canvas);
evas_object_image_source_set(o, osrc);
evas_object_name_set(o, "obj4");
evas_object_show(o);
_objs_fit(d.canvas);
evas_object_event_callback_add(bg, EVAS_CALLBACK_KEY_DOWN, _on_keydown, &d);
evas_object_event_callback_add(bg, EVAS_CALLBACK_RESIZE, _on_resize, NULL);
evas_object_event_callback_add(bg, EVAS_CALLBACK_FREE, _on_free, NULL);
ecore_animator_add(_anim_cb, &d);
ecore_main_loop_begin();
ecore_evas_shutdown();
return 0;
error:
fprintf(stderr, "error: Requires at least one Evas engine built and linked"
" to ecore-evas for this example to run properly.\n");
ecore_evas_shutdown();
return -1;
}
Ecore_Evas.h
Evas wrapper functions.
Evas_Coord
int Evas_Coord
Type used for coordinates (in pixels, int).
Definition Evas_Common.h:116
EVAS_CALLBACK_KEY_DOWN
@ EVAS_CALLBACK_KEY_DOWN
Key Press Event.
Definition Evas_Common.h:430
EVAS_CALLBACK_RESIZE
@ EVAS_CALLBACK_RESIZE
Resize Event.
Definition Evas_Common.h:437
EVAS_CALLBACK_FREE
@ EVAS_CALLBACK_FREE
Object Being Freed (Called after Del).
Definition Evas_Common.h:429
ecore_animator_add
Ecore_Animator * ecore_animator_add(Ecore_Task_Cb func, const void *data)
Adds an animator to call func at every animation tick during main loop execution.
Definition ecore_anim.c:537
ecore_evas_init
EAPI int ecore_evas_init(void)
Inits the Ecore_Evas system.
Definition ecore_evas.c:608
ecore_evas_get
EAPI Evas * ecore_evas_get(const Ecore_Evas *ee)
Gets an Ecore_Evas's Evas.
Definition ecore_evas.c:1314
ecore_evas_new
EAPI Ecore_Evas * ecore_evas_new(const char *engine_name, int x, int y, int w, int h, const char *extra_options)
Creates a new Ecore_Evas based on engine name and common parameters.
Definition ecore_evas.c:1053
ecore_evas_shutdown
EAPI int ecore_evas_shutdown(void)
Shuts down the Ecore_Evas system.
Definition ecore_evas.c:672
ecore_evas_object_associate
EAPI Eina_Bool ecore_evas_object_associate(Ecore_Evas *ee, Evas_Object *obj, Ecore_Evas_Object_Associate_Flags flags)
Associates the given object to this ecore evas.
Definition ecore_evas_util.c:223
ecore_main_loop_quit
void ecore_main_loop_quit(void)
Quits the main loop once all the events currently on the queue have been processed.
Definition ecore_main.c:1321
ecore_main_loop_begin
void ecore_main_loop_begin(void)
Runs the application main loop.
Definition ecore_main.c:1311
EINA_TRUE
#define EINA_TRUE
boolean value TRUE (numerical value 1)
Definition eina_types.h:539
EINA_FALSE
#define EINA_FALSE
boolean value FALSE (numerical value 0)
Definition eina_types.h:533
Eina_Bool
unsigned char Eina_Bool
Type to mimic a boolean.
Definition eina_types.h:527
EINA_UNUSED
#define EINA_UNUSED
Used to indicate that a function parameter is purposely unused.
Definition eina_types.h:339
Evas_Event_Key_Down
struct _Evas_Event_Key_Down Evas_Event_Key_Down
Event structure for EVAS_CALLBACK_KEY_DOWN event callbacks.
Definition Evas_Legacy.h:154
Evas
Eo Evas
An opaque handle to an Evas canvas.
Definition Evas_Common.h:163
evas_pointer_canvas_xy_get
EVAS_API void evas_pointer_canvas_xy_get(const Evas_Canvas *obj, int *x, int *y)
This function returns the current known default pointer coordinates.
Definition evas_canvas_eo.legacy.c:75
evas_output_size_get
EVAS_API void evas_output_size_get(const Evas *eo_e, int *w, int *h)
Retrieve the output size of the render engine of the given evas.
Definition evas_main.c:1402
evas_object_name_find
EVAS_API Efl_Canvas_Object * evas_object_name_find(const Evas_Canvas *obj, const char *name)
Retrieves the object on the given evas with the given name.
Definition evas_canvas_eo.legacy.c:243
evas_object_show
EVAS_API void evas_object_show(Evas_Object *eo_obj)
Makes the given Evas object visible.
Definition evas_object_main.c:1814
evas_object_name_set
EVAS_API void evas_object_name_set(Evas_Object *eo_obj, const char *name)
Sets the name of the given Evas object to the given name.
Definition evas_name.c:5
evas_object_geometry_get
EVAS_API void evas_object_geometry_get(const Evas_Object *eo_obj, Evas_Coord *x, Evas_Coord *y, Evas_Coord *w, Evas_Coord *h)
Retrieves the position and (rectangular) size of the given Evas object.
Definition evas_object_main.c:1335
evas_object_color_set
EVAS_API void evas_object_color_set(Evas_Object *obj, int r, int g, int b, int a)
Sets the general/main color of the given Evas object to the given one.
Definition evas_object_main.c:2024
evas_object_hide
EVAS_API void evas_object_hide(Evas_Object *eo_obj)
Makes the given Evas object invisible.
Definition evas_object_main.c:1823
evas_object_event_callback_add
EVAS_API void evas_object_event_callback_add(Evas_Object *eo_obj, Evas_Callback_Type type, Evas_Object_Event_Cb func, const void *data)
Add (register) a callback function to a given Evas object event.
Definition evas_callbacks.c:478
evas_map_point_color_set
EVAS_API void evas_map_point_color_set(Evas_Map *m, int idx, int r, int g, int b, int a)
Set the color of a vertex in the map.
Definition evas_map.c:822
evas_map_util_rotate
EVAS_API void evas_map_util_rotate(Evas_Map *m, double degrees, Evas_Coord cx, Evas_Coord cy)
Change the map to apply the given rotation.
Definition evas_map.c:968
evas_map_alpha_set
EVAS_API void evas_map_alpha_set(Evas_Map *m, Eina_Bool enabled)
Set the alpha flag for map rendering.
Definition evas_map.c:690
evas_map_free
EVAS_API void evas_map_free(Evas_Map *m)
Free a previously allocated map.
Definition evas_map.c:745
evas_map_util_3d_lighting
EVAS_API void evas_map_util_3d_lighting(Evas_Map *m, Evas_Coord lx, Evas_Coord ly, Evas_Coord lz, int lr, int lg, int lb, int ar, int ag, int ab)
Perform lighting calculations on the given Map.
Definition evas_map.c:1215
evas_map_util_clockwise_get
EVAS_API Eina_Bool evas_map_util_clockwise_get(Evas_Map *m)
Get the clockwise state of a map.
Definition evas_map.c:1275
evas_map_util_points_populate_from_geometry
EVAS_API void evas_map_util_points_populate_from_geometry(Evas_Map *m, Evas_Coord x, Evas_Coord y, Evas_Coord w, Evas_Coord h, Evas_Coord z)
Populate source and destination map points to match given geometry.
Definition evas_map.c:907
evas_map_smooth_set
EVAS_API void evas_map_smooth_set(Evas_Map *m, Eina_Bool enabled)
Set the smoothing for map rendering.
Definition evas_map.c:670
evas_map_util_3d_rotate
EVAS_API void evas_map_util_3d_rotate(Evas_Map *m, double dx, double dy, double dz, Evas_Coord cx, Evas_Coord cy, Evas_Coord cz)
Rotate the map around 3 axes in 3D.
Definition evas_map.c:1077
evas_map_point_coord_set
EVAS_API void evas_map_point_coord_set(Evas_Map *m, int idx, Evas_Coord x, Evas_Coord y, Evas_Coord z)
Change the map point's coordinate.
Definition evas_map.c:766
evas_map_util_points_populate_from_object
EVAS_API void evas_map_util_points_populate_from_object(Evas_Map *m, const Evas_Object *eo_obj)
Populate source and destination map points to match exactly object.
Definition evas_map.c:885
evas_map_point_image_uv_set
EVAS_API void evas_map_point_image_uv_set(Evas_Map *m, int idx, double u, double v)
Change the map point's U and V texture source point.
Definition evas_map.c:787
evas_map_util_points_color_set
EVAS_API void evas_map_util_points_color_set(Evas_Map *m, int r, int g, int b, int a)
Set color of all points to given color.
Definition evas_map.c:922
evas_map_util_points_populate_from_object_full
EVAS_API void evas_map_util_points_populate_from_object_full(Evas_Map *m, const Evas_Object *eo_obj, Evas_Coord z)
Populate source and destination map points to match exactly object.
Definition evas_map.c:863
evas_map_new
EVAS_API Evas_Map * evas_map_new(int count)
Create map of transformation points to be later used with an Evas object.
Definition evas_map.c:658
evas_map_util_3d_perspective
EVAS_API void evas_map_util_3d_perspective(Evas_Map *m, Evas_Coord px, Evas_Coord py, Evas_Coord z0, Evas_Coord foc)
Apply a perspective transform to the map.
Definition evas_map.c:1263
evas_object_move
EVAS_API void evas_object_move(Evas_Object *obj, Evas_Coord x, Evas_Coord y)
Move the given Evas object to the given location inside its canvas' viewport.
Definition evas_object_main.c:1171
Evas_Object
Efl_Canvas_Object Evas_Object
An Evas Object handle.
Definition Evas_Common.h:185
evas_object_focus_set
EVAS_API void evas_object_focus_set(Efl_Canvas_Object *obj, Eina_Bool focus)
Indicates that this object is the keyboard event receiver on its canvas.
Definition efl_canvas_object_eo.legacy.c:39
evas_object_resize
EVAS_API void evas_object_resize(Evas_Object *obj, Evas_Coord w, Evas_Coord h)
Changes the size of the given Evas object.
Definition evas_object_main.c:1236
evas_object_map_set
EVAS_API void evas_object_map_set(Evas_Object *eo_obj, const Evas_Map *map)
Set current object transformation map.
Definition evas_map.c:532
evas_object_map_enable_set
EVAS_API void evas_object_map_enable_set(Eo *eo_obj, Eina_Bool enabled)
Enable or disable the map that is set.
Definition evas_map.c:516
evas_object_image_size_get
EVAS_API void evas_object_image_size_get(const Evas_Object *obj, int *w, int *h)
Retrieves the size of the given image object.
Definition evas_image_legacy.c:159
evas_object_image_source_set
EVAS_API Eina_Bool evas_object_image_source_set(Evas_Object *eo, Evas_Object *src)
Set the source object on an image object to used as a proxy.
Definition evas_image_legacy.c:463
evas_object_image_file_set
EVAS_API void evas_object_image_file_set(Evas_Object *obj, const char *file, const char *key)
Set the source file from where an image object must fetch the real image data (it may be an Eet file,...
Definition evas_image_legacy.c:194
evas_object_image_filled_add
EVAS_API Evas_Object * evas_object_image_filled_add(Evas *eo_e)
Creates a new image object that automatically scales its bound image to the object's area,...
Definition evas_image_legacy.c:35
evas_object_rectangle_add
EVAS_API Evas_Object * evas_object_rectangle_add(Evas *e)
Adds a rectangle to the given evas.
Definition evas_object_rectangle.c:78
_Evas_Event_Key_Down::key
const char * key
The logical key : (eg shift+1 == exclamation).
Definition Evas_Legacy.h:320