2022-04-23 14:16:02 +02:00
|
|
|
#pragma once
|
|
|
|
|
2023-08-07 13:35:19 +02:00
|
|
|
#include <functional>
|
2022-04-23 14:16:02 +02:00
|
|
|
#include <any>
|
2023-08-07 13:35:19 +02:00
|
|
|
#include <chrono>
|
|
|
|
#include "Vector2D.hpp"
|
|
|
|
#include "Color.hpp"
|
|
|
|
#include "../macros.hpp"
|
2023-09-30 03:09:08 +02:00
|
|
|
#include "../debug/Log.hpp"
|
2022-04-23 14:16:02 +02:00
|
|
|
|
2023-12-06 23:54:56 +01:00
|
|
|
enum ANIMATEDVARTYPE {
|
2022-04-23 14:16:02 +02:00
|
|
|
AVARTYPE_INVALID = -1,
|
|
|
|
AVARTYPE_FLOAT,
|
|
|
|
AVARTYPE_VECTOR,
|
|
|
|
AVARTYPE_COLOR
|
|
|
|
};
|
|
|
|
|
2023-12-06 23:54:56 +01:00
|
|
|
enum AVARDAMAGEPOLICY {
|
2023-01-20 20:48:07 +01:00
|
|
|
AVARDAMAGE_NONE = -1,
|
|
|
|
AVARDAMAGE_ENTIRE = 0,
|
2022-07-16 12:44:45 +02:00
|
|
|
AVARDAMAGE_BORDER,
|
|
|
|
AVARDAMAGE_SHADOW
|
2022-05-05 14:02:30 +02:00
|
|
|
};
|
|
|
|
|
2022-04-23 14:16:02 +02:00
|
|
|
class CAnimationManager;
|
2022-05-12 11:27:31 +02:00
|
|
|
class CWorkspace;
|
2022-05-14 17:23:46 +02:00
|
|
|
struct SLayerSurface;
|
2022-07-28 13:28:43 +02:00
|
|
|
struct SAnimationPropertyConfig;
|
2022-11-06 18:52:09 +01:00
|
|
|
class CHyprRenderer;
|
2022-04-23 14:16:02 +02:00
|
|
|
|
|
|
|
class CAnimatedVariable {
|
2022-12-16 18:17:31 +01:00
|
|
|
public:
|
2022-04-23 14:16:02 +02:00
|
|
|
CAnimatedVariable(); // dummy var
|
|
|
|
|
2022-07-28 13:28:43 +02:00
|
|
|
void create(ANIMATEDVARTYPE, SAnimationPropertyConfig*, void* pWindow, AVARDAMAGEPOLICY);
|
|
|
|
void create(ANIMATEDVARTYPE, std::any val, SAnimationPropertyConfig*, void* pWindow, AVARDAMAGEPOLICY);
|
2022-04-23 14:16:02 +02:00
|
|
|
|
2023-12-06 23:54:56 +01:00
|
|
|
CAnimatedVariable(const CAnimatedVariable&) = delete;
|
|
|
|
CAnimatedVariable(CAnimatedVariable&&) = delete;
|
2023-05-16 13:49:59 +02:00
|
|
|
CAnimatedVariable& operator=(const CAnimatedVariable&) = delete;
|
2023-12-06 23:54:56 +01:00
|
|
|
CAnimatedVariable& operator=(CAnimatedVariable&&) = delete;
|
2023-05-16 13:49:59 +02:00
|
|
|
|
2022-04-23 14:16:02 +02:00
|
|
|
~CAnimatedVariable();
|
|
|
|
|
2022-05-12 11:27:31 +02:00
|
|
|
void unregister();
|
2022-11-04 16:56:31 +01:00
|
|
|
void registerVar();
|
2022-05-12 11:27:31 +02:00
|
|
|
|
2022-04-23 14:35:34 +02:00
|
|
|
// gets the current vector value (real time)
|
2022-04-23 14:40:51 +02:00
|
|
|
const Vector2D& vec() const {
|
2022-04-23 14:16:02 +02:00
|
|
|
return m_vValue;
|
|
|
|
}
|
|
|
|
|
2022-04-23 14:35:34 +02:00
|
|
|
// gets the current float value (real time)
|
2022-04-23 14:40:51 +02:00
|
|
|
const float& fl() const {
|
2022-04-23 14:16:02 +02:00
|
|
|
return m_fValue;
|
|
|
|
}
|
|
|
|
|
2022-04-23 14:35:34 +02:00
|
|
|
// gets the current color value (real time)
|
2022-04-23 14:40:51 +02:00
|
|
|
const CColor& col() const {
|
2022-04-23 14:16:02 +02:00
|
|
|
return m_cValue;
|
|
|
|
}
|
|
|
|
|
2022-04-23 14:35:34 +02:00
|
|
|
// gets the goal vector value
|
2022-04-23 14:40:51 +02:00
|
|
|
const Vector2D& goalv() const {
|
2022-04-23 14:35:34 +02:00
|
|
|
return m_vGoal;
|
|
|
|
}
|
|
|
|
|
|
|
|
// gets the goal float value
|
2022-04-23 14:40:51 +02:00
|
|
|
const float& goalf() const {
|
2022-04-23 14:35:34 +02:00
|
|
|
return m_fGoal;
|
|
|
|
}
|
|
|
|
|
|
|
|
// gets the goal color value
|
2022-04-23 14:40:51 +02:00
|
|
|
const CColor& goalc() const {
|
2022-04-23 14:35:34 +02:00
|
|
|
return m_cGoal;
|
|
|
|
}
|
|
|
|
|
2022-12-16 18:17:31 +01:00
|
|
|
CAnimatedVariable& operator=(const Vector2D& v) {
|
2023-09-04 13:11:56 +02:00
|
|
|
if (v == m_vGoal)
|
|
|
|
return *this;
|
|
|
|
|
2022-12-16 18:17:31 +01:00
|
|
|
m_vGoal = v;
|
2022-04-23 21:47:16 +02:00
|
|
|
animationBegin = std::chrono::system_clock::now();
|
2022-12-16 18:17:31 +01:00
|
|
|
m_vBegun = m_vValue;
|
2022-11-06 18:52:09 +01:00
|
|
|
|
|
|
|
onAnimationBegin();
|
2022-12-16 18:17:31 +01:00
|
|
|
|
|
|
|
return *this;
|
2022-04-23 14:16:02 +02:00
|
|
|
}
|
|
|
|
|
2022-12-16 18:17:31 +01:00
|
|
|
CAnimatedVariable& operator=(const float& v) {
|
2023-09-04 13:11:56 +02:00
|
|
|
if (v == m_fGoal)
|
|
|
|
return *this;
|
|
|
|
|
2022-12-16 18:17:31 +01:00
|
|
|
m_fGoal = v;
|
2022-04-23 21:47:16 +02:00
|
|
|
animationBegin = std::chrono::system_clock::now();
|
2022-12-16 18:17:31 +01:00
|
|
|
m_fBegun = m_fValue;
|
2022-11-06 18:52:09 +01:00
|
|
|
|
|
|
|
onAnimationBegin();
|
2022-12-16 18:17:31 +01:00
|
|
|
|
|
|
|
return *this;
|
2022-04-23 14:16:02 +02:00
|
|
|
}
|
|
|
|
|
2022-12-16 18:17:31 +01:00
|
|
|
CAnimatedVariable& operator=(const CColor& v) {
|
2023-09-04 13:11:56 +02:00
|
|
|
if (v == m_cGoal)
|
|
|
|
return *this;
|
|
|
|
|
2022-12-16 18:17:31 +01:00
|
|
|
m_cGoal = v;
|
2022-04-23 21:47:16 +02:00
|
|
|
animationBegin = std::chrono::system_clock::now();
|
2022-12-16 18:17:31 +01:00
|
|
|
m_cBegun = m_cValue;
|
2022-11-06 18:52:09 +01:00
|
|
|
|
|
|
|
onAnimationBegin();
|
2022-12-16 18:17:31 +01:00
|
|
|
|
|
|
|
return *this;
|
2022-04-23 14:16:02 +02:00
|
|
|
}
|
|
|
|
|
2022-04-23 21:47:16 +02:00
|
|
|
// Sets the actual stored value, without affecting the goal, but resets the timer
|
2022-04-23 14:16:02 +02:00
|
|
|
void setValue(const Vector2D& v) {
|
2023-09-04 13:11:56 +02:00
|
|
|
if (v == m_vValue)
|
|
|
|
return;
|
|
|
|
|
2022-12-16 18:17:31 +01:00
|
|
|
m_vValue = v;
|
2022-04-23 21:47:16 +02:00
|
|
|
animationBegin = std::chrono::system_clock::now();
|
2022-12-16 18:17:31 +01:00
|
|
|
m_vBegun = m_vValue;
|
2022-11-06 18:52:09 +01:00
|
|
|
|
|
|
|
onAnimationBegin();
|
2022-04-23 14:16:02 +02:00
|
|
|
}
|
|
|
|
|
2022-04-23 21:47:16 +02:00
|
|
|
// Sets the actual stored value, without affecting the goal, but resets the timer
|
2022-04-23 14:16:02 +02:00
|
|
|
void setValue(const float& v) {
|
2023-09-04 13:11:56 +02:00
|
|
|
if (v == m_fValue)
|
|
|
|
return;
|
|
|
|
|
2022-12-16 18:17:31 +01:00
|
|
|
m_fValue = v;
|
2022-04-23 21:47:16 +02:00
|
|
|
animationBegin = std::chrono::system_clock::now();
|
2022-12-16 18:17:31 +01:00
|
|
|
m_vBegun = m_vValue;
|
2022-11-06 18:52:09 +01:00
|
|
|
|
|
|
|
onAnimationBegin();
|
2022-04-23 14:16:02 +02:00
|
|
|
}
|
|
|
|
|
2022-04-23 21:47:16 +02:00
|
|
|
// Sets the actual stored value, without affecting the goal, but resets the timer
|
2022-04-23 14:16:02 +02:00
|
|
|
void setValue(const CColor& v) {
|
2023-09-04 13:11:56 +02:00
|
|
|
if (v == m_cValue)
|
|
|
|
return;
|
|
|
|
|
2022-12-16 18:17:31 +01:00
|
|
|
m_cValue = v;
|
2022-04-23 21:47:16 +02:00
|
|
|
animationBegin = std::chrono::system_clock::now();
|
2022-12-16 18:17:31 +01:00
|
|
|
m_vBegun = m_vValue;
|
2022-11-06 18:52:09 +01:00
|
|
|
|
|
|
|
onAnimationBegin();
|
2022-04-23 14:16:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sets the actual value and goal
|
|
|
|
void setValueAndWarp(const Vector2D& v) {
|
|
|
|
m_vGoal = v;
|
|
|
|
warp();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets the actual value and goal
|
|
|
|
void setValueAndWarp(const float& v) {
|
|
|
|
m_fGoal = v;
|
|
|
|
warp();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets the actual value and goal
|
|
|
|
void setValueAndWarp(const CColor& v) {
|
|
|
|
m_cGoal = v;
|
|
|
|
warp();
|
|
|
|
}
|
|
|
|
|
2022-05-12 11:27:31 +02:00
|
|
|
// checks if an animation is in progress
|
2023-07-19 22:40:03 +02:00
|
|
|
inline bool isBeingAnimated() {
|
|
|
|
return m_bIsBeingAnimated;
|
2022-05-12 11:27:31 +02:00
|
|
|
}
|
|
|
|
|
2022-11-06 18:52:09 +01:00
|
|
|
void warp(bool endCallback = true) {
|
2022-04-23 14:16:02 +02:00
|
|
|
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;
|
|
|
|
}
|
2022-12-16 18:17:31 +01:00
|
|
|
default: UNREACHABLE();
|
2022-04-23 14:16:02 +02:00
|
|
|
}
|
2022-11-06 18:52:09 +01:00
|
|
|
|
2023-07-19 22:40:03 +02:00
|
|
|
m_bIsBeingAnimated = false;
|
|
|
|
|
2022-11-06 18:52:09 +01:00
|
|
|
if (endCallback)
|
|
|
|
onAnimationEnd();
|
2022-04-23 14:16:02 +02:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:28:43 +02:00
|
|
|
void setConfig(SAnimationPropertyConfig* pConfig) {
|
|
|
|
m_pConfig = pConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
SAnimationPropertyConfig* getConfig() {
|
|
|
|
return m_pConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
int getDurationLeftMs();
|
|
|
|
|
2022-11-06 18:52:09 +01:00
|
|
|
/* returns the spent (completion) % */
|
|
|
|
float getPercent();
|
|
|
|
|
2022-12-29 12:30:43 +01:00
|
|
|
/* returns the current curve value */
|
|
|
|
float getCurveValue();
|
|
|
|
|
2022-11-04 16:56:31 +01:00
|
|
|
/* sets a function to be ran when the animation finishes.
|
|
|
|
if an animation is not running, runs instantly.
|
2022-11-06 18:52:09 +01:00
|
|
|
if "remove" is set to true, will remove the callback when ran. */
|
|
|
|
void setCallbackOnEnd(std::function<void(void* thisptr)> func, bool remove = true) {
|
2022-12-16 18:17:31 +01:00
|
|
|
m_fEndCallback = func;
|
2022-11-06 18:52:09 +01:00
|
|
|
m_bRemoveEndAfterRan = remove;
|
2022-11-04 16:56:31 +01:00
|
|
|
|
|
|
|
if (!isBeingAnimated())
|
|
|
|
onAnimationEnd();
|
|
|
|
}
|
|
|
|
|
2022-11-06 18:52:09 +01:00
|
|
|
/* sets a function to be ran when an animation is started.
|
|
|
|
if "remove" is set to true, will remove the callback when ran. */
|
|
|
|
void setCallbackOnBegin(std::function<void(void* thisptr)> func, bool remove = true) {
|
2022-12-16 18:17:31 +01:00
|
|
|
m_fBeginCallback = func;
|
2022-11-06 18:52:09 +01:00
|
|
|
m_bRemoveBeginAfterRan = remove;
|
|
|
|
}
|
|
|
|
|
2023-05-15 18:11:51 +02:00
|
|
|
/* Sets the update callback, called every time the value is animated and a step is done
|
|
|
|
Warning: calling unregisterVar/registerVar in this handler will cause UB */
|
|
|
|
void setUpdateCallback(std::function<void(void* thisptr)> func) {
|
|
|
|
m_fUpdateCallback = func;
|
|
|
|
}
|
|
|
|
|
2022-11-18 14:53:54 +01:00
|
|
|
/* resets all callbacks. Does not call any. */
|
|
|
|
void resetAllCallbacks() {
|
2022-12-16 18:17:31 +01:00
|
|
|
m_fBeginCallback = nullptr;
|
|
|
|
m_fEndCallback = nullptr;
|
2023-05-15 18:11:51 +02:00
|
|
|
m_fUpdateCallback = nullptr;
|
2022-11-18 14:53:54 +01:00
|
|
|
m_bRemoveBeginAfterRan = false;
|
2022-12-16 18:17:31 +01:00
|
|
|
m_bRemoveEndAfterRan = false;
|
2022-11-18 14:53:54 +01:00
|
|
|
}
|
|
|
|
|
2022-12-16 18:17:31 +01:00
|
|
|
private:
|
|
|
|
Vector2D m_vValue = Vector2D(0, 0);
|
|
|
|
float m_fValue = 0;
|
|
|
|
CColor m_cValue;
|
2022-04-23 14:16:02 +02:00
|
|
|
|
2022-12-16 18:17:31 +01:00
|
|
|
Vector2D m_vGoal = Vector2D(0, 0);
|
|
|
|
float m_fGoal = 0;
|
|
|
|
CColor m_cGoal;
|
2022-04-23 14:16:02 +02:00
|
|
|
|
2022-12-16 18:17:31 +01:00
|
|
|
Vector2D m_vBegun = Vector2D(0, 0);
|
|
|
|
float m_fBegun = 0;
|
|
|
|
CColor m_cBegun;
|
2022-04-23 21:47:16 +02:00
|
|
|
|
2022-05-14 17:23:46 +02:00
|
|
|
// owners
|
2022-12-16 18:17:31 +01:00
|
|
|
void* m_pWindow = nullptr;
|
|
|
|
void* m_pWorkspace = nullptr;
|
|
|
|
void* m_pLayer = nullptr;
|
2022-05-14 17:23:46 +02:00
|
|
|
|
2022-12-16 18:17:31 +01:00
|
|
|
SAnimationPropertyConfig* m_pConfig = nullptr;
|
2022-04-23 14:16:02 +02:00
|
|
|
|
2023-07-19 22:40:03 +02:00
|
|
|
bool m_bDummy = true;
|
|
|
|
bool m_bIsRegistered = false;
|
|
|
|
bool m_bIsBeingAnimated = false;
|
2022-04-23 14:16:02 +02:00
|
|
|
|
2022-04-23 21:47:16 +02:00
|
|
|
std::chrono::system_clock::time_point animationBegin;
|
|
|
|
|
2022-12-16 18:17:31 +01:00
|
|
|
ANIMATEDVARTYPE m_eVarType = AVARTYPE_INVALID;
|
2023-01-20 20:48:07 +01:00
|
|
|
AVARDAMAGEPOLICY m_eDamagePolicy = AVARDAMAGE_NONE;
|
2022-04-23 14:16:02 +02:00
|
|
|
|
2022-12-16 18:17:31 +01:00
|
|
|
bool m_bRemoveEndAfterRan = true;
|
|
|
|
bool m_bRemoveBeginAfterRan = true;
|
|
|
|
std::function<void(void* thisptr)> m_fEndCallback;
|
|
|
|
std::function<void(void* thisptr)> m_fBeginCallback;
|
2023-05-15 18:11:51 +02:00
|
|
|
std::function<void(void* thisptr)> m_fUpdateCallback;
|
2022-11-04 16:56:31 +01:00
|
|
|
|
2023-07-20 19:26:10 +02:00
|
|
|
bool m_bIsConnectedToActive = false;
|
|
|
|
void connectToActive();
|
|
|
|
void disconnectFromActive();
|
|
|
|
|
2022-11-04 16:56:31 +01:00
|
|
|
// methods
|
|
|
|
void onAnimationEnd() {
|
2023-07-19 22:40:03 +02:00
|
|
|
m_bIsBeingAnimated = false;
|
2023-07-20 19:26:10 +02:00
|
|
|
disconnectFromActive();
|
2023-07-19 22:40:03 +02:00
|
|
|
|
2022-11-04 16:56:31 +01:00
|
|
|
if (m_fEndCallback) {
|
2023-05-29 09:51:58 +02:00
|
|
|
// loading m_bRemoveEndAfterRan before calling the callback allows the callback to delete this animation safely if it is false.
|
|
|
|
auto removeEndCallback = m_bRemoveEndAfterRan;
|
2022-11-04 16:56:31 +01:00
|
|
|
m_fEndCallback(this);
|
2023-05-29 09:51:58 +02:00
|
|
|
if (removeEndCallback)
|
2022-12-16 18:17:31 +01:00
|
|
|
m_fEndCallback = nullptr; // reset
|
2022-11-06 18:52:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void onAnimationBegin() {
|
2023-07-19 22:40:03 +02:00
|
|
|
m_bIsBeingAnimated = true;
|
2023-07-20 19:26:10 +02:00
|
|
|
connectToActive();
|
2023-07-19 22:40:03 +02:00
|
|
|
|
2022-11-06 18:52:09 +01:00
|
|
|
if (m_fBeginCallback) {
|
|
|
|
m_fBeginCallback(this);
|
|
|
|
if (m_bRemoveBeginAfterRan)
|
2022-12-16 18:17:31 +01:00
|
|
|
m_fBeginCallback = nullptr; // reset
|
2022-11-04 16:56:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-23 14:16:02 +02:00
|
|
|
friend class CAnimationManager;
|
2022-05-12 11:27:31 +02:00
|
|
|
friend class CWorkspace;
|
2022-05-14 17:23:46 +02:00
|
|
|
friend struct SLayerSurface;
|
2022-11-06 18:52:09 +01:00
|
|
|
friend class CHyprRenderer;
|
2022-09-25 20:07:48 +02:00
|
|
|
};
|