VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
animationComponent.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2025-2026 Arnis Lektauers
3#pragma once
4
5#include <algorithm>
6#include <memory>
7#include <string>
8#include <unordered_map>
9#include <variant>
10#include <vector>
11
15#include "core/math/curveSet.h"
18
19namespace visutwin::canvas
20{
21 class GraphNode;
22
24 {
25 public:
26 using AnimationResource = std::variant<std::shared_ptr<Animation>, std::shared_ptr<AnimTrack>>;
27
29 ~AnimationComponent() override;
30
31 void initializeComponentData() override {}
32
33 static const std::vector<AnimationComponent*>& instances() { return _instances; }
34
35 void setAnimations(const std::unordered_map<std::string, AnimationResource>& value);
36 const std::unordered_map<std::string, AnimationResource>& animations() const { return _animations; }
37
38 void setAssets(const std::vector<int>& value) { _assets = value; }
39 const std::vector<int>& assets() const { return _assets; }
40
41 void setCurrentTime(float currentTime);
42 float currentTime() const;
43
44 float duration() const;
45
46 void setLoop(bool value);
47 bool loop() const { return _loop; }
48
49 void setModel(GraphNode* model);
50
51 void addAnimation(const std::string& name, const std::shared_ptr<Animation>& animation);
52 void addAnimation(const std::string& name, const std::shared_ptr<AnimTrack>& animationTrack);
53
54 void play(const std::string& name, float blendTime = 0.0f);
55
56 std::shared_ptr<Animation> getAnimation(const std::string& name) const;
57 std::shared_ptr<AnimTrack> getAnimTrack(const std::string& name) const;
58
59 void onSetAnimations();
60 void onEnable() override;
61 void onBeforeRemove();
62
63 void update(float dt);
64
65 // DEVIATION: tool-path workflows in this port require component-level curve remapping for blend alpha.
66 void setBlendCurve(const Curve& curve);
67 void clearBlendCurve();
68
69 void setActivate(bool value) { _activate = value; }
70 bool activate() const { return _activate; }
71
72 void setSpeed(float value) { _speed = value; }
73 float speed() const { return _speed; }
74
75 void setPlaying(bool value) { _playing = value; }
76 bool playing() const { return _playing; }
77
78 private:
79 void resetAnimationController();
80 void createAnimationController();
81 void stopCurrentAnimation();
82
83 std::shared_ptr<Animation> getCurrentAnimation() const;
84 std::shared_ptr<AnimTrack> getCurrentAnimTrack() const;
85
86 bool _activate = true;
87 float _speed = 1.0f;
88 bool _playing = false;
89
90 inline static std::vector<AnimationComponent*> _instances;
91
92 std::unordered_map<std::string, AnimationResource> _animations;
93 std::vector<int> _assets;
94
95 bool _loop = true;
96
97 std::unique_ptr<AnimEvaluator> _animEvaluator;
98
99 GraphNode* _model = nullptr;
100
101 std::unique_ptr<Skeleton> _skeleton;
102 std::unique_ptr<Skeleton> _fromSkel;
103 std::unique_ptr<Skeleton> _toSkel;
104
105 std::unordered_map<int, std::string> _animationsIndex;
106
107 std::string _prevAnim;
108 std::string _currAnim;
109
110 float _blend = 0.0f;
111 bool _blending = false;
112 float _blendSpeed = 0.0f;
113
114 bool _hasBlendCurve = false;
115 Curve _blendCurve;
116 };
117}
void setAnimations(const std::unordered_map< std::string, AnimationResource > &value)
std::shared_ptr< AnimTrack > getAnimTrack(const std::string &name) const
std::variant< std::shared_ptr< Animation >, std::shared_ptr< AnimTrack > > AnimationResource
const std::vector< int > & assets() const
void play(const std::string &name, float blendTime=0.0f)
const std::unordered_map< std::string, AnimationResource > & animations() const
void addAnimation(const std::string &name, const std::shared_ptr< Animation > &animation)
std::shared_ptr< Animation > getAnimation(const std::string &name) const
AnimationComponent(IComponentSystem *system, Entity *entity)
static const std::vector< AnimationComponent * > & instances()
void setAssets(const std::vector< int > &value)
Entity * entity() const
Definition component.cpp:16
Component(IComponentSystem *system, Entity *entity)
Definition component.cpp:12
IComponentSystem * system() const
Definition component.h:47
ECS entity — a GraphNode that hosts components defining its behavior.
Definition entity.h:32
Hierarchical scene graph node with local/world transforms and parent-child relationships.
Definition graphNode.h:28