diff --git a/src/GuiComponent.cpp b/src/GuiComponent.cpp index bc488f2e8..c6ab22a3e 100644 --- a/src/GuiComponent.cpp +++ b/src/GuiComponent.cpp @@ -16,11 +16,7 @@ GuiComponent::~GuiComponent() { mWindow->removeGui(this); - for(unsigned char i = 0; i < MAX_ANIMATIONS; i++) - { - if(mAnimationMap[i]) - delete mAnimationMap[i]; - } + cancelAllAnimations(); if(mParent) mParent->removeChild(this); @@ -227,6 +223,29 @@ void GuiComponent::stopAnimation(unsigned char slot) } } +void GuiComponent::cancelAnimation(unsigned char slot) +{ + assert(slot < MAX_ANIMATIONS); + if(mAnimationMap[slot]) + { + mAnimationMap[slot]->removeFinishedCallback(); + delete mAnimationMap[slot]; + mAnimationMap[slot] = NULL; + } +} + +void GuiComponent::stopAllAnimations() +{ + for(unsigned char i = 0; i < MAX_ANIMATIONS; i++) + stopAnimation(i); +} + +void GuiComponent::cancelAllAnimations() +{ + for(unsigned char i = 0; i < MAX_ANIMATIONS; i++) + cancelAnimation(i); +} + bool GuiComponent::isAnimationPlaying(unsigned char slot) const { return mAnimationMap[slot] != NULL; diff --git a/src/GuiComponent.h b/src/GuiComponent.h index fe53ad008..cea2765c5 100644 --- a/src/GuiComponent.h +++ b/src/GuiComponent.h @@ -60,6 +60,9 @@ public: int getAnimationTime(unsigned char slot) const; void setAnimation(Animation* animation, std::function finishedCallback = nullptr, bool reverse = false, unsigned char slot = 0); void stopAnimation(unsigned char slot); + void cancelAnimation(unsigned char slot); // like stopAnimation, but doesn't call finishedCallback - only removes the animation, leaving things in their current state + void stopAllAnimations(); + void cancelAllAnimations(); virtual unsigned char getOpacity() const; virtual void setOpacity(unsigned char opacity); diff --git a/src/animations/AnimationController.h b/src/animations/AnimationController.h index 91a086b9b..4df334809 100644 --- a/src/animations/AnimationController.h +++ b/src/animations/AnimationController.h @@ -7,7 +7,6 @@ class AnimationController { public: - // FinishedCallback is guaranteed to be called exactly once, even if the animation does not finish normally. // Takes ownership of anim (will delete in destructor). AnimationController(Animation* anim, std::function finishedCallback = nullptr, bool reverse = false); virtual ~AnimationController(); @@ -17,6 +16,9 @@ public: inline bool isReversed() const { return mReverse; } inline int getTime() const { return mTime; } + inline const std::function& getFinishedCallback() const { return mFinishedCallback; } + + inline void removeFinishedCallback() { mFinishedCallback = nullptr; } private: Animation* mAnimation;