summaryrefslogtreecommitdiff
path: root/src/lib/ephysics/ephysics_camera.cpp
diff options
context:
space:
mode:
authorGustavo Sverzut Barbieri <barbieri@gmail.com>2013-01-03 22:10:40 +0000
committerGustavo Sverzut Barbieri <barbieri@gmail.com>2013-01-03 22:10:40 +0000
commit9e0788cc2e261ec66cf740e35a3e71f4485df763 (patch)
tree8c1230e446edd6966d39c1b8959c7f97f5c0f731 /src/lib/ephysics/ephysics_camera.cpp
parent9edec477ebb83e64f3e464d82665e2b9f01f9bb0 (diff)
efl: merge ephysics
changes: * __UNUSED__ -> EINA_UNUSED * Fixed doc hierarchy SVN revision: 82126
Diffstat (limited to 'src/lib/ephysics/ephysics_camera.cpp')
-rw-r--r--src/lib/ephysics/ephysics_camera.cpp276
1 files changed, 276 insertions, 0 deletions
diff --git a/src/lib/ephysics/ephysics_camera.cpp b/src/lib/ephysics/ephysics_camera.cpp
new file mode 100644
index 0000000000..9ca2052f92
--- /dev/null
+++ b/src/lib/ephysics/ephysics_camera.cpp
@@ -0,0 +1,276 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include <Evas.h>
6
7#include "ephysics_private.h"
8
9#ifdef __cplusplus
10extern "C" {
11#endif
12
13struct _EPhysics_Camera {
14 EPhysics_World *world;
15 EPhysics_Body *target;
16 int x, y;
17 struct {
18 Evas_Coord px;
19 Evas_Coord py;
20 Evas_Coord z0;
21 Evas_Coord foc;
22 Eina_Bool enabled:1;
23 } perspective;
24 Eina_Bool track_horizontal:1;
25 Eina_Bool track_vertical:1;
26 Eina_Bool moved:1;
27};
28
29static void
30_ephysics_camera_target_del_cb(void *data, EPhysics_Body *body, void *event_info EINA_UNUSED)
31{
32 EPhysics_Camera *camera = (EPhysics_Camera *) data;
33
34 camera->target = NULL;
35 camera->track_horizontal = EINA_FALSE;
36 camera->track_vertical = EINA_FALSE;
37
38 INF("Camera isn't tracking body %p anymore.", body);
39}
40
41void
42ephysics_camera_target_moved(EPhysics_Camera *camera, EPhysics_Body *body)
43{
44 int x, y, w, h, ww, wh, new_x, new_y;
45
46 ephysics_body_geometry_get(body, &x, &y, NULL, &w, &h, NULL);
47 ephysics_world_render_geometry_get(camera->world, NULL, NULL, NULL,
48 &ww, &wh, NULL);
49
50 if (camera->track_horizontal)
51 {
52 new_x = x + w / 2 - ww / 2;
53 if (camera->x != new_x)
54 {
55 camera->x = new_x;
56 camera->moved = EINA_TRUE;
57 }
58 }
59
60 if (camera->track_vertical)
61 {
62 new_y = y + h / 2 - wh / 2;
63 if (camera->y != new_y)
64 {
65 camera->y = new_y;
66 camera->moved = EINA_TRUE;
67 }
68 }
69
70 INF("Camera position set to (%i, %i).", camera->x, camera->y);
71}
72
73void
74ephysics_camera_moved_set(EPhysics_Camera *camera, Eina_Bool moved)
75{
76 camera->moved = moved;
77}
78
79Eina_Bool
80ephysics_camera_moved_get(const EPhysics_Camera *camera)
81{
82 return camera->moved;
83}
84
85EPhysics_Camera *
86ephysics_camera_add(EPhysics_World *world)
87{
88 EPhysics_Camera *camera;
89
90 camera = (EPhysics_Camera *) calloc(1, sizeof(EPhysics_Camera));
91 if (!camera)
92 {
93 ERR("Couldn't create a new camera instance.");
94 return NULL;
95 }
96
97 camera->world = world;
98
99 INF("Camera created.");
100 return camera;
101}
102
103void
104ephysics_camera_del(EPhysics_Camera *camera)
105{
106 if (!camera)
107 {
108 ERR("Can't delete camera, it wasn't provided.");
109 return;
110 }
111
112 free(camera);
113 INF("Camera deleted.");
114}
115
116EAPI void
117ephysics_camera_position_set(EPhysics_Camera *camera, Evas_Coord x, Evas_Coord y)
118{
119 if (!camera)
120 {
121 ERR("Can't set camera position, camerar is null.");
122 return;
123 }
124
125 if (camera->target)
126 {
127 INF("Camera isn't tracking body %p anymore.", camera->target);
128
129 ephysics_body_event_callback_del(camera->target,
130 EPHYSICS_CALLBACK_BODY_DEL,
131 _ephysics_camera_target_del_cb);
132 camera->target = NULL;
133 camera->track_horizontal = EINA_FALSE;
134 camera->track_vertical = EINA_FALSE;
135 }
136
137 camera->x = x;
138 camera->y = y;
139 camera->moved = EINA_TRUE;
140
141 INF("Camera position set to (%i, %i).", x, y);
142}
143
144EAPI void
145ephysics_camera_position_get(const EPhysics_Camera *camera, Evas_Coord *x, Evas_Coord *y)
146{
147 if (!camera)
148 {
149 ERR("Can't get camera position, camera is null.");
150 return;
151 }
152
153 if (x) *x = camera->x;
154 if (y) *y = camera->y;
155}
156
157EAPI void
158ephysics_camera_body_track(EPhysics_Camera *camera, EPhysics_Body *body, Eina_Bool horizontal, Eina_Bool vertical)
159{
160 if (!camera)
161 {
162 ERR("Camera can't track body, camera is null.");
163 return;
164 }
165
166 camera->track_horizontal = !!horizontal;
167 camera->track_vertical = !!vertical;
168
169 if ((body) && (camera->target == body))
170 {
171 INF("Camera already tracking body %p.", body);
172 INF("Camera tracking: hor = %i, ver = %i.", !!horizontal, !!vertical);
173 return;
174 }
175
176 if (camera->target)
177 {
178 ephysics_body_event_callback_del(camera->target,
179 EPHYSICS_CALLBACK_BODY_DEL,
180 _ephysics_camera_target_del_cb);
181 }
182
183 camera->target = body;
184
185 if (!body)
186 {
187 INF("Camera isn't tracking any body.");
188 return;
189 }
190
191 ephysics_body_event_callback_add(body, EPHYSICS_CALLBACK_BODY_DEL,
192 _ephysics_camera_target_del_cb, camera);
193
194 INF("Camera is tracking body %p: hor = %i, ver = %i.", body,
195 camera->track_horizontal, camera->track_vertical);
196}
197
198EAPI void
199ephysics_camera_tracked_body_get(EPhysics_Camera *camera, EPhysics_Body **body, Eina_Bool *horizontal, Eina_Bool *vertical)
200{
201 if (!camera)
202 {
203 ERR("Can't get tracked body, camera is null.");
204 return;
205 }
206
207 if (body) *body = camera->target;
208 if (horizontal) *horizontal = camera->track_horizontal;
209 if (vertical) *vertical = camera->track_vertical;
210}
211
212EAPI void
213ephysics_camera_perspective_set(EPhysics_Camera *camera, Evas_Coord px, Evas_Coord py, Evas_Coord z0, Evas_Coord foc)
214{
215 if (!camera)
216 {
217 ERR("No camera, can't set perspective.");
218 return;
219 }
220
221 if (foc <= 0)
222 {
223 ERR("Focal distance need to be greater than 0.");
224 return;
225 }
226
227 camera->perspective.px = px;
228 camera->perspective.py = py;
229 camera->perspective.z0 = z0;
230 camera->perspective.foc = foc;
231 ephysics_world_force_update_set(camera->world, EINA_TRUE);
232}
233
234EAPI void
235ephysics_camera_perspective_get(const EPhysics_Camera *camera, Evas_Coord *px, Evas_Coord *py, Evas_Coord *z0, Evas_Coord *foc)
236{
237 if (!camera)
238 {
239 ERR("No camera, can't get perspective.");
240 return;
241 }
242
243 if (px) *px = camera->perspective.px;
244 if (py) *py = camera->perspective.py;
245 if (z0) *z0 = camera->perspective.z0;
246 if (foc) *foc = camera->perspective.foc;
247}
248
249EAPI void
250ephysics_camera_perspective_enabled_set(EPhysics_Camera *camera, Eina_Bool enabled)
251{
252 if (!camera)
253 {
254 ERR("No camera, can't enable / disable perspective.");
255 return;
256 }
257
258 camera->perspective.enabled = !!enabled;
259 ephysics_world_force_update_set(camera->world, EINA_TRUE);
260}
261
262EAPI Eina_Bool
263ephysics_camera_perspective_enabled_get(const EPhysics_Camera *camera)
264{
265 if (!camera)
266 {
267 ERR("No camera, can't get perspective behavior.");
268 return EINA_FALSE;
269 }
270
271 return camera->perspective.enabled;
272}
273
274#ifdef __cplusplus
275}
276#endif