ScenarioCreation_FromEvent.hpp
1 #pragma once
2 
3 #include <Scenario/Commands/Scenario/Creations/CreateEvent_State.hpp>
4 #include <Scenario/Commands/Scenario/Creations/CreateState.hpp>
5 #include <Scenario/Commands/Scenario/Displacement/MoveNewEvent.hpp>
6 #include <Scenario/Commands/Scenario/Displacement/MoveNewState.hpp>
7 #include <Scenario/Document/TimeSync/TimeSyncModel.hpp>
8 #include <Scenario/Palette/Tools/States/ScenarioCreationState.hpp>
9 #include <Scenario/Palette/Transitions/AnythingTransitions.hpp>
10 #include <Scenario/Palette/Transitions/EventTransitions.hpp>
11 #include <Scenario/Palette/Transitions/IntervalTransitions.hpp>
12 #include <Scenario/Palette/Transitions/NothingTransitions.hpp>
13 #include <Scenario/Palette/Transitions/StateTransitions.hpp>
14 #include <Scenario/Palette/Transitions/TimeSyncTransitions.hpp>
15 
16 #include <QFinalState>
17 
18 namespace Scenario
19 {
20 template <typename Scenario_T, typename ToolPalette_T>
21 class Creation_FromEvent final : public CreationState<Scenario_T, ToolPalette_T>
22 {
23 public:
25  const ToolPalette_T& stateMachine, const Scenario_T& scenarioPath,
26  const score::CommandStackFacade& stack, QState* parent)
27  : CreationState<Scenario_T, ToolPalette_T>{
28  stateMachine, stack, std::move(scenarioPath), parent}
29  {
30  using namespace Scenario::Command;
31  auto finalState = new QFinalState{this};
32  QObject::connect(finalState, &QState::entered, [&]() { this->clearCreatedIds(); });
33 
34  auto mainState = new QState{this};
35  mainState->setObjectName("Main state");
36  {
37  auto pressed = new QState{mainState};
38  auto released = new QState{mainState};
39  auto move_nothing = new StrongQState<MoveOnNothing>{mainState};
40  auto move_state = new StrongQState<MoveOnState>{mainState};
41  auto move_event = new StrongQState<MoveOnEvent>{mainState};
42  auto move_timesync = new StrongQState<MoveOnTimeSync>{mainState};
43 
44  pressed->setObjectName("Pressed");
45  released->setObjectName("Released");
46  move_nothing->setObjectName("Move on Nothing");
47  move_state->setObjectName("Move on State");
48  move_event->setObjectName("Move on Event");
49  move_timesync->setObjectName("Move on Sync");
50 
51  // General setup
52  mainState->setInitialState(pressed);
53  released->addTransition(finalState);
54 
55  // Release
56  score::make_transition<ReleaseOnAnything_Transition>(mainState, released);
57 
58  // Pressed -> ...
59  score::make_transition<MoveOnNothing_Transition<Scenario_T>>(
60  pressed, move_nothing, *this);
61 
63  // MoveOnNothing -> MoveOnNothing.
64  score::make_transition<MoveOnNothing_Transition<Scenario_T>>(
65  move_nothing, move_nothing, *this);
66 
67  // MoveOnNothing -> MoveOnState.
68  this->add_transition(move_nothing, move_state, [&]() {
69  this->rollback();
70  createToState();
71  });
72 
73  // MoveOnNothing -> MoveOnEvent.
74  this->add_transition(move_nothing, move_event, [&]() {
75  this->rollback();
76  createToEvent();
77  });
78 
79  // MoveOnNothing -> MoveOnTimeSync
80  this->add_transition(move_nothing, move_timesync, [&]() {
81  this->rollback();
82  createToTimeSync();
83  });
84 
86  // MoveOnState -> MoveOnNothing
87  this->add_transition(move_state, move_nothing, [&]() {
88  this->rollback();
89  createToNothing();
90  });
91 
92  // MoveOnState -> MoveOnState
93  // We don't do anything, the interval should not move.
94 
95  // MoveOnState -> MoveOnEvent
96  this->add_transition(move_state, move_event, [&]() {
97  this->rollback();
98  createToEvent();
99  });
100 
101  // MoveOnState -> MoveOnTimeSync
102  this->add_transition(move_state, move_timesync, [&]() {
103  this->rollback();
104  createToTimeSync();
105  });
106 
108  // MoveOnEvent -> MoveOnNothing
109  this->add_transition(move_event, move_nothing, [&]() {
110  this->rollback();
111  createToNothing();
112  });
113 
114  // MoveOnEvent -> MoveOnState
115  this->add_transition(move_event, move_state, [&]() {
116  this->rollback();
117  createToState();
118  });
119 
120  // MoveOnEvent -> MoveOnEvent
121  score::make_transition<MoveOnEvent_Transition<Scenario_T>>(
122  move_event, move_event, *this);
123 
124  // MoveOnEvent -> MoveOnTimeSync
125  this->add_transition(move_event, move_timesync, [&]() {
126  this->rollback();
127  createToTimeSync();
128  });
129 
131  // MoveOnTimeSync -> MoveOnNothing
132  this->add_transition(move_timesync, move_nothing, [&]() {
133  this->rollback();
134  createToNothing();
135  });
136 
137  // MoveOnTimeSync -> MoveOnState
138  this->add_transition(move_timesync, move_state, [&]() {
139  this->rollback();
140  createToState();
141  });
142 
143  // MoveOnTimeSync -> MoveOnEvent
144  this->add_transition(move_timesync, move_event, [&]() {
145  this->rollback();
146  createToEvent();
147  });
148 
149  // MoveOnTimeSync -> MoveOnTimeSync
150  score::make_transition<MoveOnTimeSync_Transition<Scenario_T>>(
151  move_timesync, move_timesync, *this);
152 
153  // What happens in each state.
154  QObject::connect(pressed, &QState::entered, [&]() {
155  this->m_clickedPoint = this->currentPoint;
156  // Create a simple state where we are
157 
158  createInitialState();
159  // createToNothing();
160  });
161 
162  QObject::connect(move_nothing, &QState::entered, [&]() {
163  if(this->createdIntervals.empty() || this->createdEvents.empty())
164  {
165  this->rollback();
166  return;
167  }
168 
169  if(this->currentPoint.date <= this->m_clickedPoint.date)
170  {
171  this->currentPoint.date = this->m_clickedPoint.date + TimeVal::fromMsecs(10);
172  ;
173  }
174 
175  this->currentPoint.date = stateMachine.magnetic().getPosition(
176  &stateMachine.model(), this->currentPoint.date);
177 
178  this->m_dispatcher.template submit<MoveNewEvent>(
179  this->m_scenario, this->createdIntervals.last(), this->createdEvents.last(),
180  this->currentPoint.date, this->currentPoint.y,
181  stateMachine.editionSettings().tool() == Tool::CreateSequence);
182  });
183 
184  QObject::connect(move_timesync, &QState::entered, [&]() {
185  if(this->createdStates.empty())
186  {
187  this->rollback();
188  return;
189  }
190 
191  if(this->currentPoint.date <= this->m_clickedPoint.date)
192  {
193  return;
194  }
195 
196  this->m_dispatcher.template submit<MoveNewState>(
197  this->m_scenario, this->createdStates.last(), this->currentPoint.y);
198  });
199 
200  QObject::connect(move_event, &QState::entered, [&]() {
201  if(this->createdStates.empty())
202  {
203  this->rollback();
204  return;
205  }
206 
207  if(this->currentPoint.date <= this->m_clickedPoint.date)
208  {
209  return;
210  }
211 
212  this->m_dispatcher.template submit<MoveNewState>(
213  this->m_scenario, this->createdStates.last(), this->currentPoint.y);
214  });
215 
216  QObject::connect(released, &QState::entered, this, &Creation_FromEvent::commit);
217  }
218 
219  auto rollbackState = new QState{this};
220  rollbackState->setObjectName("Rollback");
221  score::make_transition<score::Cancel_Transition>(mainState, rollbackState);
222  rollbackState->addTransition(finalState);
223  QObject::connect(
224  rollbackState, &QState::entered, this, &Creation_FromEvent::rollback);
225 
226  this->setInitialState(mainState);
227  }
228 
229 private:
230  void createInitialState()
231  {
232  if(this->clickedEvent)
233  {
234  auto cmd = new Scenario::Command::CreateState{
235  this->m_scenario, *this->clickedEvent, this->currentPoint.y};
236  this->m_dispatcher.submit(cmd);
237 
238  this->createdStates.append(cmd->createdState());
239  }
240  }
241 
242  void createToNothing()
243  {
244  createInitialState();
245  this->createToNothing_base(this->createdStates.first());
246  }
247 
248  void createToState()
249  {
250  createInitialState();
251  this->createToState_base(this->createdStates.first());
252  }
253 
254  // Note : clickedEvent is set at startEvent if clicking in the background.
255  void createToEvent()
256  {
257  if(this->hoveredEvent != this->clickedEvent)
258  {
259  createInitialState();
260  this->createToEvent_base(this->createdStates.first());
261  }
262  }
263 
264  void createToTimeSync()
265  {
266  createInitialState();
267  this->createToTimeSync_base(this->createdStates.first());
268  }
269 };
270 }
Definition: CreateState.hpp:22
Definition: ScenarioCreation_FromEvent.hpp:22
Creation_FromEvent(const ToolPalette_T &stateMachine, const Scenario_T &scenarioPath, const score::CommandStackFacade &stack, QState *parent)
Definition: ScenarioCreation_FromEvent.hpp:24
Definition: ScenarioCreationState.hpp:66
A small abstraction layer over the score::CommandStack.
Definition: CommandStackFacade.hpp:20
Main plug-in of score.
Definition: score-plugin-dataflow/Dataflow/PortItem.hpp:14