allow arbitrary functions to be attached to animation objects instead of only node object member functions

This commit is contained in:
ohsqueezy 2023-06-23 01:45:28 -04:00
parent 5be1c38e7f
commit 635562263b
6 changed files with 16 additions and 26 deletions

View File

@ -56,7 +56,7 @@ bool Animation::playing(bool include_delay) const
void Animation::update(float timestamp)
{
timer.update(timestamp);
if (playing() && containing_object->is_active())
if (playing())
{
if (delay > 0)
{

View File

@ -15,9 +15,7 @@
#include <algorithm>
#include "Timer.hpp"
typedef std::function<void()> callback;
class Node;
typedef std::function<void()> Callback;
class Animation
{
@ -26,8 +24,8 @@ private:
bool play_state = false, ending = false, paused = false;
float delay = 0.0f, overflow = 0.0f, frame_length = 0.0f, previous_step_time = 0.0f;
callback step = nullptr;
Node* containing_object = nullptr;
Callback step = nullptr;
// Node* containing_object = nullptr;
sb::Timer timer = sb::Timer();
public:
@ -40,23 +38,11 @@ public:
* in seconds, representing how often the animation function will run. If omitted, the
* animation function will run every time the Animation object is updated.
*/
template<typename T>
Animation(void(T::*member_function)(), T* object, float frame_length = 0) : frame_length(frame_length)
Animation(Callback step, float frame_length = 0) : frame_length(frame_length), step(step)
{
bind(member_function, object);
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);
@ -75,5 +61,3 @@ namespace sb
{
using ::Animation;
}
#include "Node.hpp"

View File

@ -12,6 +12,7 @@
#include <regex>
#include <map>
#include <functional>
#include "SDL.h"
#include "SDL_mixer.h"
#include "filesystem.hpp"
@ -36,13 +37,17 @@ struct BGM : Node
};
struct SoundEffect : Node
class SoundEffect : public Node
{
public:
Mix_Chunk* chunk = nullptr;
int channel, loops = 0;
float volume = 1.0f;
Animation play_animation = Animation(&SoundEffect::play, this);
/* The cast is required so that the overloaded play function without arguments will be selected. */
Animation play_animation = Animation(std::bind(static_cast<void(SoundEffect::*)()>(&SoundEffect::play), this));
SoundEffect(Node*);
SoundEffect();

View File

@ -13,6 +13,7 @@
#include <fstream>
#include <ostream>
#include <iomanip>
#include <functional>
#include "json/json.hpp"
#include "filesystem.hpp"
#include "Node.hpp"
@ -25,7 +26,7 @@ class Configuration : public Node
private:
Animation auto_refresher = Animation(&Configuration::refresh, this);
Animation auto_refresher = Animation(std::bind(&Configuration::refresh, this));
fs::file_time_type config_file_modification_time;
nlohmann::json config;
fs::path file_to_refresh;

View File

@ -59,7 +59,7 @@ private:
public:
inline static std::string any = "any";
Animation unsuppress_animation = Animation(&Input::unsuppress, this);
Animation unsuppress_animation = Animation(std::bind(&Input::unsuppress, this));
Input(Node*);
void respond(SDL_Event&);

View File

@ -59,7 +59,7 @@ private:
public:
Animation animation = Animation(&Recorder::add_frame, this);
Animation animation = Animation(std::bind(&Recorder::add_frame, this));
Recorder(Node*);
void respond(SDL_Event&);