summaryrefslogtreecommitdiff
path: root/legacy
diff options
context:
space:
mode:
authorBruno Dilly <bdilly@profusion.mobi>2012-08-13 22:24:47 +0000
committerBruno Dilly <bdilly@profusion.mobi>2012-08-13 22:24:47 +0000
commitae28b7a50482cfabe37a53fd12fa07e5b33d0c94 (patch)
tree04f0378389e0a2e4b3dd7da4494fd2d1e6b476ca /legacy
parent3c27a0288d12eef6a6dcd94d1598c50f734e98fe (diff)
EPhysics: listen for associated evas object resize event
So physics body will resize together. SVN revision: 75230
Diffstat (limited to 'legacy')
-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_growing_balls.c121
-rw-r--r--legacy/ephysics/src/lib/ephysics_body.cpp100
-rw-r--r--legacy/ephysics/src/lib/ephysics_private.h2
5 files changed, 192 insertions, 34 deletions
diff --git a/legacy/ephysics/src/bin/Makefile.am b/legacy/ephysics/src/bin/Makefile.am
index ad1c081148..12538f8332 100644
--- a/legacy/ephysics/src/bin/Makefile.am
+++ b/legacy/ephysics/src/bin/Makefile.am
@@ -27,6 +27,7 @@ test_collision_speed.c \
27test_constraint.c \ 27test_constraint.c \
28test_delete.c \ 28test_delete.c \
29test_falling_letters.c \ 29test_falling_letters.c \
30test_growing_balls.c \
30test_jumping_balls.c \ 31test_jumping_balls.c \
31test_no_gravity.c \ 32test_no_gravity.c \
32test_rotate.c \ 33test_rotate.c \
diff --git a/legacy/ephysics/src/bin/test.c b/legacy/ephysics/src/bin/test.c
index 74877f1152..11c666305c 100644
--- a/legacy/ephysics/src/bin/test.c
+++ b/legacy/ephysics/src/bin/test.c
@@ -20,6 +20,7 @@ void test_collision_speed(void *data, Evas_Object *obj, void *event_info);
20void test_constraint(void *data, Evas_Object *obj, void *event_info); 20void test_constraint(void *data, Evas_Object *obj, void *event_info);
21void test_delete(void *data, Evas_Object *obj, void *event_info); 21void test_delete(void *data, Evas_Object *obj, void *event_info);
22void test_falling_letters(void *data, Evas_Object *obj, void *event_info); 22void test_falling_letters(void *data, Evas_Object *obj, void *event_info);
23void test_growing_balls(void *data, Evas_Object *obj, void *event_info);
23void test_jumping_balls(void *data, Evas_Object *obj, void *event_info); 24void test_jumping_balls(void *data, Evas_Object *obj, void *event_info);
24void test_no_gravity(void *data, Evas_Object *obj, void *event_info); 25void test_no_gravity(void *data, Evas_Object *obj, void *event_info);
25void test_rotate(void *data, Evas_Object *obj, void *event_info); 26void test_rotate(void *data, Evas_Object *obj, void *event_info);
@@ -165,6 +166,7 @@ _main_win_add(char *autorun __UNUSED__, Eina_Bool test_win_only __UNUSED__)
165 ADD_TEST("CONSTRAINT", test_constraint); 166 ADD_TEST("CONSTRAINT", test_constraint);
166 ADD_TEST("DELETE BODY", test_delete); 167 ADD_TEST("DELETE BODY", test_delete);
167 ADD_TEST("FALLING LETTERS", test_falling_letters); 168 ADD_TEST("FALLING LETTERS", test_falling_letters);
169 ADD_TEST("GROWING BALLS", test_growing_balls);
168 ADD_TEST("JUMPING BALLS", test_jumping_balls); 170 ADD_TEST("JUMPING BALLS", test_jumping_balls);
169 ADD_TEST("NO GRAVITY", test_no_gravity); 171 ADD_TEST("NO GRAVITY", test_no_gravity);
170 ADD_TEST("ROTATE", test_rotate); 172 ADD_TEST("ROTATE", test_rotate);
diff --git a/legacy/ephysics/src/bin/test_growing_balls.c b/legacy/ephysics/src/bin/test_growing_balls.c
new file mode 100644
index 0000000000..e272f073ca
--- /dev/null
+++ b/legacy/ephysics/src/bin/test_growing_balls.c
@@ -0,0 +1,121 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include "ephysics_test.h"
6
7static Eina_Bool
8_grow_cb(void *data)
9{
10 Test_Data *test_data = data;
11 Evas_Object *obj;
12 Eina_List *l;
13 int size, i = -1;
14
15 EINA_LIST_FOREACH(test_data->evas_objs, l, obj)
16 {
17 evas_object_geometry_get(obj, NULL, NULL, &size, NULL);
18 size += i * 8;
19 i++;
20
21 if ((size < 20) || (size > 120))
22 continue;
23
24 evas_object_resize(obj, size, size);
25 }
26
27 return EINA_TRUE;
28}
29
30static void
31_add_sphere(Test_Data *test_data, const char *group, int size, int x, int y)
32{
33 EPhysics_Body *sphere_body;
34 Evas_Object *sphere;
35
36 sphere = elm_image_add(test_data->win);
37 elm_image_file_set(sphere, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj",
38 group);
39 evas_object_move(sphere, x, y);
40 evas_object_resize(sphere, size, size);
41 evas_object_show(sphere);
42 test_data->evas_objs = eina_list_append(test_data->evas_objs, sphere);
43
44 sphere_body = ephysics_body_circle_add(test_data->world);
45 ephysics_body_evas_object_set(sphere_body, sphere, EINA_TRUE);
46 ephysics_body_restitution_set(sphere_body, 0.5);
47 ephysics_body_central_impulse_apply(sphere_body, 10, 0);
48 test_data->bodies = eina_list_append(test_data->bodies, sphere_body);
49}
50
51static void
52_world_populate(Test_Data *test_data)
53{
54 _add_sphere(test_data, "big-red-ball", 60, 100, 100);
55 _add_sphere(test_data, "big-blue-ball", 60, 150, 150);
56 _add_sphere(test_data, "big-green-ball", 60, 200, 200);
57 test_data->data = ecore_timer_add(1, _grow_cb, test_data);
58}
59
60static void
61_win_del(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
62{
63 Test_Data *test_data = data;
64 Ecore_Timer *timer = test_data->data;
65
66 if (timer)
67 ecore_timer_del(timer);
68
69 test_data_del(test_data);
70 ephysics_shutdown();
71}
72
73static void
74_restart(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
75{
76 Test_Data *test_data = data;
77 Ecore_Timer *timer = test_data->data;
78
79 if (timer)
80 ecore_timer_del(timer);
81
82 DBG("Restart pressed");
83 test_clean(test_data);
84 _world_populate(test_data);
85}
86
87void
88test_growing_balls(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
89{
90 EPhysics_Body *boundary;
91 EPhysics_World *world;
92 Test_Data *test_data;
93
94 if (!ephysics_init())
95 return;
96
97 test_data = test_data_new();
98 test_win_add(test_data, "Growing Balls", EINA_FALSE);
99 evas_object_smart_callback_add(test_data->win, "delete,request", _win_del,
100 test_data);
101
102 elm_object_signal_emit(test_data->layout, "borders,show", "ephysics_test");
103 elm_layout_signal_callback_add(test_data->layout, "restart", "test-theme",
104 _restart, test_data);
105
106 world = ephysics_world_new();
107 ephysics_world_render_geometry_set(world, 50, 40, WIDTH - 100, FLOOR_Y - 40);
108 test_data->world = world;
109
110 boundary = ephysics_body_bottom_boundary_add(test_data->world);
111 ephysics_body_restitution_set(boundary, 0.8);
112
113 boundary = ephysics_body_top_boundary_add(test_data->world);
114 ephysics_body_restitution_set(boundary, 0.8);
115 boundary = ephysics_body_left_boundary_add(test_data->world);
116 ephysics_body_restitution_set(boundary, 0.8);
117 boundary = ephysics_body_right_boundary_add(test_data->world);
118 ephysics_body_restitution_set(boundary, 0.8);
119
120 _world_populate(test_data);
121}
diff --git a/legacy/ephysics/src/lib/ephysics_body.cpp b/legacy/ephysics/src/lib/ephysics_body.cpp
index fff2fc378c..6fc1379f35 100644
--- a/legacy/ephysics/src/lib/ephysics_body.cpp
+++ b/legacy/ephysics/src/lib/ephysics_body.cpp
@@ -256,34 +256,6 @@ _ephysics_body_evas_obj_del_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj
256} 256}
257 257
258static void 258static void
259_ephysics_body_del(EPhysics_Body *body)
260{
261 EPhysics_Body_Callback *cb;
262 void *group;
263
264 if (body->evas_obj)
265 evas_object_event_callback_del(body->evas_obj, EVAS_CALLBACK_DEL,
266 _ephysics_body_evas_obj_del_cb);
267
268 while (body->callbacks)
269 {
270 cb = EINA_INLIST_CONTAINER_GET(body->callbacks,
271 EPhysics_Body_Callback);
272 body->callbacks = eina_inlist_remove(body->callbacks, body->callbacks);
273 free(cb);
274 }
275
276 EINA_LIST_FREE(body->collision_groups, group)
277 eina_stringshare_del((Eina_Stringshare *)group);
278
279 delete body->rigid_body->getMotionState();
280 delete body->collision_shape;
281 delete body->rigid_body;
282
283 free(body);
284}
285
286static void
287_ephysics_body_geometry_set(EPhysics_Body *body, Evas_Coord x, Evas_Coord y, Evas_Coord w, Evas_Coord h) 259_ephysics_body_geometry_set(EPhysics_Body *body, Evas_Coord x, Evas_Coord y, Evas_Coord w, Evas_Coord h)
288{ 260{
289 double rate, mx, my, sx, sy; 261 double rate, mx, my, sx, sy;
@@ -310,11 +282,61 @@ _ephysics_body_geometry_set(EPhysics_Body *body, Evas_Coord x, Evas_Coord y, Eva
310 282
311 body->rigid_body->getMotionState()->setWorldTransform(trans); 283 body->rigid_body->getMotionState()->setWorldTransform(trans);
312 284
285 body->w = w;
286 body->h = h;
287
313 DBG("Body %p position changed to %lf, %lf.", body, mx, my); 288 DBG("Body %p position changed to %lf, %lf.", body, mx, my);
314 DBG("Body %p scale changed to %lf, %lf.", body, sx, sy); 289 DBG("Body %p scale changed to %lf, %lf.", body, sx, sy);
315} 290}
316 291
317static void 292static void
293_ephysics_body_evas_obj_resize_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
294{
295 EPhysics_Body *body = (EPhysics_Body *) data;
296 int x, y, w, h;
297
298 evas_object_geometry_get(obj, NULL, NULL, &w, &h);
299 if ((w == body->w) && (h == body->h))
300 return;
301
302 DBG("Resizing body %p to w=%i, h=%i", body, w, h);
303 ephysics_body_geometry_get(body, &x, &y, NULL, NULL);
304 _ephysics_body_geometry_set(body, x, y, w, h);
305}
306
307static void
308_ephysics_body_del(EPhysics_Body *body)
309{
310 EPhysics_Body_Callback *cb;
311 void *group;
312
313 if (body->evas_obj)
314 {
315 evas_object_event_callback_del(body->evas_obj, EVAS_CALLBACK_DEL,
316 _ephysics_body_evas_obj_del_cb);
317 evas_object_event_callback_del(body->evas_obj, EVAS_CALLBACK_RESIZE,
318 _ephysics_body_evas_obj_resize_cb);
319 }
320
321 while (body->callbacks)
322 {
323 cb = EINA_INLIST_CONTAINER_GET(body->callbacks,
324 EPhysics_Body_Callback);
325 body->callbacks = eina_inlist_remove(body->callbacks, body->callbacks);
326 free(cb);
327 }
328
329 EINA_LIST_FREE(body->collision_groups, group)
330 eina_stringshare_del((Eina_Stringshare *)group);
331
332 delete body->rigid_body->getMotionState();
333 delete body->collision_shape;
334 delete body->rigid_body;
335
336 free(body);
337}
338
339static void
318_ephysics_body_evas_object_default_update(EPhysics_Body *body) 340_ephysics_body_evas_object_default_update(EPhysics_Body *body)
319{ 341{
320 int x, y, w, h, wx, wy, wh, cx, cy; 342 int x, y, w, h, wx, wy, wh, cx, cy;
@@ -668,8 +690,12 @@ ephysics_body_evas_object_set(EPhysics_Body *body, Evas_Object *evas_obj, Eina_B
668 } 690 }
669 691
670 if (body->evas_obj) 692 if (body->evas_obj)
671 evas_object_event_callback_del(body->evas_obj, EVAS_CALLBACK_DEL, 693 {
672 _ephysics_body_evas_obj_del_cb); 694 evas_object_event_callback_del(body->evas_obj, EVAS_CALLBACK_DEL,
695 _ephysics_body_evas_obj_del_cb);
696 evas_object_event_callback_del(body->evas_obj, EVAS_CALLBACK_RESIZE,
697 _ephysics_body_evas_obj_resize_cb);
698 }
673 699
674 body->evas_obj = evas_obj; 700 body->evas_obj = evas_obj;
675 evas_object_event_callback_add(evas_obj, EVAS_CALLBACK_DEL, 701 evas_object_event_callback_add(evas_obj, EVAS_CALLBACK_DEL,
@@ -678,6 +704,8 @@ ephysics_body_evas_object_set(EPhysics_Body *body, Evas_Object *evas_obj, Eina_B
678 if (!use_obj_pos) 704 if (!use_obj_pos)
679 return; 705 return;
680 706
707 evas_object_event_callback_add(evas_obj, EVAS_CALLBACK_RESIZE,
708 _ephysics_body_evas_obj_resize_cb, body);
681 evas_object_geometry_get(body->evas_obj, &obj_x, &obj_y, &obj_w, &obj_h); 709 evas_object_geometry_get(body->evas_obj, &obj_x, &obj_y, &obj_w, &obj_h);
682 _ephysics_body_geometry_set(body, obj_x, obj_y, obj_w, obj_h); 710 _ephysics_body_geometry_set(body, obj_x, obj_y, obj_w, obj_h);
683} 711}
@@ -696,8 +724,12 @@ ephysics_body_evas_object_unset(EPhysics_Body *body)
696 body->evas_obj = NULL; 724 body->evas_obj = NULL;
697 725
698 if (obj) 726 if (obj)
699 evas_object_event_callback_del(obj, EVAS_CALLBACK_DEL, 727 {
700 _ephysics_body_evas_obj_del_cb); 728 evas_object_event_callback_del(obj, EVAS_CALLBACK_DEL,
729 _ephysics_body_evas_obj_del_cb);
730 evas_object_event_callback_del(obj, EVAS_CALLBACK_RESIZE,
731 _ephysics_body_evas_obj_resize_cb);
732 }
701 733
702 return obj; 734 return obj;
703} 735}
@@ -756,8 +788,8 @@ ephysics_body_geometry_get(const EPhysics_Body *body, Evas_Coord *x, Evas_Coord
756 if (x) *x = round((trans.getOrigin().getX() - vector.x() / 2) * rate); 788 if (x) *x = round((trans.getOrigin().getX() - vector.x() / 2) * rate);
757 if (y) *y = height - round((trans.getOrigin().getY() + vector.y() / 2) 789 if (y) *y = height - round((trans.getOrigin().getY() + vector.y() / 2)
758 * rate); 790 * rate);
759 if (w) *w = round(vector.x() * rate); 791 if (w) *w = body->w;
760 if (h) *h = round(vector.y() * rate); 792 if (h) *h = body->h;
761} 793}
762 794
763EAPI void 795EAPI void
diff --git a/legacy/ephysics/src/lib/ephysics_private.h b/legacy/ephysics/src/lib/ephysics_private.h
index b878533740..01d0a5e261 100644
--- a/legacy/ephysics/src/lib/ephysics_private.h
+++ b/legacy/ephysics/src/lib/ephysics_private.h
@@ -68,6 +68,8 @@ struct _EPhysics_Body {
68 Evas_Object *evas_obj; 68 Evas_Object *evas_obj;
69 EPhysics_World *world; 69 EPhysics_World *world;
70 int walking; 70 int walking;
71 Evas_Coord w;
72 Evas_Coord h;
71 void *data; 73 void *data;
72 Eina_Inlist *callbacks; 74 Eina_Inlist *callbacks;
73 Eina_List *collision_groups; 75 Eina_List *collision_groups;