summaryrefslogtreecommitdiff
path: root/src/helpers
diff options
context:
space:
mode:
authorGartoxFR <ewan.chorynski@insa-lyon.fr>2024-03-02 01:35:17 +0100
committerGitHub <noreply@github.com>2024-03-02 00:35:17 +0000
commitb2c34404777ea0850f90f4c38f64484f03599d14 (patch)
tree60f42773d2d606ae2072884cb537c751a7ab4b0f /src/helpers
parentf115ba94d2ec6e093c94f425535b027bc570185e (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')
-rw-r--r--src/helpers/AnimatedVariable.cpp54
-rw-r--r--src/helpers/AnimatedVariable.hpp313
-rw-r--r--src/helpers/Monitor.cpp4
-rw-r--r--src/helpers/WLClasses.cpp20
-rw-r--r--src/helpers/WLClasses.hpp20
5 files changed, 170 insertions, 241 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
+}
diff --git a/src/helpers/AnimatedVariable.hpp b/src/helpers/AnimatedVariable.hpp
index d87273b3..6efb394b 100644
--- a/src/helpers/AnimatedVariable.hpp
+++ b/src/helpers/AnimatedVariable.hpp
@@ -3,6 +3,7 @@
#include <functional>
#include <any>
#include <chrono>
+#include <type_traits>
#include "Vector2D.hpp"
#include "Color.hpp"
#include "../macros.hpp"
@@ -15,6 +16,30 @@ enum ANIMATEDVARTYPE {
AVARTYPE_COLOR
};
+// Utility to bind a type with its corresponding ANIMATEDVARTYPE
+template <class T>
+struct typeToANIMATEDVARTYPE_t {
+ static constexpr ANIMATEDVARTYPE value = AVARTYPE_INVALID;
+};
+
+template <>
+struct typeToANIMATEDVARTYPE_t<float> {
+ static constexpr ANIMATEDVARTYPE value = AVARTYPE_FLOAT;
+};
+
+template <>
+struct typeToANIMATEDVARTYPE_t<Vector2D> {
+ static constexpr ANIMATEDVARTYPE value = AVARTYPE_VECTOR;
+};
+
+template <>
+struct typeToANIMATEDVARTYPE_t<CColor> {
+ static constexpr ANIMATEDVARTYPE value = AVARTYPE_COLOR;
+};
+
+template <class T>
+inline constexpr ANIMATEDVARTYPE typeToANIMATEDVARTYPE = typeToANIMATEDVARTYPE_t<T>::value;
+
enum AVARDAMAGEPOLICY {
AVARDAMAGE_NONE = -1,
AVARDAMAGE_ENTIRE = 0,
@@ -28,174 +53,34 @@ struct SLayerSurface;
struct SAnimationPropertyConfig;
class CHyprRenderer;
-class CAnimatedVariable {
- public:
- CAnimatedVariable(); // dummy var
-
- void create(ANIMATEDVARTYPE, SAnimationPropertyConfig*, void* pWindow, AVARDAMAGEPOLICY);
- void create(ANIMATEDVARTYPE, std::any val, SAnimationPropertyConfig*, void* pWindow, AVARDAMAGEPOLICY);
-
- CAnimatedVariable(const CAnimatedVariable&) = delete;
- CAnimatedVariable(CAnimatedVariable&&) = delete;
- CAnimatedVariable& operator=(const CAnimatedVariable&) = delete;
- CAnimatedVariable& operator=(CAnimatedVariable&&) = delete;
-
- ~CAnimatedVariable();
-
- void unregister();
- void registerVar();
-
- // gets the current vector value (real time)
- const Vector2D& vec() const {
- return m_vValue;
- }
-
- // gets the current float value (real time)
- const float& fl() const {
- return m_fValue;
- }
-
- // gets the current color value (real time)
- const CColor& col() const {
- return m_cValue;
- }
-
- // gets the goal vector value
- const Vector2D& goalv() const {
- return m_vGoal;
- }
-
- // gets the goal float value
- const float& goalf() const {
- return m_fGoal;
- }
-
- // gets the goal color value
- const CColor& goalc() const {
- return m_cGoal;
- }
-
- CAnimatedVariable& operator=(const Vector2D& v) {
- if (v == m_vGoal)
- return *this;
-
- m_vGoal = v;
- animationBegin = std::chrono::system_clock::now();
- m_vBegun = m_vValue;
-
- onAnimationBegin();
-
- return *this;
- }
-
- CAnimatedVariable& operator=(const float& v) {
- if (v == m_fGoal)
- return *this;
-
- m_fGoal = v;
- animationBegin = std::chrono::system_clock::now();
- m_fBegun = m_fValue;
-
- onAnimationBegin();
-
- return *this;
- }
-
- CAnimatedVariable& operator=(const CColor& v) {
- if (v == m_cGoal)
- return *this;
-
- m_cGoal = v;
- animationBegin = std::chrono::system_clock::now();
- m_cBegun = m_cValue;
-
- onAnimationBegin();
-
- return *this;
- }
-
- // Sets the actual stored value, without affecting the goal, but resets the timer
- void setValue(const Vector2D& v) {
- if (v == m_vValue)
- return;
-
- m_vValue = v;
- animationBegin = std::chrono::system_clock::now();
- m_vBegun = m_vValue;
-
- onAnimationBegin();
- }
-
- // Sets the actual stored value, without affecting the goal, but resets the timer
- void setValue(const float& v) {
- if (v == m_fValue)
- return;
-
- m_fValue = v;
- animationBegin = std::chrono::system_clock::now();
- m_vBegun = m_vValue;
-
- onAnimationBegin();
- }
-
- // Sets the actual stored value, without affecting the goal, but resets the timer
- void setValue(const CColor& v) {
- if (v == m_cValue)
- return;
-
- m_cValue = v;
- animationBegin = std::chrono::system_clock::now();
- m_vBegun = m_vValue;
-
- onAnimationBegin();
- }
-
- // Sets the actual value and goal
- void setValueAndWarp(const Vector2D& v) {
- m_vGoal = v;
- warp();
- }
+// Utility to define a concept as a list of possible type
+template <class T, class... U>
+concept OneOf = (... or std::same_as<T, U>);
- // Sets the actual value and goal
- void setValueAndWarp(const float& v) {
- m_fGoal = v;
- warp();
- }
+// Concept to describe which type can be placed into CAnimatedVariable
+// This is mainly to get better errors if we put a type that's not supported
+// Otherwise template errors are ugly
+template <class T>
+concept Animable = OneOf<T, Vector2D, float, CColor>;
- // Sets the actual value and goal
- void setValueAndWarp(const CColor& v) {
- m_cGoal = v;
- warp();
- }
+class CBaseAnimatedVariable {
+ public:
+ CBaseAnimatedVariable(ANIMATEDVARTYPE type);
+ void create(SAnimationPropertyConfig* pAnimConfig, void* pWindow, AVARDAMAGEPOLICY policy);
- // checks if an animation is in progress
- inline bool isBeingAnimated() {
- return m_bIsBeingAnimated;
- }
+ CBaseAnimatedVariable(const CBaseAnimatedVariable&) = delete;
+ CBaseAnimatedVariable(CBaseAnimatedVariable&&) = delete;
+ CBaseAnimatedVariable& operator=(const CBaseAnimatedVariable&) = delete;
+ CBaseAnimatedVariable& operator=(CBaseAnimatedVariable&&) = delete;
- void warp(bool endCallback = true) {
- switch (m_eVarType) {
- case AVARTYPE_FLOAT: {
- m_fValue = m_fGoal;
- break;
- }
- case AVARTYPE_VECTOR: {
- m_vValue = m_vGoal;
- break;
- }
- case AVARTYPE_COLOR: {
- m_cValue = m_cGoal;
- break;
- }
- default: UNREACHABLE();
- }
+ virtual ~CBaseAnimatedVariable();
- m_bIsBeingAnimated = false;
+ void unregister();
+ void registerVar();
- if (endCallback)
- onAnimationEnd();
- }
+ virtual void warp(bool endCallback = true) = 0;
+ //
void setConfig(SAnimationPropertyConfig* pConfig) {
m_pConfig = pConfig;
}
@@ -212,6 +97,11 @@ class CAnimatedVariable {
/* returns the current curve value */
float getCurveValue();
+ // checks if an animation is in progress
+ inline bool isBeingAnimated() {
+ return m_bIsBeingAnimated;
+ }
+
/* sets a function to be ran when the animation finishes.
if an animation is not running, runs instantly.
if "remove" is set to true, will remove the callback when ran. */
@@ -245,20 +135,7 @@ class CAnimatedVariable {
m_bRemoveEndAfterRan = false;
}
- private:
- Vector2D m_vValue = Vector2D(0, 0);
- float m_fValue = 0;
- CColor m_cValue;
-
- Vector2D m_vGoal = Vector2D(0, 0);
- float m_fGoal = 0;
- CColor m_cGoal;
-
- Vector2D m_vBegun = Vector2D(0, 0);
- float m_fBegun = 0;
- CColor m_cBegun;
-
- // owners
+ protected:
void* m_pWindow = nullptr;
void* m_pWorkspace = nullptr;
void* m_pLayer = nullptr;
@@ -271,8 +148,8 @@ class CAnimatedVariable {
std::chrono::system_clock::time_point animationBegin;
- ANIMATEDVARTYPE m_eVarType = AVARTYPE_INVALID;
AVARDAMAGEPOLICY m_eDamagePolicy = AVARDAMAGE_NONE;
+ ANIMATEDVARTYPE m_Type;
bool m_bRemoveEndAfterRan = true;
bool m_bRemoveBeginAfterRan = true;
@@ -281,7 +158,9 @@ class CAnimatedVariable {
std::function<void(void* thisptr)> m_fUpdateCallback;
bool m_bIsConnectedToActive = false;
+
void connectToActive();
+
void disconnectFromActive();
// methods
@@ -314,3 +193,85 @@ class CAnimatedVariable {
friend struct SLayerSurface;
friend class CHyprRenderer;
};
+
+template <Animable VarType>
+class CAnimatedVariable : public CBaseAnimatedVariable {
+ public:
+ CAnimatedVariable() : CBaseAnimatedVariable(typeToANIMATEDVARTYPE<VarType>) {} // dummy var
+
+ void create(const VarType& value, SAnimationPropertyConfig* pAnimConfig, void* pWindow, AVARDAMAGEPOLICY policy) {
+ create(pAnimConfig, pWindow, policy);
+ m_Value = value;
+ }
+
+ using CBaseAnimatedVariable::create;
+
+ CAnimatedVariable(const CAnimatedVariable&) = delete;
+ CAnimatedVariable(CAnimatedVariable&&) = delete;
+ CAnimatedVariable& operator=(const CAnimatedVariable&) = delete;
+ CAnimatedVariable& operator=(CAnimatedVariable&&) = delete;
+
+ ~CAnimatedVariable() = default;
+
+ // gets the current vector value (real time)
+ const VarType& value() const {
+ return m_Value;
+ }
+
+ // gets the goal vector value
+ const VarType& goal() const {
+ return m_Goal;
+ }
+
+ CAnimatedVariable& operator=(const VarType& v) {
+ if (v == m_Goal)
+ return *this;
+
+ m_Goal = v;
+ animationBegin = std::chrono::system_clock::now();
+ m_Begun = m_Value;
+
+ onAnimationBegin();
+
+ return *this;
+ }
+
+ // Sets the actual stored value, without affecting the goal, but resets the timer
+ void setValue(const VarType& v) {
+ if (v == m_Value)
+ return;
+
+ m_Value = v;
+ animationBegin = std::chrono::system_clock::now();
+ m_Begun = m_Value;
+
+ onAnimationBegin();
+ }
+
+ // Sets the actual value and goal
+ void setValueAndWarp(const VarType& v) {
+ m_Goal = v;
+ warp();
+ }
+
+ void warp(bool endCallback = true) override {
+ m_Value = m_Goal;
+
+ m_bIsBeingAnimated = false;
+
+ if (endCallback)
+ onAnimationEnd();
+ }
+
+ private:
+ VarType m_Value{};
+ VarType m_Goal{};
+ VarType m_Begun{};
+
+ // owners
+
+ friend class CAnimationManager;
+ friend class CWorkspace;
+ friend struct SLayerSurface;
+ friend class CHyprRenderer;
+};
diff --git a/src/helpers/Monitor.cpp b/src/helpers/Monitor.cpp
index 2dd2a2d9..7f36ccd4 100644
--- a/src/helpers/Monitor.cpp
+++ b/src/helpers/Monitor.cpp
@@ -661,11 +661,11 @@ void CMonitor::setSpecialWorkspace(CWorkspace* const pWorkspace) {
if (w->m_bIsFloating && !VECINRECT(MIDDLE, vecPosition.x, vecPosition.y, vecPosition.x + vecSize.x, vecPosition.y + vecSize.y) && w->m_iX11Type != 2) {
// if it's floating and the middle isnt on the current mon, move it to the center
const auto PMONFROMMIDDLE = g_pCompositor->getMonitorFromVector(MIDDLE);
- Vector2D pos = w->m_vRealPosition.goalv();
+ Vector2D pos = w->m_vRealPosition.goal();
if (!VECINRECT(MIDDLE, PMONFROMMIDDLE->vecPosition.x, PMONFROMMIDDLE->vecPosition.y, PMONFROMMIDDLE->vecPosition.x + PMONFROMMIDDLE->vecSize.x,
PMONFROMMIDDLE->vecPosition.y + PMONFROMMIDDLE->vecSize.y)) {
// not on any monitor, center
- pos = middle() / 2.f - w->m_vRealSize.goalv() / 2.f;
+ pos = middle() / 2.f - w->m_vRealSize.goal() / 2.f;
} else
pos = pos - PMONFROMMIDDLE->vecPosition + vecPosition;
diff --git a/src/helpers/WLClasses.cpp b/src/helpers/WLClasses.cpp
index 43bba7c5..31db2c55 100644
--- a/src/helpers/WLClasses.cpp
+++ b/src/helpers/WLClasses.cpp
@@ -3,9 +3,9 @@
#include "../Compositor.hpp"
SLayerSurface::SLayerSurface() {
- alpha.create(AVARTYPE_FLOAT, g_pConfigManager->getAnimationPropertyConfig("fadeLayers"), nullptr, AVARDAMAGE_ENTIRE);
- realPosition.create(AVARTYPE_VECTOR, g_pConfigManager->getAnimationPropertyConfig("layers"), nullptr, AVARDAMAGE_ENTIRE);
- realSize.create(AVARTYPE_VECTOR, g_pConfigManager->getAnimationPropertyConfig("layers"), nullptr, AVARDAMAGE_ENTIRE);
+ alpha.create(g_pConfigManager->getAnimationPropertyConfig("fadeLayers"), nullptr, AVARDAMAGE_ENTIRE);
+ realPosition.create(g_pConfigManager->getAnimationPropertyConfig("layers"), nullptr, AVARDAMAGE_ENTIRE);
+ realSize.create(g_pConfigManager->getAnimationPropertyConfig("layers"), nullptr, AVARDAMAGE_ENTIRE);
alpha.m_pLayer = this;
realPosition.m_pLayer = this;
realSize.m_pLayer = this;
@@ -181,11 +181,11 @@ CRegion SConstraint::getLogicCoordsRegion() {
result.add(&constraint->region); // surface-local coords
if (!PWINDOWOWNER->m_bIsX11) {
- result.translate(PWINDOWOWNER->m_vRealPosition.goalv());
+ result.translate(PWINDOWOWNER->m_vRealPosition.goal());
return result;
}
- const auto COORDS = PWINDOWOWNER->m_bIsMapped ? PWINDOWOWNER->m_vRealPosition.goalv() :
+ const auto COORDS = PWINDOWOWNER->m_bIsMapped ? PWINDOWOWNER->m_vRealPosition.goal() :
g_pXWaylandManager->xwaylandToWaylandCoords({PWINDOWOWNER->m_uSurface.xwayland->x, PWINDOWOWNER->m_uSurface.xwayland->y});
const auto PMONITOR = PWINDOWOWNER->m_bIsMapped ? g_pCompositor->getMonitorFromID(PWINDOWOWNER->m_iMonitorID) : g_pCompositor->getMonitorFromVector(COORDS);
@@ -210,9 +210,9 @@ Vector2D SConstraint::getLogicConstraintPos() {
return {};
if (!PWINDOWOWNER->m_bIsX11)
- return PWINDOWOWNER->m_vRealPosition.goalv();
+ return PWINDOWOWNER->m_vRealPosition.goal();
- const auto COORDS = PWINDOWOWNER->m_bIsMapped ? PWINDOWOWNER->m_vRealPosition.goalv() :
+ const auto COORDS = PWINDOWOWNER->m_bIsMapped ? PWINDOWOWNER->m_vRealPosition.goal() :
g_pXWaylandManager->xwaylandToWaylandCoords({PWINDOWOWNER->m_uSurface.xwayland->x, PWINDOWOWNER->m_uSurface.xwayland->y});
return COORDS;
@@ -228,7 +228,7 @@ Vector2D SConstraint::getLogicConstraintSize() {
return {};
if (!PWINDOWOWNER->m_bIsX11)
- return PWINDOWOWNER->m_vRealSize.goalv();
+ return PWINDOWOWNER->m_vRealSize.goal();
const auto PMONITOR = PWINDOWOWNER->m_bIsMapped ?
g_pCompositor->getMonitorFromID(PWINDOWOWNER->m_iMonitorID) :
@@ -237,8 +237,8 @@ Vector2D SConstraint::getLogicConstraintSize() {
if (!PMONITOR)
return {};
- const auto SIZE = PWINDOWOWNER->m_bIsMapped ? PWINDOWOWNER->m_vRealSize.goalv() :
+ const auto SIZE = PWINDOWOWNER->m_bIsMapped ? PWINDOWOWNER->m_vRealSize.goal() :
Vector2D{PWINDOWOWNER->m_uSurface.xwayland->width, PWINDOWOWNER->m_uSurface.xwayland->height} * PMONITOR->xwaylandScale;
return SIZE;
-} \ No newline at end of file
+}
diff --git a/src/helpers/WLClasses.hpp b/src/helpers/WLClasses.hpp
index b307191c..c9cb47e4 100644
--- a/src/helpers/WLClasses.hpp
+++ b/src/helpers/WLClasses.hpp
@@ -19,19 +19,19 @@ struct SLayerSurface {
SLayerSurface();
~SLayerSurface();
- void applyRules();
- void startAnimation(bool in, bool instant = false);
- bool isFadedOut();
+ void applyRules();
+ void startAnimation(bool in, bool instant = false);
+ bool isFadedOut();
- CAnimatedVariable realPosition;
- CAnimatedVariable realSize;
+ CAnimatedVariable<Vector2D> realPosition;
+ CAnimatedVariable<Vector2D> realSize;
- wlr_layer_surface_v1* layerSurface;
- wl_list link;
+ wlr_layer_surface_v1* layerSurface;
+ wl_list link;
- bool keyboardExclusive = false;
+ bool keyboardExclusive = false;
- CWLSurface surface;
+ CWLSurface surface;
// desktop components
std::unique_ptr<CPopup> popupHead;
@@ -51,7 +51,7 @@ struct SLayerSurface {
std::string szNamespace = "";
- CAnimatedVariable alpha;
+ CAnimatedVariable<float> alpha;
bool fadingOut = false;
bool readyToDelete = false;
bool noProcess = false;