Skip to content

Commit

Permalink
don't break API, more robust code
Browse files Browse the repository at this point in the history
  • Loading branch information
vaxerski committed Jan 26, 2025
1 parent 06ec9a2 commit 21dac77
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 51 deletions.
34 changes: 17 additions & 17 deletions include/hyprutils/animation/AnimatedVariable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,13 @@
#include "../memory/WeakPtr.hpp"
#include "../memory/SharedPtr.hpp"
#include "../signal/Signal.hpp"
#include "AnimationManager.hpp"

#include <functional>
#include <chrono>

namespace Hyprutils {
namespace Animation {
class CAnimationManager;

struct SAnimVarEvents {
Signal::CSignal connect;
Signal::CSignal disconnect;
};

/* A base class for animated variables. */
class CBaseAnimatedVariable {
Expand All @@ -26,7 +21,7 @@ namespace Hyprutils {
; // m_bDummy = true;
};

void create(int, Memory::CSharedPointer<CBaseAnimatedVariable>, Memory::CSharedPointer<SAnimVarEvents> events);
void create(CAnimationManager*, int, Memory::CSharedPointer<CBaseAnimatedVariable>);
void connectToActive();
void disconnectFromActive();

Expand All @@ -42,7 +37,8 @@ namespace Hyprutils {
CBaseAnimatedVariable& operator=(const CBaseAnimatedVariable&) = delete;
CBaseAnimatedVariable& operator=(CBaseAnimatedVariable&&) = delete;

void setConfig(Memory::CSharedPointer<SAnimationPropertyConfig> pConfig) {
//
void setConfig(Memory::CSharedPointer<SAnimationPropertyConfig> pConfig) {
m_pConfig = pConfig;
}

Expand All @@ -57,9 +53,8 @@ namespace Hyprutils {
/* returns the spent (completion) % */
float getPercent() const;

/* returns the current curve value.
needs a reference to the animationmgr to get the bezier curve with the configured name from it */
float getCurveValue(CAnimationManager*) const;
/* returns the current curve value. */
float getCurveValue() const;

/* checks if an animation is in progress */
bool isBeingAnimated() const {
Expand Down Expand Up @@ -90,17 +85,22 @@ namespace Hyprutils {
void onAnimationEnd();
void onAnimationBegin();

/* returns whether the parent CAnimationManager is dead */
bool isAnimationManagerDead() const;

int m_Type = -1;

protected:
friend class CAnimationManager;

bool m_bIsConnectedToActive = false;
bool m_bIsBeingAnimated = false;
CAnimationManager* m_pAnimationManager = nullptr;

bool m_bIsConnectedToActive = false;
bool m_bIsBeingAnimated = false;

Memory::CWeakPointer<CBaseAnimatedVariable> m_pSelf;
Memory::CWeakPointer<CBaseAnimatedVariable> m_pSelf;

Memory::CWeakPointer<SAnimVarEvents> m_events;
Memory::CWeakPointer<CAnimationManager::SAnimationManagerSignals> m_pSignals;

private:
Memory::CWeakPointer<SAnimationPropertyConfig> m_pConfig;
Expand Down Expand Up @@ -136,13 +136,13 @@ namespace Hyprutils {
public:
CGenericAnimatedVariable() = default;

void create(const int typeInfo, Memory::CSharedPointer<CGenericAnimatedVariable<VarType, AnimationContext>> pSelf, Memory::CSharedPointer<SAnimVarEvents> events,
void create(const int typeInfo, CAnimationManager* pAnimationManager, Memory::CSharedPointer<CGenericAnimatedVariable<VarType, AnimationContext>> pSelf,
const VarType& initialValue) {
m_Begun = initialValue;
m_Value = initialValue;
m_Goal = initialValue;

CBaseAnimatedVariable::create(typeInfo, pSelf, events);
CBaseAnimatedVariable::create(pAnimationManager, typeInfo, pSelf);
}

CGenericAnimatedVariable(const CGenericAnimatedVariable&) = delete;
Expand Down
24 changes: 15 additions & 9 deletions include/hyprutils/animation/AnimationManager.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include "./BezierCurve.hpp"
#include "./AnimatedVariable.hpp"
#include "../math/Vector2D.hpp"
#include "../memory/WeakPtr.hpp"
#include "../signal/Signal.hpp"
Expand All @@ -12,6 +11,8 @@

namespace Hyprutils {
namespace Animation {
class CBaseAnimatedVariable;

/* A class for managing bezier curves and variables that are being animated. */
class CAnimationManager {
public:
Expand All @@ -33,25 +34,30 @@ namespace Hyprutils {

const std::unordered_map<std::string, Memory::CSharedPointer<CBezierCurve>>& getAllBeziers();

Memory::CSharedPointer<SAnimVarEvents> getEvents() const;
struct SAnimationManagerSignals {
Signal::CSignal connect; // WP<CBaseAnimatedVariable>
Signal::CSignal disconnect; // WP<CBaseAnimatedVariable>
};

Memory::CWeakPointer<SAnimationManagerSignals> getSignals() const;

std::vector<Memory::CWeakPointer<CBaseAnimatedVariable>> m_vActiveAnimatedVariables;
Memory::CSharedPointer<SAnimVarEvents> m_events;
std::vector<Memory::CWeakPointer<CBaseAnimatedVariable>> m_vActiveAnimatedVariables;

private:
std::unordered_map<std::string, Memory::CSharedPointer<CBezierCurve>> m_mBezierCurves;

bool m_bTickScheduled = false;

void connectListener(std::any data);
void disconnectListener(std::any data);
void onConnect(std::any data);
void onDisconnect(std::any data);

struct {
struct SAnimVarListeners {
Signal::CHyprSignalListener connect;
Signal::CHyprSignalListener disconnect;
} m_sListeners;
};

friend class CBaseAnimatedVariable;
Memory::CUniquePointer<SAnimVarListeners> m_listeners;
Memory::CUniquePointer<SAnimationManagerSignals> m_events;
};
}
}
32 changes: 18 additions & 14 deletions src/animation/AnimatedVariable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,28 @@ using namespace Hyprutils::Memory;
#define SP CSharedPointer
#define WP CWeakPointer

void CBaseAnimatedVariable::create(int typeInfo, SP<CBaseAnimatedVariable> pSelf, SP<SAnimVarEvents> events) {
void CBaseAnimatedVariable::create(CAnimationManager* pManager, int typeInfo, SP<CBaseAnimatedVariable> pSelf) {
m_Type = typeInfo;
m_pSelf = pSelf;

m_events = events;
m_bDummy = false;
m_pAnimationManager = pManager;
m_pSignals = pManager->getSignals();
m_bDummy = false;
}

void CBaseAnimatedVariable::connectToActive() {
if (m_bDummy || m_bIsConnectedToActive)
if (m_bDummy || m_bIsConnectedToActive || isAnimationManagerDead())
return;

if (const auto PEVENTS = m_events.lock()) {
PEVENTS->connect.emit(m_pSelf.lock());
m_bIsConnectedToActive = true;
}
m_pSignals->connect.emit(m_pSelf);
m_bIsConnectedToActive = true;
}

void CBaseAnimatedVariable::disconnectFromActive() {
if (const auto PEVENTS = m_events.lock())
PEVENTS->disconnect.emit(static_cast<void*>(this));
if (isAnimationManagerDead())
return;

m_pSignals->disconnect.emit(m_pSelf);
m_bIsConnectedToActive = false;
}

Expand Down Expand Up @@ -75,8 +75,8 @@ float CBaseAnimatedVariable::getPercent() const {
return 1.f;
}

float CBaseAnimatedVariable::getCurveValue(CAnimationManager* pAnimationManager) const {
if (!m_bIsBeingAnimated || !pAnimationManager)
float CBaseAnimatedVariable::getCurveValue() const {
if (!m_bIsBeingAnimated || isAnimationManagerDead())
return 1.f;

std::string bezierName = "";
Expand All @@ -86,7 +86,7 @@ float CBaseAnimatedVariable::getCurveValue(CAnimationManager* pAnimationManager)
bezierName = PVALUES->internalBezier;
}

const auto BEZIER = pAnimationManager->getBezier(bezierName);
const auto BEZIER = m_pAnimationManager->getBezier(bezierName);
if (!BEZIER)
return 1.f;

Expand All @@ -98,7 +98,7 @@ float CBaseAnimatedVariable::getCurveValue(CAnimationManager* pAnimationManager)
}

bool CBaseAnimatedVariable::ok() const {
return m_pConfig && !m_bDummy && !m_events.expired();
return m_pConfig && !m_bDummy && !isAnimationManagerDead();
}

void CBaseAnimatedVariable::onUpdate() {
Expand Down Expand Up @@ -155,3 +155,7 @@ void CBaseAnimatedVariable::onAnimationBegin() {
m_fBeginCallback = nullptr; // reset
}
}

bool CBaseAnimatedVariable::isAnimationManagerDead() const {
return m_pSignals.expired();
}
23 changes: 15 additions & 8 deletions src/animation/AnimationManager.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#include <hyprutils/animation/AnimationManager.hpp>
#include <hyprutils/animation/AnimatedVariable.hpp>

using namespace Hyprutils::Animation;
using namespace Hyprutils::Math;
using namespace Hyprutils::Memory;
using namespace Hyprutils::Signal;

#define SP CSharedPointer
#define WP CWeakPointer

const std::array<Vector2D, 2> DEFAULTBEZIERPOINTS = {Vector2D(0.0, 0.75), Vector2D(0.15, 1.0)};

Expand All @@ -14,32 +16,33 @@ CAnimationManager::CAnimationManager() {
BEZIER->setup(DEFAULTBEZIERPOINTS);
m_mBezierCurves["default"] = BEZIER;

m_events = makeShared<SAnimVarEvents>();
m_events = makeUnique<SAnimationManagerSignals>();
m_listeners = makeUnique<SAnimVarListeners>();

m_sListeners.connect = m_events->connect.registerListener([this](std::any data) { connectListener(data); });
m_sListeners.disconnect = m_events->disconnect.registerListener([this](std::any data) { disconnectListener(data); });
m_listeners->connect = m_events->connect.registerListener([this](std::any data) { onConnect(data); });
m_listeners->disconnect = m_events->disconnect.registerListener([this](std::any data) { onDisconnect(data); });
}

void CAnimationManager::connectListener(std::any data) {
void CAnimationManager::onConnect(std::any data) {
if (!m_bTickScheduled)
scheduleTick();

try {
const auto PAV = std::any_cast<SP<CBaseAnimatedVariable>>(data);
const auto PAV = std::any_cast<WP<CBaseAnimatedVariable>>(data);
if (!PAV)
return;

m_vActiveAnimatedVariables.emplace_back(PAV);
} catch (const std::bad_any_cast&) { return; }
}

void CAnimationManager::disconnectListener(std::any data) {
void CAnimationManager::onDisconnect(std::any data) {
try {
const auto PAV = std::any_cast<void*>(data);
const auto PAV = std::any_cast<WP<CBaseAnimatedVariable>>(data);
if (!PAV)
return;

std::erase_if(m_vActiveAnimatedVariables, [&](const auto& other) { return other.get() == PAV; });
std::erase_if(m_vActiveAnimatedVariables, [&](const auto& other) { return !other || other == PAV; });
} catch (const std::bad_any_cast&) { return; }
}

Expand Down Expand Up @@ -104,3 +107,7 @@ SP<CBezierCurve> CAnimationManager::getBezier(const std::string& name) {
const std::unordered_map<std::string, SP<CBezierCurve>>& CAnimationManager::getAllBeziers() {
return m_mBezierCurves;
}

CWeakPointer<CAnimationManager::SAnimationManagerSignals> CAnimationManager::getSignals() const {
return m_events;
}
7 changes: 4 additions & 3 deletions tests/animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class CMyAnimationManager : public CAnimationManager {
constexpr const eAVTypes EAVTYPE = std::is_same_v<VarType, int> ? eAVTypes::INT : eAVTypes::TEST;
const auto PAV = makeShared<CGenericAnimatedVariable<VarType, EmtpyContext>>();

PAV->create(EAVTYPE, PAV, m_events, v);
PAV->create(EAVTYPE, static_cast<CAnimationManager*>(this), PAV, v);
PAV->setConfig(animationTree.getConfig(animationConfigName));
av = std::move(PAV);
}
Expand Down Expand Up @@ -339,9 +339,9 @@ int main(int argc, char** argv, char** envp) {

// test getCurveValue
*s.m_iA = 0;
EXPECT(s.m_iA->getCurveValue(pAnimationManager.get()), 0.f);
EXPECT(s.m_iA->getCurveValue(), 0.f);
s.m_iA->warp();
EXPECT(s.m_iA->getCurveValue(pAnimationManager.get()), 1.f);
EXPECT(s.m_iA->getCurveValue(), 1.f);
EXPECT(endCallbackRan, 6);

// Test duplicate active anim vars are not allowed
Expand All @@ -365,6 +365,7 @@ int main(int argc, char** argv, char** envp) {
pAnimationManager->createAnimation(1, a, "default");
*a = 10;
pAnimationManager.reset();
EXPECT(a->isAnimationManagerDead(), true);
a->setValueAndWarp(11);
EXPECT(a->value(), 11);
*a = 12;
Expand Down

0 comments on commit 21dac77

Please sign in to comment.