diff --git a/include/hyprutils/animation/AnimatedVariable.hpp b/include/hyprutils/animation/AnimatedVariable.hpp index 113793f..668661b 100644 --- a/include/hyprutils/animation/AnimatedVariable.hpp +++ b/include/hyprutils/animation/AnimatedVariable.hpp @@ -22,6 +22,7 @@ namespace Hyprutils { }; void create(CAnimationManager*, int, Memory::CSharedPointer); + void create2(CAnimationManager*, int, Memory::CWeakPointer); void connectToActive(); void disconnectFromActive(); @@ -136,6 +137,7 @@ namespace Hyprutils { public: CGenericAnimatedVariable() = default; + /* Deprecated: use create2 */ void create(const int typeInfo, CAnimationManager* pAnimationManager, Memory::CSharedPointer> pSelf, const VarType& initialValue) { m_Begun = initialValue; @@ -145,6 +147,16 @@ namespace Hyprutils { CBaseAnimatedVariable::create(pAnimationManager, typeInfo, pSelf); } + /* Equivalent to create, except that it allows animated variables to be UP's */ + void create2(const int typeInfo, CAnimationManager* pAnimationManager, Memory::CWeakPointer> pSelf, + const VarType& initialValue) { + m_Begun = initialValue; + m_Value = initialValue; + m_Goal = initialValue; + + CBaseAnimatedVariable::create2(pAnimationManager, typeInfo, pSelf); + } + CGenericAnimatedVariable(const CGenericAnimatedVariable&) = delete; CGenericAnimatedVariable(CGenericAnimatedVariable&&) = delete; CGenericAnimatedVariable& operator=(const CGenericAnimatedVariable&) = delete; diff --git a/src/animation/AnimatedVariable.cpp b/src/animation/AnimatedVariable.cpp index 13115fd..81da415 100644 --- a/src/animation/AnimatedVariable.cpp +++ b/src/animation/AnimatedVariable.cpp @@ -13,7 +13,16 @@ static const std::string DEFAULTSTYLE = ""; void CBaseAnimatedVariable::create(CAnimationManager* pManager, int typeInfo, SP pSelf) { m_Type = typeInfo; - m_pSelf = pSelf; + m_pSelf = std::move(pSelf); + + m_pAnimationManager = pManager; + m_pSignals = pManager->getSignals(); + m_bDummy = false; +} + +void CBaseAnimatedVariable::create2(CAnimationManager* pManager, int typeInfo, WP pSelf) { + m_Type = typeInfo; + m_pSelf = std::move(pSelf); m_pAnimationManager = pManager; m_pSignals = pManager->getSignals(); @@ -37,28 +46,22 @@ void CBaseAnimatedVariable::disconnectFromActive() { } bool Hyprutils::Animation::CBaseAnimatedVariable::enabled() const { - if (const auto PCONFIG = m_pConfig.lock()) { - const auto PVALUES = PCONFIG->pValues.lock(); - return PVALUES ? PVALUES->internalEnabled : false; - } + if (m_pConfig && m_pConfig->pValues) + return m_pConfig->pValues->internalEnabled; return false; } const std::string& CBaseAnimatedVariable::getBezierName() const { - if (const auto PCONFIG = m_pConfig.lock()) { - const auto PVALUES = PCONFIG->pValues.lock(); - return PVALUES ? PVALUES->internalBezier : DEFAULTBEZIERNAME; - } + if (m_pConfig && m_pConfig->pValues) + return m_pConfig->pValues->internalBezier; return DEFAULTBEZIERNAME; } const std::string& CBaseAnimatedVariable::getStyle() const { - if (const auto PCONFIG = m_pConfig.lock()) { - const auto PVALUES = PCONFIG->pValues.lock(); - return PVALUES ? PVALUES->internalStyle : DEFAULTSTYLE; - } + if (m_pConfig && m_pConfig->pValues) + return m_pConfig->pValues->internalStyle; return DEFAULTSTYLE; } @@ -66,10 +69,8 @@ const std::string& CBaseAnimatedVariable::getStyle() const { float CBaseAnimatedVariable::getPercent() const { const auto DURATIONPASSED = std::chrono::duration_cast(std::chrono::steady_clock::now() - animationBegin).count(); - if (const auto PCONFIG = m_pConfig.lock()) { - const auto PVALUES = PCONFIG->pValues.lock(); - return PVALUES ? std::clamp((DURATIONPASSED / 100.f) / PVALUES->internalSpeed, 0.f, 1.f) : 1.f; - } + if (m_pConfig && m_pConfig->pValues) + return std::clamp((DURATIONPASSED / 100.f) / m_pConfig->pValues->internalSpeed, 0.f, 1.f); return 1.f; } @@ -78,14 +79,7 @@ float CBaseAnimatedVariable::getCurveValue() const { if (!m_bIsBeingAnimated || isAnimationManagerDead()) return 1.f; - std::string bezierName = ""; - if (const auto PCONFIG = m_pConfig.lock()) { - const auto PVALUES = PCONFIG->pValues.lock(); - if (PVALUES) - bezierName = PVALUES->internalBezier; - } - - const auto BEZIER = m_pAnimationManager->getBezier(bezierName); + const auto BEZIER = m_pAnimationManager->getBezier(getBezierName()); if (!BEZIER) return 1.f; diff --git a/src/animation/AnimationManager.cpp b/src/animation/AnimationManager.cpp index 446b2d9..2910549 100644 --- a/src/animation/AnimationManager.cpp +++ b/src/animation/AnimationManager.cpp @@ -64,14 +64,13 @@ void CAnimationManager::rotateActive() { std::vector> active; active.reserve(m_vActiveAnimatedVariables.size()); // avoid reallocations for (auto const& av : m_vActiveAnimatedVariables) { - const auto PAV = av.lock(); - if (!PAV) + if (!av) continue; - if (PAV->ok() && PAV->isBeingAnimated()) + if (av->ok() && av->isBeingAnimated()) active.emplace_back(av); else - PAV->m_bIsConnectedToActive = false; + av->m_bIsConnectedToActive = false; } m_vActiveAnimatedVariables = std::move(active); diff --git a/tests/animation/Animation.cpp b/tests/animation/Animation.cpp index 1d2521b..7a2b639 100644 --- a/tests/animation/Animation.cpp +++ b/tests/animation/Animation.cpp @@ -21,7 +21,7 @@ template using CAnimatedVariable = CGenericAnimatedVariable; template -using PANIMVAR = SP>; +using PANIMVAR = UP>; template using PANIMVARREF = WP>; @@ -47,8 +47,7 @@ CAnimationConfigTree animationTree; class CMyAnimationManager : public CAnimationManager { public: void tick() { - for (size_t i = 0; i < m_vActiveAnimatedVariables.size(); i++) { - const auto PAV = m_vActiveAnimatedVariables[i].lock(); + for (const auto& PAV : m_vActiveAnimatedVariables) { if (!PAV || !PAV->ok() || !PAV->isBeingAnimated()) continue; @@ -68,7 +67,7 @@ class CMyAnimationManager : public CAnimationManager { if (!avInt) std::cout << "Dynamic cast upcast failed\n"; - const auto DELTA = avInt->goal() - avInt->value(); + const auto DELTA = avInt->goal() - avInt->begun(); avInt->value() = avInt->begun() + (DELTA * POINTY); } break; case eAVTypes::TEST: { @@ -93,11 +92,10 @@ class CMyAnimationManager : public CAnimationManager { template void createAnimation(const VarType& v, PANIMVAR& av, const std::string& animationConfigName) { constexpr const eAVTypes EAVTYPE = std::is_same_v ? eAVTypes::INT : eAVTypes::TEST; - const auto PAV = makeShared>(); + av = makeUnique>(); - PAV->create(EAVTYPE, sc(this), PAV, v); - PAV->setConfig(animationTree.getConfig(animationConfigName)); - av = std::move(PAV); + av->create2(EAVTYPE, sc(this), av, v); + av->setConfig(animationTree.getConfig(animationConfigName)); } virtual void scheduleTick() { @@ -298,7 +296,7 @@ TEST(Animation, animation) { // test adding / removing vars during a tick s.m_iA->resetAllCallbacks(); s.m_iA->setUpdateCallback([&vars](WP v) { - if (v.lock() != vars.back()) + if (v.get() != vars.back().get()) vars.back()->warp(); }); s.m_iA->setCallbackOnEnd([&s, &vars](auto) { @@ -350,7 +348,7 @@ TEST(Animation, animation) { *s.m_iA = 5; s.m_iA->setCallbackOnEnd([&endCallbackRan](WP v) { endCallbackRan++; - const auto PAV = dc*>(v.lock().get()); + const auto PAV = dc*>(v.get()); *PAV = 10; PAV->setCallbackOnEnd([&endCallbackRan](WP v) { endCallbackRan++; });