summaryrefslogtreecommitdiff
path: root/legacy/ephysics/src/bin/test.c
blob: 50720405d87c2c5416ae00bfba7c0c3d8fe2552c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include "ephysics_test.h"

#define EPHYSICS_TEST_LOG_COLOR EINA_COLOR_GREEN

int _ephysics_test_log_dom = -1;

typedef struct _EPhysics_Test EPhysics_Test;
struct _EPhysics_Test {
     const char *name;
     void (*func)(void *data, Evas_Object *obj, void *event_info);
};

/* examples prototypes */
void test_bouncing_ball(void *data, Evas_Object *obj, void *event_info);
void test_bouncing_text(void *data, Evas_Object *obj, void *event_info);
void test_camera(void *data, Evas_Object *obj, void *event_info);
void test_camera_track(void *data, Evas_Object *obj, void *event_info);
void test_colliding_balls(void *data, Evas_Object *obj, void *event_info);
void test_collision(void *data, Evas_Object *obj, void *event_info);
void test_collision_filter(void *data, Evas_Object *obj, void *event_info);
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_flag(void *data, Evas_Object *obj, void *event_info);
void test_forces(void *data, Evas_Object *obj, void *event_info);
void test_grab(void *data, Evas_Object *obj, void *event_info);
void test_growing_balls(void *data, Evas_Object *obj, void *event_info);
void test_heavy(void *data, Evas_Object *obj, void *event_info);
void test_jumping_balls(void *data, Evas_Object *obj, void *event_info);
void test_material(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);
void test_rotating_forever(void *data, Evas_Object *obj, void *event_info);
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_soft_body(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},
       {"Bouncing Text", test_bouncing_text},
       {"Camera", test_camera},
       {"Camera Track", test_camera_track},
       {"Colliding Balls", test_colliding_balls},
       {"Collision Detection", test_collision},
       {"Collision Filter", test_collision_filter},
       {"Collision High Speed", test_collision_speed},
       {"Constraint", test_constraint},
       {"Delete Body", test_delete},
       {"Falling Letters", test_falling_letters},
       {"Flag - Cloth", test_flag},
       {"Forces", test_forces},
       {"Grab", test_grab},
       {"Growing Balls", test_growing_balls},
       {"Heavy", test_heavy},
       {"Jumping Balls", test_jumping_balls},
       {"Material", test_material},
       {"No Gravity", test_no_gravity},
       {"Rotate", test_rotate},
       {"Rotating Forever", test_rotating_forever},
       {"Velocity", test_velocity},
       {"Shapes", test_shapes},
       {"Sleeping Threshold", test_sleeping},
       {"Slider", test_slider},
       {"Soft Body", test_soft_body},
       {"Win Resize", test_win_resize},
};

static void
_win_del(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
{
   elm_exit();
}

static void
_subwin_del_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
{
   test_data_del(data);
   ephysics_shutdown();
}

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;
   Evas_Object *edje;
   int x, y, w, h;

   ephysics_body_evas_object_update(body);
   evas_object_geometry_get(sphere, &x, &y, &w, &h);

   if (y > SH_THRESHOLD)
     {
        int sh_w;

        msg.val = (double) (y - SH_THRESHOLD) / (FLOOR_Y - SH_THRESHOLD - h);
        sh_w = (1 + 2 * msg.val) * w / 3;
        evas_object_resize(shadow, sh_w, 3);
        evas_object_move(shadow, x + (w - sh_w) / 2, FLOOR_Y);
     }
   else
        msg.val = 0;

   edje = elm_layout_edje_get(shadow);
   edje_object_message_send(edje, EDJE_MESSAGE_FLOAT, SHADOW_ALPHA_ID, &msg);
}

void
test_clean(Test_Data *test_data)
{
   EPhysics_Constraint *constraint;
   Evas_Object *evas_obj;
   EPhysics_Body *body;

   EINA_LIST_FREE(test_data->constraints, constraint)
      ephysics_constraint_del(constraint);

   EINA_LIST_FREE(test_data->bodies, body)
      ephysics_body_del(body);

   EINA_LIST_FREE(test_data->evas_objs, evas_obj)
      evas_object_del(evas_obj);
}

void
test_data_del(Test_Data *test_data)
{
   Evas_Object *evas_obj;
   EINA_LIST_FREE(test_data->evas_objs, evas_obj)
      evas_object_del(evas_obj);
   evas_object_del(test_data->layout);
   ephysics_world_del(test_data->world);
   free(test_data);
}

