AddTrigger.hpp
1 #pragma once
2 
3 #include <State/Expression.hpp>
4 
5 #include <Scenario/Commands/Interval/SetRigidity.hpp>
6 #include <Scenario/Commands/ScenarioCommandFactory.hpp>
7 #include <Scenario/Document/TimeSync/TimeSyncModel.hpp>
8 
9 #include <score/command/Command.hpp>
10 #include <score/document/DocumentInterface.hpp>
11 #include <score/model/path/Path.hpp>
13 
14 namespace Scenario
15 {
16 class TimeSyncModel;
17 namespace Command
18 {
19 template <typename Scenario_T>
20 class AddTrigger final : public score::Command
21 {
22 public:
23  const CommandGroupKey& parentKey() const noexcept override
24  {
25  return ::CommandFactoryName<Scenario_T>();
26  }
27  const CommandKey& key() const noexcept override { return static_key(); }
28  QString description() const override { return QObject::tr("Add a trigger"); }
29  static const CommandKey& static_key() noexcept
30  {
31  static const CommandKey kagi{
32  QString("AddTrigger_") + Metadata<ObjectKey_k, Scenario_T>::get()};
33  return kagi;
34  }
35 
36  AddTrigger() = default;
37 
38  AddTrigger(const TimeSyncModel& tn)
39  : m_path{tn}
40  {
41  Scenario_T* scenar = safe_cast<Scenario_T*>(tn.parent());
42  for(const auto& cstrId : intervalsBeforeTimeSync(*scenar, tn.id()))
43  {
44  m_cmds.emplace_back(scenar->interval(cstrId), false);
45  }
46  }
47 
48  void undo(const score::DocumentContext& ctx) const override
49  {
50  auto& tn = m_path.find(ctx);
51  tn.setActive(false);
52 
53  for(const auto& cmd : m_cmds)
54  {
55  cmd.undo(ctx);
56  }
57  }
58 
59  void redo(const score::DocumentContext& ctx) const override
60  {
61  auto& tn = m_path.find(ctx);
62  tn.setActive(true);
63 
64  for(const auto& cmd : m_cmds)
65  {
66  cmd.redo(ctx);
67  }
68  }
69 
70 protected:
71  void serializeImpl(DataStreamInput& s) const override
72  {
73  s << m_path;
74  s << (int32_t)m_cmds.size();
75 
76  for(const auto& cmd : m_cmds)
77  {
78  s << cmd.serialize();
79  }
80  }
81 
82  void deserializeImpl(DataStreamOutput& s) override
83  {
84  int32_t n;
85  s >> m_path;
86  s >> n;
87  m_cmds.resize(n);
88  for(int i = 0; i < n; i++)
89  {
90  QByteArray a;
91  s >> a;
92  m_cmds[i].deserialize(a);
93  }
94  }
95 
96 private:
97  Path<TimeSyncModel> m_path;
98  std::vector<SetRigidity> m_cmds;
99 };
100 }
101 }
102 
103 #include <Scenario/Process/ScenarioModel.hpp>
105 
106 #include <Scenario/Document/BaseScenario/BaseScenario.hpp>
The Path class is a typesafe wrapper around ObjectPath.
Definition: Path.hpp:52
Definition: AddTrigger.hpp:21
Definition: TimeSyncModel.hpp:34
The Command class.
Definition: Command.hpp:34
Main plug-in of score.
Definition: score-plugin-dataflow/Dataflow/PortItem.hpp:14
Definition: DataStreamHelpers.hpp:99
Definition: DataStreamHelpers.hpp:103
Static metadata implementation.
Definition: lib/score/tools/Metadata.hpp:36
Definition: DocumentContext.hpp:18