|
|
@ -20,10 +20,12 @@ |
|
|
|
#ifndef ANTKEEPER_ANIMATION_HPP
|
|
|
|
#define ANTKEEPER_ANIMATION_HPP
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <functional>
|
|
|
|
#include <set>
|
|
|
|
#include <tuple>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstract base class for animations. |
|
|
@ -84,6 +86,9 @@ public: |
|
|
|
/// Returns the current loop count of the animation.
|
|
|
|
int get_loop_count() const; |
|
|
|
|
|
|
|
/// Returns the duration of the animation.
|
|
|
|
virtual double get_duration() const = 0; |
|
|
|
|
|
|
|
/// Sets the callback that's executed when the animation is started from a stopped state.
|
|
|
|
void set_start_callback(std::function<void()> callback); |
|
|
|
|
|
|
@ -135,24 +140,25 @@ inline int animation_base::get_loop_count() const |
|
|
|
return loop_count; |
|
|
|
} |
|
|
|
|
|
|
|
/**
|
|
|
|
* Templated keyframe animation class. |
|
|
|
*/ |
|
|
|
template <typename T> |
|
|
|
class animation: public animation_base |
|
|
|
class animation_channel |
|
|
|
{ |
|
|
|
public: |
|
|
|
/// Scheduled function consisting of a time and function object.
|
|
|
|
/// Keyframe consisting of a time and a value.
|
|
|
|
typedef std::tuple<double, T> keyframe; |
|
|
|
|
|
|
|
/// Interpolator function type.
|
|
|
|
typedef typename std::decay<std::function<T(const T&, const T&, double)>>::type interpolator_type; |
|
|
|
/**
|
|
|
|
* Creates an animation channel. |
|
|
|
* |
|
|
|
* @param id ID of the channel. |
|
|
|
*/ |
|
|
|
animation_channel(int id); |
|
|
|
|
|
|
|
/// Creates an animation
|
|
|
|
animation(); |
|
|
|
/// Creates an animation channel.
|
|
|
|
animation_channel(); |
|
|
|
|
|
|
|
/// @copydoc animation_base::advance()
|
|
|
|
virtual void advance(double dt); |
|
|
|
/// Creates an animation channel.
|
|
|
|
animation_channel(const animation_channel& other); |
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a keyframe to the animation. |
|
|
@ -169,35 +175,33 @@ public: |
|
|
|
*/ |
|
|
|
void remove_keyframes(double start, double end); |
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all the keyframes on `[start, end)`. |
|
|
|
* |
|
|
|
* @param start Starting position in time (inclusive). |
|
|
|
* @param end Ending position in time (non-inclusive). |
|
|
|
* @return All keyframes on `[start, end)`. |
|
|
|
*/ |
|
|
|
std::list<keyframe> get_keyframes(double start, double end) const; |
|
|
|
|
|
|
|
/// Removes all keyframes from the animation.
|
|
|
|
void clear(); |
|
|
|
void remove_keyframes(); |
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the frame interpolator function object. |
|
|
|
* Finds the keyframes to the left and right of @p position. |
|
|
|
* |
|
|
|
* @param interpolator Frame interpolator function object. |
|
|
|
* @param position Position in time. |
|
|
|
* @return Array containing the the keyframes on the left and right of @p position. |
|
|
|
*/ |
|
|
|
void set_interpolator(interpolator_type interpolator); |
|
|
|
std::array<const keyframe*, 2> find_keyframes(double position) const; |
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the callback that's executed on each frame of animation. |
|
|
|
* Finds all the keyframes on `[start, end)`. |
|
|
|
* |
|
|
|
* @param callback Frame callback which receives the value of the interpolated frames. |
|
|
|
* @param start Starting position in time (inclusive). |
|
|
|
* @param end Ending position in time (non-inclusive). |
|
|
|
* @return All keyframes on `[start, end)`. |
|
|
|
*/ |
|
|
|
void set_frame_callback(std::function<void(const T&)> callback); |
|
|
|
|
|
|
|
private: |
|
|
|
//static bool keyframe_compare(const keyframe& a, const keyframe & b);
|
|
|
|
std::list<keyframe> find_keyframes(double start, double end) const; |
|
|
|
|
|
|
|
/// Returns the ID of the animation channel.
|
|
|
|
int get_id() const; |
|
|
|
|
|
|
|
/// Returns the duration of the animation channel.
|
|
|
|
double get_duration() const; |
|
|
|
|
|
|
|
private: |
|
|
|
struct keyframe_compare |
|
|
|
{ |
|
|
|
inline bool operator()(const keyframe& lhs, const keyframe& rhs) const |
|
|
@ -206,30 +210,180 @@ private: |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
interpolator_type interpolator; |
|
|
|
std::function<void(const T&)> frame_callback; |
|
|
|
int id; |
|
|
|
std::set<keyframe, keyframe_compare> keyframes; |
|
|
|
}; |
|
|
|
|
|
|
|
/*
|
|
|
|
template <typename T> |
|
|
|
bool animation<T>::keyframe_compare(const keyframe& a, const keyframe & b) |
|
|
|
animation_channel<T>::animation_channel(int id): |
|
|
|
id(id), |
|
|
|
keyframes(keyframe_compare()) |
|
|
|
{} |
|
|
|
|
|
|
|
template <typename T> |
|
|
|
animation_channel<T>::animation_channel(): |
|
|
|
animation_channel(-1) |
|
|
|
{} |
|
|
|
|
|
|
|
template <typename T> |
|
|
|
animation_channel<T>::animation_channel(const animation_channel& other): |
|
|
|
id(other.id), |
|
|
|
keyframes(other.keyframes) |
|
|
|
{} |
|
|
|
|
|
|
|
template <typename T> |
|
|
|
void animation_channel<T>::insert_keyframe(const keyframe& k) |
|
|
|
{ |
|
|
|
keyframes.emplace(k); |
|
|
|
} |
|
|
|
|
|
|
|
template <typename T> |
|
|
|
void animation_channel<T>::remove_keyframes(double start, double end) |
|
|
|
{ |
|
|
|
return std::get<0>(a) < std::get<0>(b); |
|
|
|
auto lower_bound = keyframes.lower_bound({start, T()}); |
|
|
|
auto upper_bound = keyframes.upper_bound({end, T()}); |
|
|
|
keyframes.erase(lower_bound, upper_bound); |
|
|
|
} |
|
|
|
*/ |
|
|
|
|
|
|
|
template <typename T> |
|
|
|
void animation_channel<T>::remove_keyframes() |
|
|
|
{ |
|
|
|
keyframes.clear(); |
|
|
|
} |
|
|
|
|
|
|
|
template <typename T> |
|
|
|
std::array<const typename animation_channel<T>::keyframe*, 2> animation_channel<T>::find_keyframes(double position) const |
|
|
|
{ |
|
|
|
// Find the following keyframe
|
|
|
|
auto upper_bound = keyframes.upper_bound({position, T()}); |
|
|
|
|
|
|
|
// Find the preceding keyframe
|
|
|
|
auto lower_bound = upper_bound; |
|
|
|
--lower_bound; |
|
|
|
|
|
|
|
std::array<const keyframe*, 2> frames; |
|
|
|
frames[0] = (lower_bound != keyframes.end()) ? &(*lower_bound) : nullptr; |
|
|
|
frames[1] = (upper_bound != keyframes.end()) ? &(*upper_bound) : nullptr; |
|
|
|
|
|
|
|
return frames; |
|
|
|
} |
|
|
|
|
|
|
|
template <typename T> |
|
|
|
std::list<typename animation_channel<T>::keyframe> animation_channel<T>::find_keyframes(double start, double end) const |
|
|
|
{ |
|
|
|
std::list<keyframe> keyframe_list; |
|
|
|
|
|
|
|
auto lower_bound = keyframes.lower_bound({start, T()}); |
|
|
|
auto upper_bound = keyframes.upper_bound({end, T()}); |
|
|
|
for (auto iterator = lower_bound; iterator != upper_bound; ++iterator) |
|
|
|
{ |
|
|
|
keyframe_list.push_back(*iterator); |
|
|
|
} |
|
|
|
|
|
|
|
return keyframe_list; |
|
|
|
} |
|
|
|
|
|
|
|
template <typename T> |
|
|
|
inline int animation_channel<T>::get_id() const |
|
|
|
{ |
|
|
|
return id; |
|
|
|
} |
|
|
|
|
|
|
|
template <typename T> |
|
|
|
double animation_channel<T>::get_duration() const |
|
|
|
{ |
|
|
|
if (keyframes.empty()) |
|
|
|
{ |
|
|
|
return 0.0; |
|
|
|
} |
|
|
|
|
|
|
|
return std::get<0>(*keyframes.rbegin()); |
|
|
|
} |
|
|
|
|
|
|
|
/**
|
|
|
|
* Templated keyframe animation class. |
|
|
|
*/ |
|
|
|
template <typename T> |
|
|
|
class animation: public animation_base |
|
|
|
{ |
|
|
|
public: |
|
|
|
/// Channel for this animation type.
|
|
|
|
typedef animation_channel<T> channel; |
|
|
|
|
|
|
|
// Keyframe type for this animation.
|
|
|
|
typedef typename channel::keyframe keyframe; |
|
|
|
|
|
|
|
/// Interpolator function type.
|
|
|
|
typedef typename std::decay<std::function<T(const T&, const T&, double)>>::type interpolator_type; |
|
|
|
|
|
|
|
/// Creates an animation.
|
|
|
|
animation(); |
|
|
|
|
|
|
|
/// @copydoc animation_base::advance()
|
|
|
|
virtual void advance(double dt); |
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a channel to the animation. |
|
|
|
* |
|
|
|
* @param id ID of the channel. |
|
|
|
* @return Added or pre-existing channel. |
|
|
|
*/ |
|
|
|
channel* add_channel(int id); |
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a channel from the animation. |
|
|
|
* |
|
|
|
* @param id ID of the channel to remove. |
|
|
|
*/ |
|
|
|
void remove_channel(int id); |
|
|
|
|
|
|
|
/// Removes all channels from the animation.
|
|
|
|
void remove_channels(); |
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the frame interpolator function object. |
|
|
|
* |
|
|
|
* @param interpolator Frame interpolator function object. |
|
|
|
*/ |
|
|
|
void set_interpolator(interpolator_type interpolator); |
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the callback that's executed on each frame of animation. |
|
|
|
* |
|
|
|
* @param callback Frame callback which receives the index of an animation channel and value of an interpolated frame. |
|
|
|
*/ |
|
|
|
void set_frame_callback(std::function<void(int, const T&)> callback); |
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the channel with the specified ID. |
|
|
|
* |
|
|
|
* @param id ID of the channel to get. |
|
|
|
*/ |
|
|
|
const channel* get_channel(int id) const; |
|
|
|
|
|
|
|
/// @copydoc animation::get_channel(int) const
|
|
|
|
channel* get_channel(int id); |
|
|
|
|
|
|
|
/// @copydoc animation_base::get_duration() const
|
|
|
|
virtual double get_duration() const; |
|
|
|
|
|
|
|
|
|
|
|
private: |
|
|
|
std::unordered_map<int, channel> channels; |
|
|
|
interpolator_type interpolator; |
|
|
|
std::function<void(int, const T&)> frame_callback; |
|
|
|
}; |
|
|
|
|
|
|
|
template <typename T> |
|
|
|
animation<T>::animation(): |
|
|
|
interpolator(nullptr), |
|
|
|
frame_callback(nullptr), |
|
|
|
keyframes(keyframe_compare()) |
|
|
|
frame_callback(nullptr) |
|
|
|
{} |
|
|
|
|
|
|
|
template <typename T> |
|
|
|
void animation<T>::advance(double dt) |
|
|
|
{ |
|
|
|
if (paused || stopped || keyframes.empty()) |
|
|
|
if (paused || stopped) |
|
|
|
{ |
|
|
|
return; |
|
|
|
} |
|
|
@ -237,25 +391,39 @@ void animation::advance(double dt) |
|
|
|
// Advance position by dt
|
|
|
|
position += dt * speed; |
|
|
|
|
|
|
|
// Find the following keyframe
|
|
|
|
auto upper_bound = keyframes.upper_bound({position, T()}); |
|
|
|
// Determine duration of the animation
|
|
|
|
double duration = get_duration(); |
|
|
|
|
|
|
|
// Find the preceding keyframe
|
|
|
|
auto lower_bound = upper_bound; |
|
|
|
--lower_bound; |
|
|
|
|
|
|
|
if (upper_bound != keyframes.end()) |
|
|
|
if (position < duration) |
|
|
|
{ |
|
|
|
if (frame_callback != nullptr && interpolator != nullptr) |
|
|
|
{ |
|
|
|
// Calculate interpolated frame
|
|
|
|
double t0 = std::get<0>(*lower_bound); |
|
|
|
double t1 = std::get<0>(*upper_bound); |
|
|
|
double alpha = (position - t0) / (t1 - t0); |
|
|
|
T frame = interpolator(std::get<1>(*lower_bound), std::get<1>(*upper_bound), alpha); |
|
|
|
|
|
|
|
// Pass frame to frame callback
|
|
|
|
frame_callback(frame); |
|
|
|
for (std::size_t i = 0; i < channels.size(); ++i) |
|
|
|
{ |
|
|
|
auto frames = channels[i].find_keyframes(position); |
|
|
|
|
|
|
|
if (frames[0] != nullptr && frames[1] != nullptr) |
|
|
|
{ |
|
|
|
// Calculate interpolated frame
|
|
|
|
double t0 = std::get<0>(*frames[0]); |
|
|
|
double t1 = std::get<0>(*frames[1]); |
|
|
|
double alpha = (position - t0) / (t1 - t0); |
|
|
|
T frame = interpolator(std::get<1>(*frames[0]), std::get<1>(*frames[1]), alpha); |
|
|
|
|
|
|
|
// Pass frame to frame callback
|
|
|
|
frame_callback(static_cast<int>(i), frame); |
|
|
|
} |
|
|
|
else if (frames[0] != nullptr) |
|
|
|
{ |
|
|
|
// Pass frame to frame callback
|
|
|
|
frame_callback(static_cast<int>(i), std::get<1>(*frames[0])); |
|
|
|
} |
|
|
|
else if (frames[1] != nullptr) |
|
|
|
{ |
|
|
|
// Pass frame to frame callback
|
|
|
|
frame_callback(static_cast<int>(i), std::get<1>(*frames[1])); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
@ -265,7 +433,7 @@ void animation::advance(double dt) |
|
|
|
++loop_count; |
|
|
|
|
|
|
|
// Subtract duration of animation from position
|
|
|
|
position -= std::get<0>(*lower_bound); |
|
|
|
position -= duration; |
|
|
|
|
|
|
|
// Execute loop callback
|
|
|
|
if (loop_callback) |
|
|
@ -280,13 +448,7 @@ void animation::advance(double dt) |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
// Call frame callback on final keyframe
|
|
|
|
if (frame_callback) |
|
|
|
{ |
|
|
|
frame_callback(std::get<1>(*lower_bound)); |
|
|
|
} |
|
|
|
|
|
|
|
{ |
|
|
|
stopped = true; |
|
|
|
|
|
|
|
// Call end callback
|
|
|
@ -299,50 +461,74 @@ void animation::advance(double dt) |
|
|
|
} |
|
|
|
|
|
|
|
template <typename T> |
|
|
|
void animation<T>::insert_keyframe(const keyframe& k) |
|
|
|
typename animation<T>::channel* animation<T>::add_channel(int id) |
|
|
|
{ |
|
|
|
keyframes.emplace(k); |
|
|
|
return &(*channels.emplace(id, id).first).second; |
|
|
|
} |
|
|
|
|
|
|
|
template <typename T> |
|
|
|
void animation<T>::remove_keyframes(double start, double end) |
|
|
|
void animation<T>::remove_channel(int id) |
|
|
|
{ |
|
|
|
auto lower_bound = keyframes.lower_bound({start, T()}); |
|
|
|
auto upper_bound = keyframes.upper_bound({end, T()}); |
|
|
|
keyframes.erase(lower_bound, upper_bound); |
|
|
|
auto it = channels.find(id); |
|
|
|
if (it != channels.end()) |
|
|
|
{ |
|
|
|
channels.erase(it); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
template <typename T> |
|
|
|
std::list<typename animation<T>::keyframe> animation<T>::get_keyframes(double start, double end) const |
|
|
|
void animation<T>::remove_channels() |
|
|
|
{ |
|
|
|
std::list<keyframe> keyframe_list; |
|
|
|
channels.clear(); |
|
|
|
} |
|
|
|
|
|
|
|
auto lower_bound = keyframes.lower_bound({start, T()}); |
|
|
|
auto upper_bound = keyframes.upper_bound({end, T()}); |
|
|
|
for (auto iterator = lower_bound; iterator != upper_bound; ++iterator) |
|
|
|
{ |
|
|
|
keyframe_list.push_back(*iterator); |
|
|
|
} |
|
|
|
template <typename T> |
|
|
|
void animation<T>::set_interpolator(interpolator_type interpolator) |
|
|
|
{ |
|
|
|
this->interpolator = interpolator; |
|
|
|
} |
|
|
|
|
|
|
|
return keyframe_list; |
|
|
|
template <typename T> |
|
|
|
void animation<T>::set_frame_callback(std::function<void(int, const T&)> callback) |
|
|
|
{ |
|
|
|
this->frame_callback = callback; |
|
|
|
} |
|
|
|
|
|
|
|
template <typename T> |
|
|
|
void animation<T>::clear() |
|
|
|
const typename animation<T>::channel* animation<T>::get_channel(int id) const |
|
|
|
{ |
|
|
|
keyframes.clear(); |
|
|
|
auto it = channels.find(id); |
|
|
|
if (it != channels.end()) |
|
|
|
{ |
|
|
|
return &it->second; |
|
|
|
} |
|
|
|
|
|
|
|
return nullptr; |
|
|
|
} |
|
|
|
|
|
|
|
template <typename T> |
|
|
|
void animation<T>::set_interpolator(interpolator_type interpolator) |
|
|
|
typename animation<T>::channel* animation<T>::get_channel(int id) |
|
|
|
{ |
|
|
|
this->interpolator = interpolator; |
|
|
|
auto it = channels.find(id); |
|
|
|
if (it != channels.end()) |
|
|
|
{ |
|
|
|
return &it->second; |
|
|
|
} |
|
|
|
|
|
|
|
return nullptr; |
|
|
|
} |
|
|
|
|
|
|
|
template <typename T> |
|
|
|
void animation<T>::set_frame_callback(std::function<void(const T&)> callback) |
|
|
|
double animation<T>::get_duration() const |
|
|
|
{ |
|
|
|
this->frame_callback = callback; |
|
|
|
double duration = 0.0; |
|
|
|
|
|
|
|
for (auto it = channels.begin(); it != channels.end(); ++it) |
|
|
|
{ |
|
|
|
duration = std::max<double>(duration, it->second.get_duration()); |
|
|
|
} |
|
|
|
|
|
|
|
return duration; |
|
|
|
} |
|
|
|
|
|
|
|
#endif // ANTKEEPER_ANIMATION_HPP
|