Test_Data *
test_data_new(void)
{
   Test_Data *test_data;

   test_data = calloc(1, sizeof(Test_Data));
   if (!test_data)
     ERR("Failed to create test data");

   return test_data;
}

void
test_win_add(Test_Data *test_data, const char *title, Eina_Bool autodel)
{
   Evas_Object *win, *ly;

   win = elm_win_add(NULL, title, ELM_WIN_BASIC);
   elm_win_title_set(win, title);
   elm_win_autodel_set(win, EINA_TRUE);
   evas_object_show(win);
   test_data->win = win;
   if (autodel)
     evas_object_event_callback_add(win, EVAS_CALLBACK_DEL,
                                    _subwin_del_cb, test_data);

   ly = elm_layout_add(win);
   elm_win_resize_object_add(win, ly);
   evas_object_size_hint_min_set(ly, WIDTH, HEIGHT);
   evas_object_size_hint_max_set(ly, WIDTH, HEIGHT);
   elm_layout_file_set(ly, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj",
                       "frame");
   evas_object_show(ly);
   test_data->layout = ly;
}

static void
_test_run(char *autorun)
{
   unsigned int i;

   for (i = 0; i < EINA_C_ARRAY_LENGTH(tests); i++)
     {
        EPhysics_Test test = tests[i];
        if (!strcmp(test.name, autorun))
          {
             test.func(NULL, NULL, NULL);
             return;
          }
     }
}

static void
_main_win_add(char *autorun)
{
   Elm_Object_Item *selected = NULL;
   Evas_Object *win, *list;
   unsigned int i;

   win = elm_win_add(NULL, "main", ELM_WIN_BASIC);
   elm_win_title_set(win, "EPhysics Tests");
   evas_object_smart_callback_add(win, "delete,request", _win_del, NULL);
   elm_win_autodel_set(win, EINA_TRUE);
   evas_object_resize(win, 460, 560);
   evas_object_show(win);

   list = elm_list_add(win);
   elm_object_style_set(list, "ephysics-test");
   elm_list_select_mode_set(list, ELM_OBJECT_SELECT_MODE_ALWAYS);
   evas_object_size_hint_weight_set(list, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
   elm_win_resize_object_add(win, list);
   evas_object_show(list);

   for (i = 0; i < EINA_C_ARRAY_LENGTH(tests); i++)
     {
        Elm_Object_Item *item;
        EPhysics_Test test;

        test = tests[i];
        item = elm_list_item_append(list, test.name, NULL, NULL, test.func,
                                    NULL);

        if ((autorun) && (!strcmp(test.name, autorun)))
          selected = item;
     }

   elm_list_go(list);

   if (selected)
     elm_list_item_bring_in(selected);

   if (autorun)
     _test_run(autorun);
}

EAPI_MAIN int
elm_main(int argc, char **argv)
{
   Eina_Bool test_win_only = EINA_FALSE;
   char *autorun = NULL;

   _ephysics_test_log_dom = eina_log_domain_register(
      "ephysics-test", EPHYSICS_TEST_LOG_COLOR);
   if (_ephysics_test_log_dom < 0)
     {
        EINA_LOG_CRIT("Could not register log domain: ephysics-test");
        return -1;
     }

   elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
   elm_policy_set(ELM_POLICY_EXIT, ELM_POLICY_EXIT_WINDOWS_DEL);

   elm_app_compile_bin_dir_set(PACKAGE_BIN_DIR);
   elm_app_compile_data_dir_set(PACKAGE_DATA_DIR);

   /* if called with a single argument try to autorun a test with
    * the same name as the given param
    * ex: ephysics_test "Jumping Balls" */
   if (argc == 2)
     autorun = argv[1];
   else if (argc == 3)
     {
        if ((!strcmp(argv[1], "--test-win-only")) ||
            (!strcmp(argv[1], "-to")))
          {
             test_win_only = EINA_TRUE;
             autorun = argv[2];
          }
     }

   elm_theme_extension_add(NULL,
                           PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj");

   if (test_win_only)
       _test_run(autorun);
   else
       _main_win_add(autorun);

   elm_run();

   eina_log_domain_unregister(_ephysics_test_log_dom);
   _ephysics_test_log_dom = -1;

   elm_shutdown();
   return 0;
}
ELM_MAIN()