spacebox/src/Animation.hpp

70 lines
2.0 KiB
C++

/* +------------------------------------------------------+
____/ \____ /| - Open source game framework licensed to freely use, |
\ / / | copy, modify and sell without restriction |
+--\ ^__^ /--+ | |
| ~/ \~ | | - created for <https://foam.shampoo.ooo> |
| ~~~~~~~~~~~~ | +------------------------------------------------------+
| SPACE ~~~~~ | /
| ~~~~~~~ BOX |/
+-------------*/
#pragma once
#include <vector>
#include <functional>
#include <algorithm>
#include "Timer.hpp"
typedef std::function<void()> callback;
class Node;
class Animation
{
private:
bool play_state = false, ending = false, paused = false;
int previous_step_time = 0;
float delay = 0, overflow = 0, frame_length = 0;
callback step = nullptr;
Node* containing_object = nullptr;
Timer timer = Timer();
public:
Animation();
/* Constructor that allows passing a pointer to an object and a pointer to one of its
* member functions that will be used as the animation function. Frame length can be supplied
* in milliseconds, representing how often the animation function will run. If omitted, the
* animation function will run every time the Animation object is updated (generally, once per
* frame of the application). */
template<typename T>
Animation(void(T::*member_function)(), T* object, float frame_length = 0) : frame_length(frame_length)
{
bind(member_function, object);
timer.toggle(Timer::OFF);
}
/* Set the animation function by supplying an object and one of its member functions */
template<typename T>
void bind(void(T::*f)(), T* o)
{
step = std::bind(f, o);
containing_object = static_cast<Node*>(o);
}
void set_frame_length(float);
void play(float = 0, bool = false);
void play_once(float = 0);
void pause();
void unpause();
void reset();
bool playing(bool = true) const;
void update();
};
#include "Node.hpp"