-
-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathLoadPresetCommand.hpp
More file actions
170 lines (142 loc) · 4.72 KB
/
Copy pathLoadPresetCommand.hpp
File metadata and controls
170 lines (142 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#pragma once
#include <State/Value.hpp>
#include <State/ValueSerialization.hpp>
#include <Process/Commands/LoadPresetCommandFactory.hpp>
#include <Process/Dataflow/Port.hpp>
#include <Process/Process.hpp>
#include <Scenario/Commands/ScenarioCommandFactory.hpp>
#include <Dataflow/Commands/CableHelpers.hpp>
#include <score/model/path/PathSerialization.hpp>
#include <score_plugin_scenario_export.h>
namespace Scenario::Command
{
class LoadPreset final : public score::Command
{
SCORE_COMMAND_DECL(
Scenario::Command::CommandFactoryName(), LoadPreset, "Load a preset")
public:
LoadPreset(const Process::ProcessModel& obj, Process::Preset newval)
: m_path{obj}
, m_old{obj.savePreset()}
, m_new{std::move(newval)}
{
}
private:
void undo(const score::DocumentContext& ctx) const final override
{
m_path.find(ctx).loadPreset(m_old);
}
void redo(const score::DocumentContext& ctx) const final override
{
m_path.find(ctx).loadPreset(m_new);
}
void serializeImpl(DataStreamInput& stream) const final override
{
stream << m_path << m_old << m_new;
}
void deserializeImpl(DataStreamOutput& stream) final override
{
stream >> m_path >> m_old >> m_new;
}
Path<Process::ProcessModel> m_path;
Process::Preset m_old;
Process::Preset m_new;
};
// Note: see ScriptEditCommand.hpp which is very similar!
class LoadPresetWithCablesBackup final : public score::Command
{
SCORE_COMMAND_DECL(
Scenario::Command::CommandFactoryName(), LoadPresetWithCablesBackup,
"Load a preset")
public:
LoadPresetWithCablesBackup(
const Process::ProcessModel& model, Process::Preset newval,
const score::DocumentContext& ctx)
: m_path{model}
, m_old{model.savePreset()}
, m_new{std::move(newval)}
{
m_oldCables
= Dataflow::saveCables({const_cast<Process::ProcessModel*>(&model)}, ctx);
for(auto& port : model.inlets())
m_oldInlets.emplace_back(
Dataflow::SavedPort{port->name(), port->type(), port->saveData()});
for(auto& port : model.outlets())
m_oldOutlets.emplace_back(
Dataflow::SavedPort{port->name(), port->type(), port->saveData()});
}
private:
void undo(const score::DocumentContext& ctx) const final override
{
auto& cmt = m_path.find(ctx);
// Remove all the cables that could have been added during
// the creation
Dataflow::removeCables(m_oldCables, ctx);
// Set the old preset
cmt.loadPreset(m_old);
// We expect the inputs / outputs to revert back to the
// exact same state
SCORE_ASSERT(m_oldInlets.size() == cmt.inlets().size());
SCORE_ASSERT(m_oldOutlets.size() == cmt.outlets().size());
// So we can reload their data identically
for(std::size_t i = 0; i < m_oldInlets.size(); i++)
{
cmt.inlets()[i]->loadData(m_oldInlets[i].data);
}
for(std::size_t i = 0; i < m_oldOutlets.size(); i++)
{
cmt.outlets()[i]->loadData(m_oldOutlets[i].data);
}
// Recreate the old cables
auto cables = Dataflow::restoreCablesWithoutTouchingPorts(m_oldCables, ctx);
cmt.inletsChanged();
cmt.outletsChanged();
Dataflow::notifyAddedCables(cables, ctx);
}
void redo(const score::DocumentContext& ctx) const final override
{
Dataflow::removeCables(m_oldCables, ctx);
auto& cmt = m_path.find(ctx);
cmt.loadPreset(m_new);
auto cables = Dataflow::reloadPortsInNewProcess(
m_oldInlets, m_oldOutlets, m_oldCables, cmt,
Process::PortLoadDataFlags::ReloadValue, ctx);
cmt.inletsChanged();
cmt.outletsChanged();
Dataflow::notifyAddedCables(cables, ctx);
}
void serializeImpl(DataStreamInput& stream) const final override
{
stream << m_path << m_old << m_new << m_oldInlets << m_oldOutlets << m_oldCables;
}
void deserializeImpl(DataStreamOutput& stream) final override
{
stream >> m_path >> m_old >> m_new >> m_oldInlets >> m_oldOutlets >> m_oldCables;
}
Path<Process::ProcessModel> m_path;
Process::Preset m_old;
Process::Preset m_new;
std::vector<Dataflow::SavedPort> m_oldInlets, m_oldOutlets;
Dataflow::SerializedCables m_oldCables;
};
class LoadPresetCommandFactory final : public Process::LoadPresetCommandFactory
{
SCORE_CONCRETE("b04a9d8a-1374-4223-a258-0f90e8e0aef6")
public:
bool matches(
const Process::ProcessModel& obj, unused_t,
const score::DocumentContext& ctx) const noexcept override
{
return true;
}
score::Command* make(
const Process::ProcessModel& obj, Process::Preset newval,
const score::DocumentContext& ctx) const override
{
if(obj.flags() & Process::ProcessFlags::DynamicPorts)
return new LoadPresetWithCablesBackup{obj, std::move(newval), ctx};
else
return new LoadPreset{obj, std::move(newval)};
}
};
}