-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
animation: add CAnimationConfigTree (#29)
Co-authored-by: Maximilian Seidler <[email protected]>
- Loading branch information
1 parent
c42ce87
commit 6a26d08
Showing
4 changed files
with
205 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#pragma once | ||
|
||
#include "../memory/WeakPtr.hpp" | ||
#include "hyprutils/memory/WeakPtr.hpp" | ||
|
||
#include <unordered_map> | ||
|
||
namespace Hyprutils { | ||
namespace Animation { | ||
/* | ||
Structure for animation properties. | ||
Config properties need to have a static lifetime to allow for config reload. | ||
*/ | ||
struct SAnimationPropertyConfig { | ||
bool overridden = false; | ||
|
||
std::string internalBezier = ""; | ||
std::string internalStyle = ""; | ||
float internalSpeed = 0.f; | ||
int internalEnabled = -1; | ||
|
||
Memory::CWeakPointer<SAnimationPropertyConfig> pValues; | ||
Memory::CWeakPointer<SAnimationPropertyConfig> pParentAnimation; | ||
}; | ||
|
||
/* A class to manage SAnimationPropertyConfig objects in a tree structure */ | ||
class CAnimationConfigTree { | ||
public: | ||
CAnimationConfigTree() = default; | ||
~CAnimationConfigTree() = default; | ||
|
||
/* Add a new animation node inheriting from a parent. | ||
If parent is empty, a root node will be created that references it's own values. | ||
Make sure the parent node has already been created through this interface. */ | ||
void createNode(const std::string& nodeName, const std::string& parent = ""); | ||
|
||
/* check if a node name has been created using createNode */ | ||
bool nodeExists(const std::string& nodeName) const; | ||
|
||
/* Override the values of a node. The root node can also be overriden. */ | ||
void setConfigForNode(const std::string& nodeName, int enabled, float speed, const std::string& bezier, const std::string& style = ""); | ||
|
||
Memory::CSharedPointer<SAnimationPropertyConfig> getConfig(const std::string& name) const; | ||
const std::unordered_map<std::string, Memory::CSharedPointer<SAnimationPropertyConfig>>& getFullConfig() const; | ||
|
||
CAnimationConfigTree(const CAnimationConfigTree&) = delete; | ||
CAnimationConfigTree(CAnimationConfigTree&&) = delete; | ||
CAnimationConfigTree& operator=(const CAnimationConfigTree&) = delete; | ||
CAnimationConfigTree& operator=(CAnimationConfigTree&&) = delete; | ||
|
||
private: | ||
void setAnimForChildren(Memory::CSharedPointer<SAnimationPropertyConfig> PANIM); | ||
std::unordered_map<std::string, Memory::CSharedPointer<SAnimationPropertyConfig>> m_mAnimationConfig; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#include <hyprutils/animation/AnimationConfig.hpp> | ||
|
||
using namespace Hyprutils::Animation; | ||
using namespace Hyprutils::Memory; | ||
|
||
#define SP CSharedPointer | ||
#define WP CWeakPointer | ||
|
||
void CAnimationConfigTree::createNode(const std::string& nodeName, const std::string& parent) { | ||
auto pConfig = m_mAnimationConfig[nodeName]; | ||
if (!pConfig) | ||
pConfig = makeShared<SAnimationPropertyConfig>(); | ||
|
||
WP<SAnimationPropertyConfig> parentRef; | ||
if (!parent.empty() && m_mAnimationConfig.find(parent) != m_mAnimationConfig.end()) | ||
parentRef = m_mAnimationConfig[parent]; | ||
|
||
*pConfig = { | ||
.overridden = false, | ||
.internalBezier = "", | ||
.internalStyle = "", | ||
.internalSpeed = 0.f, | ||
.internalEnabled = -1, | ||
.pValues = (parentRef) ? parentRef->pValues : pConfig, | ||
.pParentAnimation = (parentRef) ? parentRef : pConfig, | ||
}; | ||
|
||
m_mAnimationConfig[nodeName] = pConfig; | ||
} | ||
|
||
bool CAnimationConfigTree::nodeExists(const std::string& nodeName) const { | ||
return m_mAnimationConfig.find(nodeName) != m_mAnimationConfig.end(); | ||
} | ||
|
||
void CAnimationConfigTree::setConfigForNode(const std::string& nodeName, int enabled, float speed, const std::string& bezier, const std::string& style) { | ||
auto pConfig = m_mAnimationConfig[nodeName]; | ||
if (!pConfig) | ||
return; | ||
|
||
*pConfig = { | ||
.overridden = true, | ||
.internalBezier = bezier, | ||
.internalStyle = style, | ||
.internalSpeed = speed, | ||
.internalEnabled = enabled, | ||
.pValues = pConfig, | ||
.pParentAnimation = pConfig->pParentAnimation, // keep the parent! | ||
}; | ||
|
||
setAnimForChildren(pConfig); | ||
} | ||
|
||
SP<SAnimationPropertyConfig> CAnimationConfigTree::getConfig(const std::string& name) const { | ||
return m_mAnimationConfig.at(name); | ||
} | ||
|
||
const std::unordered_map<std::string, SP<SAnimationPropertyConfig>>& CAnimationConfigTree::getFullConfig() const { | ||
return m_mAnimationConfig; | ||
} | ||
|
||
void CAnimationConfigTree::setAnimForChildren(SP<SAnimationPropertyConfig> PANIM) { | ||
for (auto& [name, anim] : m_mAnimationConfig) { | ||
if (anim->pParentAnimation == PANIM && !anim->overridden) { | ||
// if a child isnt overridden, set the values of the parent | ||
anim->pValues = PANIM->pValues; | ||
|
||
setAnimForChildren(anim); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters