diff options
| author | GartoxFR <ewan.chorynski@insa-lyon.fr> | 2024-03-02 01:35:17 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-02 00:35:17 +0000 |
| commit | b2c34404777ea0850f90f4c38f64484f03599d14 (patch) | |
| tree | 60f42773d2d606ae2072884cb537c751a7ab4b0f /src/helpers/AnimatedVariable.cpp | |
| parent | f115ba94d2ec6e093c94f425535b027bc570185e (diff) | |
animations: Refactor AnimatedVariable (#4911)
* animation: Refactor AnimatedVariable
This commit decomposes the AnimatedVariable class into a base class
with the common attribute to all variable types and a templated derived
type containing strongly typed info on the type being animated.
Access to the typed version is perfomed using the visitor pattern. A
utility is provided to build a visitor on the fly using lambdas.
Adding a new type to be animated should just be a matter of adding the
typed in the list defined by the ANIMABLE_TYPES macro
The size of the commit is justified by the API change in the
AnimatedVariable class. No more vec(), fl() or col() method but a unified
value() method.
* animation: Remove visitor pattern
* animation: Fix coding style
* animation: Fix coding style
Diffstat (limited to 'src/helpers/AnimatedVariable.cpp')
| -rw-r--r-- | src/helpers/AnimatedVariable.cpp | 54 |
1 files changed, 11 insertions, 43 deletions
diff --git a/src/helpers/AnimatedVariable.cpp b/src/helpers/AnimatedVariable.cpp index 740e604f..3a891e37 100644 --- a/src/helpers/AnimatedVariable.cpp +++ b/src/helpers/AnimatedVariable.cpp @@ -2,12 +2,11 @@ #include "../managers/AnimationManager.hpp" #include "../config/ConfigManager.hpp" -CAnimatedVariable::CAnimatedVariable() { +CBaseAnimatedVariable::CBaseAnimatedVariable(ANIMATEDVARTYPE type) : m_Type(type) { ; // dummy var } -void CAnimatedVariable::create(ANIMATEDVARTYPE type, SAnimationPropertyConfig* pAnimConfig, void* pWindow, AVARDAMAGEPOLICY policy) { - m_eVarType = type; +void CBaseAnimatedVariable::create(SAnimationPropertyConfig* pAnimConfig, void* pWindow, AVARDAMAGEPOLICY policy) { m_eDamagePolicy = policy; m_pConfig = pAnimConfig; m_pWindow = pWindow; @@ -15,42 +14,11 @@ void CAnimatedVariable::create(ANIMATEDVARTYPE type, SAnimationPropertyConfig* p m_bDummy = false; } -void CAnimatedVariable::create(ANIMATEDVARTYPE type, std::any val, SAnimationPropertyConfig* pAnimConfig, void* pWindow, AVARDAMAGEPOLICY policy) { - create(type, pAnimConfig, pWindow, policy); - - try { - switch (type) { - case AVARTYPE_FLOAT: { - const auto V = std::any_cast<float>(val); - m_fValue = V; - m_fGoal = V; - break; - } - case AVARTYPE_VECTOR: { - const auto V = std::any_cast<Vector2D>(val); - m_vValue = V; - m_vGoal = V; - break; - } - case AVARTYPE_COLOR: { - const auto V = std::any_cast<CColor>(val); - m_cValue = V; - m_cGoal = V; - break; - } - default: ASSERT(false); break; - } - } catch (std::exception& e) { - Debug::log(ERR, "CAnimatedVariable create error: {}", e.what()); - RASSERT(false, "CAnimatedVariable create error: {}", e.what()); - } -} - -CAnimatedVariable::~CAnimatedVariable() { +CBaseAnimatedVariable::~CBaseAnimatedVariable() { unregister(); } -void CAnimatedVariable::unregister() { +void CBaseAnimatedVariable::unregister() { if (!g_pAnimationManager) return; std::erase_if(g_pAnimationManager->m_vAnimatedVariables, [&](const auto& other) { return other == this; }); @@ -58,23 +26,23 @@ void CAnimatedVariable::unregister() { disconnectFromActive(); } -void CAnimatedVariable::registerVar() { +void CBaseAnimatedVariable::registerVar() { if (!m_bIsRegistered) g_pAnimationManager->m_vAnimatedVariables.push_back(this); m_bIsRegistered = true; } -int CAnimatedVariable::getDurationLeftMs() { +int CBaseAnimatedVariable::getDurationLeftMs() { return std::max( (int)(m_pConfig->pValues->internalSpeed * 100) - (int)std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() - animationBegin).count(), 0); } -float CAnimatedVariable::getPercent() { +float CBaseAnimatedVariable::getPercent() { const auto DURATIONPASSED = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() - animationBegin).count(); return std::clamp((DURATIONPASSED / 100.f) / m_pConfig->pValues->internalSpeed, 0.f, 1.f); } -float CAnimatedVariable::getCurveValue() { +float CBaseAnimatedVariable::getCurveValue() { if (!m_bIsBeingAnimated) return 1.f; @@ -86,7 +54,7 @@ float CAnimatedVariable::getCurveValue() { return g_pAnimationManager->getBezier(m_pConfig->pValues->internalBezier)->getYForPoint(SPENT); } -void CAnimatedVariable::connectToActive() { +void CBaseAnimatedVariable::connectToActive() { g_pAnimationManager->scheduleTick(); // otherwise the animation manager will never pick this up if (!m_bIsConnectedToActive) @@ -95,7 +63,7 @@ void CAnimatedVariable::connectToActive() { m_bIsConnectedToActive = true; } -void CAnimatedVariable::disconnectFromActive() { +void CBaseAnimatedVariable::disconnectFromActive() { std::erase_if(g_pAnimationManager->m_vActiveAnimatedVariables, [&](const auto& other) { return other == this; }); m_bIsConnectedToActive = false; -}
\ No newline at end of file +} |
