Bump version to 24.04.3.4
[LibreOffice.git] / slideshow / source / inc / box2dtools.hxx
blobe876468df7e63558a312e526e095982ae26be922
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #pragma once
12 #include "shape.hxx"
13 #include "shapeattributelayer.hxx"
14 #include "attributemap.hxx"
15 #include <unordered_map>
16 #include <queue>
18 class b2Body;
19 class b2World;
21 namespace slideshow::internal
23 class ShapeManager;
24 typedef std::shared_ptr<ShapeManager> ShapeManagerSharedPtr;
27 namespace box2d::utils
29 class box2DBody;
30 class box2DWorld;
31 typedef std::shared_ptr<box2DWorld> Box2DWorldSharedPtr;
32 typedef std::shared_ptr<box2DBody> Box2DBodySharedPtr;
34 enum box2DBodyType
36 BOX2D_STATIC_BODY = 0,
37 BOX2D_KINEMATIC_BODY,
38 BOX2D_DYNAMIC_BODY
41 enum box2DNonsimulatedShapeUpdateType
43 BOX2D_UPDATE_POSITION_CHANGE,
44 BOX2D_UPDATE_POSITION,
45 BOX2D_UPDATE_ANGLE,
46 BOX2D_UPDATE_SIZE,
47 BOX2D_UPDATE_VISIBILITY,
48 BOX2D_UPDATE_LINEAR_VELOCITY,
49 BOX2D_UPDATE_ANGULAR_VELOCITY
52 /// Holds required information to perform an update to box2d
53 /// body of a shape that was altered by an animation effect
54 struct Box2DDynamicUpdateInformation
56 /// reference to the shape that the update belongs to
57 css::uno::Reference<css::drawing::XShape> mxShape;
58 union {
59 ::basegfx::B2DPoint maPosition;
60 ::basegfx::B2DVector maVelocity;
61 double mfAngle;
62 double mfAngularVelocity;
63 bool mbVisibility;
65 box2DNonsimulatedShapeUpdateType meUpdateType;
66 /// amount of steps to delay the update for
67 int mnDelayForSteps = 0;
70 /** Class that manages the Box2D World
72 This class is used when there's a physics animation going on,
73 it handles the stepping through the box2d world, updating the
74 shapes in the box2d world if they were changed by ongoing animations.
76 class box2DWorld
78 private:
79 /// Pointer to the real Box2D World that this class manages
80 std::unique_ptr<b2World> mpBox2DWorld;
81 /// Scale factor for conversions between LO user space coordinates to Box2D World coordinates
82 double mfScaleFactor;
83 bool mbShapesInitialized;
84 /// Holds whether or not there is a PhysicsAnimation that
85 /// is stepping the Box2D World. Used to create a lock mechanism
86 bool mbHasWorldStepper;
87 /// Flag used to stop overstepping that occurs when a physics
88 /// animation effect transfers step-lock to another one.
89 bool mbAlreadyStepped;
90 /// Number of Physics Animations going on
91 int mnPhysicsAnimationCounter;
92 std::unordered_map<css::uno::Reference<css::drawing::XShape>, Box2DBodySharedPtr>
93 mpXShapeToBodyMap;
95 /** Queue that holds any required information to keep LO animation effects
96 and Box2DWorld in sync
98 Is processed before every step of the box2d world by processUpdateQueue.
99 Holds position, rotation, visibility etc. changes and associated values.
101 std::queue<Box2DDynamicUpdateInformation> maShapeParallelUpdateQueue;
103 /// Creates a static frame in Box2D world that corresponds to the slide borders
104 void createStaticFrameAroundSlide(const ::basegfx::B2DVector& rSlideSize);
106 /** Sets shape's corresponding Box2D body to the specified position
108 Body is teleported to the specified position, not moved
110 @param xShape
111 Shape reference
113 @param rOutPos
114 Position in LO user space coordinates
116 void setShapePosition(const css::uno::Reference<css::drawing::XShape> xShape,
117 const ::basegfx::B2DPoint& rOutPos);
119 /** Moves shape's corresponding Box2D body to specified position
121 Moves shape's corresponding Box2D body to specified position as if
122 the body had velocity to reach that point in given time frame.
124 @param xShape
125 Shape reference
127 @param rOutPos
128 Position in LO user space coordinates
130 @param fPassedTime
131 Time frame which the Box2D body should move to the specified position.
133 void setShapePositionByLinearVelocity(const css::uno::Reference<css::drawing::XShape> xShape,
134 const ::basegfx::B2DPoint& rOutPos,
135 const double fPassedTime);
137 /** Sets linear velocity of the shape's corresponding Box2D body
139 Moves shape's corresponding Box2D body to specified position as if
140 the body had velocity to reach that point in given time frame.
142 @param xShape
143 Shape reference
145 @param rVelocity
146 Velocity vector in LO user space coordinates.
148 void setShapeLinearVelocity(const css::uno::Reference<com::sun::star::drawing::XShape> xShape,
149 const basegfx::B2DVector& rVelocity);
151 /** Sets rotation angle of the shape's corresponding Box2D body
153 @param xShape
154 Shape reference
156 @param fAngle
157 Angle of rotation in degrees.
159 void setShapeAngle(const css::uno::Reference<com::sun::star::drawing::XShape> xShape,
160 const double fAngle);
162 /** Rotates shape's corresponding Box2D body to specified angle
164 Rotates the Box2D body to specified angle as if the body
165 had exact angular velocity to reach that point in given
166 time frame
168 @param xShape
169 Shape reference
171 @param fAngle
172 Angle of rotation in degrees.
174 @param fPassedTime
175 Time frame which the Box2D body should rotate to the specified angle.
177 void setShapeAngleByAngularVelocity(
178 const css::uno::Reference<com::sun::star::drawing::XShape> xShape, const double fAngle,
179 const double fPassedTime);
181 /** Sets angular velocity of the shape's corresponding Box2D body.
183 @param xShape
184 Shape reference
186 @param fAngularVelocity
187 Angular velocity in degrees per second.
189 void setShapeAngularVelocity(const css::uno::Reference<com::sun::star::drawing::XShape> xShape,
190 const double fAngularVelocity);
192 /** Sets whether a shape's corresponding Box2D body has collision in the Box2D World or not
194 Used for animations that change the visibility of the shape.
196 @param xShape
197 Shape reference
199 @param bCanCollide
200 true if collisions should be enabled for the corresponding Box2D body of this shape
201 and false if it should be disabled.
203 void setShapeCollision(const css::uno::Reference<com::sun::star::drawing::XShape> xShape,
204 const bool bCanCollide);
206 /** Process the updates queued in the maShapeParallelUpdateQueue
208 Called on each step of the box2DWorld.
210 @param fPassedTime
211 Time frame to process the updates accordingly (needed for proper simulations)
213 void processUpdateQueue(const double fPassedTime);
215 /** Simulate and step through time in the Box2D World
217 Used in stepAmount
219 @attention fTimeStep should not vary.
221 void step(const float fTimeStep = 1.0f / 100.0f, const int nVelocityIterations = 6,
222 const int nPositionIterations = 2);
224 /// Queue a rotation update that is simulated as if shape's corresponding box2D body rotated to given angle when processed
225 void
226 queueDynamicRotationUpdate(const css::uno::Reference<com::sun::star::drawing::XShape>& xShape,
227 const double fAngle);
229 /// Queue an angular velocity update that sets the shape's corresponding box2D body angular velocity to the given value when processed
230 void
231 queueAngularVelocityUpdate(const css::uno::Reference<com::sun::star::drawing::XShape>& xShape,
232 const double fAngularVelocity, const int nDelayForSteps = 0);
234 /// Queue an collision update that sets the collision of shape's corresponding box2D body when processed
235 void queueShapeVisibilityUpdate(const css::uno::Reference<css::drawing::XShape>& xShape,
236 const bool bVisibility);
238 void queueShapePositionUpdate(const css::uno::Reference<css::drawing::XShape>& xShape,
239 const ::basegfx::B2DPoint& rOutPos);
241 public:
242 box2DWorld(const ::basegfx::B2DVector& rSlideSize);
243 ~box2DWorld();
245 bool initiateWorld(const ::basegfx::B2DVector& rSlideSize);
247 /** Simulate and step through a given amount of time in the Box2D World
249 @param fPassedTime
250 Amount of time to step through
252 @return Amount of time actually stepped through, since it is possible
253 to only step through a multiple of fTimeStep
255 @attention fTimeStep should not vary.
257 double stepAmount(const double fPassedTime, const float fTimeStep = 1.0f / 100.0f,
258 const int nVelocityIterations = 6, const int nPositionIterations = 2);
260 /// @return whether shapes in the slide are initialized as Box2D bodies or not
261 bool shapesInitialized();
262 /// @return whether the Box2D World is initialized or not
263 bool isInitialized() const;
265 /** Make the shape's corresponding box2D body a dynamic one.
267 A dynamic body will be affected by other bodies and the gravity.
269 @param xShape
270 Shape reference
272 @param rStartVelocity
273 Velocity of the shape after making it dynamic
275 @param fDensity
276 Density of the body that is in kg/m^2
278 @param fBounciness
279 Bounciness of the body that is usually in between [0,1].
280 Even though it could take values that are >1, it is way too chaotic.
282 @return box2d body pointer
284 Box2DBodySharedPtr makeShapeDynamic(const css::uno::Reference<css::drawing::XShape>& xShape,
285 const basegfx::B2DVector& rStartVelocity,
286 const double fDensity, const double fBounciness);
288 /** Make the Box2D body corresponding to the given shape a static one
290 A static body will not be affected by other bodies and the gravity. But will
291 affect other bodies that are dynamic (will still collide with them but won't
292 move etc.)
294 @param pShape
295 Pointer to the shape to alter the corresponding Box2D body of
297 @return box2d body pointer
299 Box2DBodySharedPtr makeShapeStatic(const slideshow::internal::ShapeSharedPtr& pShape);
301 /** Create a static body that is represented by the shape's geometry
303 @return pointer to the box2d body
305 Box2DBodySharedPtr createStaticBody(const slideshow::internal::ShapeSharedPtr& rShape,
306 const float fDensity = 1.0f, const float fFriction = 0.3f);
308 /// Initiate all the shapes in the current slide in the box2DWorld as static ones
309 void initiateAllShapesAsStaticBodies(
310 const slideshow::internal::ShapeManagerSharedPtr& pShapeManager);
312 /// @return whether the box2DWorld has a stepper or not
313 bool hasWorldStepper() const;
315 /// Set the flag for whether the box2DWorld has a stepper or not
316 void setHasWorldStepper(const bool bHasWorldStepper);
318 /// Queue a position update that is simulated as if shape's corresponding box2D body moved to given position when processed
319 void queueDynamicPositionUpdate(const css::uno::Reference<css::drawing::XShape>& xShape,
320 const ::basegfx::B2DPoint& rOutPos);
322 /// Queue a update that sets the corresponding box2D body's linear velocity to the given value when processed
323 void queueLinearVelocityUpdate(const css::uno::Reference<css::drawing::XShape>& xShape,
324 const ::basegfx::B2DVector& rVelocity,
325 const int nDelayForSteps = 0);
327 /// Queue an appropriate update for the animation effect that is in parallel with a physics animation
328 void
329 queueShapeAnimationUpdate(const css::uno::Reference<css::drawing::XShape>& xShape,
330 const slideshow::internal::ShapeAttributeLayerSharedPtr& pAttrLayer,
331 const slideshow::internal::AttributeType eAttrType,
332 const bool bIsFirstUpdate);
334 /// Queue an appropriate update for a path animation that is in parallel with a physics animation
335 void queueShapePathAnimationUpdate(
336 const css::uno::Reference<com::sun::star::drawing::XShape>& xShape,
337 const slideshow::internal::ShapeAttributeLayerSharedPtr& pAttrLayer,
338 const bool bIsFirstUpdate);
340 /// Queue an appropriate update for the animation effect that just ended
341 void queueShapeAnimationEndUpdate(const css::uno::Reference<css::drawing::XShape>& xShape,
342 const slideshow::internal::AttributeType eAttrType);
344 /** Alert that a physics animation effect has ended
346 Makes the given shape static, if this was the last physics animation effect
347 that was in parallel, box2d bodies that are owned by the mpXShapeToBodyMap
348 are dumped and potentially destroyed.
350 @attention the box2d body owned by the PhysicsAnimation won't be destroyed.
352 void alertPhysicsAnimationEnd(const slideshow::internal::ShapeSharedPtr& pShape);
354 /** Alert that a physics animation effect has started
356 Initiates the box2D world if it is not initiated yet, and likewise constructs
357 box2d bodies for the shapes in the current slide if they are not constructed.
359 void
360 alertPhysicsAnimationStart(const ::basegfx::B2DVector& rSlideSize,
361 const slideshow::internal::ShapeManagerSharedPtr& pShapeManager);
364 /// Class that manages a single box2D Body
365 class box2DBody
367 private:
368 /// Pointer to the body that this class manages
369 std::shared_ptr<b2Body> mpBox2DBody;
370 /// Scale factor for conversions between LO user space coordinates to Box2D World coordinates
371 double mfScaleFactor;
373 public:
374 box2DBody(std::shared_ptr<b2Body> pBox2DBody, double fScaleFactor);
376 /// @return current position in LO user space coordinates
377 ::basegfx::B2DPoint getPosition() const;
379 /** Set the position of box2d body
381 @param rPos
382 Position in LO user space coordinates
384 void setPosition(const ::basegfx::B2DPoint& rPos);
386 /** Moves body to the specified position
388 Moves body to the specified position by setting velocity of
389 the body so that it reaches to rDesiredPos in given time fram
391 @param rDesiredPos
392 Position to arrive in the time frame
394 @param fPassedTime
395 Amount of time for the movement to take place
397 void setPositionByLinearVelocity(const ::basegfx::B2DPoint& rDesiredPos,
398 const double fPassedTime);
400 /** Sets linear velocity of the body
402 @param rVelocity
403 Velocity vector in LO user space coordinates
405 void setLinearVelocity(const ::basegfx::B2DVector& rVelocity);
407 /** Rotate body to specified angle of rotation
409 Rotates body to specified rotation as if the body had
410 angular velocity to reach that state in given time frame
412 @param fDesiredAngle
413 Rotation angle in degrees to arrive in the time frame
415 @param fPassedTime
416 Amount of time for the movement to take place
418 void setAngleByAngularVelocity(const double fDesiredAngle, const double fPassedTime);
420 /** Sets angular velocity of the body
422 @param fAngularVelocity
423 Angular velocity in degrees per second
425 void setAngularVelocity(const double fAngularVelocity);
427 /// Sets whether the body have collisions or not
428 void setCollision(const bool bCanCollide);
430 /// @return current angle of rotation of the body
431 double getAngle() const;
433 /** Set angle of the box2d body
435 @param fAngle
436 Angle in degrees
438 void setAngle(const double fAngle);
440 /** Set density and restitution of the box2d body
442 @param fDensity
443 Density in kg/m^2
445 @param fRestitution
446 Restitution (elasticity) coefficient, usually in the range [0,1]
448 void setDensityAndRestitution(const double fDensity, const double fRestitution);
450 /** Set restitution of the box2d body
452 @param fRestitution
453 Restitution (elasticity) coefficient, usually in the range [0,1]
455 void setRestitution(const double fRestitution);
457 /// Set type of the body
458 void setType(box2DBodyType eType);
460 /// @return type of the body
461 box2DBodyType getType() const;
464 /** Make the Box2D body a dynamic one
466 A dynamic body will be affected by other bodies and the gravity.
468 @param pBox2DBody
469 Pointer to the Box2D body
471 Box2DBodySharedPtr makeBodyDynamic(const Box2DBodySharedPtr& pBox2DBody);
473 /** Make the Box2D body a static one
475 A static body will not be affected by other bodies and the gravity.
477 @param pBox2DBody
478 Pointer to the Box2D body
480 Box2DBodySharedPtr makeBodyStatic(const Box2DBodySharedPtr& pBox2DBody);
483 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */