forked from plugdata-team/plugdata
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectReferenceDialog.h
More file actions
456 lines (341 loc) · 17.3 KB
/
ObjectReferenceDialog.h
File metadata and controls
456 lines (341 loc) · 17.3 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
#pragma once
/*
// Copyright (c) 2022 Timothy Schoen
// For information on usage and redistribution, and for a DISCLAIMER OF ALL
// WARRANTIES, see the file, "LICENSE.txt," in this distribution.
*/
#include <juce_gui_basics/juce_gui_basics.h>
#include "LookAndFeel.h"
#include "PluginEditor.h"
#include "PluginProcessor.h"
class ObjectInfoPanel final : public Component {
struct CategoryPanel final : public Component {
CategoryPanel(String const& name, SmallArray<std::pair<String, String>> const& content)
: categoryName(name)
, panelContent(content)
{
}
void paint(Graphics& g) override
{
g.setColour(findColour(PlugDataColour::outlineColourId));
g.drawLine(0, 1, getWidth(), 0);
Fonts::drawStyledText(g, categoryName, getLocalBounds().toFloat().removeFromTop(24).translated(2, 0), findColour(PlugDataColour::panelTextColourId), FontStyle::Bold, 14.0f);
float totalHeight = 24;
for (int i = 0; i < panelContent.size(); i++) {
auto const textHeight = layouts[i].getHeight();
auto bounds = Rectangle<float>(36.0f, totalHeight + 6.0f, getWidth() - 48.0f, textHeight);
auto const nameWidth = std::max(Fonts::getSemiBoldFont().getStringWidth(panelContent[i].first), 64);
Fonts::drawStyledText(g, panelContent[i].first, bounds.removeFromLeft(nameWidth), findColour(PlugDataColour::panelTextColourId), FontStyle::Semibold, 13.5f);
layouts[i].draw(g, bounds);
g.setColour(findColour(PlugDataColour::outlineColourId));
g.drawLine(36.0f, totalHeight, getWidth() - 24.0f, totalHeight);
totalHeight += textHeight + 12;
}
}
void recalculateLayout(int const width)
{
layouts.clear();
int totalHeight = 24;
for (auto const& [name, description] : panelContent) {
auto const nameWidth = std::max(Fonts::getSemiBoldFont().getStringWidth(name), 64);
AttributedString str;
auto lines = StringArray::fromLines(description);
// Draw anything between () as bold
for (auto const& line : lines) {
if (line.contains("(") && line.contains(")")) {
auto const type = line.fromFirstOccurrenceOf("(", false, false).upToFirstOccurrenceOf(")", false, false);
auto const description = line.fromFirstOccurrenceOf(")", false, false);
str.append(type + ":", Fonts::getSemiBoldFont().withHeight(13.5f), findColour(PlugDataColour::panelTextColourId));
str.append(description + "\n", Font(13.5f), findColour(PlugDataColour::panelTextColourId));
} else {
str.append(line, Font(13.5f), findColour(PlugDataColour::panelTextColourId));
}
}
TextLayout layout;
layout.createLayout(str, width - (nameWidth + 64.0f));
layouts.add(layout);
totalHeight += layout.getHeight() + 12;
}
setSize(width, totalHeight);
}
String const categoryName;
SmallArray<std::pair<String, String>> const panelContent;
SmallArray<TextLayout> layouts;
};
public:
ObjectInfoPanel()
{
categoriesViewport.setViewedComponent(&categoriesHolder, false);
addAndMakeVisible(categoriesViewport);
categoriesViewport.setScrollBarsShown(true, false, false, false);
categoriesHolder.setVisible(true);
}
void showObject(ValueTree const& objectInfo)
{
categories.clear();
SmallArray<std::pair<String, String>> inletInfo;
SmallArray<std::pair<String, String>> outletInfo;
auto ioletDescriptions = objectInfo.getChildWithName("iolets");
for (auto iolet : ioletDescriptions) {
if (iolet.getType() == Identifier("inlet")) {
auto tooltip = iolet.getProperty("tooltip").toString();
inletInfo.add({ String(inletInfo.size() + 1), tooltip });
} else {
auto tooltip = iolet.getProperty("tooltip").toString();
outletInfo.add({ String(outletInfo.size() + 1), tooltip });
}
}
if (inletInfo.size()) {
categories.add(new CategoryPanel("Inlets", inletInfo));
}
if (outletInfo.size()) {
categories.add(new CategoryPanel("Outlets", outletInfo));
}
SmallArray<std::pair<String, String>> argsInfo;
auto arguments = objectInfo.getChildWithName("arguments");
for (auto argument : arguments) {
auto type = argument.getProperty("type").toString();
auto desc = argument.getProperty("description").toString();
auto def = argument.getProperty("default").toString();
String argumentDescription;
argumentDescription += type.isNotEmpty() ? "(" + type + ") " : "";
argumentDescription += desc;
argumentDescription += def.isNotEmpty() && !desc.contains("(default") ? " (default: " + def + ")" : "";
argsInfo.add({ String(argsInfo.size() + 1), argumentDescription });
}
if (argsInfo.size()) {
categories.add(new CategoryPanel("Arguments", argsInfo));
}
SmallArray<std::pair<String, String>> methodsInfo;
auto methods = objectInfo.getChildWithName("methods");
for (auto method : methods) {
auto type = method.getProperty("type").toString();
auto description = method.getProperty("description").toString();
methodsInfo.add({ type, description });
}
if (methodsInfo.size()) {
categories.add(new CategoryPanel("Methods", methodsInfo));
}
SmallArray<std::pair<String, String>> flagsInfo;
auto flags = objectInfo.getChildWithName("flags");
for (auto flag : flags) {
auto name = flag.getProperty("name").toString().trim();
auto description = flag.getProperty("description").toString();
auto def = flag.getProperty("default").toString();
if (def.isNotEmpty())
description += " (default: " + def + ")";
if (!name.startsWith("-"))
name = "- " + name;
flagsInfo.add({ name, description });
}
if (flagsInfo.size()) {
categories.add(new CategoryPanel("Flags", flagsInfo));
}
for (auto* category : categories) {
categoriesHolder.addAndMakeVisible(category);
}
}
void resized() override
{
categoriesViewport.setBounds(getLocalBounds());
int totalHeight = 24;
for (auto* category : categories) {
category->recalculateLayout(getWidth());
category->setTopLeftPosition(0, totalHeight);
totalHeight += category->getHeight();
}
categoriesHolder.setSize(getWidth(), totalHeight);
}
BouncingViewport categoriesViewport;
Component categoriesHolder;
OwnedArray<CategoryPanel> categories;
};
class ObjectReferenceDialog final : public Component {
public:
ObjectReferenceDialog(PluginEditor* editor, bool const showBackButton)
: library(*editor->pd->objectLibrary)
{
// We only need to respond to explicit repaints anyway!
setBufferedToImage(true);
if (showBackButton) {
addAndMakeVisible(backButton);
}
backButton.onClick = [this] {
setVisible(false);
};
addAndMakeVisible(objectInfoPanel);
}
void resized() override
{
backButton.setBounds(2, 0, 40, 40);
auto const rightPanelBounds = getLocalBounds().withTrimmedTop(40).removeFromRight(getLocalBounds().proportionOfWidth(0.65f)).reduced(20, 0);
objectInfoPanel.setBounds(rightPanelBounds);
}
void paint(Graphics& g) override
{
g.setColour(findColour(PlugDataColour::panelBackgroundColourId));
g.fillRoundedRectangle(getLocalBounds().reduced(1).toFloat(), Corners::windowCornerRadius);
g.setColour(findColour(PlugDataColour::panelBackgroundColourId));
g.fillRoundedRectangle(getLocalBounds().reduced(1).toFloat(), Corners::windowCornerRadius);
auto const titlebarBounds = getLocalBounds().removeFromTop(40).toFloat();
Path p;
p.addRoundedRectangle(titlebarBounds.getX(), titlebarBounds.getY(), titlebarBounds.getWidth(), titlebarBounds.getHeight(), Corners::windowCornerRadius, Corners::windowCornerRadius, true, true, false, false);
g.setColour(findColour(PlugDataColour::toolbarBackgroundColourId));
g.fillPath(p);
g.setColour(findColour(PlugDataColour::toolbarOutlineColourId));
g.drawHorizontalLine(40, 0.0f, getWidth());
if (objectName.isEmpty())
return;
auto leftPanelBounds = getLocalBounds().withTrimmedRight(getLocalBounds().proportionOfWidth(0.65f));
g.drawVerticalLine(leftPanelBounds.getRight(), 40.0f, getHeight() - 40.0f);
auto infoBounds = leftPanelBounds.withTrimmedBottom(100).withTrimmedTop(140).withTrimmedLeft(5).reduced(10);
auto const objectDisplayBounds = leftPanelBounds.removeFromTop(140);
Fonts::drawStyledText(g, "Object Reference: " + objectName, getLocalBounds().removeFromTop(35).translated(0, 4), findColour(PlugDataColour::panelTextColourId), Bold, 16, Justification::centred);
auto const colour = findColour(PlugDataColour::panelTextColourId);
auto numInlets = unknownInletLayout ? "Unknown" : String(inlets.size());
auto numOutlets = unknownOutletLayout ? "Unknown" : String(outlets.size());
StringArray const infoNames = { "Categories:", "Origin:", "Type:", "Num. Inlets:", "Num. Outlets:" };
StringArray const infoText = { categories, origin, objectName.contains("~") ? String("Signal") : String("Data"), numInlets, numOutlets };
for (int i = 0; i < infoNames.size(); i++) {
auto localBounds = infoBounds.removeFromTop(25);
Fonts::drawText(g, infoNames[i], localBounds.removeFromLeft(90), colour, 15, Justification::topLeft);
Fonts::drawText(g, infoText[i], localBounds, colour, 15, Justification::topLeft);
}
auto descriptionBounds = infoBounds.removeFromTop(25);
Fonts::drawText(g, "Description: ", descriptionBounds.removeFromLeft(90), colour, 15, Justification::topLeft);
Fonts::drawFittedText(g, description, descriptionBounds.withHeight(180), colour, 10, 0.9f, 15, Justification::topLeft);
if (!unknownInletLayout && !unknownOutletLayout) {
drawObject(g, objectDisplayBounds);
} else {
auto const questionMarkBounds = objectDisplayBounds.withSizeKeepingCentre(48, 48);
g.drawRoundedRectangle(questionMarkBounds.toFloat(), 6.0f, 3.0f);
Fonts::drawText(g, "?", questionMarkBounds, colour, 40, Justification::centred);
}
}
void drawObject(Graphics& g, Rectangle<int> objectRect)
{
constexpr int ioletSize = 8;
int const ioletWidth = (ioletSize + 4) * std::max<int>(inlets.size(), outlets.size());
int const textWidth = Font(15).getStringWidth(objectName);
int const width = std::max(ioletWidth, textWidth) + 14;
auto const outlineBounds = objectRect.withSizeKeepingCentre(width, 22).toFloat();
g.setColour(findColour(PlugDataColour::objectOutlineColourId));
g.drawRoundedRectangle(outlineBounds, Corners::objectCornerRadius, 1.0f);
auto const textBounds = outlineBounds.reduced(2.0f);
Fonts::drawText(g, objectName, textBounds.toNearestInt(), findColour(PlugDataColour::panelTextColourId), 15, Justification::centred);
auto const themeTree = SettingsFile::getInstance()->getCurrentTheme();
auto squareIolets = static_cast<bool>(themeTree.getProperty("square_iolets"));
auto drawIolet = [this, squareIolets](Graphics& g, Rectangle<float> bounds, bool const type) mutable {
g.setColour(type ? findColour(PlugDataColour::signalColourId) : findColour(PlugDataColour::dataColourId));
if (squareIolets) {
g.fillRect(bounds);
g.setColour(findColour(PlugDataColour::objectOutlineColourId));
g.drawRect(bounds, 1.0f);
} else {
g.fillEllipse(bounds);
g.setColour(findColour(PlugDataColour::objectOutlineColourId));
g.drawEllipse(bounds, 1.0f);
}
};
auto const ioletBounds = outlineBounds.reduced(8, 0);
for (int i = 0; i < inlets.size(); i++) {
auto inletBounds = Rectangle<int>();
int const total = inlets.size();
float const yPosition = ioletBounds.getY() + 1 - ioletSize / 2.0f;
if (total == 1 && i == 0) {
int const xPosition = getWidth() < 40 ? ioletBounds.getCentreX() - ioletSize / 2.0f : ioletBounds.getX();
inletBounds = Rectangle<int>(xPosition, yPosition, ioletSize, ioletSize);
} else if (total > 1) {
float const ratio = (ioletBounds.getWidth() - ioletSize) / static_cast<float>(total - 1);
inletBounds = Rectangle<int>(ioletBounds.getX() + ratio * i, yPosition, ioletSize, ioletSize);
}
drawIolet(g, inletBounds.toFloat(), inlets[i]);
}
for (int i = 0; i < outlets.size(); i++) {
auto outletBounds = Rectangle<int>();
int const total = outlets.size();
float const yPosition = ioletBounds.getBottom() - ioletSize / 2.0f;
if (total == 1 && i == 0) {
int const xPosition = getWidth() < 40 ? ioletBounds.getCentreX() - ioletSize / 2.0f : ioletBounds.getX();
outletBounds = Rectangle<int>(xPosition, yPosition, ioletSize, ioletSize);
} else if (total > 1) {
float const ratio = (ioletBounds.getWidth() - ioletSize) / static_cast<float>(total - 1);
outletBounds = Rectangle<int>(ioletBounds.getX() + ratio * i, yPosition, ioletSize, ioletSize);
}
drawIolet(g, outletBounds.toFloat(), outlets[i]);
}
}
void showObject(String const& name)
{
inlets.clear();
outlets.clear();
bool const valid = name.isNotEmpty();
if (!valid) {
objectName = "";
unknownInletLayout = false;
unknownOutletLayout = false;
repaint();
return;
}
bool hasUnknownInletLayout = false;
bool hasUnknownOutletLayout = false;
auto const objectInfo = library.getObjectInfo(name);
if (!objectInfo.isValid())
return;
auto const ioletDescriptions = objectInfo.getChildWithName("iolets");
for (auto iolet : ioletDescriptions) {
auto const variable = iolet.getProperty("variable").toString() == "1";
if (iolet.getType() == Identifier("inlet")) {
if (variable)
hasUnknownInletLayout = true;
auto tooltip = iolet.getProperty("tooltip").toString();
inlets.add(tooltip.contains("(signal)"));
} else {
if (variable)
hasUnknownOutletLayout = true;
auto tooltip = iolet.getProperty("tooltip").toString();
outlets.add(tooltip.contains("(signal)"));
}
}
unknownInletLayout = hasUnknownInletLayout;
unknownOutletLayout = hasUnknownOutletLayout;
objectName = name;
categories = "";
origin = "";
auto const categoriesTree = objectInfo.getChildWithName("categories");
for (auto category : categoriesTree) {
auto cat = category.getProperty("name").toString();
if (pd::Library::objectOrigins.contains(cat)) {
origin = cat;
} else {
categories += cat + ", ";
}
}
if (categories.isEmpty()) {
categories = "Unknown";
} else {
categories = categories.dropLastCharacters(2);
}
if (origin.isEmpty()) {
origin = "Unknown";
}
description = objectInfo.getProperty("description");
if (description.isEmpty()) {
description = "No description available";
}
setVisible(true);
objectInfoPanel.showObject(objectInfo);
objectInfoPanel.resized();
}
bool unknownInletLayout = false;
bool unknownOutletLayout = false;
String objectName;
SmallArray<bool> inlets;
SmallArray<bool> outlets;
ObjectInfoPanel objectInfoPanel;
MainToolbarButton backButton = MainToolbarButton(Icons::Back);
String categories;
String origin;
String description;
pd::Library& library;
};