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:
47
54
56 virtual float get();
57
64 virtual float put(float value);
65
67 virtual float mapTo(float toLow, float toHigh);
68
73 void easing(easing_function easing);
74
76 void noEasing() { easing(easeNone); }
77
82 virtual void to(float to);
83
88 virtual void from(float from);
89
95 virtual void fromTo(float from, float to);
96
98 virtual void duration(float duration);
99
101 virtual float duration() const { return AbstractTimer::duration(); }
102
104 virtual void speed(float speed);
105
107 virtual float speed() const;
108
110 virtual void start();
111
112 // virtual void go(float from, float to, float durationOrSpeed, uint8_t mode, easing_function easing=0);
120 virtual void go(float from, float to, float durationOrSpeed, easing_function easing=0);
121
128 virtual void go(float to, float durationOrSpeed, easing_function easing=0);
129
135 virtual void go(float to, easing_function easing=0);
136
138 virtual void mode(uint8_t mode);
139
141 uint8_t mode() const { return _mode; }
142
144 virtual bool finished() { return (_finishedState == JUST_FINISHED); }
145
147 virtual void onFinish(EventCallback callback) { onEvent(callback, EVENT_FINISH); }
148
150 virtual void setTime(float time);
151
153 float durationToSpeed(float duration) const;
154
156 float speedToDuration(float speed) const;
157
159 [[deprecated("Use go(float,easing_function) instead.")]]
160 virtual void start(float to, float durationOrSpeed, easing_function easing=0);
161
163 [[deprecated("Use go(float,float,easing_function) instead.")]]
164 virtual void start(float from, float to, float durationOrSpeed, easing_function easing=0);
165
166private:
167 // Finished states.
168 enum {
169 NOT_FINISHED, // before finished
170 JUST_FINISHED, // just finished this step
171 POST_FINISHED // after finished triggered
172 };
173
174protected:
175 virtual void begin();
176 virtual void step();
177
179 virtual bool eventTriggered(EventType eventType) {
180 switch (eventType) {
181 case EVENT_FINISH: return finished();
182 default: return Unit::eventTriggered(eventType);
183 }
184 }
185
186 // Sets duration or speed (depending on current mode).
187 void _durationOrSpeed(float durationOrSpeed);
188 float _durationOrSpeed() const;
189
190 // Returns current absolute time (in seconds).
191 virtual float _time() const;
192
193 // The starting point.
194 float _from;
195
196 // The end point.
197 float _to;
198
199 // The current value.
200 float _value;
201
202 // Optional easing function.
203 easing_function _easing;
204
205#if PQ_OPTIMIZE_FOR_CPU
206 // Speed.
207 float _speed;
208#endif
209
210 // Mode (DURATION or SPEED).
211 uint8_t _mode : 1;
212
213 // Finished flag.
214 uint8_t _finishedState : 2;
215
216 // Flag that makes sure the value is updated only on a need basis.
217 bool _valueNeedsUpdate : 1;
218
219 // Unused extra space.
220 uint8_t _data : 4;
221};
222
223}
224
225#endif
Definition AbstractTimer.h:29
virtual float duration() const
Returns duration.
Definition AbstractTimer.h:45
The main Plaquette static class containing all the units.
Definition PqCore.h:60
static Engine & primary()
Returns the main instance of Plaquette.
Definition PqCore.cpp:31
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 void onFinish(EventCallback callback)
Registers event callback on finish event.
Definition Ramp.h:147
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:179
virtual float speed() const
Returns speed (rate of change) of the ramp in change-per-second.
Definition Ramp.cpp:128
virtual void to(float to)
Assign final value of the ramp starting from current value.
Definition Ramp.cpp:85
void noEasing()
Remove easing function (linear/no easing).
Definition Ramp.h:76
uint8_t mode() const
Returns the mode of the component (RAMP_DURATION or RAMP_SPEED).
Definition Ramp.h:141
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
virtual void go(float from, float to, float durationOrSpeed, easing_function easing=0)
Starts a new ramp.
Definition Ramp.cpp:149
virtual float duration() const
Returns duration.
Definition Ramp.h:101
virtual void start()
Starts/restarts the ramp. Will repeat the last ramp.
Definition Ramp.cpp:136
virtual void from(float from)
Assign initial value of the ramp.
Definition Ramp.cpp:89
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:144
A generic class representing a unit in the system.
Definition PqCore.h:335
virtual void onEvent(EventCallback callback, EventType eventType)
Registers event callback.
Definition PqCore.cpp:246
virtual bool eventTriggered(EventType eventType)
Returns true iff an event of a certain type has been triggered.
Definition PqCore.h:371
Engine * engine() const
Returns the engine that owns this unit.
Definition PqCore.h:382