pixled-lib  1.0
animation.h
1 #ifndef ANIMATION_H
2 #define ANIMATION_H
3 
4 #include <vector>
5 #include <map>
6 #include "../chrono/chrono.h"
7 #include "../signal/signal.h"
8 #include "../geometry/geometry.h"
9 #include "../signal/signal.h"
10 #include "../arithmetic/arithmetic.h"
11 
12 namespace pixled { namespace animation {
13 
29  template<typename R>
30  class Wave : public Function<Wave<R>, R, time, R, R> {
31  public:
32  using Function<Wave<R>, R, time, R, R>::Function;
33 
34  R operator()(led l, time t) const override {
35  return this->template call<1>(l, t)
36  + this->template call<2>(l, t) * std::sin(
37  2*PI * t / this->template call<0>(l, t)
38  );
39  }
40  };
41 
51  public:
53 
54  /*
55  * f1 : lambda
56  * f2 : origin line
57  * f3 : time period
58  */
59  float operator()(led l, time t) const override;
60  };
61 
71  public:
73 
74  /*
75  * f1 : lambda
76  * f2 : origin line
77  * f3 : time period
78  */
79  float operator()(led l, time t) const override;
80  };
81 
89  private:
91  Cast<float>(chrono::T()) / Cast<float>(this->arg<0>())
92  };
93 
94  public:
96 
97  float operator()(led l, time t) const override;
98  };
99 
109  public:
111 
112  float operator()(led l, time t) const override;
113  };
114 
124  public:
126 
127  float operator()(led l, time t) const override;
128  };
129 
140  public:
142 
143  color operator()(led l, time t) const override;
144  };
145 
146 
155  private:
156  signal::Square square {
157  Cast<float>(chrono::T()) // parameter
158  / Cast<float>(this->arg<1>()) // period
159  };
160  color black {color::rgb(0, 0, 0)};
161 
162  public:
164 
165  color operator()(led l, time t) const override;
166  };
167 
172  struct SequenceItem {
181 
189  template<typename Anim>
190  SequenceItem(Anim&& animation, time duration)
191  : animation(std::forward<Anim>(animation)), duration(duration) {}
192  };
193 
194 
199  private:
200  std::map<time, FctWrapper<color>> animations;
201  time duration = 0;
202 
203  mutable const FctWrapper<color>* cache;
204  mutable time cache_time = 0;
205  mutable time cache_time_duration = 0;
206  public:
213  }
214 
233  Sequence(std::vector<SequenceItem> sequence) {
234  for(auto item : sequence)
235  this->add(item.animation, item.duration);
236  }
237 
255  template<typename Anim>
256  Sequence& add(Anim&& animation, time duration) {
257  animations.insert({this->duration, std::forward<Anim>(animation)});
258  cache_time = this->duration;
259  cache = &animations.at(cache_time);
260  this->duration+=duration;
261  cache_time_duration=duration;
262  return *this;
263  }
264 
265  color operator()(led l, time t) const override;
266 
267  Sequence* copy() const override;
268  };
269 
270 }}
271 #endif
Definition: function.h:136
Definition: function.h:349
Function(Fcts &&... fcts)
Definition: function.h:383
Definition: animation.h:139
Definition: animation.h:50
Definition: animation.h:123
Definition: animation.h:70
Definition: animation.h:108
Definition: animation.h:88
Definition: animation.h:198
Sequence()
Definition: animation.h:212
Sequence & add(Anim &&animation, time duration)
Definition: animation.h:256
Sequence(std::vector< SequenceItem > sequence)
Definition: animation.h:233
Definition: animation.h:30
R operator()(led l, time t) const override
Definition: animation.h:34
Definition: chrono.h:14
Definition: color.h:10
static color rgb(uint8_t r, uint8_t g, uint8_t b)
Definition: color.cpp:169
Definition: signal.h:53
Definition: signal.h:90
Definition: animation.cpp:3
float sin(const angle &a)
Definition: geometry.cpp:61
unsigned long time
Definition: time.h:10
static const float PI
Definition: geometry.h:13
Definition: animation.h:172
FctWrapper< color > animation
Definition: animation.h:176
SequenceItem(Anim &&animation, time duration)
Definition: animation.h:190
time duration
Definition: animation.h:180
Definition: mapping.h:19