From ae28b7a50482cfabe37a53fd12fa07e5b33d0c94 Mon Sep 17 00:00:00 2001 From: Bruno Dilly Date: Mon, 13 Aug 2012 22:24:47 +0000 Subject: [PATCH] EPhysics: listen for associated evas object resize event So physics body will resize together. SVN revision: 75230 --- legacy/ephysics/src/bin/Makefile.am | 1 + legacy/ephysics/src/bin/test.c | 2 + legacy/ephysics/src/bin/test_growing_balls.c | 121 +++++++++++++++++++ legacy/ephysics/src/lib/ephysics_body.cpp | 100 +++++++++------ legacy/ephysics/src/lib/ephysics_private.h | 2 + 5 files changed, 192 insertions(+), 34 deletions(-) create mode 100644 legacy/ephysics/src/bin/test_growing_balls.c 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 \ test_constraint.c \ test_delete.c \ test_falling_letters.c \ +test_growing_balls.c \ test_jumping_balls.c \ test_no_gravity.c \ test_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); void test_constraint(void *data, Evas_Object *obj, void *event_info); void test_delete(void *data, Evas_Object *obj, void *event_info); void test_falling_letters(void *data, Evas_Object *obj, void *event_info); +void test_growing_balls(void *data, Evas_Object *obj, void *event_info); void test_jumping_balls(void *data, Evas_Object *obj, void *event_info); void test_no_gravity(void *data, Evas_Object *obj, void *event_info); void 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__) ADD_TEST("CONSTRAINT", test_constraint); ADD_TEST("DELETE BODY", test_delete); ADD_TEST("FALLING LETTERS", test_falling_letters); + ADD_TEST("GROWING BALLS", test_growing_balls); ADD_TEST("JUMPING BALLS", test_jumping_balls); ADD_TEST("NO GRAVITY", test_no_gravity); 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 @@ +#ifdef HAVE_CONFIG_H +# include +#endif + +#include "ephysics_test.h" + +static Eina_Bool +_grow_cb(void *data) +{ + Test_Data *test_data = data; + Evas_Object *obj; + Eina_List *l; + int size, i = -1; + + EINA_LIST_FOREACH(test_data->evas_objs, l, obj) + { + evas_object_geometry_get(obj, NULL, NULL, &size, NULL); + size += i * 8; + i++; + + if ((size < 20) || (size > 120)) + continue; + + evas_object_resize(obj, size, size); + } + + return EINA_TRUE; +} + +static void +_add_sphere(Test_Data *test_data, const char *group, int size, int x, int y) +{ + EPhysics_Body *sphere_body; + Evas_Object *sphere; + + sphere = elm_image_add(test_data->win); + elm_image_file_set(sphere, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", + group); + evas_object_move(sphere, x, y); + evas_object_resize(sphere, size, size); + evas_object_show(sphere); + test_data->evas_objs = eina_list_append(test_data->evas_objs, sphere); + + sphere_body = ephysics_body_circle_add(test_data->world); + ephysics_body_evas_object_set(sphere_body, sphere, EINA_TRUE); + ephysics_body_restitution_set(sphere_body, 0.5); + ephysics_body_central_impulse_apply(sphere_body, 10, 0); + test_data->bodies = eina_list_append(test_data->bodies, sphere_body); +} + +static void +_world_populate(Test_Data *test_data) +{ + _add_sphere(test_data, "big-red-ball", 60, 100, 100); + _add_sphere(test_data, "big-blue-ball", 60, 150, 150); + _add_sphere(test_data, "big-green-ball", 60, 200, 200); + test_data->data = ecore_timer_add(1, _grow_cb, test_data); +} + +static void +_win_del(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__) +{ + Test_Data *test_data = data; + Ecore_Timer *timer = test_data->data; + + if (timer) + ecore_timer_del(timer); + + test_data_del(test_data); + ephysics_shutdown(); +} + +static void +_restart(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__) +{ + Test_Data *test_data = data; + Ecore_Timer *timer = test_data->data; + + if (timer) + ecore_timer_del(timer); + + DBG("Restart pressed"); + test_clean(test_data); + _world_populate(test_data); +} + +void +test_growing_balls(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__) +{ + EPhysics_Body *boundary; + EPhysics_World *world; + Test_Data *test_data; + + if (!ephysics_init()) + return; + + test_data = test_data_new(); + test_win_add(test_data, "Growing Balls", EINA_FALSE); + evas_object_smart_callback_add(test_data->win, "delete,request", _win_del, + test_data); + + elm_object_signal_emit(test_data->layout, "borders,show", "ephysics_test"); + elm_layout_signal_callback_add(test_data->layout, "restart", "test-theme", + _restart, test_data); + + world = ephysics_world_new(); + ephysics_world_render_geometry_set(world, 50, 40, WIDTH - 100, FLOOR_Y - 40); + test_data->world = world; + + boundary = ephysics_body_bottom_boundary_add(test_data->world); + ephysics_body_restitution_set(boundary, 0.8); + + boundary = ephysics_body_top_boundary_add(test_data->world); + ephysics_body_restitution_set(boundary, 0.8); + boundary = ephysics_body_left_boundary_add(test_data->world); + ephysics_body_restitution_set(boundary, 0.8); + boundary = ephysics_body_right_boundary_add(test_data->world); + ephysics_body_restitution_set(boundary, 0.8); + + _world_populate(test_data); +} 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 @@ -255,34 +255,6 @@ _ephysics_body_evas_obj_del_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj DBG("Evas object deleted. Updating body: %p", body); } -static void -_ephysics_body_del(EPhysics_Body *body) -{ - EPhysics_Body_Callback *cb; - void *group; - - if (body->evas_obj) - evas_object_event_callback_del(body->evas_obj, EVAS_CALLBACK_DEL, - _ephysics_body_evas_obj_del_cb); - - while (body->callbacks) - { - cb = EINA_INLIST_CONTAINER_GET(body->callbacks, - EPhysics_Body_Callback); - body->callbacks = eina_inlist_remove(body->callbacks, body->callbacks); - free(cb); - } - - EINA_LIST_FREE(body->collision_groups, group) - eina_stringshare_del((Eina_Stringshare *)group); - - delete body->rigid_body->getMotionState(); - delete body->collision_shape; - delete body->rigid_body; - - free(body); -} - static void _ephysics_body_geometry_set(EPhysics_Body *body, Evas_Coord x, Evas_Coord y, Evas_Coord w, Evas_Coord h) { @@ -310,10 +282,60 @@ _ephysics_body_geometry_set(EPhysics_Body *body, Evas_Coord x, Evas_Coord y, Eva body->rigid_body->getMotionState()->setWorldTransform(trans); + body->w = w; + body->h = h; + DBG("Body %p position changed to %lf, %lf.", body, mx, my); DBG("Body %p scale changed to %lf, %lf.", body, sx, sy); } +static void +_ephysics_body_evas_obj_resize_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__) +{ + EPhysics_Body *body = (EPhysics_Body *) data; + int x, y, w, h; + + evas_object_geometry_get(obj, NULL, NULL, &w, &h); + if ((w == body->w) && (h == body->h)) + return; + + DBG("Resizing body %p to w=%i, h=%i", body, w, h); + ephysics_body_geometry_get(body, &x, &y, NULL, NULL); + _ephysics_body_geometry_set(body, x, y, w, h); +} + +static void +_ephysics_body_del(EPhysics_Body *body) +{ + EPhysics_Body_Callback *cb; + void *group; + + if (body->evas_obj) + { + evas_object_event_callback_del(body->evas_obj, EVAS_CALLBACK_DEL, + _ephysics_body_evas_obj_del_cb); + evas_object_event_callback_del(body->evas_obj, EVAS_CALLBACK_RESIZE, + _ephysics_body_evas_obj_resize_cb); + } + + while (body->callbacks) + { + cb = EINA_INLIST_CONTAINER_GET(body->callbacks, + EPhysics_Body_Callback); + body->callbacks = eina_inlist_remove(body->callbacks, body->callbacks); + free(cb); + } + + EINA_LIST_FREE(body->collision_groups, group) + eina_stringshare_del((Eina_Stringshare *)group); + + delete body->rigid_body->getMotionState(); + delete body->collision_shape; + delete body->rigid_body; + + free(body); +} + static void _ephysics_body_evas_object_default_update(EPhysics_Body *body) { @@ -668,8 +690,12 @@ ephysics_body_evas_object_set(EPhysics_Body *body, Evas_Object *evas_obj, Eina_B } if (body->evas_obj) - evas_object_event_callback_del(body->evas_obj, EVAS_CALLBACK_DEL, - _ephysics_body_evas_obj_del_cb); + { + evas_object_event_callback_del(body->evas_obj, EVAS_CALLBACK_DEL, + _ephysics_body_evas_obj_del_cb); + evas_object_event_callback_del(body->evas_obj, EVAS_CALLBACK_RESIZE, + _ephysics_body_evas_obj_resize_cb); + } body->evas_obj = evas_obj; 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 if (!use_obj_pos) return; + evas_object_event_callback_add(evas_obj, EVAS_CALLBACK_RESIZE, + _ephysics_body_evas_obj_resize_cb, body); evas_object_geometry_get(body->evas_obj, &obj_x, &obj_y, &obj_w, &obj_h); _ephysics_body_geometry_set(body, obj_x, obj_y, obj_w, obj_h); } @@ -696,8 +724,12 @@ ephysics_body_evas_object_unset(EPhysics_Body *body) body->evas_obj = NULL; if (obj) - evas_object_event_callback_del(obj, EVAS_CALLBACK_DEL, - _ephysics_body_evas_obj_del_cb); + { + evas_object_event_callback_del(obj, EVAS_CALLBACK_DEL, + _ephysics_body_evas_obj_del_cb); + evas_object_event_callback_del(obj, EVAS_CALLBACK_RESIZE, + _ephysics_body_evas_obj_resize_cb); + } return obj; } @@ -756,8 +788,8 @@ ephysics_body_geometry_get(const EPhysics_Body *body, Evas_Coord *x, Evas_Coord if (x) *x = round((trans.getOrigin().getX() - vector.x() / 2) * rate); if (y) *y = height - round((trans.getOrigin().getY() + vector.y() / 2) * rate); - if (w) *w = round(vector.x() * rate); - if (h) *h = round(vector.y() * rate); + if (w) *w = body->w; + if (h) *h = body->h; } EAPI 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 { Evas_Object *evas_obj; EPhysics_World *world; int walking; + Evas_Coord w; + Evas_Coord h; void *data; Eina_Inlist *callbacks; Eina_List *collision_groups;