-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGeneratorPresetEditor.h
More file actions
484 lines (418 loc) · 17.7 KB
/
GeneratorPresetEditor.h
File metadata and controls
484 lines (418 loc) · 17.7 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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
// Super Timecode Converter
// Copyright (c) 2026 Fiverecords -- MIT License
// https://github.com/fiverecords/SuperTimecodeConverter
#pragma once
#include <JuceHeader.h>
#include "AppSettings.h"
//==============================================================================
// GeneratorPresetEditor -- Table editor for named timecode generator presets.
//
// Each preset has a Name, Start TC, and Stop TC. The editor mirrors the
// TrackMapEditor UX: table + form at the bottom for add/edit.
// Calls onChange() whenever the preset map is modified.
//==============================================================================
class GeneratorPresetEditor : public juce::Component,
public juce::TableListBoxModel
{
public:
GeneratorPresetEditor(GeneratorPresetMap& map)
: presetMap(map)
{
setSize(480, 360);
rebuildRows();
// --- Table ---
addAndMakeVisible(table);
table.setModel(this);
table.setColour(juce::ListBox::backgroundColourId, bgDarker);
table.setColour(juce::ListBox::outlineColourId, borderCol);
table.setOutlineThickness(1);
table.setRowHeight(24);
table.setHeaderHeight(22);
table.getHeader().setStretchToFitActive(true);
auto& hdr = table.getHeader();
hdr.addColumn("Name", ColName, 160, 80, 300, juce::TableHeaderComponent::notSortable);
hdr.addColumn("Start TC", ColStart, 120, 80, 180, juce::TableHeaderComponent::notSortable);
hdr.addColumn("Stop TC", ColStop, 120, 80, 180, juce::TableHeaderComponent::notSortable);
// --- Buttons ---
auto addBtn = [this](juce::TextButton& btn, const juce::String& text)
{
addAndMakeVisible(btn);
btn.setButtonText(text);
btn.setColour(juce::TextButton::buttonColourId, bgPanel);
btn.setColour(juce::TextButton::textColourOffId, textBright);
};
addBtn(btnAdd, "Add");
addBtn(btnSave, "Save");
addBtn(btnDelete, "Delete");
addBtn(btnClearAll, "Clear All");
addBtn(btnImport, "Import");
addBtn(btnExport, "Export");
addBtn(btnOscHelp, "OSC ?");
btnSave.setColour(juce::TextButton::buttonColourId, juce::Colour(0xFF22AA44).withAlpha(0.3f));
btnSave.setColour(juce::TextButton::textColourOffId, juce::Colour(0xFF22DD55));
btnImport.setColour(juce::TextButton::buttonColourId, juce::Colour(0xFF2244AA).withAlpha(0.3f));
btnImport.setColour(juce::TextButton::textColourOffId, juce::Colour(0xFF4488FF));
btnExport.setColour(juce::TextButton::buttonColourId, juce::Colour(0xFF2244AA).withAlpha(0.3f));
btnExport.setColour(juce::TextButton::textColourOffId, juce::Colour(0xFF4488FF));
btnOscHelp.setColour(juce::TextButton::buttonColourId, juce::Colour(0xFF555555));
btnOscHelp.setColour(juce::TextButton::textColourOffId, juce::Colour(0xFFFFCC44));
btnAdd.onClick = [this] { addNewPreset(); };
btnSave.onClick = [this] { saveSelectedPreset(); };
btnDelete.onClick = [this] { deleteSelected(); };
btnClearAll.onClick = [this] { clearAll(); };
btnImport.onClick = [this] { importPresets(); };
btnExport.onClick = [this] { exportPresets(); };
btnOscHelp.onClick = [this] { showOscReference(); };
// --- Form fields ---
auto addField = [this](juce::Label& lbl, juce::TextEditor& ed,
const juce::String& labelText, const juce::String& defaultText)
{
addAndMakeVisible(lbl);
lbl.setText(labelText, juce::dontSendNotification);
lbl.setFont(juce::Font(juce::FontOptions(9.0f)));
lbl.setColour(juce::Label::textColourId, textMid);
lbl.setJustificationType(juce::Justification::centredRight);
addAndMakeVisible(ed);
ed.setFont(juce::Font(juce::FontOptions(11.0f)));
ed.setColour(juce::TextEditor::backgroundColourId, juce::Colour(0xFF222222));
ed.setColour(juce::TextEditor::textColourId, juce::Colours::white);
ed.setText(defaultText, false);
};
addField(lblName, edName, "Name:", "");
addField(lblStartTC, edStartTC, "Start TC:", "00:00:00:00");
addField(lblStopTC, edStopTC, "Stop TC:", "00:00:00:00");
edName.onReturnKey = [this] { saveSelectedPreset(); };
edStartTC.onReturnKey = [this] { saveSelectedPreset(); };
edStopTC.onReturnKey = [this] { saveSelectedPreset(); };
}
~GeneratorPresetEditor() override = default;
std::function<void()> onChange;
//--------------------------------------------------------------------------
// TableListBoxModel
//--------------------------------------------------------------------------
int getNumRows() override { return (int)rows.size(); }
void paintRowBackground(juce::Graphics& g, int rowNumber, int, int,
bool rowIsSelected) override
{
g.fillAll(rowIsSelected ? accentCyan.withAlpha(0.15f)
: (rowNumber % 2 == 0 ? bgDarker : bgDarker.brighter(0.03f)));
}
void paintCell(juce::Graphics& g, int rowNumber, int columnId,
int width, int height, bool /*rowIsSelected*/) override
{
if (rowNumber < 0 || rowNumber >= (int)rows.size()) return;
auto& p = rows[(size_t)rowNumber];
g.setColour(textBright);
g.setFont(juce::Font(juce::FontOptions(11.0f)));
juce::String text;
switch (columnId)
{
case ColName: text = p.name; break;
case ColStart: text = p.startTC; break;
case ColStop: text = p.stopTC; break;
}
g.drawText(text, 4, 0, width - 8, height, juce::Justification::centredLeft, true);
}
void selectedRowsChanged(int lastRowSelected) override
{
if (lastRowSelected >= 0 && lastRowSelected < (int)rows.size())
{
auto& p = rows[(size_t)lastRowSelected];
edName.setText(p.name, false);
edStartTC.setText(p.startTC, false);
edStopTC.setText(p.stopTC, false);
}
}
void cellDoubleClicked(int rowNumber, int, const juce::MouseEvent&) override
{
// Double-click = populate form for editing
selectedRowsChanged(rowNumber);
}
//--------------------------------------------------------------------------
// Layout
//--------------------------------------------------------------------------
void resized() override
{
auto area = getLocalBounds().reduced(8);
// Bottom form: 3 rows of label+editor + 2 button rows
auto formArea = area.removeFromBottom(140);
area.removeFromBottom(6);
// File operations row (Import / Export / OSC Help)
auto fileRow = formArea.removeFromBottom(26);
int fileBtnW = 80, gap = 4;
btnImport.setBounds(fileRow.removeFromLeft(fileBtnW)); fileRow.removeFromLeft(gap);
btnExport.setBounds(fileRow.removeFromLeft(fileBtnW)); fileRow.removeFromLeft(gap);
btnOscHelp.setBounds(fileRow.removeFromRight(50));
formArea.removeFromBottom(4);
// Edit button row
auto btnRow = formArea.removeFromBottom(26);
int btnW = 65;
btnAdd .setBounds(btnRow.removeFromLeft(btnW)); btnRow.removeFromLeft(gap);
btnSave .setBounds(btnRow.removeFromLeft(btnW)); btnRow.removeFromLeft(gap);
btnDelete .setBounds(btnRow.removeFromLeft(btnW)); btnRow.removeFromLeft(gap);
btnClearAll.setBounds(btnRow.removeFromLeft(btnW));
formArea.removeFromBottom(6);
// Form fields
auto layField = [](juce::Label& lbl, juce::TextEditor& ed, juce::Rectangle<int>& area)
{
auto row = area.removeFromTop(22);
lbl.setBounds(row.removeFromLeft(60));
row.removeFromLeft(4);
ed.setBounds(row);
area.removeFromTop(3);
};
layField(lblName, edName, formArea);
layField(lblStartTC, edStartTC, formArea);
layField(lblStopTC, edStopTC, formArea);
// Table fills the rest
table.setBounds(area);
}
private:
enum { ColName = 1, ColStart, ColStop };
GeneratorPresetMap& presetMap;
juce::TableListBox table { "Presets", this };
std::vector<GeneratorPreset> rows;
// Buttons
juce::TextButton btnAdd, btnSave, btnDelete, btnClearAll;
juce::TextButton btnImport, btnExport, btnOscHelp;
// Form
juce::Label lblName, lblStartTC, lblStopTC;
juce::TextEditor edName, edStartTC, edStopTC;
// File chooser (must persist during async operation)
std::unique_ptr<juce::FileChooser> fileChooser;
// Colors
juce::Colour bgDarker { 0xFF1A1A1A };
juce::Colour bgPanel { 0xFF333333 };
juce::Colour borderCol { 0xFF444444 };
juce::Colour textBright { 0xFFDDDDDD };
juce::Colour textMid { 0xFF999999 };
juce::Colour accentCyan { 0xFF00AAFF };
void rebuildRows()
{
rows = presetMap.getAllSorted();
}
void notifyChange()
{
presetMap.save();
rebuildRows();
table.updateContent();
table.repaint();
if (onChange) onChange();
}
/// Normalize a timecode string (HH:MM:SS:FF) by carrying overflow.
static juce::String normalizeTC(const juce::String& tc)
{
auto parts = juce::StringArray::fromTokens(tc, ":.", "");
int h = 0, m = 0, s = 0, f = 0;
if (parts.size() >= 1) h = parts[0].getIntValue();
if (parts.size() >= 2) m = parts[1].getIntValue();
if (parts.size() >= 3) s = parts[2].getIntValue();
if (parts.size() >= 4) f = parts[3].getIntValue();
if (f < 0) f = 0;
if (s < 0) s = 0;
if (m < 0) m = 0;
if (h < 0) h = 0;
// Carry overflow
s += f / 30; f %= 30; // assume max 30fps for frame carry
m += s / 60; s %= 60;
h += m / 60; m %= 60;
h %= 24;
return juce::String(h).paddedLeft('0', 2) + ":"
+ juce::String(m).paddedLeft('0', 2) + ":"
+ juce::String(s).paddedLeft('0', 2) + ":"
+ juce::String(f).paddedLeft('0', 2);
}
void addNewPreset()
{
auto name = edName.getText().trim();
if (name.isEmpty())
{
edName.grabKeyboardFocus();
return;
}
GeneratorPreset p;
p.name = name;
p.startTC = edStartTC.getText().trim();
p.stopTC = edStopTC.getText().trim();
if (p.startTC.isEmpty()) p.startTC = "00:00:00:00";
if (p.stopTC.isEmpty()) p.stopTC = "00:00:00:00";
p.startTC = normalizeTC(p.startTC);
p.stopTC = normalizeTC(p.stopTC);
edStartTC.setText(p.startTC, false);
edStopTC.setText(p.stopTC, false);
presetMap.addOrUpdate(p);
notifyChange();
// Select the newly added row
for (int i = 0; i < (int)rows.size(); ++i)
{
if (rows[(size_t)i].name.equalsIgnoreCase(name))
{
table.selectRow(i);
break;
}
}
}
void saveSelectedPreset()
{
auto name = edName.getText().trim();
if (name.isEmpty()) return;
// If a row is selected and the name matches, update it
int sel = table.getSelectedRow();
if (sel >= 0 && sel < (int)rows.size())
{
auto& existing = rows[(size_t)sel];
// Remove old entry if name changed
if (!existing.name.equalsIgnoreCase(name))
presetMap.remove(existing.name);
}
GeneratorPreset p;
p.name = name;
p.startTC = edStartTC.getText().trim();
p.stopTC = edStopTC.getText().trim();
if (p.startTC.isEmpty()) p.startTC = "00:00:00:00";
if (p.stopTC.isEmpty()) p.stopTC = "00:00:00:00";
p.startTC = normalizeTC(p.startTC);
p.stopTC = normalizeTC(p.stopTC);
edStartTC.setText(p.startTC, false);
edStopTC.setText(p.stopTC, false);
presetMap.addOrUpdate(p);
notifyChange();
}
void deleteSelected()
{
int sel = table.getSelectedRow();
if (sel < 0 || sel >= (int)rows.size()) return;
presetMap.remove(rows[(size_t)sel].name);
notifyChange();
// Clear form
edName.clear();
edStartTC.setText("00:00:00:00", false);
edStopTC.setText("00:00:00:00", false);
}
void clearAll()
{
if (rows.empty()) return;
auto options = juce::MessageBoxOptions()
.withIconType(juce::MessageBoxIconType::WarningIcon)
.withTitle("Clear All Presets")
.withMessage("Remove all generator presets? This cannot be undone.")
.withButton("Clear All")
.withButton("Cancel");
confirmBox = juce::AlertWindow::showScopedAsync(options,
[this](int result)
{
if (result == 1)
{
presetMap.clear();
notifyChange();
edName.clear();
edStartTC.setText("00:00:00:00", false);
edStopTC.setText("00:00:00:00", false);
}
});
}
juce::ScopedMessageBox confirmBox;
void exportPresets()
{
if (rows.empty()) return;
fileChooser = std::make_unique<juce::FileChooser>(
"Export Generator Presets",
juce::File::getSpecialLocation(juce::File::userDesktopDirectory)
.getChildFile("generator_presets.json"),
"*.json");
fileChooser->launchAsync(
juce::FileBrowserComponent::saveMode | juce::FileBrowserComponent::canSelectFiles,
[this](const juce::FileChooser& fc)
{
auto file = fc.getResult();
if (file == juce::File()) return;
auto* root = new juce::DynamicObject();
root->setProperty("version", 1);
juce::Array<juce::var> arr;
for (auto& p : rows)
arr.add(p.toVar());
root->setProperty("presets", arr);
file.replaceWithText(juce::JSON::toString(juce::var(root)));
});
}
void importPresets()
{
fileChooser = std::make_unique<juce::FileChooser>(
"Import Generator Presets",
juce::File::getSpecialLocation(juce::File::userDesktopDirectory),
"*.json");
fileChooser->launchAsync(
juce::FileBrowserComponent::openMode | juce::FileBrowserComponent::canSelectFiles,
[this](const juce::FileChooser& fc)
{
auto file = fc.getResult();
if (file == juce::File() || !file.existsAsFile()) return;
auto parsed = juce::JSON::parse(file.loadFileAsString());
auto* obj = parsed.getDynamicObject();
if (!obj || !obj->hasProperty("presets"))
{
juce::AlertWindow::showMessageBoxAsync(
juce::MessageBoxIconType::WarningIcon,
"Import Failed",
"This file is not a valid generator presets file.");
return;
}
auto* arr = obj->getProperty("presets").getArray();
if (!arr) return;
int imported = 0;
for (auto& item : *arr)
{
GeneratorPreset p;
p.fromVar(item);
if (p.hasValidKey())
{
presetMap.addOrUpdate(p);
++imported;
}
}
if (imported > 0)
notifyChange();
});
}
void showOscReference()
{
juce::String help;
help << "OSC COMMAND REFERENCE\n";
help << "==============================================\n\n";
help << "Default port: 9800 (configurable in Generator panel)\n\n";
help << "ENGINE TARGETING (N = engine number, required)\n\n";
help << "TRANSPORT\n";
help << " /stc/N/gen/play\n";
help << " /stc/N/gen/pause\n";
help << " /stc/N/gen/stop\n\n";
help << "MODE\n";
help << " /stc/N/gen/clock (int) 0=transport, 1=clock\n\n";
help << "TIMECODE\n";
help << " /stc/N/gen/start (string) \"HH:MM:SS:FF\"\n";
help << " /stc/N/gen/stoptime (string) \"HH:MM:SS:FF\"\n\n";
help << "PRESETS\n";
help << " /stc/N/gen/preset (string) preset name (loads + plays)\n\n";
help << "N = 1 to 8 (engine number)\n\n";
help << "EXAMPLES\n\n";
help << "QLab (Network Cue, type OSC):\n";
help << " Destination: <STC IP>:9800\n";
help << " /stc/1/gen/play\n";
help << " /stc/1/gen/stop\n";
help << " /stc/1/gen/start 01:30:00:00\n";
help << " /stc/1/gen/stoptime 02:00:00:00\n";
help << " /stc/1/gen/preset MyPreset\n\n";
help << "Resolume / Companion / TouchOSC:\n";
help << " Host: <STC IP>, Port: 9800\n";
help << " Same address patterns as above\n\n";
help << "TIMECODE FORMAT\n";
help << " String argument: \"HH:MM:SS:FF\"\n";
help << " 00:00:00:00 = midnight\n";
help << " 01:30:00:00 = 1 hour 30 minutes\n";
help << " 00:05:30:15 = 5 min, 30 sec, frame 15";
juce::AlertWindow::showMessageBoxAsync(
juce::MessageBoxIconType::InfoIcon,
"OSC Command Reference",
help);
}
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(GeneratorPresetEditor)
};