slideshow/source/engine/animationfactory.cxx | 2 slideshow/source/engine/box2dtools.cxx | 57 +++++---------------------- slideshow/source/inc/box2dtools.hxx | 37 +---------------- 3 files changed, 16 insertions(+), 80 deletions(-)
New commits: commit f6a1ff865e69ff4bd7f6b41dc3328a6593ce40ff Author: Xisco Fauli <[email protected]> AuthorDate: Tue Apr 8 11:40:22 2025 +0200 Commit: Xisco Fauli <[email protected]> CommitDate: Tue Apr 8 14:20:12 2025 +0200 slideshow: refactor code a bit * return from initiateWorld is unused * drop step. it's called only from one place * makeShapeStatic is unused * Drop default parameters * Drop duplicated asserts already checked in the caller Change-Id: I8d77cf1b4e71d5f0f9a7f6da61672d24bd83725b Reviewed-on: https://gerrit.libreoffice.org/c/core/+/183840 Reviewed-by: Xisco Fauli <[email protected]> Tested-by: Jenkins diff --git a/slideshow/source/engine/animationfactory.cxx b/slideshow/source/engine/animationfactory.cxx index c7641b2690b6..e399aa2ac530 100644 --- a/slideshow/source/engine/animationfactory.cxx +++ b/slideshow/source/engine/animationfactory.cxx @@ -293,7 +293,7 @@ namespace slideshow::internal // if there is a physics animation going on report the animation ending // and zero out the velocity of the shape if( mpBox2DWorld->isInitialized() ) - mpBox2DWorld->queueLinearVelocityUpdate( mpShape->getXShape(), {0,0}); + mpBox2DWorld->queueLinearVelocityUpdate( mpShape->getXShape(), {0,0}, 0); } // NumberAnimation interface diff --git a/slideshow/source/engine/box2dtools.cxx b/slideshow/source/engine/box2dtools.cxx index 5d99aef07e3e..a71d0ad9e1f9 100644 --- a/slideshow/source/engine/box2dtools.cxx +++ b/slideshow/source/engine/box2dtools.cxx @@ -221,23 +221,9 @@ box2DWorld::box2DWorld(const ::basegfx::B2DVector& rSlideSize) box2DWorld::~box2DWorld() = default; -bool box2DWorld::initiateWorld(const ::basegfx::B2DVector& rSlideSize) -{ - if (!mpBox2DWorld) - { - mpBox2DWorld = std::make_unique<b2World>(b2Vec2(0.0f, -30.0f)); - createStaticFrameAroundSlide(rSlideSize); - return false; - } - else - { - return true; - } -} - void box2DWorld::createStaticFrameAroundSlide(const ::basegfx::B2DVector& rSlideSize) { - assert(mpBox2DWorld); + mpBox2DWorld = std::make_unique<b2World>(b2Vec2(0.0f, -30.0f)); float fWidth = static_cast<float>(rSlideSize.getX() * mfScaleFactor); float fHeight = static_cast<float>(rSlideSize.getY() * mfScaleFactor); @@ -280,7 +266,6 @@ void box2DWorld::setShapePositionByLinearVelocity( const css::uno::Reference<css::drawing::XShape>& xShape, const basegfx::B2DPoint& rOutPos, const double fPassedTime) { - assert(mpBox2DWorld); if (fPassedTime > 0) // this only makes sense if there was an advance in time { const auto iter = mpXShapeToBodyMap.find(xShape); @@ -293,7 +278,6 @@ void box2DWorld::setShapePositionByLinearVelocity( void box2DWorld::setShapeLinearVelocity(const css::uno::Reference<css::drawing::XShape>& xShape, const basegfx::B2DVector& rVelocity) { - assert(mpBox2DWorld); const auto iter = mpXShapeToBodyMap.find(xShape); assert(iter != mpXShapeToBodyMap.end()); Box2DBodySharedPtr pBox2DBody = iter->second; @@ -313,7 +297,6 @@ void box2DWorld::setShapeAngleByAngularVelocity( const css::uno::Reference<css::drawing::XShape>& xShape, const double fAngle, const double fPassedTime) { - assert(mpBox2DWorld); if (fPassedTime > 0) // this only makes sense if there was an advance in time { const auto iter = mpXShapeToBodyMap.find(xShape); @@ -326,7 +309,6 @@ void box2DWorld::setShapeAngleByAngularVelocity( void box2DWorld::setShapeAngularVelocity(const css::uno::Reference<css::drawing::XShape>& xShape, const double fAngularVelocity) { - assert(mpBox2DWorld); const auto iter = mpXShapeToBodyMap.find(xShape); assert(iter != mpXShapeToBodyMap.end()); Box2DBodySharedPtr pBox2DBody = iter->second; @@ -336,7 +318,6 @@ void box2DWorld::setShapeAngularVelocity(const css::uno::Reference<css::drawing: void box2DWorld::setShapeCollision(const css::uno::Reference<css::drawing::XShape>& xShape, bool bCanCollide) { - assert(mpBox2DWorld); const auto iter = mpXShapeToBodyMap.find(xShape); assert(iter != mpXShapeToBodyMap.end()); Box2DBodySharedPtr pBox2DBody = iter->second; @@ -389,8 +370,6 @@ void box2DWorld::processUpdateQueue(const double fPassedTime) void box2DWorld::initiateAllShapesAsStaticBodies( const slideshow::internal::ShapeManagerSharedPtr& pShapeManager) { - assert(mpBox2DWorld); - mbShapesInitialized = true; auto aXShapeToShapeMap = pShapeManager->getXShapeToShapeMap(); @@ -585,7 +564,7 @@ void box2DWorld::alertPhysicsAnimationStart( const slideshow::internal::ShapeManagerSharedPtr& pShapeManager) { if (!mpBox2DWorld) - initiateWorld(rSlideSize); + createStaticFrameAroundSlide(rSlideSize); if (!mbShapesInitialized) initiateAllShapesAsStaticBodies(pShapeManager); @@ -593,17 +572,14 @@ void box2DWorld::alertPhysicsAnimationStart( mnPhysicsAnimationCounter++; } -void box2DWorld::step(const float fTimeStep, const int nVelocityIterations, - const int nPositionIterations) +double box2DWorld::stepAmount(const double fPassedTime) { assert(mpBox2DWorld); - mpBox2DWorld->Step(fTimeStep, nVelocityIterations, nPositionIterations); -} -double box2DWorld::stepAmount(const double fPassedTime, const float fTimeStep, - const int nVelocityIterations, const int nPositionIterations) -{ - assert(mpBox2DWorld); + // attention fTimeStep should not vary. + const float fTimeStep = 1.0f / 100.0f; + const int nVelocityIterations = 6; + const int nPositionIterations = 2; unsigned int nStepAmount = static_cast<unsigned int>(std::round(fPassedTime / fTimeStep)); // find the actual time that will be stepped through so @@ -617,7 +593,7 @@ double box2DWorld::stepAmount(const double fPassedTime, const float fTimeStep, { for (unsigned int nStepCounter = 0; nStepCounter < nStepAmount; nStepCounter++) { - step(fTimeStep, nVelocityIterations, nPositionIterations); + mpBox2DWorld->Step(fTimeStep, nVelocityIterations, nPositionIterations); } } else @@ -661,16 +637,6 @@ Box2DBodySharedPtr makeBodyDynamic(const Box2DBodySharedPtr& pBox2DBody) return pBox2DBody; } -Box2DBodySharedPtr box2DWorld::makeShapeStatic(const slideshow::internal::ShapeSharedPtr& pShape) -{ - assert(mpBox2DWorld); - assert(pShape && pShape->getXShape()); - const auto iter = mpXShapeToBodyMap.find(pShape->getXShape()); - assert(iter != mpXShapeToBodyMap.end()); - Box2DBodySharedPtr pBox2DBody = iter->second; - return makeBodyStatic(pBox2DBody); -} - Box2DBodySharedPtr makeBodyStatic(const Box2DBodySharedPtr& pBox2DBody) { if (pBox2DBody->getType() != BOX2D_STATIC_BODY) @@ -680,11 +646,10 @@ Box2DBodySharedPtr makeBodyStatic(const Box2DBodySharedPtr& pBox2DBody) return pBox2DBody; } -Box2DBodySharedPtr box2DWorld::createStaticBody(const slideshow::internal::ShapeSharedPtr& rShape, - const float fDensity, const float fFriction) +Box2DBodySharedPtr box2DWorld::createStaticBody(const slideshow::internal::ShapeSharedPtr& rShape) { - assert(mpBox2DWorld); - + const float fDensity = 1.0f; + const float fFriction = 0.3f; ::basegfx::B2DRectangle aShapeBounds = rShape->getBounds(); b2BodyDef aBodyDef; diff --git a/slideshow/source/inc/box2dtools.hxx b/slideshow/source/inc/box2dtools.hxx index 037c5ef63b6c..7d85f9eaebe5 100644 --- a/slideshow/source/inc/box2dtools.hxx +++ b/slideshow/source/inc/box2dtools.hxx @@ -211,22 +211,13 @@ private: */ void processUpdateQueue(const double fPassedTime); - /** Simulate and step through time in the Box2D World - - Used in stepAmount - - @attention fTimeStep should not vary. - */ - void step(const float fTimeStep = 1.0f / 100.0f, const int nVelocityIterations = 6, - const int nPositionIterations = 2); - /// Queue a rotation update that is simulated as if shape's corresponding box2D body rotated to given angle when processed void queueDynamicRotationUpdate(const css::uno::Reference<css::drawing::XShape>& xShape, const double fAngle); /// Queue an angular velocity update that sets the shape's corresponding box2D body angular velocity to the given value when processed void queueAngularVelocityUpdate(const css::uno::Reference<css::drawing::XShape>& xShape, - const double fAngularVelocity, const int nDelayForSteps = 0); + const double fAngularVelocity, const int nDelayForSteps); /// Queue an collision update that sets the collision of shape's corresponding box2D body when processed void queueShapeVisibilityUpdate(const css::uno::Reference<css::drawing::XShape>& xShape, @@ -239,8 +230,6 @@ public: box2DWorld(const ::basegfx::B2DVector& rSlideSize); ~box2DWorld(); - bool initiateWorld(const ::basegfx::B2DVector& rSlideSize); - /** Simulate and step through a given amount of time in the Box2D World @param fPassedTime @@ -248,11 +237,8 @@ public: @return Amount of time actually stepped through, since it is possible to only step through a multiple of fTimeStep - - @attention fTimeStep should not vary. */ - double stepAmount(const double fPassedTime, const float fTimeStep = 1.0f / 100.0f, - const int nVelocityIterations = 6, const int nPositionIterations = 2); + double stepAmount(const double fPassedTime); /// @return whether shapes in the slide are initialized as Box2D bodies or not bool shapesInitialized(); @@ -282,25 +268,11 @@ public: const basegfx::B2DVector& rStartVelocity, const double fDensity, const double fBounciness); - /** Make the Box2D body corresponding to the given shape a static one - - A static body will not be affected by other bodies and the gravity. But will - affect other bodies that are dynamic (will still collide with them but won't - move etc.) - - @param pShape - Pointer to the shape to alter the corresponding Box2D body of - - @return box2d body pointer - */ - Box2DBodySharedPtr makeShapeStatic(const slideshow::internal::ShapeSharedPtr& pShape); - /** Create a static body that is represented by the shape's geometry @return pointer to the box2d body */ - Box2DBodySharedPtr createStaticBody(const slideshow::internal::ShapeSharedPtr& rShape, - const float fDensity = 1.0f, const float fFriction = 0.3f); + Box2DBodySharedPtr createStaticBody(const slideshow::internal::ShapeSharedPtr& rShape); /// Initiate all the shapes in the current slide in the box2DWorld as static ones void initiateAllShapesAsStaticBodies( @@ -318,8 +290,7 @@ public: /// Queue a update that sets the corresponding box2D body's linear velocity to the given value when processed void queueLinearVelocityUpdate(const css::uno::Reference<css::drawing::XShape>& xShape, - const ::basegfx::B2DVector& rVelocity, - const int nDelayForSteps = 0); + const ::basegfx::B2DVector& rVelocity, const int nDelayForSteps); /// Queue an appropriate update for the animation effect that is in parallel with a physics animation void
