20 #ifndef ANTKEEPER_ANIMATION_HPP
21 #define ANTKEEPER_ANIMATION_HPP
26 #include <type_traits>
27 #include <unordered_map>
55 void loop(
bool enabled);
147 template <
typename T>
158 typedef typename std::decay<std::function<T(
const T&,
const T&,
float)>>::type
interpolator_type;
212 std::unordered_map<int, channel> channels;
214 std::function<void(
int,
const T&)> frame_callback;
217 template <
typename T>
219 interpolator(nullptr),
220 frame_callback(nullptr)
223 template <
typename T>
226 if (paused || stopped)
235 float duration = get_duration();
239 if (frame_callback !=
nullptr && interpolator !=
nullptr)
241 for (
const auto&
channel: channels)
245 if (frames[0] !=
nullptr && frames[1] !=
nullptr)
248 float t0 = std::get<0>(*frames[0]);
249 float t1 = std::get<0>(*frames[1]);
250 float alpha = (
position - t0) / (t1 - t0);
251 T frame = interpolator(std::get<1>(*frames[0]), std::get<1>(*frames[1]), alpha);
254 frame_callback(
channel.first, frame);
256 else if (frames[0] !=
nullptr)
259 frame_callback(
channel.first, std::get<1>(*frames[0]));
261 else if (frames[1] !=
nullptr)
264 frame_callback(
channel.first, std::get<1>(*frames[1]));
281 loop_callback(loop_count);
293 if (frame_callback !=
nullptr)
298 if (frames[0] !=
nullptr)
300 frame_callback(
channel.first, std::get<1>(*frames[0]));
316 template <
typename T>
319 return &(*channels.emplace(
id,
id).first).second;
322 template <
typename T>
325 auto it = channels.find(
id);
326 if (it != channels.end())
332 template <
typename T>
338 template <
typename T>
341 this->interpolator = interpolator;
344 template <
typename T>
347 this->frame_callback = callback;
350 template <
typename T>
353 auto it = channels.find(
id);
354 if (it != channels.end())
362 template <
typename T>
365 auto it = channels.find(
id);
366 if (it != channels.end())
374 template <
typename T>
377 float duration = 0.0;
379 for (
auto it = channels.begin(); it != channels.end(); ++it)
381 duration = std::max<float>(duration, it->second.get_duration());
Abstract base class for keyframe animations.
void seek(float t)
Sets the animation position to t.
void pause()
Pauses the animation.
bool is_paused() const
Returns true if the animation is paused, false otherwise.
bool is_stopped() const
Returns true if the animation is stopped, false otherwise.
void set_loop_callback(std::function< void(int)> callback)
Sets the callback that's executed when the animation loops.
std::function< void()> start_callback
void set_end_callback(std::function< void()> callback)
Sets the callback that's executed when a non-looped animation has finished.
void loop(bool enabled)
Enables or disables looping of the animation.
void set_speed(float speed)
Sets the speed of the animation.
void rewind()
Sets the animation position to 0.0.
int get_loop_count() const
Returns the current loop count of the animation.
virtual float get_duration() const =0
Returns the duration of the animation.
void play()
Plays the animation.
virtual void advance(float dt)=0
Advances the animation position (t) by dt.
void set_start_callback(std::function< void()> callback)
Sets the callback that's executed when the animation is started from a stopped state.
std::function< void(int)> loop_callback
float get_position() const
Returns the current position in time of the animation.
bool is_looped() const
Returns true if looping of the animation is enabled, false otherwise.
void stop()
Stops the animation, rewinds it, and resets the loop count.
std::function< void()> end_callback
Single channel in a keyframe animation.
float get_duration() const
Returns the duration of the animation channel.
std::tuple< float, T > keyframe
Keyframe consisting of a time and a value.
std::array< const keyframe *, 2 > find_keyframes(float position) const
Finds the keyframes to the left and right of position.
virtual float get_duration() const
Returns the duration of the animation.
const channel * get_channel(int id) const
Returns the channel with the specified ID.
animation_channel< T > channel
Channel for this animation type.
void remove_channels()
Removes all channels from the animation.
void set_interpolator(interpolator_type interpolator)
Sets the frame interpolator function object.
channel * add_channel(int id)
Adds a channel to the animation.
void set_frame_callback(std::function< void(int, const T &)> callback)
Sets the callback that's executed on each frame of animation.
channel * get_channel(int id)
Returns the channel with the specified ID.
virtual void advance(float dt)
Advances the animation position (t) by dt.
channel::keyframe keyframe
animation()
Creates an animation.
std::decay< std::function< T(const T &, const T &, float)> >::type interpolator_type
Interpolator function type.
void remove_channel(int id)
Removes a channel from the animation.
@ position
Vertex position (vec3)