Loading...
Searching...
No Matches
PatternProperties.hpp
1#pragma once
2#include <Midi/Commands/CommandFactory.hpp>
3
4#include <score/command/Command.hpp>
5#include <score/command/PropertyCommand.hpp>
6#include <score/model/path/PathSerialization.hpp>
7
8#include <Patternist/PatternModel.hpp>
9namespace Patternist
10{
11inline auto& CommandFactoryName()
12{
13 return Midi::CommandFactoryName();
14}
17class UpdatePattern final : public score::Command
18{
19 SCORE_COMMAND_DECL(Midi::CommandFactoryName(), UpdatePattern, "Update a pattern")
20public:
21 UpdatePattern(const ProcessModel& model, int p, const Pattern& pat)
22 : m_model{model}
23 , m_old{model.patterns()}
24 , m_new{model.patterns()}
25 {
26 update(model, p, pat);
27 }
28
29 void undo(const score::DocumentContext& ctx) const override
30 {
31 m_model.find(ctx).setPatterns(m_old);
32 }
33
34 void redo(const score::DocumentContext& ctx) const override
35 {
36 m_model.find(ctx).setPatterns(m_new);
37 }
38
39 void update(const ProcessModel& model, int p, const Pattern& pat)
40 {
41 if(ossia::valid_index(p, m_new))
42 m_new[p] = pat;
43 }
44
45protected:
46 void serializeImpl(DataStreamInput& s) const override
47 {
48 s << m_model << m_old << m_new;
49 }
50
51 void deserializeImpl(DataStreamOutput& s) override
52 {
53 s >> m_model >> m_old >> m_new;
54 }
55
56private:
57 Path<ProcessModel> m_model;
58 std::vector<Pattern> m_old;
59 std::vector<Pattern> m_new;
60};
61
62class UpdatePatterns final : public score::Command
63{
64 SCORE_COMMAND_DECL(Midi::CommandFactoryName(), UpdatePatterns, "Update patterns")
65public:
66 UpdatePatterns(const ProcessModel& model, std::vector<Pattern> pat)
67 : m_model{model}
68 , m_old{model.patterns()}
69 , m_new{std::move(pat)}
70 {
71 }
72
73 void undo(const score::DocumentContext& ctx) const override
74 {
75 m_model.find(ctx).setPatterns(m_old);
76 }
77
78 void redo(const score::DocumentContext& ctx) const override
79 {
80 m_model.find(ctx).setPatterns(m_new);
81 }
82
83protected:
84 void serializeImpl(DataStreamInput& s) const override
85 {
86 s << m_model << m_old << m_new;
87 }
88
89 void deserializeImpl(DataStreamOutput& s) override { s >> m_model >> m_old >> m_new; }
90
91private:
92 Path<ProcessModel> m_model;
93 std::vector<Pattern> m_old;
94 std::vector<Pattern> m_new;
95};
96
99class ReplacePatterns final : public score::Command
100{
101 SCORE_COMMAND_DECL(Midi::CommandFactoryName(), ReplacePatterns, "Change the patterns")
102public:
103 ReplacePatterns(const ProcessModel& model, std::vector<Pattern> pat, int current)
104 : m_model{model}
105 , m_old{model.patterns()}
106 , m_new{std::move(pat)}
107 , m_oldCurrent{model.currentPattern()}
108 , m_newCurrent{current}
109 {
110 }
111
112 void undo(const score::DocumentContext& ctx) const override
113 {
114 auto& m = m_model.find(ctx);
115 m.setPatterns(m_old);
116 m.setCurrentPattern(m_oldCurrent);
117 }
118
119 void redo(const score::DocumentContext& ctx) const override
120 {
121 auto& m = m_model.find(ctx);
122 m.setPatterns(m_new);
123 m.setCurrentPattern(m_newCurrent);
124 }
125
126protected:
127 void serializeImpl(DataStreamInput& s) const override
128 {
129 s << m_model << m_old << m_new << m_oldCurrent << m_newCurrent;
130 }
131
132 void deserializeImpl(DataStreamOutput& s) override
133 {
134 s >> m_model >> m_old >> m_new >> m_oldCurrent >> m_newCurrent;
135 }
136
137private:
138 Path<ProcessModel> m_model;
139 std::vector<Pattern> m_old;
140 std::vector<Pattern> m_new;
141 int m_oldCurrent{};
142 int m_newCurrent{};
143};
144}
145PROPERTY_COMMAND_T(
146 Patternist, SetPatternChannel, ProcessModel::p_channel, "Change channel")
147SCORE_COMMAND_DECL_T(Patternist::SetPatternChannel)
148PROPERTY_COMMAND_T(
149 Patternist, SetCurrentPattern, ProcessModel::p_currentPattern, "Change pattern")
150SCORE_COMMAND_DECL_T(Patternist::SetCurrentPattern)
151PROPERTY_COMMAND_T(
152 Patternist, SetLaneLabels, ProcessModel::p_laneLabels, "Change lane labels")
153SCORE_COMMAND_DECL_T(Patternist::SetLaneLabels)
The Path class is a typesafe wrapper around ObjectPath.
Definition Path.hpp:52
Definition PatternModel.hpp:247
Definition PatternProperties.hpp:100
Definition PatternProperties.hpp:18
Definition PatternProperties.hpp:63
The Command class.
Definition Command.hpp:34
Definition DataStreamHelpers.hpp:99
Definition DataStreamHelpers.hpp:103
Definition PatternModel.hpp:139
Definition DocumentContext.hpp:18