Plaquette
 
Loading...
Searching...
No Matches
Ramp.h
1/*
2 * Ramp.h
3 *
4 * (c) 2015 Sofian Audry :: info(@)sofianaudry(.)com
5 * (c) 2015 Thomas O Fredericks :: tof(@)t-o-f(.)info
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#ifndef RAMP_H_
22#define RAMP_H_
23
24#include "PqCore.h"
25#include "AbstractTimer.h"
26#include "pq_easing.h"
27
28namespace pq {
29
31enum {
32 RAMP_DURATION,
33 RAMP_SPEED
34};
35
40class Ramp : public Unit, public AbstractTimer {
41public:
42 typedef ParameterSlot<Ramp> Parameter;
43
49
56
58 virtual float get();
59
66 virtual float put(float value);
67
69 virtual float mapTo(float toLow, float toHigh);
70
75 void easing(easing_function easing);
76
78 void noEasing() { easing(easeNone); }
79
81 virtual float to() const { return _to; }
82
87 virtual void to(float to);
88
90 virtual float from() const { return _from; }
91
96 virtual void from(float from);
97
103 virtual void fromTo(float from, float to);
104
106 float duration() const override { return AbstractTimer::duration(); }
107
109 void duration(float duration) override;
110
112 virtual void speed(float speed);
113
115 virtual float speed() const;
116
118 Parameter Speed() { return Parameter(this, &Ramp::speed, &Ramp::speed); }
119
121 virtual void start();
122
123 // virtual void go(float from, float to, float durationOrSpeed, uint8_t mode, easing_function easing=0);
131 virtual void go(float from, float to, float durationOrSpeed, easing_function easing=0);
132
139 virtual void go(float to, float durationOrSpeed, easing_function easing=0);
140
146 virtual void go(float to, easing_function easing=0);
147
149 virtual void mode(uint8_t mode);
150
152 uint8_t mode() const { return _mode; }
153
155 virtual bool finished() { return (_finishedState == JUST_FINISHED); }
156
158 virtual void onFinish(EventCallback callback) { onEvent(callback, EVENT_FINISH); }
159
161 virtual void setTime(float time);
162
164 float durationToSpeed(float duration) const;
165
167 float speedToDuration(float speed) const;
168
170 [[deprecated("Use go(float,easing_function) instead.")]]
171 virtual void start(float to, float durationOrSpeed, easing_function easing=0);
172
174 [[deprecated("Use go(float,float,easing_function) instead.")]]
175 virtual void start(float from, float to, float durationOrSpeed, easing_function easing=0);
176
177private:
178 // Finished states.
179 enum {
180 NOT_FINISHED, // before finished
181 JUST_FINISHED, // just finished this step
182 POST_FINISHED // after finished triggered
183 };
184
185protected:
186 virtual void begin();
187 virtual void step();
188
190 virtual bool eventTriggered(EventType eventType) {
191 switch (eventType) {
192 case EVENT_FINISH: return finished();
193 default: return Unit::eventTriggered(eventType);
194 }
195 }
196
197 // Sets duration or speed (depending on current mode).
198 void _durationOrSpeed(float durationOrSpeed);
199 float _durationOrSpeed() const;
200
201 // Returns current absolute time (in seconds).
202 virtual float _time() const;
203
204 // The starting point.
205 float _from;
206
207 // The end point.
208 float _to;
209
210 // The current value.
211 float _value;
212
213 // Optional easing function.
214 easing_function _easing;
215
216#if PQ_OPTIMIZE_FOR_CPU
217 // Speed.
218 float _speed;
219#endif
220
221 // Mode (DURATION or SPEED).
222 uint8_t _mode : 1;
223
224 // Finished flag.
225 uint8_t _finishedState : 2;
226
227 // Flag that makes sure the value is updated only on a need basis.
228 bool _valueNeedsUpdate : 1;
229
230 // Unused extra space.
231 uint8_t _data : 4;
232};
233
234}
235
236#endif
Definition AbstractTimer.h:29
virtual float duration() const
Returns duration.
Definition AbstractTimer.h:48
The main Plaquette static class containing all the units.
Definition PqCore.h:63
static Engine & primary()
Returns the main instance of Plaquette.
Definition PqCore.cpp:30
Provides a ramping / tweening mechanism that allows smooth transitions between two values.
Definition Ramp.h:40
virtual float get()
Returns value of ramp.
Definition Ramp.cpp:44
virtual float from() const
Returns initial value of the ramp.
Definition Ramp.h:90
virtual void onFinish(EventCallback callback)
Registers event callback on finish event.
Definition Ramp.h:158
virtual float put(float value)
Forces value in the ramp.
Definition Ramp.cpp:57
virtual float mapTo(float toLow, float toHigh)
Maps value to new range.
Definition Ramp.cpp:73
float speedToDuration(float speed) const
Returns duration based on speed.
Definition Ramp.cpp:231
virtual bool eventTriggered(EventType eventType)
Returns true iff an event of a certain type has been triggered.
Definition Ramp.h:190
Parameter Speed()
Returns speed (rate of change) as a parameter.
Definition Ramp.h:118
virtual float speed() const
Returns speed (rate of change) of the ramp in change-per-second.
Definition Ramp.cpp:128
void noEasing()
Remove easing function (linear/no easing).
Definition Ramp.h:78
uint8_t mode() const
Returns the mode of the component (RAMP_DURATION or RAMP_SPEED).
Definition Ramp.h:152
float durationToSpeed(float duration) const
Returns speed based on duration.
Definition Ramp.cpp:219
void easing(easing_function easing)
Sets easing function to apply to ramp.
Definition Ramp.cpp:77
virtual void setTime(float time)
Forces current time (in seconds).
Definition Ramp.cpp:214
float duration() const override
Returns the duration of the ramp (in seconds).
Definition Ramp.h:106
virtual void go(float from, float to, float durationOrSpeed, easing_function easing=0)
Starts a new ramp.
Definition Ramp.cpp:149
virtual float to() const
Returns destination value of the ramp,.
Definition Ramp.h:81
virtual void start()
Starts/restarts the ramp. Will repeat the last ramp.
Definition Ramp.cpp:136
virtual void fromTo(float from, float to)
Assign initial and final values of the ramp.
Definition Ramp.cpp:93
virtual bool finished()
Returns true iff the ramp just finished its process this step.
Definition Ramp.h:155
A generic class representing a unit in the system.
Definition PqCore.h:373
virtual void onEvent(EventCallback callback, EventType eventType)
Registers event callback.
Definition PqCore.cpp:240
virtual bool eventTriggered(EventType eventType)
Returns true iff an event of a certain type has been triggered.
Definition PqCore.h:409
Engine * engine() const
Returns the engine that owns this unit.
Definition PqCore.h:420