animation: allow/intend for animated vars to be unique pointers (#93)

This commit is contained in:
Maximilian Seidler 2025-11-28 17:08:50 +00:00 committed by GitHub
parent 0168583075
commit a64517c236
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 42 additions and 39 deletions

View file

@ -22,6 +22,7 @@ namespace Hyprutils {
}; };
void create(CAnimationManager*, int, Memory::CSharedPointer<CBaseAnimatedVariable>); void create(CAnimationManager*, int, Memory::CSharedPointer<CBaseAnimatedVariable>);
void create2(CAnimationManager*, int, Memory::CWeakPointer<CBaseAnimatedVariable>);
void connectToActive(); void connectToActive();
void disconnectFromActive(); void disconnectFromActive();
@ -136,6 +137,7 @@ namespace Hyprutils {
public: public:
CGenericAnimatedVariable() = default; CGenericAnimatedVariable() = default;
/* Deprecated: use create2 */
void create(const int typeInfo, CAnimationManager* pAnimationManager, Memory::CSharedPointer<CGenericAnimatedVariable<VarType, AnimationContext>> pSelf, void create(const int typeInfo, CAnimationManager* pAnimationManager, Memory::CSharedPointer<CGenericAnimatedVariable<VarType, AnimationContext>> pSelf,
const VarType& initialValue) { const VarType& initialValue) {
m_Begun = initialValue; m_Begun = initialValue;
@ -145,6 +147,16 @@ namespace Hyprutils {
CBaseAnimatedVariable::create(pAnimationManager, typeInfo, pSelf); 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<CGenericAnimatedVariable<VarType, AnimationContext>> pSelf,
const VarType& initialValue) {
m_Begun = initialValue;
m_Value = initialValue;
m_Goal = initialValue;
CBaseAnimatedVariable::create2(pAnimationManager, typeInfo, pSelf);
}
CGenericAnimatedVariable(const CGenericAnimatedVariable&) = delete; CGenericAnimatedVariable(const CGenericAnimatedVariable&) = delete;
CGenericAnimatedVariable(CGenericAnimatedVariable&&) = delete; CGenericAnimatedVariable(CGenericAnimatedVariable&&) = delete;
CGenericAnimatedVariable& operator=(const CGenericAnimatedVariable&) = delete; CGenericAnimatedVariable& operator=(const CGenericAnimatedVariable&) = delete;

View file

@ -13,7 +13,16 @@ static const std::string DEFAULTSTYLE = "";
void CBaseAnimatedVariable::create(CAnimationManager* pManager, int typeInfo, SP<CBaseAnimatedVariable> pSelf) { void CBaseAnimatedVariable::create(CAnimationManager* pManager, int typeInfo, SP<CBaseAnimatedVariable> pSelf) {
m_Type = typeInfo; 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<CBaseAnimatedVariable> pSelf) {
m_Type = typeInfo;
m_pSelf = std::move(pSelf);
m_pAnimationManager = pManager; m_pAnimationManager = pManager;
m_pSignals = pManager->getSignals(); m_pSignals = pManager->getSignals();
@ -37,28 +46,22 @@ void CBaseAnimatedVariable::disconnectFromActive() {
} }
bool Hyprutils::Animation::CBaseAnimatedVariable::enabled() const { bool Hyprutils::Animation::CBaseAnimatedVariable::enabled() const {
if (const auto PCONFIG = m_pConfig.lock()) { if (m_pConfig && m_pConfig->pValues)
const auto PVALUES = PCONFIG->pValues.lock(); return m_pConfig->pValues->internalEnabled;
return PVALUES ? PVALUES->internalEnabled : false;
}
return false; return false;
} }
const std::string& CBaseAnimatedVariable::getBezierName() const { const std::string& CBaseAnimatedVariable::getBezierName() const {
if (const auto PCONFIG = m_pConfig.lock()) { if (m_pConfig && m_pConfig->pValues)
const auto PVALUES = PCONFIG->pValues.lock(); return m_pConfig->pValues->internalBezier;
return PVALUES ? PVALUES->internalBezier : DEFAULTBEZIERNAME;
}
return DEFAULTBEZIERNAME; return DEFAULTBEZIERNAME;
} }
const std::string& CBaseAnimatedVariable::getStyle() const { const std::string& CBaseAnimatedVariable::getStyle() const {
if (const auto PCONFIG = m_pConfig.lock()) { if (m_pConfig && m_pConfig->pValues)
const auto PVALUES = PCONFIG->pValues.lock(); return m_pConfig->pValues->internalStyle;
return PVALUES ? PVALUES->internalStyle : DEFAULTSTYLE;
}
return DEFAULTSTYLE; return DEFAULTSTYLE;
} }
@ -66,10 +69,8 @@ const std::string& CBaseAnimatedVariable::getStyle() const {
float CBaseAnimatedVariable::getPercent() const { float CBaseAnimatedVariable::getPercent() const {
const auto DURATIONPASSED = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - animationBegin).count(); const auto DURATIONPASSED = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - animationBegin).count();
if (const auto PCONFIG = m_pConfig.lock()) { if (m_pConfig && m_pConfig->pValues)
const auto PVALUES = PCONFIG->pValues.lock(); return std::clamp((DURATIONPASSED / 100.f) / m_pConfig->pValues->internalSpeed, 0.f, 1.f);
return PVALUES ? std::clamp((DURATIONPASSED / 100.f) / PVALUES->internalSpeed, 0.f, 1.f) : 1.f;
}
return 1.f; return 1.f;
} }
@ -78,14 +79,7 @@ float CBaseAnimatedVariable::getCurveValue() const {
if (!m_bIsBeingAnimated || isAnimationManagerDead()) if (!m_bIsBeingAnimated || isAnimationManagerDead())
return 1.f; return 1.f;
std::string bezierName = ""; const auto BEZIER = m_pAnimationManager->getBezier(getBezierName());
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);
if (!BEZIER) if (!BEZIER)
return 1.f; return 1.f;

View file

@ -64,14 +64,13 @@ void CAnimationManager::rotateActive() {
std::vector<CWeakPointer<CBaseAnimatedVariable>> active; std::vector<CWeakPointer<CBaseAnimatedVariable>> active;
active.reserve(m_vActiveAnimatedVariables.size()); // avoid reallocations active.reserve(m_vActiveAnimatedVariables.size()); // avoid reallocations
for (auto const& av : m_vActiveAnimatedVariables) { for (auto const& av : m_vActiveAnimatedVariables) {
const auto PAV = av.lock(); if (!av)
if (!PAV)
continue; continue;
if (PAV->ok() && PAV->isBeingAnimated()) if (av->ok() && av->isBeingAnimated())
active.emplace_back(av); active.emplace_back(av);
else else
PAV->m_bIsConnectedToActive = false; av->m_bIsConnectedToActive = false;
} }
m_vActiveAnimatedVariables = std::move(active); m_vActiveAnimatedVariables = std::move(active);

View file

@ -21,7 +21,7 @@ template <typename VarType>
using CAnimatedVariable = CGenericAnimatedVariable<VarType, EmtpyContext>; using CAnimatedVariable = CGenericAnimatedVariable<VarType, EmtpyContext>;
template <typename VarType> template <typename VarType>
using PANIMVAR = SP<CAnimatedVariable<VarType>>; using PANIMVAR = UP<CAnimatedVariable<VarType>>;
template <typename VarType> template <typename VarType>
using PANIMVARREF = WP<CAnimatedVariable<VarType>>; using PANIMVARREF = WP<CAnimatedVariable<VarType>>;
@ -47,8 +47,7 @@ CAnimationConfigTree animationTree;
class CMyAnimationManager : public CAnimationManager { class CMyAnimationManager : public CAnimationManager {
public: public:
void tick() { void tick() {
for (size_t i = 0; i < m_vActiveAnimatedVariables.size(); i++) { for (const auto& PAV : m_vActiveAnimatedVariables) {
const auto PAV = m_vActiveAnimatedVariables[i].lock();
if (!PAV || !PAV->ok() || !PAV->isBeingAnimated()) if (!PAV || !PAV->ok() || !PAV->isBeingAnimated())
continue; continue;
@ -68,7 +67,7 @@ class CMyAnimationManager : public CAnimationManager {
if (!avInt) if (!avInt)
std::cout << "Dynamic cast upcast failed\n"; 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); avInt->value() = avInt->begun() + (DELTA * POINTY);
} break; } break;
case eAVTypes::TEST: { case eAVTypes::TEST: {
@ -93,11 +92,10 @@ class CMyAnimationManager : public CAnimationManager {
template <typename VarType> template <typename VarType>
void createAnimation(const VarType& v, PANIMVAR<VarType>& av, const std::string& animationConfigName) { void createAnimation(const VarType& v, PANIMVAR<VarType>& av, const std::string& animationConfigName) {
constexpr const eAVTypes EAVTYPE = std::is_same_v<VarType, int> ? eAVTypes::INT : eAVTypes::TEST; constexpr const eAVTypes EAVTYPE = std::is_same_v<VarType, int> ? eAVTypes::INT : eAVTypes::TEST;
const auto PAV = makeShared<CGenericAnimatedVariable<VarType, EmtpyContext>>(); av = makeUnique<CGenericAnimatedVariable<VarType, EmtpyContext>>();
PAV->create(EAVTYPE, sc<CAnimationManager*>(this), PAV, v); av->create2(EAVTYPE, sc<CAnimationManager*>(this), av, v);
PAV->setConfig(animationTree.getConfig(animationConfigName)); av->setConfig(animationTree.getConfig(animationConfigName));
av = std::move(PAV);
} }
virtual void scheduleTick() { virtual void scheduleTick() {
@ -298,7 +296,7 @@ TEST(Animation, animation) {
// test adding / removing vars during a tick // test adding / removing vars during a tick
s.m_iA->resetAllCallbacks(); s.m_iA->resetAllCallbacks();
s.m_iA->setUpdateCallback([&vars](WP<CBaseAnimatedVariable> v) { s.m_iA->setUpdateCallback([&vars](WP<CBaseAnimatedVariable> v) {
if (v.lock() != vars.back()) if (v.get() != vars.back().get())
vars.back()->warp(); vars.back()->warp();
}); });
s.m_iA->setCallbackOnEnd([&s, &vars](auto) { s.m_iA->setCallbackOnEnd([&s, &vars](auto) {
@ -350,7 +348,7 @@ TEST(Animation, animation) {
*s.m_iA = 5; *s.m_iA = 5;
s.m_iA->setCallbackOnEnd([&endCallbackRan](WP<CBaseAnimatedVariable> v) { s.m_iA->setCallbackOnEnd([&endCallbackRan](WP<CBaseAnimatedVariable> v) {
endCallbackRan++; endCallbackRan++;
const auto PAV = dc<CAnimatedVariable<int>*>(v.lock().get()); const auto PAV = dc<CAnimatedVariable<int>*>(v.get());
*PAV = 10; *PAV = 10;
PAV->setCallbackOnEnd([&endCallbackRan](WP<CBaseAnimatedVariable> v) { endCallbackRan++; }); PAV->setCallbackOnEnd([&endCallbackRan](WP<CBaseAnimatedVariable> v) { endCallbackRan++; });