forked from Makuna/NeoPixelBus
ChangeAnimationDuration (#219)
This commit is contained in:
@@ -117,6 +117,7 @@ StopAnimation KEYWORD2
|
|||||||
RestartAnimation KEYWORD2
|
RestartAnimation KEYWORD2
|
||||||
IsAnimationActive KEYWORD2
|
IsAnimationActive KEYWORD2
|
||||||
AnimationDuration KEYWORD2
|
AnimationDuration KEYWORD2
|
||||||
|
ChangeAnimationDuration KEYWORD2
|
||||||
UpdateAnimations KEYWORD2
|
UpdateAnimations KEYWORD2
|
||||||
IsPaused KEYWORD2
|
IsPaused KEYWORD2
|
||||||
Pause KEYWORD2
|
Pause KEYWORD2
|
||||||
|
@@ -109,6 +109,8 @@ public:
|
|||||||
return _animations[indexAnimation]._duration;
|
return _animations[indexAnimation]._duration;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ChangeAnimationDuration(uint16_t indexAnimation, uint16_t newDuration);
|
||||||
|
|
||||||
void UpdateAnimations();
|
void UpdateAnimations();
|
||||||
|
|
||||||
bool IsPaused()
|
bool IsPaused()
|
||||||
@@ -159,6 +161,11 @@ private:
|
|||||||
_remaining = 0;
|
_remaining = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
float CurrentProgress()
|
||||||
|
{
|
||||||
|
return (float)(_duration - _remaining) / (float)_duration;
|
||||||
|
}
|
||||||
|
|
||||||
uint16_t _duration;
|
uint16_t _duration;
|
||||||
uint16_t _remaining;
|
uint16_t _remaining;
|
||||||
|
|
||||||
|
@@ -142,7 +142,7 @@ void NeoPixelAnimator::UpdateAnimations()
|
|||||||
if (pAnim->_remaining > delta)
|
if (pAnim->_remaining > delta)
|
||||||
{
|
{
|
||||||
param.state = (pAnim->_remaining == pAnim->_duration) ? AnimationState_Started : AnimationState_Progress;
|
param.state = (pAnim->_remaining == pAnim->_duration) ? AnimationState_Started : AnimationState_Progress;
|
||||||
param.progress = (float)(pAnim->_duration - pAnim->_remaining) / (float)pAnim->_duration;
|
param.progress = pAnim->CurrentProgress();
|
||||||
|
|
||||||
fnUpdate(param);
|
fnUpdate(param);
|
||||||
|
|
||||||
@@ -164,3 +164,33 @@ void NeoPixelAnimator::UpdateAnimations()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void NeoPixelAnimator::ChangeAnimationDuration(uint16_t indexAnimation, uint16_t newDuration)
|
||||||
|
{
|
||||||
|
if (indexAnimation >= _countAnimations)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
AnimationContext* pAnim = &_animations[indexAnimation];
|
||||||
|
|
||||||
|
// calc the current animation progress
|
||||||
|
float progress = pAnim->CurrentProgress();
|
||||||
|
|
||||||
|
// keep progress in range just in case
|
||||||
|
if (progress < 0.0f)
|
||||||
|
{
|
||||||
|
progress = 0.0f;
|
||||||
|
}
|
||||||
|
else if (progress > 1.0f)
|
||||||
|
{
|
||||||
|
progress = 1.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
// change the duration
|
||||||
|
pAnim->_duration = newDuration;
|
||||||
|
|
||||||
|
// _remaining time must also be reset after a duration change;
|
||||||
|
// use the progress to recalculate it
|
||||||
|
pAnim->_remaining = uint16_t(pAnim->_duration * (1.0f - progress));
|
||||||
|
}
|
Reference in New Issue
Block a user