GA::kit v0.3
G&A's in-house C++ application framework
Loading...
Searching...
No Matches
timeline_component.h
Go to the documentation of this file.
1#pragma once
3#include "ga/signal.h"
4#include "ga/tween.h"
5#include <map>
6
7namespace ga {
8
9// Tween Timeline
10class Timeline : public virtual Component
11{
12public:
13 void update() override
14 {
15 std::vector<std::shared_ptr<TweenBase>> deleteKeys;
16 for ( auto& el : m_tweenRefMap ) {
17 auto& tween = el.first;
18 auto& updateFn = el.second;
19 bool keep = false;
20 if ( tween ) {
21 keep = tween->update_();
22 if ( updateFn && tween->isStarted_() ) {
23 updateFn( tween );
24 }
25 if ( tween->isDone_() ) {
26 tween->end( true );
27 }
28 }
29 if ( !keep ) {
30 deleteKeys.push_back( tween );
31 }
32 }
33 bool wasEmpty = m_tweenRefMap.empty();
34 for ( auto& key : deleteKeys ) {
35 m_tweenRefMap.erase( key );
36 }
37 bool isEmpty = m_tweenRefMap.empty();
38 if ( !wasEmpty && isEmpty ) {
39 onTimelineDone( this );
40 } else if ( wasEmpty && !isEmpty ) {
41 onTimelineStart( this );
42 }
43 }
44
45 template <typename T, typename Fn>
46 void add( std::shared_ptr<Tween<T>> tween, Fn updateFn = nullptr )
47 {
48 if ( updateFn ) {
49 m_tweenRefMap[tween] = [updateFn]( std::shared_ptr<TweenBase> t ) { updateFn( std::static_pointer_cast<Tween<T>>( t )->getValue() ); };
50 } else {
51 m_tweenRefMap[tween] = nullptr;
52 }
53 }
54
55 template <typename T>
56 void add( std::shared_ptr<Tween<T>> tween, std::function<void( const T& )> updateFn = nullptr )
57 {
58 if ( updateFn ) {
59 m_tweenRefMap[tween] = [updateFn]( std::shared_ptr<TweenBase> t ) { updateFn( std::static_pointer_cast<Tween<T>>( t )->getValue() ); };
60 } else {
61 m_tweenRefMap[tween] = nullptr;
62 }
63 }
64
65 template <typename T, typename Fn>
66 std::shared_ptr<Tween<T>> add( const T& startVal, const T& endVal, std::function<float( float )> easeFn, Fn updateFn = nullptr, std::function<void()> onDone = nullptr )
67 {
68 auto tween = std::make_shared<Tween<T>>( startVal, endVal, easeFn, onDone );
69 this->add( tween, std::function<void( const T& )>( updateFn ) );
70 return tween;
71 }
72
73 template <typename T>
74 std::shared_ptr<Tween<T>> add( const T& startVal, const T& endVal, std::function<float( float )> easeFn, std::function<void( const T& )> updateFn = nullptr, std::function<void()> onDone = nullptr )
75 {
76 auto tween = std::make_shared<Tween<T>>( startVal, endVal, easeFn, onDone );
77 this->add( tween, updateFn );
78 return tween;
79 }
80
81 template <typename T>
82 bool setTweenUpdate( std::shared_ptr<Tween<T>> tween, std::function<void( const T& )> updateFn )
83 {
84 try {
85 m_tweenRefMap.at( tween ) = [updateFn]( std::shared_ptr<TweenBase> t ) { updateFn( std::static_pointer_cast<Tween<T>>( t )->getValue() ); };
86 return true;
87 } catch ( ... ) {
88 return false;
89 }
90 }
91
92 void clear()
93 {
94 m_tweenRefMap.clear();
95 }
96
97 std::vector<std::shared_ptr<TweenBase>> getTweens()
98 {
99 std::vector<std::shared_ptr<TweenBase>> refs;
100 refs.reserve( m_tweenRefMap.size() );
101 for ( auto& el : m_tweenRefMap ) {
102 refs.push_back( el.first );
103 }
104 return refs;
105 }
106
107 bool isActive()
108 {
109 return m_tweenRefMap.size();
110 }
111
113
114protected:
115 std::map<std::shared_ptr<TweenBase>, std::function<void( std::shared_ptr<TweenBase> )>> m_tweenRefMap;
116};
117} // namespace ga
Definition: component.h:11
Definition: timeline_component.h:11
bool setTweenUpdate(std::shared_ptr< Tween< T > > tween, std::function< void(const T &)> updateFn)
Definition: timeline_component.h:82
std::shared_ptr< Tween< T > > add(const T &startVal, const T &endVal, std::function< float(float)> easeFn, Fn updateFn=nullptr, std::function< void()> onDone=nullptr)
Definition: timeline_component.h:66
void add(std::shared_ptr< Tween< T > > tween, Fn updateFn=nullptr)
Definition: timeline_component.h:46
ga::Signal< Timeline * > onTimelineStart
Definition: timeline_component.h:112
ga::Signal< Timeline * > onTimelineDone
Definition: timeline_component.h:112
std::shared_ptr< Tween< T > > add(const T &startVal, const T &endVal, std::function< float(float)> easeFn, std::function< void(const T &)> updateFn=nullptr, std::function< void()> onDone=nullptr)
Definition: timeline_component.h:74
void update() override
Definition: timeline_component.h:13
bool isActive()
Definition: timeline_component.h:107
void add(std::shared_ptr< Tween< T > > tween, std::function< void(const T &)> updateFn=nullptr)
Definition: timeline_component.h:56
std::map< std::shared_ptr< TweenBase >, std::function< void(std::shared_ptr< TweenBase >)> > m_tweenRefMap
Definition: timeline_component.h:115
void clear()
Definition: timeline_component.h:92
std::vector< std::shared_ptr< TweenBase > > getTweens()
Definition: timeline_component.h:97
Definition: tween.h:28
Definition: sigslot.hpp:1134
Definition: color.h:9
std::function< float(float)> easeFn(const EaseType &type)
Definition: easing.h:102