-
Notifications
You must be signed in to change notification settings - Fork 33
/
GroupTriggersCard.cpp
286 lines (251 loc) · 9.47 KB
/
GroupTriggersCard.cpp
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
* Shortcircuit XT - a Surge Synth Team product
*
* A fully featured creative sampler, available as a standalone
* and plugin for multiple platforms.
*
* Copyright 2019 - 2024, Various authors, as described in the github
* transaction log.
*
* ShortcircuitXT is released under the Gnu General Public Licence
* V3 or later (GPL-3.0-or-later). The license is found in the file
* "LICENSE" in the root of this repository or at
* https://www.gnu.org/licenses/gpl-3.0.en.html
*
* Individual sections of code which comprises ShortcircuitXT in this
* repository may also be used under an MIT license. Please see the
* section "Licensing" in "README.md" for details.
*
* ShortcircuitXT is inspired by, and shares code with, the
* commercial product Shortcircuit 1 and 2, released by VemberTech
* in the mid 2000s. The code for Shortcircuit 2 was opensourced in
* 2020 at the outset of this project.
*
* All source for ShortcircuitXT is available at
* https://github.com/surge-synthesizer/shortcircuit-xt
*/
#include "GroupTriggersCard.h"
#include "sst/jucegui/components/ToggleButton.h"
#include "sst/jucegui/components/TextPushButton.h"
#include "sst/jucegui/components/MenuButton.h"
#include "sst/jucegui/components/DraggableTextEditableValue.h"
#include "app/SCXTEditor.h"
#include "messaging/client/client_messages.h"
#include "connectors/PayloadDataAttachment.h"
namespace scxt::ui::app::edit_screen
{
namespace jcmp = sst::jucegui::components;
struct GroupTriggersCard::ConditionRow : juce::Component, HasEditor
{
using booleanAttachment_t =
connectors::BooleanPayloadDataAttachment<scxt::engine::GroupTriggerConditions>;
using floatAttachment_t =
connectors::PayloadDataAttachment<scxt::engine::GroupTriggerConditions>;
GroupTriggersCard *parent{nullptr};
engine::GroupTriggerID lastID{engine::GroupTriggerID::NONE};
int index{-1};
bool withCondition{false};
ConditionRow(GroupTriggersCard *p, int index, bool withCond)
: parent(p), index(index), withCondition(withCond), HasEditor(p)
{
activeA = std::make_unique<booleanAttachment_t>(
"Active",
[this](const auto &a) {
setupValuesFromData();
parent->pushUpdate();
},
parent->cond.active[index]);
activeB = std::make_unique<jcmp::ToggleButton>();
activeB->setLabel(std::to_string(index + 1));
activeB->setSource(activeA.get());
addAndMakeVisible(*activeB);
auto mkm = [this](auto tx, auto cs) {
auto res = std::make_unique<jcmp::TextPushButton>();
res->setLabel(tx);
res->setOnCallback(
editor->makeComingSoon(std::string() + "Trigger Condition Pane " + cs));
addAndMakeVisible(*res);
return res;
};
typeM = mkm("TYPE", "Trigger Mode");
typeM->setOnCallback([w = juce::Component::SafePointer(this)]() {
if (!w)
return;
w->showTypeMenu();
});
if (withCond)
cM = mkm("&", "Conjunction");
setupValuesFromData();
}
void setupValuesFromData()
{
auto &sr = parent->cond.storage[index];
if (sr.id != lastID)
{
lastID = sr.id;
auto onArgChanged = [this](const auto &a) { parent->pushUpdate(); };
if (sr.id == engine::GroupTriggerID::NONE)
{
SCLOG("NONE");
a1A.reset();
a2A.reset();
a1M.reset();
a2M.reset();
}
else if ((int)sr.id >= (int)engine::GroupTriggerID::MACRO &&
(int)sr.id <= (int)engine::GroupTriggerID::MACRO + scxt::macrosPerPart)
{
auto dm = datamodel::pmd()
.asFloat()
.withRange(0, 1)
.withLinearScaleFormatting("")
.withDecimalPlaces(2)
.withDefault(0.5);
a1A = std::make_unique<floatAttachment_t>(dm, onArgChanged, sr.args[0]);
a2A = std::make_unique<floatAttachment_t>(dm, onArgChanged, sr.args[1]);
a1M = std::make_unique<jcmp::DraggableTextEditableValue>();
a1M->setSource(a1A.get());
addAndMakeVisible(*a1M);
a2M = std::make_unique<jcmp::DraggableTextEditableValue>();
a2M->setSource(a2A.get());
addAndMakeVisible(*a2M);
}
else if ((int)sr.id >= (int)engine::GroupTriggerID::MIDICC &&
(int)sr.id <= (int)engine::GroupTriggerID::LAST_MIDICC)
{
auto dm = datamodel::pmd()
.asFloat()
.withRange(0, 127)
.withLinearScaleFormatting("")
.withDecimalPlaces(0)
.withDefault(64);
a1A = std::make_unique<floatAttachment_t>(dm, onArgChanged, sr.args[0]);
a2A = std::make_unique<floatAttachment_t>(dm, onArgChanged, sr.args[1]);
a1M = std::make_unique<jcmp::DraggableTextEditableValue>();
a1M->setSource(a1A.get());
addAndMakeVisible(*a1M);
a2M = std::make_unique<jcmp::DraggableTextEditableValue>();
a2M->setSource(a2A.get());
addAndMakeVisible(*a2M);
}
else
{
SCLOG_UNIMPL("No midi CC for type " << (int)sr.id);
}
resized();
}
typeM->setLabel(engine::getGroupTriggerDisplayName(sr.id));
auto showRest = (sr.id != engine::GroupTriggerID::NONE);
if (a1M)
a1M->setVisible(showRest);
if (a2M)
a2M->setVisible(showRest);
if (cM)
cM->setVisible(showRest);
auto ac = parent->cond.active[index];
typeM->setEnabled(ac);
if (a1M)
a1M->setEnabled(ac);
if (a2M)
a2M->setEnabled(ac);
if (cM)
cM->setEnabled(ac);
repaint();
}
void showTypeMenu()
{
auto p = juce::PopupMenu();
p.addSectionHeader("Trigger Mode");
p.addSeparator();
auto mkv = [this](auto v) {
auto that = this; // darn you MSVC
return [w = juce::Component::SafePointer(that), v]() {
if (!w)
return;
w->parent->cond.storage[w->index].id = (engine::GroupTriggerID)v;
w->parent->pushUpdate();
w->setupValuesFromData();
};
};
p.addItem("NONE", mkv((int)engine::GroupTriggerID::NONE));
auto mcc = juce::PopupMenu();
for (int i = 0; i < 128; ++i)
{
mcc.addItem(fmt::format("CC {:3d}", i), mkv((int)engine::GroupTriggerID::MIDICC + i));
}
p.addSubMenu("MIDI CC", mcc);
auto msm = juce::PopupMenu();
for (int i = 0; i < scxt::macrosPerPart; ++i)
{
msm.addItem("Macro " + std::to_string(i + 1),
mkv((int)engine::GroupTriggerID::MACRO + i));
}
p.addSubMenu("Macros", msm);
p.showMenuAsync(editor->defaultPopupMenuOptions());
}
void resized() override
{
auto rb = getLocalBounds().withHeight(16);
auto tb = rb.withWidth(16);
activeB->setBounds(tb);
tb = tb.translated(tb.getWidth() + 2, 0).withWidth(72);
typeM->setBounds(tb);
tb = tb.translated(tb.getWidth() + 2, 0).withWidth(32);
if (a1M)
a1M->setBounds(tb);
tb = tb.translated(tb.getWidth() + 2, 0).withWidth(32);
if (a2M)
a2M->setBounds(tb);
if (cM)
{
tb = tb.translated(tb.getWidth() + 2, 0).withWidth(16);
cM->setBounds(tb);
}
}
std::unique_ptr<booleanAttachment_t> activeA;
std::unique_ptr<floatAttachment_t> a1A, a2A;
std::unique_ptr<jcmp::ToggleButton> activeB;
std::unique_ptr<jcmp::TextPushButton> typeM, cM;
std::unique_ptr<jcmp::DraggableTextEditableValue> a1M, a2M;
};
GroupTriggersCard::GroupTriggersCard(SCXTEditor *e) : HasEditor(e)
{
for (int i = 0; i < scxt::triggerConditionsPerGroup; ++i)
{
rows[i] = std::make_unique<ConditionRow>(this, i, i != scxt::triggerConditionsPerGroup - 1);
addAndMakeVisible(*rows[i]);
}
}
GroupTriggersCard::~GroupTriggersCard() = default;
void GroupTriggersCard::resized()
{
int rowHeight = 24;
int componentHeight = 16;
auto b = getLocalBounds().withTrimmedTop(rowHeight - 4);
auto r = b.withHeight(componentHeight);
for (int i = 0; i < scxt::triggerConditionsPerGroup; ++i)
{
rows[i]->setBounds(r);
r = r.translated(0, rowHeight);
}
}
void GroupTriggersCard::paint(juce::Graphics &g)
{
auto ft = editor->themeApplier.interMediumFor(13);
auto co = editor->themeColor(theme::ColorMap::generic_content_low);
g.setFont(ft);
g.setColour(co);
g.drawText("TRIGGER CONDITIONS", getLocalBounds(), juce::Justification::topLeft);
}
void GroupTriggersCard::setGroupTriggerConditions(const scxt::engine::GroupTriggerConditions &c)
{
cond = c;
for (auto &r : rows)
r->setupValuesFromData();
repaint();
}
void GroupTriggersCard::pushUpdate()
{
sendToSerialization(scxt::messaging::client::UpdateGroupTriggerConditions(cond));
}
} // namespace scxt::ui::app::edit_screen