@@ -25,28 +25,134 @@ using namespace juce;
2525
2626AEOLUS_NAMESPACE_BEGIN
2727
28+ // ==============================================================================
29+
30+ var Sequencer::DivisionState::getPersistentState () const
31+ {
32+ auto * obj = new DynamicObject ();
33+
34+ Array<var> stopsArr;
35+
36+ for (const auto & s : stops)
37+ stopsArr.add (s);
38+
39+ obj->setProperty (" stops" , stopsArr);
40+ obj->setProperty (" tremulant" , tremulant);
41+
42+ return var{obj};
43+ }
44+
45+ void Sequencer::DivisionState::setPersistentState (const var& v)
46+ {
47+ if (const auto * obj = v.getDynamicObject ()) {
48+ if (const auto * stopsArr = obj->getProperty (" stops" ).getArray ()) {
49+ if (stopsArr->size () == stops.size ()) {
50+ for (int i = 0 ; i < stops.size (); ++i)
51+ stops[i] = stopsArr->getUnchecked (i);
52+ }
53+ }
54+
55+ tremulant = obj->getProperty (" tremulant" );
56+ }
57+ }
58+
59+ // ==============================================================================
60+
61+ var Sequencer::OrganState::getPersistentState () const
62+ {
63+ auto * obj = new DynamicObject ();
64+
65+ Array<var> divisionsArr;
66+
67+ for (const auto & division : divisions)
68+ divisionsArr.add (division.getPersistentState ());
69+
70+ obj->setProperty (" divisions" , divisionsArr);
71+
72+ return var{obj};
73+ }
74+
75+ void Sequencer::OrganState::setPersistentState (const var& v)
76+ {
77+ if (const auto * obj = v.getDynamicObject ()) {
78+ if (const auto * divisionsArr = obj->getProperty (" divisions" ).getArray ()) {
79+ if (divisionsArr->size () == divisions.size ()) {
80+ for (int i = 0 ; i < divisions.size (); ++i)
81+ divisions[i].setPersistentState (divisionsArr->getUnchecked (i));
82+ }
83+ }
84+ }
85+ }
86+
87+ // ==============================================================================
88+
2889Sequencer::Sequencer (Engine& engine, int numSteps)
2990 : _engine{engine}
3091 , _steps(numSteps)
3192 , _currentStep{0 }
93+ , _listeners{}
3294{
3395 jassert (_steps.size () > 0 );
3496
3597 initFromEngine ();
3698}
3799
100+ var Sequencer::getPersistentState () const
101+ {
102+ auto * sequencerObj = new DynamicObject ();
103+
104+ Array<var> stepsArr;
105+
106+ for (int stepIdx = 0 ; stepIdx < _steps.size (); ++stepIdx)
107+ stepsArr.add (_steps[stepIdx].getPersistentState ());
108+
109+ sequencerObj->setProperty (" steps" , stepsArr);
110+ sequencerObj->setProperty (" current_step" , _currentStep);
111+
112+ return var{sequencerObj};
113+ }
114+
115+ void Sequencer::setPersistentState (const var& v)
116+ {
117+ if (auto * sequencerObj = v.getDynamicObject ()) {
118+
119+ if (auto * stepsArr = sequencerObj->getProperty (" steps" ).getArray ()) {
120+ if (stepsArr->size () == _steps.size ()) {
121+ for (int stepIdx = 0 ; stepIdx < _steps.size (); ++stepIdx)
122+ _steps[stepIdx].setPersistentState (stepsArr->getUnchecked (stepIdx));
123+ }
124+ }
125+
126+ const int currentStep = sequencerObj->getProperty (" current_step" );
127+
128+ if (currentStep >= 0 && currentStep < (int )_steps.size ()) {
129+ // Don't capture current state as it is unititialised
130+ // and should not go into the sequencer.
131+ setStep (currentStep, false );
132+ }
133+ }
134+ }
135+
38136void Sequencer::captureCurrentStep ()
39137{
40138 captureState (_steps[_currentStep]);
41139}
42140
43- void Sequencer::setStep (int index)
141+ void Sequencer::setStep (int index, bool captureCurrentState )
44142{
45143 jassert (index >= 0 && index < (int )_steps.size ());
46144
47- captureCurrentStep ();
48- _currentStep = index;
49- recallState (_steps[_currentStep]);
145+ if (index != _currentStep) {
146+ if (captureCurrentState)
147+ captureCurrentStep ();
148+
149+ _currentStep = index;
150+ recallState (_steps[_currentStep]);
151+
152+ _listeners.call ([index](Listener& listener) {
153+ listener.sequencerStepChanged (index);
154+ });
155+ }
50156}
51157
52158void Sequencer::stepForward ()
0 commit comments