]> git.tdb.fi Git - libs/demoscene.git/blobdiff - source/sequencer.h
Use LoadableTypeRegistry to manage action types in Sequencer
[libs/demoscene.git] / source / sequencer.h
index 028c5d5161d5094ffb92bbd0f59b333ecfbabd6b..d01ee6865da58811745c592ee17d83b2cfde007e 100644 (file)
@@ -3,42 +3,67 @@
 
 #include <vector>
 #include <sigc++/signal.h>
+#include <msp/core/maputils.h>
+#include <msp/core/refptr.h>
+#include <msp/datafile/loadabletyperegistry.h>
+#include <msp/datafile/objectloader.h>
 #include <msp/time/timedelta.h>
 #include <msp/time/timestamp.h>
+#include "action.h"
+
+namespace Msp {
+namespace DemoScene {
+
+class Demo;
 
 class Sequencer
 {
 public:
-       class Action
+       class Loader: public Msp::DataFile::ObjectLoader<Sequencer>
        {
-       protected:
-               Action() { }
+       private:
+               Demo &demo;
+               float base_beat;
+
        public:
-               virtual ~Action() { }
+               Loader(Sequencer &, Demo &);
 
-               virtual void start(float, float) { }
-               virtual void beat(int) { }
-               virtual void tick(float, float) { }
-               virtual void end(float) { }
+       private:
+               void base(float);
+               void define_action(const std::string &);
+               void instant(float);
+               void repeat(float, float, unsigned);
+               void segment(float, float);
        };
 
-       class InterpolationAction: public Action
+private:
+       class ActionDefLoader: public Msp::DataFile::ObjectLoader<Sequencer>
        {
        protected:
-               bool hermite;
-               float start_beat;
-               float duration;
+               template<typename T>
+               struct AddAction
+               {
+                       static void add(ActionDefLoader &ldr, const std::string &kw) { ldr.add(kw, &ActionDefLoader::action_def<T>); }
+               };
 
-               InterpolationAction(bool = false);
+               Demo &demo;
+               Msp::RefPtr<Action> action;
 
        public:
-               virtual void start(float, float);
-               virtual void tick(float, float);
-               virtual void end(float);
-               virtual void interpolate(float, float) { }
+               ActionDefLoader(Sequencer &, Demo &);
+
+               Action *get_action() { return action.release(); }
+
+       protected:
+               virtual void action_loaded() { }
+
+       private:
+               template<typename T>
+               void action_def();
+
+               friend class Sequencer;
        };
 
-private:
        struct Segment
        {
                Action *action;
@@ -46,10 +71,29 @@ private:
                float end_beat;
        };
 
+       class SegmentLoader: public ActionDefLoader
+       {
+       private:
+               float start_beat;
+               float end_beat;
+
+       public:
+               SegmentLoader(Sequencer &, float, float, Demo &);
+
+       private:
+               virtual void action_loaded();
+
+               void apply(const std::string &);
+       };
+
 public:
        sigc::signal<void> signal_finished;
 
 private:
+       DataFile::LoadableTypeRegistry<ActionDefLoader, ActionDefLoader::AddAction> action_registry;
+       std::map<std::string, Action *> named_actions;
+       std::vector<Action *> anonymous_actions;
+
        Msp::Time::TimeDelta secs_per_beat;
        std::vector<Action *> static_actions;
        std::vector<Segment> segments;
@@ -62,6 +106,9 @@ private:
 public:
        Sequencer(float = 120.0f);
 
+       template<typename T>
+       void register_action_type(const std::string &);
+
        void set_beats_per_minute(float);
        float get_beats_per_minute() const { return Msp::Time::min/secs_per_beat; }
        void add_static_action(Action &);
@@ -77,4 +124,26 @@ public:
        float get_current_beat() const { return beat; }
 };
 
+template<typename T>
+inline void Sequencer::register_action_type(const std::string &n)
+{
+       action_registry.register_type<T>(n);
+}
+
+template<typename T>
+void Sequencer::ActionDefLoader::action_def()
+{
+       if(action)
+               throw std::runtime_error("Only one action per definition is allowed");
+
+       Msp::RefPtr<T> act = new T;
+       load_sub(*act, demo);
+       act->validate();
+       action = act.release();
+       action_loaded();
+}
+
+} // namespace DemoScene
+} // namespace Msp
+
 #endif