forked from plugdata-team/plugdata
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubpatchObject.h
More file actions
168 lines (141 loc) · 5.64 KB
/
SubpatchObject.h
File metadata and controls
168 lines (141 loc) · 5.64 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
/*
// Copyright (c) 2021-2025 Timothy Schoen
// For information on usage and redistribution, and for a DISCLAIMER OF ALL
// WARRANTIES, see the file, "LICENSE.txt," in this distribution.
*/
#pragma once
class SubpatchObject final : public TextBase
{
pd::Patch::Ptr subpatch;
Value isGraphChild = SynchronousValue(var(false));
public:
SubpatchObject(pd::WeakReference obj, Object* object)
: TextBase(obj, object)
, subpatch(new pd::Patch(obj, cnv->pd, false))
{
objectParameters.addParamBool("Is graph", cGeneral, &isGraphChild, { "No", "Yes" });
// There is a possibility that a donecanvasdialog message is sent inbetween the initialisation in pd and the initialisation of the plugdata object, making it possible to miss this message. This especially tends to happen if the messagebox is connected to a loadbang.
// By running another update call asynchrounously, we can still respond to the new state
MessageManager::callAsync([_this = SafePointer(this)] {
if (_this) {
_this->update();
_this->propertyChanged(_this->isGraphChild);
}
});
setRepaintsOnMouseActivity(true);
}
~SubpatchObject() override
{
if(!getValue<bool>(isGraphChild)) {
closeOpenedSubpatchers();
}
}
void render(NVGcontext* nvg) override
{
TextBase::render(nvg);
}
void update() override
{
// Change from subpatch to graph
if (auto canvas = ptr.get<t_canvas>()) {
isGraphChild = static_cast<bool>(canvas->gl_isgraph);
} else {
return;
}
}
void mouseDown(MouseEvent const& e) override
{
if (!e.mods.isLeftButtonDown())
return;
if (isLocked && click(e.getPosition(), e.mods.isShiftDown(), e.mods.isAltDown())) {
return;
}
// If locked and it's a left click
if (isLocked && !e.mods.isRightButtonDown()) {
openSubpatch();
return;
}
TextBase::mouseDown(e);
}
pd::Patch::Ptr getPatch() override
{
return subpatch;
}
void propertyChanged(Value& v) override
{
if (v.refersToSameSourceAs(sizeProperty)) {
// forward the value change to the text object
TextBase::propertyChanged(v);
} else if (v.refersToSameSourceAs(isGraphChild)) {
int const isGraph = getValue<bool>(isGraphChild);
if (auto glist = ptr.get<t_glist>()) {
canvas_setgraph(glist.get(), isGraph + 2 * glist->gl_hidetext, 0);
}
if (isGraph) {
MessageManager::callAsync([this, _this = SafePointer(this)] {
if (!_this)
return;
_this->cnv->setSelected(object, false);
_this->object->editor->sidebar->hideParameters();
_this->object->setType(_this->getText(), ptr);
});
}
}
}
void receiveObjectMessage(hash32 const symbol, SmallArray<pd::Atom> const& atoms) override
{
switch (symbol) {
case hash("donecanvasdialog"):
case hash("coords"): {
update();
break;
}
default:
break;
}
}
void getMenuOptions(PopupMenu& menu) override
{
menu.addItem("Open", [_this = SafePointer(this)] { if(_this) _this->openSubpatch(); });
}
bool showParametersWhenSelected() override
{
return cnv->isGraph;
}
bool checkHvccCompatibility() override
{
return recurseHvccCompatibility(objectText, subpatch.get());
}
static bool recurseHvccCompatibility(String const& objectText, pd::Patch::Ptr patch, String const& prefix = "")
{
auto instance = patch->instance;
if (objectText.startsWith("pd @hv_obj") || HeavyCompatibleObjects::isCompatible(objectText)) {
return true;
}
bool compatible = true;
for (auto object : patch->getObjects()) {
if (auto ptr = object.get<t_pd>()) {
String const type = pd::Interface::getObjectClassName(ptr.get());
if (type == "canvas" || type == "graph") {
pd::Patch::Ptr const subpatch = new pd::Patch(object, instance, false);
if(subpatch->isSubpatch()) {
char* text = nullptr;
int size = 0;
pd::Interface::getObjectText(&ptr.cast<t_canvas>()->gl_obj, &text, &size);
auto objName = String::fromUTF8(text, size);
compatible = recurseHvccCompatibility(objName, subpatch, prefix + objName + " -> ") && compatible;
freebytes(text, static_cast<size_t>(size) * sizeof(char));
}
else if(!HeavyCompatibleObjects::isCompatible(type)) {
compatible = false;
instance->logWarning(String("Warning: object \"" + prefix + type + "\" is not supported in Compiled Mode"));
}
} else if (!HeavyCompatibleObjects::isCompatible(type)) {
compatible = false;
instance->logWarning(String("Warning: object \"" + prefix + type + "\" is not supported in Compiled Mode"));
}
}
}
return compatible;
}
};