LibreOffice Module slideshow (master) 1
box2dtools.hxx
Go to the documentation of this file.
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.
4 *
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 */
9
10#pragma once
11
12#include "shape.hxx"
14#include "attributemap.hxx"
15#include <unordered_map>
16#include <queue>
17
18class b2Body;
19class b2World;
20
21namespace slideshow::internal
22{
23class ShapeManager;
24typedef std::shared_ptr<ShapeManager> ShapeManagerSharedPtr;
25}
26
27namespace box2d::utils
28{
29class box2DBody;
30class box2DWorld;
31typedef std::shared_ptr<box2DWorld> Box2DWorldSharedPtr;
32typedef std::shared_ptr<box2DBody> Box2DBodySharedPtr;
33
35{
39};
40
42{
50};
51
55{
57 css::uno::Reference<css::drawing::XShape> mxShape;
58 union {
61 double mfAngle;
64 };
68};
69
77{
78private:
80 std::unique_ptr<b2World> mpBox2DWorld;
92 std::unordered_map<css::uno::Reference<css::drawing::XShape>, Box2DBodySharedPtr>
94
101 std::queue<Box2DDynamicUpdateInformation> maShapeParallelUpdateQueue;
102
104 void createStaticFrameAroundSlide(const ::basegfx::B2DVector& rSlideSize);
105
116 void setShapePosition(const css::uno::Reference<css::drawing::XShape> xShape,
117 const ::basegfx::B2DPoint& rOutPos);
118
133 void setShapePositionByLinearVelocity(const css::uno::Reference<css::drawing::XShape> xShape,
134 const ::basegfx::B2DPoint& rOutPos,
135 const double fPassedTime);
136
148 void setShapeLinearVelocity(const css::uno::Reference<com::sun::star::drawing::XShape> xShape,
149 const basegfx::B2DVector& rVelocity);
150
159 void setShapeAngle(const css::uno::Reference<com::sun::star::drawing::XShape> xShape,
160 const double fAngle);
161
178 const css::uno::Reference<com::sun::star::drawing::XShape> xShape, const double fAngle,
179 const double fPassedTime);
180
189 void setShapeAngularVelocity(const css::uno::Reference<com::sun::star::drawing::XShape> xShape,
190 const double fAngularVelocity);
191
203 void setShapeCollision(const css::uno::Reference<com::sun::star::drawing::XShape> xShape,
204 const bool bCanCollide);
205
213 void processUpdateQueue(const double fPassedTime);
214
221 void step(const float fTimeStep = 1.0f / 100.0f, const int nVelocityIterations = 6,
222 const int nPositionIterations = 2);
223
225 void
226 queueDynamicRotationUpdate(const css::uno::Reference<com::sun::star::drawing::XShape>& xShape,
227 const double fAngle);
228
230 void
231 queueAngularVelocityUpdate(const css::uno::Reference<com::sun::star::drawing::XShape>& xShape,
232 const double fAngularVelocity, const int nDelayForSteps = 0);
233
235 void queueShapeVisibilityUpdate(const css::uno::Reference<css::drawing::XShape>& xShape,
236 const bool bVisibility);
237
238 void queueShapePositionUpdate(const css::uno::Reference<css::drawing::XShape>& xShape,
239 const ::basegfx::B2DPoint& rOutPos);
240
241public:
242 box2DWorld(const ::basegfx::B2DVector& rSlideSize);
244
245 bool initiateWorld(const ::basegfx::B2DVector& rSlideSize);
246
257 double stepAmount(const double fPassedTime, const float fTimeStep = 1.0f / 100.0f,
258 const int nVelocityIterations = 6, const int nPositionIterations = 2);
259
261 bool shapesInitialized();
263 bool isInitialized() const;
264
284 Box2DBodySharedPtr makeShapeDynamic(const css::uno::Reference<css::drawing::XShape>& xShape,
285 const basegfx::B2DVector& rStartVelocity,
286 const double fDensity, const double fBounciness);
287
300
306 const float fDensity = 1.0f, const float fFriction = 0.3f);
307
310 const slideshow::internal::ShapeManagerSharedPtr& pShapeManager);
311
313 bool hasWorldStepper() const;
314
316 void setHasWorldStepper(const bool bHasWorldStepper);
317
319 void queueDynamicPositionUpdate(const css::uno::Reference<css::drawing::XShape>& xShape,
320 const ::basegfx::B2DPoint& rOutPos);
321
323 void queueLinearVelocityUpdate(const css::uno::Reference<css::drawing::XShape>& xShape,
324 const ::basegfx::B2DVector& rVelocity,
325 const int nDelayForSteps = 0);
326
328 void
329 queueShapeAnimationUpdate(const css::uno::Reference<css::drawing::XShape>& xShape,
332 const bool bIsFirstUpdate);
333
336 const css::uno::Reference<com::sun::star::drawing::XShape>& xShape,
338 const bool bIsFirstUpdate);
339
341 void queueShapeAnimationEndUpdate(const css::uno::Reference<css::drawing::XShape>& xShape,
342 const slideshow::internal::AttributeType eAttrType);
343
353
359 void
360 alertPhysicsAnimationStart(const ::basegfx::B2DVector& rSlideSize,
361 const slideshow::internal::ShapeManagerSharedPtr& pShapeManager);
362};
363
366{
367private:
369 std::shared_ptr<b2Body> mpBox2DBody;
372
373public:
374 box2DBody(std::shared_ptr<b2Body> pBox2DBody, double fScaleFactor);
375
378
384 void setPosition(const ::basegfx::B2DPoint& rPos);
385
397 void setPositionByLinearVelocity(const ::basegfx::B2DPoint& rDesiredPos,
398 const double fPassedTime);
399
405 void setLinearVelocity(const ::basegfx::B2DVector& rVelocity);
406
418 void setAngleByAngularVelocity(const double fDesiredAngle, const double fPassedTime);
419
425 void setAngularVelocity(const double fAngularVelocity);
426
428 void setCollision(const bool bCanCollide);
429
431 double getAngle() const;
432
438 void setAngle(const double fAngle);
439
448 void setDensityAndRestitution(const double fDensity, const double fRestitution);
449
455 void setRestitution(const double fRestitution);
456
458 void setType(box2DBodyType eType);
459
461 box2DBodyType getType() const;
462};
463
472
481}
482
483/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
Class that manages a single box2D Body.
Definition: box2dtools.hxx:366
void setPosition(const ::basegfx::B2DPoint &rPos)
Set the position of box2d body.
Definition: box2dtools.cxx:789
std::shared_ptr< b2Body > mpBox2DBody
Pointer to the body that this class manages.
Definition: box2dtools.hxx:369
void setAngularVelocity(const double fAngularVelocity)
Sets angular velocity of the body.
Definition: box2dtools.cxx:833
void setCollision(const bool bCanCollide)
Sets whether the body have collisions or not.
Definition: box2dtools.cxx:839
void setPositionByLinearVelocity(const ::basegfx::B2DPoint &rDesiredPos, const double fPassedTime)
Moves body to the specified position.
Definition: box2dtools.cxx:795
box2DBodyType getType() const
Definition: box2dtools.cxx:891
box2DBody(std::shared_ptr< b2Body > pBox2DBody, double fScaleFactor)
Definition: box2dtools.cxx:775
void setAngle(const double fAngle)
Set angle of the box2d body.
Definition: box2dtools.cxx:859
void setLinearVelocity(const ::basegfx::B2DVector &rVelocity)
Sets linear velocity of the body.
Definition: box2dtools.cxx:826
void setType(box2DBodyType eType)
Set type of the body.
Definition: box2dtools.cxx:886
::basegfx::B2DPoint getPosition() const
Definition: box2dtools.cxx:781
double mfScaleFactor
Scale factor for conversions between LO user space coordinates to Box2D World coordinates.
Definition: box2dtools.hxx:371
void setRestitution(const double fRestitution)
Set restitution of the box2d body.
Definition: box2dtools.cxx:877
void setDensityAndRestitution(const double fDensity, const double fRestitution)
Set density and restitution of the box2d body.
Definition: box2dtools.cxx:864
double getAngle() const
Definition: box2dtools.cxx:853
void setAngleByAngularVelocity(const double fDesiredAngle, const double fPassedTime)
Rotate body to specified angle of rotation.
Definition: box2dtools.cxx:809
Class that manages the Box2D World.
Definition: box2dtools.hxx:77
void queueDynamicRotationUpdate(const css::uno::Reference< com::sun::star::drawing::XShape > &xShape, const double fAngle)
Queue a rotation update that is simulated as if shape's corresponding box2D body rotated to given ang...
Definition: box2dtools.cxx:473
double mfScaleFactor
Scale factor for conversions between LO user space coordinates to Box2D World coordinates.
Definition: box2dtools.hxx:82
void setShapeAngularVelocity(const css::uno::Reference< com::sun::star::drawing::XShape > xShape, const double fAngularVelocity)
Sets angular velocity of the shape's corresponding Box2D body.
Definition: box2dtools.cxx:331
void queueShapePositionUpdate(const css::uno::Reference< css::drawing::XShape > &xShape, const ::basegfx::B2DPoint &rOutPos)
Definition: box2dtools.cxx:499
void initiateAllShapesAsStaticBodies(const slideshow::internal::ShapeManagerSharedPtr &pShapeManager)
Initiate all the shapes in the current slide in the box2DWorld as static ones.
Definition: box2dtools.cxx:391
Box2DBodySharedPtr makeShapeStatic(const slideshow::internal::ShapeSharedPtr &pShape)
Make the Box2D body corresponding to the given shape a static one.
Definition: box2dtools.cxx:669
void queueShapeAnimationUpdate(const css::uno::Reference< css::drawing::XShape > &xShape, const slideshow::internal::ShapeAttributeLayerSharedPtr &pAttrLayer, const slideshow::internal::AttributeType eAttrType, const bool bIsFirstUpdate)
Queue an appropriate update for the animation effect that is in parallel with a physics animation.
Definition: box2dtools.cxx:518
void queueShapeAnimationEndUpdate(const css::uno::Reference< css::drawing::XShape > &xShape, const slideshow::internal::AttributeType eAttrType)
Queue an appropriate update for the animation effect that just ended.
Definition: box2dtools.cxx:544
bool mbAlreadyStepped
Flag used to stop overstepping that occurs when a physics animation effect transfers step-lock to ano...
Definition: box2dtools.hxx:89
void setHasWorldStepper(const bool bHasWorldStepper)
Set the flag for whether the box2DWorld has a stepper or not.
Definition: box2dtools.cxx:449
void setShapeCollision(const css::uno::Reference< com::sun::star::drawing::XShape > xShape, const bool bCanCollide)
Sets whether a shape's corresponding Box2D body has collision in the Box2D World or not.
Definition: box2dtools.cxx:340
std::queue< Box2DDynamicUpdateInformation > maShapeParallelUpdateQueue
Queue that holds any required information to keep LO animation effects and Box2DWorld in sync.
Definition: box2dtools.hxx:101
void queueShapeVisibilityUpdate(const css::uno::Reference< css::drawing::XShape > &xShape, const bool bVisibility)
Queue an collision update that sets the collision of shape's corresponding box2D body when processed.
Definition: box2dtools.cxx:491
void setShapePositionByLinearVelocity(const css::uno::Reference< css::drawing::XShape > xShape, const ::basegfx::B2DPoint &rOutPos, const double fPassedTime)
Moves shape's corresponding Box2D body to specified position.
Definition: box2dtools.cxx:291
void queueLinearVelocityUpdate(const css::uno::Reference< css::drawing::XShape > &xShape, const ::basegfx::B2DVector &rVelocity, const int nDelayForSteps=0)
Queue a update that sets the corresponding box2D body's linear velocity to the given value when proce...
Definition: box2dtools.cxx:463
void alertPhysicsAnimationStart(const ::basegfx::B2DVector &rSlideSize, const slideshow::internal::ShapeManagerSharedPtr &pShapeManager)
Alert that a physics animation effect has started.
Definition: box2dtools.cxx:588
box2DWorld(const ::basegfx::B2DVector &rSlideSize)
Definition: box2dtools.cxx:224
void setShapePosition(const css::uno::Reference< css::drawing::XShape > xShape, const ::basegfx::B2DPoint &rOutPos)
Sets shape's corresponding Box2D body to the specified position.
Definition: box2dtools.cxx:284
void alertPhysicsAnimationEnd(const slideshow::internal::ShapeSharedPtr &pShape)
Alert that a physics animation effect has ended.
Definition: box2dtools.cxx:564
void step(const float fTimeStep=1.0f/100.0f, const int nVelocityIterations=6, const int nPositionIterations=2)
Simulate and step through time in the Box2D World.
Definition: box2dtools.cxx:601
void queueDynamicPositionUpdate(const css::uno::Reference< css::drawing::XShape > &xShape, const ::basegfx::B2DPoint &rOutPos)
Queue a position update that is simulated as if shape's corresponding box2D body moved to given posit...
Definition: box2dtools.cxx:454
double stepAmount(const double fPassedTime, const float fTimeStep=1.0f/100.0f, const int nVelocityIterations=6, const int nPositionIterations=2)
Simulate and step through a given amount of time in the Box2D World.
Definition: box2dtools.cxx:608
int mnPhysicsAnimationCounter
Number of Physics Animations going on.
Definition: box2dtools.hxx:91
bool isInitialized() const
Definition: box2dtools.cxx:640
void queueAngularVelocityUpdate(const css::uno::Reference< com::sun::star::drawing::XShape > &xShape, const double fAngularVelocity, const int nDelayForSteps=0)
Queue an angular velocity update that sets the shape's corresponding box2D body angular velocity to t...
Definition: box2dtools.cxx:481
void processUpdateQueue(const double fPassedTime)
Process the updates queued in the maShapeParallelUpdateQueue.
Definition: box2dtools.cxx:348
void setShapeLinearVelocity(const css::uno::Reference< com::sun::star::drawing::XShape > xShape, const basegfx::B2DVector &rVelocity)
Sets linear velocity of the shape's corresponding Box2D body.
Definition: box2dtools.cxx:303
bool initiateWorld(const ::basegfx::B2DVector &rSlideSize)
Definition: box2dtools.cxx:238
bool hasWorldStepper() const
Definition: box2dtools.cxx:447
Box2DBodySharedPtr createStaticBody(const slideshow::internal::ShapeSharedPtr &rShape, const float fDensity=1.0f, const float fFriction=0.3f)
Create a static body that is represented by the shape's geometry.
Definition: box2dtools.cxx:685
bool mbHasWorldStepper
Holds whether or not there is a PhysicsAnimation that is stepping the Box2D World.
Definition: box2dtools.hxx:86
std::unique_ptr< b2World > mpBox2DWorld
Pointer to the real Box2D World that this class manages.
Definition: box2dtools.hxx:80
void createStaticFrameAroundSlide(const ::basegfx::B2DVector &rSlideSize)
Creates a static frame in Box2D world that corresponds to the slide borders.
Definition: box2dtools.cxx:252
Box2DBodySharedPtr makeShapeDynamic(const css::uno::Reference< css::drawing::XShape > &xShape, const basegfx::B2DVector &rStartVelocity, const double fDensity, const double fBounciness)
Make the shape's corresponding box2D body a dynamic one.
Definition: box2dtools.cxx:649
void setShapeAngleByAngularVelocity(const css::uno::Reference< com::sun::star::drawing::XShape > xShape, const double fAngle, const double fPassedTime)
Rotates shape's corresponding Box2D body to specified angle.
Definition: box2dtools.cxx:319
void setShapeAngle(const css::uno::Reference< com::sun::star::drawing::XShape > xShape, const double fAngle)
Sets rotation angle of the shape's corresponding Box2D body.
Definition: box2dtools.cxx:312
void queueShapePathAnimationUpdate(const css::uno::Reference< com::sun::star::drawing::XShape > &xShape, const slideshow::internal::ShapeAttributeLayerSharedPtr &pAttrLayer, const bool bIsFirstUpdate)
Queue an appropriate update for a path animation that is in parallel with a physics animation.
Definition: box2dtools.cxx:508
std::unordered_map< css::uno::Reference< css::drawing::XShape >, Box2DBodySharedPtr > mpXShapeToBodyMap
Definition: box2dtools.hxx:93
Box2DBodySharedPtr makeBodyStatic(const Box2DBodySharedPtr &pBox2DBody)
Make the Box2D body a static one.
Definition: box2dtools.cxx:676
Box2DBodySharedPtr makeBodyDynamic(const Box2DBodySharedPtr &pBox2DBody)
Make the Box2D body a dynamic one.
Definition: box2dtools.cxx:660
@ BOX2D_KINEMATIC_BODY
Definition: box2dtools.hxx:37
std::shared_ptr< box2DBody > Box2DBodySharedPtr
Definition: box2dtools.hxx:32
::std::shared_ptr< box2DWorld > Box2DWorldSharedPtr
box2DNonsimulatedShapeUpdateType
Definition: box2dtools.hxx:42
@ BOX2D_UPDATE_LINEAR_VELOCITY
Definition: box2dtools.hxx:48
@ BOX2D_UPDATE_POSITION_CHANGE
Definition: box2dtools.hxx:43
@ BOX2D_UPDATE_VISIBILITY
Definition: box2dtools.hxx:47
@ BOX2D_UPDATE_ANGULAR_VELOCITY
Definition: box2dtools.hxx:49
@ BOX2D_UPDATE_POSITION
Definition: box2dtools.hxx:44
::std::shared_ptr< ShapeAttributeLayer > ShapeAttributeLayerSharedPtr
AttributeType
Type of to-be-animated attribute.
std::shared_ptr< ShapeManager > ShapeManagerSharedPtr
Definition: box2dtools.hxx:23
::std::shared_ptr< Shape > ShapeSharedPtr
Holds required information to perform an update to box2d body of a shape that was altered by an anima...
Definition: box2dtools.hxx:55
int mnDelayForSteps
amount of steps to delay the update for
Definition: box2dtools.hxx:67
css::uno::Reference< css::drawing::XShape > mxShape
reference to the shape that the update belongs to
Definition: box2dtools.hxx:57
box2DNonsimulatedShapeUpdateType meUpdateType
Definition: box2dtools.hxx:65