summaryrefslogtreecommitdiff
path: root/legacy/ephysics
diff options
context:
space:
mode:
authorLeandro Dorileo <dorileo@profusion.mobi>2012-10-08 18:01:06 +0000
committerBruno Dilly <bdilly@profusion.mobi>2012-10-08 18:01:06 +0000
commite084960cf38003b7c17fcf9929137dc91c8d180b (patch)
treebf9267c9a9f8a2a804dfdc5ccf4c9b5242bbb206 /legacy/ephysics
parent3db5bdd77120032c0bd9f92ca0ac9e559e85ca95 (diff)
EPhysics: dragging body test
New test where the user can drag a body around the world by grabbing and dragging. Patch by: Leandro Dorileo <dorileo@profusion.mobi> SVN revision: 77585
Diffstat (limited to 'legacy/ephysics')
-rw-r--r--legacy/ephysics/data/themes/cubes.edc8
-rw-r--r--legacy/ephysics/data/themes/frame.edc31
-rw-r--r--legacy/ephysics/src/bin/Makefile.am1
-rw-r--r--legacy/ephysics/src/bin/test.c2
-rw-r--r--legacy/ephysics/src/bin/test_grab.c174
5 files changed, 212 insertions, 4 deletions
diff --git a/legacy/ephysics/data/themes/cubes.edc b/legacy/ephysics/data/themes/cubes.edc
index 6c553435f3..3cc2be29bd 100644
--- a/legacy/ephysics/data/themes/cubes.edc
+++ b/legacy/ephysics/data/themes/cubes.edc
@@ -10,8 +10,9 @@
10 10
11 part { 11 part {
12 name: "cube"; 12 name: "cube";
13 mouse_events: 0;
14 type: IMAGE; 13 type: IMAGE;
14 mouse_events: 1;
15 repeat_events: 0;
15 description { 16 description {
16 state: "default" 0.0; 17 state: "default" 0.0;
17 image.normal: "cube-blue.png"; 18 image.normal: "cube-blue.png";
@@ -19,7 +20,6 @@
19 } 20 }
20 21
21 } 22 }
22
23 } 23 }
24 24
25 group { 25 group {
@@ -29,8 +29,9 @@
29 29
30 part { 30 part {
31 name: "cube"; 31 name: "cube";
32 mouse_events: 0;
33 type: IMAGE; 32 type: IMAGE;
33 mouse_events: 1;
34 repeat_events: 0;
34 description { 35 description {
35 state: "default" 0.0; 36 state: "default" 0.0;
36 image.normal: "cube-purple.png"; 37 image.normal: "cube-purple.png";
@@ -38,5 +39,4 @@
38 } 39 }
39 40
40 } 41 }
41
42 } 42 }
diff --git a/legacy/ephysics/data/themes/frame.edc b/legacy/ephysics/data/themes/frame.edc
index 6bbd23cb6e..7148a43508 100644
--- a/legacy/ephysics/data/themes/frame.edc
+++ b/legacy/ephysics/data/themes/frame.edc
@@ -103,6 +103,28 @@
103 } 103 }
104 104
105 part { 105 part {
106 name: "grab_msg";
107 type: TEXT;
108 mouse_events: 0;
109 description {
110 state: "default" 0.0;
111 color: 232 224 215 0;
112 rel1.relative: 0.08 0.96;
113 rel2.relative: 0.92 1.0;
114 text {
115 font: "Sans";
116 size: 12;
117 text: "Drag a body to move it.";
118 }
119 }
120 description {
121 state: "visible" 0.0;
122 inherit: "default" 0.0;
123 color: 95 56 19 255;
124 }
125 }
126
127 part {
106 name: "linear_vel"; 128 name: "linear_vel";
107 type: TEXT; 129 type: TEXT;
108 mouse_events: 0; 130 mouse_events: 0;
@@ -268,6 +290,15 @@
268 } 290 }
269 291
270 program { 292 program {
293 name: "grab,show";
294 source: "ephysics_test";
295 signal: "grab,show";
296 action: STATE_SET "visible" 0.0;
297 target: "arrows";
298 target: "grab_msg";
299 }
300
301 program {
271 name: "borders,show"; 302 name: "borders,show";
272 source: "ephysics_test"; 303 source: "ephysics_test";
273 signal: "borders,show"; 304 signal: "borders,show";
diff --git a/legacy/ephysics/src/bin/Makefile.am b/legacy/ephysics/src/bin/Makefile.am
index 8cb344701e..e16755a05b 100644
--- a/legacy/ephysics/src/bin/Makefile.am
+++ b/legacy/ephysics/src/bin/Makefile.am
@@ -28,6 +28,7 @@ test_constraint.c \
28test_delete.c \ 28test_delete.c \
29test_falling_letters.c \ 29test_falling_letters.c \
30test_forces.c \ 30test_forces.c \
31test_grab.c \
31test_growing_balls.c \ 32test_growing_balls.c \
32test_heavy.c \ 33test_heavy.c \
33test_jumping_balls.c \ 34test_jumping_balls.c \
diff --git a/legacy/ephysics/src/bin/test.c b/legacy/ephysics/src/bin/test.c
index ce1f7f60b7..50720405d8 100644
--- a/legacy/ephysics/src/bin/test.c
+++ b/legacy/ephysics/src/bin/test.c
@@ -28,6 +28,7 @@ void test_delete(void *data, Evas_Object *obj, void *event_info);
28void test_falling_letters(void *data, Evas_Object *obj, void *event_info); 28void test_falling_letters(void *data, Evas_Object *obj, void *event_info);
29void test_flag(void *data, Evas_Object *obj, void *event_info); 29void test_flag(void *data, Evas_Object *obj, void *event_info);
30void test_forces(void *data, Evas_Object *obj, void *event_info); 30void test_forces(void *data, Evas_Object *obj, void *event_info);
31void test_grab(void *data, Evas_Object *obj, void *event_info);
31void test_growing_balls(void *data, Evas_Object *obj, void *event_info); 32void test_growing_balls(void *data, Evas_Object *obj, void *event_info);
32void test_heavy(void *data, Evas_Object *obj, void *event_info); 33void test_heavy(void *data, Evas_Object *obj, void *event_info);
33void test_jumping_balls(void *data, Evas_Object *obj, void *event_info); 34void test_jumping_balls(void *data, Evas_Object *obj, void *event_info);
@@ -56,6 +57,7 @@ static const EPhysics_Test tests[] = {
56 {"Falling Letters", test_falling_letters}, 57 {"Falling Letters", test_falling_letters},
57 {"Flag - Cloth", test_flag}, 58 {"Flag - Cloth", test_flag},
58 {"Forces", test_forces}, 59 {"Forces", test_forces},
60 {"Grab", test_grab},
59 {"Growing Balls", test_growing_balls}, 61 {"Growing Balls", test_growing_balls},
60 {"Heavy", test_heavy}, 62 {"Heavy", test_heavy},
61 {"Jumping Balls", test_jumping_balls}, 63 {"Jumping Balls", test_jumping_balls},
diff --git a/legacy/ephysics/src/bin/test_grab.c b/legacy/ephysics/src/bin/test_grab.c
new file mode 100644
index 0000000000..fae6e772b9
--- /dev/null
+++ b/legacy/ephysics/src/bin/test_grab.c
@@ -0,0 +1,174 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include "ephysics_test.h"
6
7typedef struct _Dragging_Data
8{
9 int mouse_status; // 0, up, 1, down
10 EPhysics_Body *body;
11 double curr_mass;
12 struct {
13 int x;
14 int y;
15 } clicked_position;
16} Dragging_Data;
17
18static void
19_on_delete(void *data __UNUSED__, EPhysics_Body *body, void *event_info __UNUSED__)
20{
21 Dragging_Data *dragging = ephysics_body_data_get(body);
22 free(dragging);
23}
24
25static void
26_mouse_down_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
27{
28 Dragging_Data *dragging = ephysics_body_data_get(data);
29 Evas_Event_Mouse_Down *mdown = event_info;
30 Evas_Coord x, y;
31
32 evas_object_geometry_get(obj, &x, &y, NULL, NULL);
33 dragging->mouse_status = 1;
34 dragging->clicked_position.x = mdown->output.x - x;
35 dragging->clicked_position.y = mdown->output.y - y;
36 ephysics_body_mass_get(data);
37 dragging->curr_mass = ephysics_body_mass_get(data);
38 ephysics_body_mass_set(data, 0);
39}
40
41static void
42_mouse_up_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
43{
44 Dragging_Data *dragging = ephysics_body_data_get(data);
45 dragging->mouse_status = 0;
46 ephysics_body_mass_set(data, dragging->curr_mass);
47 dragging->curr_mass = 0;
48}
49
50static void
51_mouse_move_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info)
52{
53 Dragging_Data *dragging = ephysics_body_data_get(data);
54 Evas_Event_Mouse_Move *mmove = event_info;
55 Evas_Coord x, y, w, h, nx, ny;
56
57 if (!dragging->mouse_status) return;
58
59 nx = mmove->cur.output.x - dragging->clicked_position.x;
60 ny = mmove->cur.output.y - dragging->clicked_position.y;
61
62 if (nx < 0 || ny < 0) return;
63
64 evas_object_geometry_get(obj, &x, &y, &w, &h);
65 ephysics_body_move(dragging->body, nx, ny);
66}
67
68static void
69_box_add(Test_Data *test_data, Evas_Coord x, Evas_Coord y, const char *file)
70{
71 Evas_Object *evas, *shadow;
72 EPhysics_Body *body;
73 Dragging_Data *dragging;
74
75 shadow = elm_layout_add(test_data->win);
76 elm_layout_file_set(
77 shadow, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "shadow-cube");
78 evas_object_move(shadow, x, FLOOR_Y);
79 evas_object_resize(shadow, 70, 3);
80 evas_object_show(shadow);
81 test_data->evas_objs = eina_list_append(test_data->evas_objs, shadow);
82
83 evas = elm_image_add(test_data->win);
84 elm_image_file_set(
85 evas, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", file);
86 evas_object_move(evas, x, y - 70);
87 evas_object_resize(evas, 70, 70);
88 evas_object_show(evas);
89 test_data->evas_objs = eina_list_append(test_data->evas_objs, evas);
90
91 body = ephysics_body_box_add(test_data->world);
92 ephysics_body_evas_object_set(body, evas, EINA_TRUE);
93 ephysics_body_event_callback_add(body, EPHYSICS_CALLBACK_BODY_UPDATE,
94 update_object_cb, shadow);
95 test_data->bodies = eina_list_append(test_data->bodies, body);
96 test_data->data = body;
97
98 dragging = calloc(1, sizeof(Dragging_Data));
99 dragging->body = body;
100 ephysics_body_data_set(body, dragging);
101
102 evas_object_event_callback_add(evas, EVAS_CALLBACK_MOUSE_DOWN, _mouse_down_cb,
103 body);
104 evas_object_event_callback_add(evas, EVAS_CALLBACK_MOUSE_UP, _mouse_up_cb,
105 body);
106 evas_object_event_callback_add(evas, EVAS_CALLBACK_MOUSE_MOVE, _mouse_move_cb,
107 body);
108
109 ephysics_body_event_callback_add(body, EPHYSICS_CALLBACK_BODY_DEL, _on_delete,
110 NULL);
111}
112
113static void
114_world_populate(Test_Data *test_data)
115{
116 _box_add(test_data, WIDTH / 3, FLOOR_Y, "blue-cube");
117 _box_add(test_data, WIDTH / 3 - 70, FLOOR_Y, "purple-cube");
118 _box_add(test_data, WIDTH / 3 + 70, FLOOR_Y, "purple-cube");
119 _box_add(test_data, WIDTH / 3 + 140, FLOOR_Y, "blue-cube");
120 _box_add(test_data, WIDTH / 3 - 35, FLOOR_Y - 70, "purple-cube");
121 _box_add(test_data, WIDTH / 3 + 35, FLOOR_Y - 70, "blue-cube");
122 _box_add(test_data, WIDTH / 3 + 105, FLOOR_Y - 70, "purple-cube");
123 _box_add(test_data, WIDTH / 3, FLOOR_Y - 140, "blue-cube");
124 _box_add(test_data, WIDTH / 3 + 70, FLOOR_Y - 140, "purple-cube");
125}
126
127static void
128_restart(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
129{
130 Test_Data *test_data = data;
131
132 DBG("Restart pressed");
133 test_clean(test_data);
134 _world_populate(test_data);
135}
136
137void
138test_grab(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
139{
140 EPhysics_Body *boundary;
141 EPhysics_World *world;
142 Test_Data *test_data;
143
144 if (!ephysics_init())
145 return;
146
147 test_data = test_data_new();
148 test_win_add(test_data, "Grab", EINA_TRUE);
149
150 elm_layout_signal_callback_add(test_data->layout, "restart", "test-theme",
151 _restart, test_data);
152 elm_object_signal_emit(test_data->layout, "borders,show", "ephysics_test");
153 elm_object_signal_emit(test_data->layout, "grab,show", "ephysics_test");
154
155 world = ephysics_world_new();
156 ephysics_world_render_geometry_set(world, 50, 40, WIDTH - 100, FLOOR_Y - 40);
157 test_data->world = world;
158
159 boundary = ephysics_body_bottom_boundary_add(test_data->world);
160 ephysics_body_restitution_set(boundary, 0.65);
161 ephysics_body_friction_set(boundary, 4);
162
163 boundary = ephysics_body_right_boundary_add(test_data->world);
164 ephysics_body_restitution_set(boundary, 0.4);
165 ephysics_body_friction_set(boundary, 3);
166
167 boundary = ephysics_body_left_boundary_add(test_data->world);
168 ephysics_body_restitution_set(boundary, 0.4);
169 ephysics_body_friction_set(boundary, 3);
170
171 ephysics_body_top_boundary_add(test_data->world);
172
173 _world_populate(test_data);
174}