From b007921b94bcf7627771a1ce95a29e8bd297feff Mon Sep 17 00:00:00 2001 From: Bruno Dilly Date: Thu, 16 Aug 2012 21:35:25 +0000 Subject: [PATCH] EPhysics: add test for windows resize It listen for windows resize and changes objects size, world render geometry and world rate. SVN revision: 75336 --- legacy/ephysics/data/themes/frame.edc | 3 - legacy/ephysics/src/bin/Makefile.am | 3 +- legacy/ephysics/src/bin/test.c | 3 +- legacy/ephysics/src/bin/test_win_resize.c | 198 ++++++++++++++++++++++ 4 files changed, 202 insertions(+), 5 deletions(-) create mode 100644 legacy/ephysics/src/bin/test_win_resize.c diff --git a/legacy/ephysics/data/themes/frame.edc b/legacy/ephysics/data/themes/frame.edc index 4605e57939..9a663a9e63 100644 --- a/legacy/ephysics/data/themes/frame.edc +++ b/legacy/ephysics/data/themes/frame.edc @@ -14,7 +14,6 @@ description { state: "default" 0.0; min: 750 224; - max: 750 224; image.normal: "bg_floor.jpg"; } } @@ -40,7 +39,6 @@ description { state: "default" 0.0; min: 512 384; - max: 512 384; color: 153 134 117 255; } } @@ -52,7 +50,6 @@ description { state: "default" 0.0; min: 512 384; - max: 512 384; image.normal: "background.jpg"; visible: 0; } diff --git a/legacy/ephysics/src/bin/Makefile.am b/legacy/ephysics/src/bin/Makefile.am index 12538f8332..1ebd3d6a8e 100644 --- a/legacy/ephysics/src/bin/Makefile.am +++ b/legacy/ephysics/src/bin/Makefile.am @@ -34,7 +34,8 @@ test_rotate.c \ test_velocity.c \ test_shapes.c \ test_sleeping_threshold.c \ -test_slider.c +test_slider.c \ +test_win_resize.c ephysics_logo_SOURCES = \ ephysics_logo.c diff --git a/legacy/ephysics/src/bin/test.c b/legacy/ephysics/src/bin/test.c index 6971894368..d3bad043e4 100644 --- a/legacy/ephysics/src/bin/test.c +++ b/legacy/ephysics/src/bin/test.c @@ -34,6 +34,7 @@ void test_velocity(void *data, Evas_Object *obj, void *event_info); void test_shapes(void *data, Evas_Object *obj, void *event_info); void test_sleeping(void *data, Evas_Object *obj, void *event_info); void test_slider(void *data, Evas_Object *obj, void *event_info); +void test_win_resize(void *data, Evas_Object *obj, void *event_info); static const EPhysics_Test tests[] = { {"Bouncing Ball", test_bouncing_ball}, @@ -55,6 +56,7 @@ static const EPhysics_Test tests[] = { {"Shapes", test_shapes}, {"Sleeping Threshold", test_sleeping}, {"Slider", test_slider}, + {"Win Resize", test_win_resize}, }; static void @@ -144,7 +146,6 @@ test_win_add(Test_Data *test_data, const char *title, Eina_Bool autodel) win = elm_win_add(NULL, title, ELM_WIN_BASIC); elm_win_title_set(win, title); elm_win_autodel_set(win, EINA_TRUE); - elm_win_screen_constrain_set(win, EINA_TRUE); evas_object_show(win); test_data->win = win; if (autodel) diff --git a/legacy/ephysics/src/bin/test_win_resize.c b/legacy/ephysics/src/bin/test_win_resize.c new file mode 100644 index 0000000000..9b93b8b110 --- /dev/null +++ b/legacy/ephysics/src/bin/test_win_resize.c @@ -0,0 +1,198 @@ +#ifdef HAVE_CONFIG_H +# include +#endif + +#include "ephysics_test.h" + +typedef struct _Old_Rate Old_Rate; +struct _Old_Rate { + EPhysics_Body *body; + double w; + double h; +}; + +static void +_del_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__) +{ + free(data); +} + +static void +_win_resize_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__) +{ + Test_Data *test_data = data; + EPhysics_Body *sphere_body; + int ww, wh, x, y, w, h; + double ratew, rateh; + Evas_Object *sphere; + Old_Rate *old_rate; + + evas_object_geometry_get(obj, NULL, NULL, &ww, &wh); + ratew = (double) ww / WIDTH; + rateh = (double) wh / HEIGHT; + + /* Since we don't support different rates on ephysics world, we + should make sure window keeps its aspect. + Maybe if we decide it should be supported someday, + this example could be modified just dropping the following check. */ + if (ratew > rateh) + { + ww = rateh * WIDTH; + evas_object_resize(obj, ww, wh); + return; + } + else if (ratew < rateh) + { + wh = ratew * HEIGHT; + evas_object_resize(obj, ww, wh); + return; + } + + evas_object_resize(test_data->layout, ww, wh); + ephysics_world_rate_set(test_data->world, 30 * rateh); + ephysics_world_render_geometry_set(test_data->world, 50 * ratew, 40 *rateh, + (WIDTH - 100) * ratew, + (FLOOR_Y - 40) * rateh); + + old_rate = test_data->data; + if (!old_rate) return; + sphere_body = old_rate->body; + + sphere = ephysics_body_evas_object_get(sphere_body); + evas_object_geometry_get(sphere, &x, &y, &w, &h); + + /* FIXME: add resize / move to API */ + ephysics_body_geometry_set(sphere_body, x * (ratew / old_rate->w), + y * (rateh / old_rate->h), 1, 1); + evas_object_resize(sphere, 70 * ratew, 70 * rateh); + + old_rate->w = ratew; + old_rate->h = rateh; +} + +static void +_update_object_cb(void *data, EPhysics_Body *body, void *event_info) +{ + Evas_Object *sphere = event_info; + Evas_Object *shadow = data; + Edje_Message_Float msg; + Old_Rate *old_rate; + Evas_Object *edje; + int x, y, w, h; + + ephysics_body_evas_object_update(body); + old_rate = ephysics_body_data_get(body); + evas_object_geometry_get(sphere, &x, &y, &w, &h); + + if (y > SH_THRESHOLD * old_rate->h) + { + int sh_w; + + msg.val = (double) (y - SH_THRESHOLD * old_rate->h) / + ((FLOOR_Y - SH_THRESHOLD) * old_rate->h - h); + sh_w = (1 + 2 * msg.val) * w / 3; + evas_object_resize(shadow, sh_w, 3 * old_rate->h); + evas_object_move(shadow, x + (w - sh_w) / 2, FLOOR_Y * old_rate->h); + } + else + msg.val = 0; + + edje = elm_layout_edje_get(shadow); + edje_object_message_send(edje, EDJE_MESSAGE_FLOAT, SHADOW_ALPHA_ID, &msg); +} + +static void +_world_populate(Test_Data *test_data) +{ + Evas_Object *sphere, *shadow; + EPhysics_Body *sphere_body; + Old_Rate *old_rate; + int w, h; + + old_rate = test_data->data; + if (!test_data->data) + old_rate = calloc(1, sizeof(Old_Rate)); + if (!old_rate) return; + test_data->data = old_rate; + + evas_object_geometry_get(test_data->win, NULL, NULL, &w, &h); + old_rate->w = (double) w / WIDTH; + old_rate->h = (double) h / HEIGHT; + + shadow = elm_layout_add(test_data->win); + elm_layout_file_set( + shadow, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "shadow-ball"); + evas_object_move(shadow, WIDTH / 3 * old_rate->w, FLOOR_Y * old_rate->h); + evas_object_resize(shadow, 70 * old_rate->w, 3 * old_rate->h); + evas_object_show(shadow); + test_data->evas_objs = eina_list_append(test_data->evas_objs, shadow); + + sphere = elm_image_add(test_data->win); + elm_image_file_set( + sphere, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "big-blue-ball"); + evas_object_move(sphere, WIDTH / 3 * old_rate->w, HEIGHT / 2 * old_rate->h); + evas_object_resize(sphere, 70 * old_rate->w, 70 * old_rate->h); + 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, 1.0); + ephysics_body_event_callback_add(sphere_body, EPHYSICS_CALLBACK_BODY_UPDATE, + _update_object_cb, shadow); + test_data->bodies = eina_list_append(test_data->bodies, sphere_body); + old_rate->body = sphere_body; + ephysics_body_data_set(sphere_body, old_rate); +} + +static void +_restart(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__) +{ + Test_Data *test_data = data; + + DBG("Restart pressed"); + test_clean(test_data); + _world_populate(test_data); +} + +void +test_win_resize(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, "Win Resize", EINA_TRUE); + evas_object_event_callback_add(test_data->win, EVAS_CALLBACK_RESIZE, + _win_resize_cb, test_data); + + elm_layout_signal_callback_add(test_data->layout, "restart", "test-theme", + _restart, test_data); + elm_object_signal_emit(test_data->layout, "borders,show", "ephysics_test"); + evas_object_size_hint_weight_set(test_data->layout, EVAS_HINT_EXPAND, + EVAS_HINT_EXPAND); + elm_win_aspect_set(test_data->win, (double) WIDTH / HEIGHT); + evas_object_smart_callback_add(test_data->win, "delete,request", _del_cb, + test_data->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, 1.0); + + boundary = ephysics_body_right_boundary_add(test_data->world); + ephysics_body_restitution_set(boundary, 1.0); + + boundary = ephysics_body_left_boundary_add(test_data->world); + ephysics_body_restitution_set(boundary, 1.0); + + ephysics_body_top_boundary_add(test_data->world); + + _world_populate(test_data); +}