Hyprland/src/helpers/AnimatedVariable.cpp

101 lines
3.2 KiB
C++
Raw Normal View History

2022-04-23 14:16:02 +02:00
#include "AnimatedVariable.hpp"
#include "../managers/AnimationManager.hpp"
2022-07-28 13:28:43 +02:00
#include "../config/ConfigManager.hpp"
2022-04-23 14:16:02 +02:00
CAnimatedVariable::CAnimatedVariable() {
; // dummy var
}
2022-07-28 13:28:43 +02:00
void CAnimatedVariable::create(ANIMATEDVARTYPE type, SAnimationPropertyConfig* pAnimConfig, void* pWindow, AVARDAMAGEPOLICY policy) {
m_eVarType = type;
m_eDamagePolicy = policy;
m_pConfig = pAnimConfig;
m_pWindow = pWindow;
2022-04-23 14:16:02 +02:00
m_bDummy = false;
}
2022-07-28 13:28:43 +02:00
void CAnimatedVariable::create(ANIMATEDVARTYPE type, std::any val, SAnimationPropertyConfig* pAnimConfig, void* pWindow, AVARDAMAGEPOLICY policy) {
create(type, pAnimConfig, pWindow, policy);
2022-04-23 14:16:02 +02:00
try {
switch (type) {
case AVARTYPE_FLOAT: {
const auto V = std::any_cast<float>(val);
m_fValue = V;
m_fGoal = V;
2022-04-23 14:16:02 +02:00
break;
}
case AVARTYPE_VECTOR: {
const auto V = std::any_cast<Vector2D>(val);
m_vValue = V;
m_vGoal = V;
2022-04-23 14:16:02 +02:00
break;
}
case AVARTYPE_COLOR: {
const auto V = std::any_cast<CColor>(val);
m_cValue = V;
m_cGoal = V;
2022-04-23 14:16:02 +02:00
break;
}
default: ASSERT(false); break;
2022-04-23 14:16:02 +02:00
}
} catch (std::exception& e) {
2023-09-06 12:51:36 +02:00
Debug::log(ERR, "CAnimatedVariable create error: {}", e.what());
RASSERT(false, "CAnimatedVariable create error: {}", e.what());
2022-04-23 14:16:02 +02:00
}
}
CAnimatedVariable::~CAnimatedVariable() {
2022-05-12 11:27:31 +02:00
unregister();
}
void CAnimatedVariable::unregister() {
if (!g_pAnimationManager)
return;
std::erase_if(g_pAnimationManager->m_vAnimatedVariables, [&](const auto& other) { return other == this; });
2022-11-05 14:37:29 +01:00
m_bIsRegistered = false;
disconnectFromActive();
2022-07-28 13:28:43 +02:00
}
void CAnimatedVariable::registerVar() {
2022-11-05 14:37:29 +01:00
if (!m_bIsRegistered)
g_pAnimationManager->m_vAnimatedVariables.push_back(this);
2022-11-05 14:37:29 +01:00
m_bIsRegistered = true;
}
2022-07-28 13:28:43 +02:00
int CAnimatedVariable::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);
2022-07-28 13:28:43 +02:00
}
2022-11-06 18:52:09 +01:00
float CAnimatedVariable::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);
2022-12-29 12:30:43 +01:00
}
float CAnimatedVariable::getCurveValue() {
if (!m_bIsBeingAnimated)
return 1.f;
2022-12-29 12:30:43 +01:00
const auto SPENT = getPercent();
if (SPENT >= 1.f)
return 1.f;
return g_pAnimationManager->getBezier(m_pConfig->pValues->internalBezier)->getYForPoint(SPENT);
}
void CAnimatedVariable::connectToActive() {
2023-08-05 23:29:33 +02:00
g_pAnimationManager->scheduleTick(); // otherwise the animation manager will never pick this up
if (!m_bIsConnectedToActive)
g_pAnimationManager->m_vActiveAnimatedVariables.push_back(this);
2023-08-05 23:29:33 +02:00
m_bIsConnectedToActive = true;
}
void CAnimatedVariable::disconnectFromActive() {
std::erase_if(g_pAnimationManager->m_vActiveAnimatedVariables, [&](const auto& other) { return other == this; });
m_bIsConnectedToActive = false;
2022-11-06 18:52:09 +01:00
}