summaryrefslogtreecommitdiff
path: root/unsorted/elementary/evas3d_object_on_button_example.c
diff options
context:
space:
mode:
authorCedric BAIL <cedric@osg.samsung.com>2016-03-23 12:56:14 -0700
committerCedric BAIL <cedric@osg.samsung.com>2016-03-23 13:24:41 -0700
commit83ad67f514917d00fb2a9476c63e2f862ee8f3a6 (patch)
tree98549588d3c2ccfdbf6abbfb865bfa34b02005f4 /unsorted/elementary/evas3d_object_on_button_example.c
parentf802f50a95cddfaafd81c86c758b4c677d974035 (diff)
elementary: move all legacy files to their expected new location.
Diffstat (limited to 'unsorted/elementary/evas3d_object_on_button_example.c')
-rw-r--r--unsorted/elementary/evas3d_object_on_button_example.c289
1 files changed, 289 insertions, 0 deletions
diff --git a/unsorted/elementary/evas3d_object_on_button_example.c b/unsorted/elementary/evas3d_object_on_button_example.c
new file mode 100644
index 0000000..842cdfd
--- /dev/null
+++ b/unsorted/elementary/evas3d_object_on_button_example.c
@@ -0,0 +1,289 @@
1/*
2 * This example shows the mechanism of elementary widget adding (button) to the 3d scene object (cube) and
3 * illustrates the work of callback of event from mouse.
4 *
5 * Compile with:
6 * gcc -o evas3d_object_on_button_example evas3d_object_on_button_example.c -g `pkg-config --libs --cflags evas ecore eo elementary`
7 */
8
9
10#define EFL_EO_API_SUPPORT
11#define EFL_BETA_API_SUPPORT
12
13#include <Eo.h>
14#include <Evas.h>
15#include <Ecore.h>
16#include <Elementary.h>
17
18#define WIDTH 400
19#define HEIGHT 400
20
21typedef struct _Scene_Data
22{
23 Eo *scene;
24 Eo *root_node;
25 Eo *camera_node;
26 Eo *light_node;
27 Eo *mesh_node;
28
29 Eo *camera;
30 Eo *light;
31 Eo *mesh;
32 Eo *material;
33 Eo *texture;
34} Scene_Data;
35
36Evas_Object *win = NULL;
37static Evas *evas = NULL;
38static Eo *image = NULL;
39static Eo *btn = NULL;
40static float d_angle = 0.5;
41
42static const float cube_vertices[] =
43{
44 /* Front */
45 -1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0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0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0,
70 1.0, 1.0, -1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0,
71 -1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0,
72 1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0,
73
74 /* Bottom */
75 1.0, -1.0, -1.0, 0.0, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0,
76 -1.0, -1.0, -1.0, 0.0, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0,
77 1.0, -1.0, 1.0, 0.0, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0,
78 -1.0, -1.0, 1.0, 0.0, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0,
79};
80
81static const unsigned short cube_indices[] =
82{
83 /* Front */
84 0, 1, 2, 2, 1, 3,
85
86 /* Back */
87 4, 5, 6, 6, 5, 7,
88
89 /* Left */
90 8, 9, 10, 10, 9, 11,
91
92 /* Right */
93 12, 13, 14, 14, 13, 15,
94
95 /* Top */
96 16, 17, 18, 18, 17, 19,
97
98 /* Bottom */
99 20, 21, 22, 22, 21, 23
100};
101
102static void
103_stop_scene(void *data,
104 Evas *e EINA_UNUSED,
105 Evas_Object *eo EINA_UNUSED,
106 void *event_info)
107{
108 Evas_Event_Mouse_Down *ev = event_info;
109 Evas_Canvas3D_Node *n;
110 Evas_Canvas3D_Mesh *m;
111 Evas_Real s, t;
112 Scene_Data *d = (Scene_Data *)data;
113
114 if (ev->button == 1)
115 {
116 Eina_Bool ret;
117
118 evas_canvas3d_scene_color_pick_enable_set(d->scene, EINA_TRUE);
119 ret = evas_canvas3d_scene_pick(d->scene, ev->canvas.x, ev->canvas.y, &n, &m, &s, &t);
120 if (ret)
121 {
122 d_angle = 0.0;
123 elm_object_signal_emit(btn, "mouse,down,1", "event");
124 evas_canvas3d_node_position_set(d->mesh_node, 0.0, 0.0, -0.2);
125 }
126 }
127}
128
129static void
130_play_scene(void *data,
131 Evas *e EINA_UNUSED,
132 Evas_Object *eo EINA_UNUSED,
133 void *event_info EINA_UNUSED)
134{
135 Scene_Data *d = (Scene_Data *)data;
136
137 d_angle = 0.5;
138 elm_object_signal_emit(btn, "mouse,up,1", "event");
139 evas_canvas3d_node_position_set(d->mesh_node, 0.0, 0.0, 0.0);
140}
141
142
143static Eina_Bool
144_animate_scene(void *data)
145{
146 static float angle = 0.0f;
147 Scene_Data *scene = (Scene_Data *)data;
148
149 angle += d_angle;
150
151 evas_canvas3d_node_orientation_angle_axis_set(scene->mesh_node, angle, 1.0, 1.0, 1.0);
152
153 /* Rotate */
154 if (angle > 360.0) angle -= 360.0f;
155
156 return EINA_TRUE;
157}
158
159static void
160_camera_setup(Scene_Data *data)
161{
162 data->camera = eo_add(EVAS_CANVAS3D_CAMERA_CLASS, evas);
163
164 evas_canvas3d_camera_projection_perspective_set(data->camera, 20.0, 1.0, 2.0, 50.0);
165
166 data->camera_node =
167 eo_add(EVAS_CANVAS3D_NODE_CLASS, evas, evas_canvas3d_node_constructor(eo_self, EVAS_CANVAS3D_NODE_TYPE_CAMERA));
168 evas_canvas3d_node_camera_set(data->camera_node, data->camera);
169 evas_canvas3d_node_position_set(data->camera_node, 0.0, 0.0, 15.0);
170 evas_canvas3d_node_look_at_set(data->camera_node, EVAS_CANVAS3D_SPACE_PARENT, 0.0, 0.0, 0.0, EVAS_CANVAS3D_SPACE_PARENT, 0.0, -1.0, 0.0);
171 evas_canvas3d_node_member_add(data->root_node, data->camera_node);
172}
173
174static void
175_light_setup(Scene_Data *data)
176{
177 data->light = eo_add(EVAS_CANVAS3D_LIGHT_CLASS, evas);
178 evas_canvas3d_light_ambient_set(data->light, 0.2, 0.2, 0.2, 1.0);
179 evas_canvas3d_light_diffuse_set(data->light, 1.0, 1.0, 1.0, 1.0);
180 evas_canvas3d_light_specular_set(data->light, 1.0, 1.0, 1.0, 1.0);
181
182 data->light_node =
183 eo_add(EVAS_CANVAS3D_NODE_CLASS, evas, evas_canvas3d_node_constructor(eo_self, EVAS_CANVAS3D_NODE_TYPE_LIGHT));
184 evas_canvas3d_node_light_set(data->light_node, data->light);
185 evas_canvas3d_node_position_set(data->light_node, 0.0, 0.0, 10.0);
186 evas_canvas3d_node_look_at_set(data->light_node, EVAS_CANVAS3D_SPACE_PARENT, 0.0, 0.0, 0.0, EVAS_CANVAS3D_SPACE_PARENT, 0.0, 1.0, 0.0);
187 evas_canvas3d_node_member_add(data->root_node, data->light_node);
188}
189static void
190_mesh_setup(Scene_Data *data)
191{
192 /* Setup material and texture as widget button. */
193 data->material = eo_add(EVAS_CANVAS3D_MATERIAL_CLASS, evas);
194 data->texture = eo_add(EVAS_CANVAS3D_TEXTURE_CLASS, evas);
195
196 evas_canvas3d_texture_source_set(data->texture, btn);
197 evas_canvas3d_texture_source_visible_set(data->texture, EINA_FALSE);
198
199 evas_canvas3d_material_texture_set(data->material, EVAS_CANVAS3D_MATERIAL_ATTRIB_DIFFUSE, data->texture);
200 evas_canvas3d_material_enable_set(data->material, EVAS_CANVAS3D_MATERIAL_ATTRIB_AMBIENT, EINA_TRUE);
201 evas_canvas3d_material_enable_set(data->material, EVAS_CANVAS3D_MATERIAL_ATTRIB_DIFFUSE, EINA_TRUE);
202 evas_canvas3d_material_enable_set(data->material, EVAS_CANVAS3D_MATERIAL_ATTRIB_SPECULAR, EINA_TRUE);
203 evas_canvas3d_material_color_set(data->material, EVAS_CANVAS3D_MATERIAL_ATTRIB_AMBIENT, 0.2, 0.2, 0.2, 1.0);
204 evas_canvas3d_material_color_set(data->material, EVAS_CANVAS3D_MATERIAL_ATTRIB_DIFFUSE, 0.8, 0.8, 0.8, 1.0);
205 evas_canvas3d_material_color_set(data->material, EVAS_CANVAS3D_MATERIAL_ATTRIB_SPECULAR, 1.0, 1.0, 1.0, 1.0);
206 evas_canvas3d_material_shininess_set(data->material, 100.0);
207
208 /* Setup mesh. */
209 data->mesh = eo_add(EVAS_CANVAS3D_MESH_CLASS, evas);
210 evas_canvas3d_mesh_vertex_count_set(data->mesh, 24);
211 evas_canvas3d_mesh_frame_add(data->mesh, 0);
212 evas_canvas3d_mesh_frame_vertex_data_set(data->mesh, 0, EVAS_CANVAS3D_VERTEX_ATTRIB_POSITION, 12 * sizeof(float), &cube_vertices[ 0]);
213 evas_canvas3d_mesh_frame_vertex_data_set(data->mesh, 0, EVAS_CANVAS3D_VERTEX_ATTRIB_NORMAL, 12 * sizeof(float), &cube_vertices[ 3]);
214 evas_canvas3d_mesh_frame_vertex_data_set(data->mesh, 0, EVAS_CANVAS3D_VERTEX_ATTRIB_COLOR, 12 * sizeof(float), &cube_vertices[ 6]);
215 evas_canvas3d_mesh_frame_vertex_data_set(data->mesh, 0, EVAS_CANVAS3D_VERTEX_ATTRIB_TEXCOORD, 12 * sizeof(float), &cube_vertices[10]);
216 evas_canvas3d_mesh_index_data_set(data->mesh, EVAS_CANVAS3D_INDEX_FORMAT_UNSIGNED_SHORT, 36, &cube_indices[0]);
217 evas_canvas3d_mesh_vertex_assembly_set(data->mesh, EVAS_CANVAS3D_VERTEX_ASSEMBLY_TRIANGLES);
218 evas_canvas3d_mesh_shade_mode_set(data->mesh, EVAS_CANVAS3D_SHADE_MODE_PHONG);
219 evas_canvas3d_mesh_frame_material_set(data->mesh, 0, data->material);
220 evas_canvas3d_mesh_color_pick_enable_set(data->mesh, EINA_TRUE);
221
222 data->mesh_node =
223 eo_add(EVAS_CANVAS3D_NODE_CLASS, evas, evas_canvas3d_node_constructor(eo_self, EVAS_CANVAS3D_NODE_TYPE_MESH));
224 evas_canvas3d_node_member_add(data->root_node, data->mesh_node);
225 evas_canvas3d_node_mesh_add(data->mesh_node, data->mesh);
226}
227
228static void
229_scene_setup(Scene_Data *data)
230{
231 data->scene = eo_add(EVAS_CANVAS3D_SCENE_CLASS, evas);
232 evas_canvas3d_scene_size_set(data->scene, WIDTH, HEIGHT);
233 evas_canvas3d_scene_background_color_set(data->scene, 0.0, 0.0, 0.0, 0.0);
234
235 data->root_node =
236 eo_add(EVAS_CANVAS3D_NODE_CLASS, evas, evas_canvas3d_node_constructor(eo_self, EVAS_CANVAS3D_NODE_TYPE_NODE));
237
238 _camera_setup(data);
239 _light_setup(data);
240 _mesh_setup(data);
241
242 evas_canvas3d_scene_root_node_set(data->scene, data->root_node);
243 evas_canvas3d_scene_camera_node_set(data->scene, data->camera_node);
244}
245
246int
247elm_main(int argc, char **argv)
248{
249 Scene_Data data;
250
251 elm_config_accel_preference_set("3d");
252 elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
253
254 win = elm_win_util_standard_add("evas3d-object-button", "A button on the 3d object");
255 elm_win_autodel_set(win, EINA_TRUE);
256
257 evas = evas_object_evas_get(win);
258
259 /* Add an image object for 3D scene rendering. */
260 image = eo_add(EFL_CANVAS_SCENE3D_CLASS, evas);
261 efl_gfx_size_set(image, WIDTH, HEIGHT);
262 efl_gfx_visible_set(image, EINA_TRUE);
263
264 btn = elm_button_add(win);
265 evas_object_resize(btn, (WIDTH * 2) / 3, (HEIGHT * 2) / 3);
266 elm_object_text_set(btn, "3D Button");
267 elm_object_scale_set(btn, 3.0);
268 evas_object_show(btn);
269
270 /* Set the image object as render target for 3D scene. */
271 _scene_setup(&data);
272 evas_object_focus_set(image, EINA_TRUE);
273 efl_canvas_scene3d_set(image, data.scene);
274
275 evas_object_event_callback_add(image, EVAS_CALLBACK_MOUSE_DOWN, _stop_scene, &data);
276 evas_object_event_callback_add(image, EVAS_CALLBACK_MOUSE_UP, _play_scene, &data);
277
278 /* Add animation timer callback. */
279 ecore_timer_add(0.016, _animate_scene, &data);
280
281 evas_object_resize(win, WIDTH, HEIGHT);
282 evas_object_show(win);
283
284 /* Enter main loop. */
285 elm_run();
286
287 return 0;
288}
289ELM_MAIN()