-
Notifications
You must be signed in to change notification settings - Fork 16
/
Base.cpp
executable file
·291 lines (253 loc) · 8.29 KB
/
Base.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
287
288
289
290
291
#include "pch_RulrCore.h"
#include "Base.h"
#include "ofxRulr/Graph/Editor/NodeHost.h"
#include "ofxRulr/Graph/World.h"
#include "../Exception.h"
#include "GraphicsManager.h"
using namespace ofxCvGui;
namespace ofxRulr {
namespace Nodes {
//----------
Base::Base() {
this->initialized = false;
this->lastFrameUpdate = 0;
this->updateAllInputsFirst = true;
this->whenDrawOnWorldStage = WhenDrawOnWorldStage::Always;
}
//----------
Base::~Base() {
if (this->initialized) {
//pins will try to notify this node when connections are dropped, so drop the pins first
for (auto pin : this->inputPins) {
pin->resetConnection();
}
this->onDestroy.notifyListenersInReverse();
this->initialized = false;
}
}
//----------
string Base::getTypeName() const {
return "Node";
}
//----------
void Base::init() {
this->onPopulateInspector.addListener([this](ofxCvGui::InspectArguments & args) {
this->populateInspector(args);
}, this, 99999); // populate the inspector with this at the top. We call notify in reverse for inheritance
this->onSerialize.addListener([this](Json::Value & json) {
json["whenDrawOnWorldStage"] = (int) this->whenDrawOnWorldStage;
}, this);
this->onDeserialize.addListener([this](const Json::Value & json) {
if (json.isMember("whenDrawOnWorldStage")) {
this->whenDrawOnWorldStage = (WhenDrawOnWorldStage::Options) json["whenDrawOnWorldStage"].asInt();
}
}, this);
//notify the subclasses to init
this->onInit.notifyListeners();
this->initialized = true;
}
//----------
void Base::update() {
auto currentFrameIndex = ofGetFrameNum() + 1; // otherwise confusions at 0th frame
if (currentFrameIndex > this->lastFrameUpdate) {
this->lastFrameUpdate = currentFrameIndex;
if (this->updateAllInputsFirst) {
for (auto inputPin : this->inputPins) {
auto inputNode = inputPin->getConnectionUntyped();
if (inputNode) {
inputNode->update();
}
}
}
this->onUpdate.notifyListeners();
}
//check for loopback connections
for (auto pin : this->inputPins) {
if (pin->isConnected() && !pin->getLoopbackEnabled()) {
//check if it's a loopback
const auto connection = pin->getConnectionUntyped();
if (connection == this->shared_from_this()) {
//it's a loopback, reset it
pin->resetConnection();
}
}
}
}
//----------
string Base::getName() const {
if (this->name.empty()) {
return this->getTypeName();
} else {
return this->name;
}
}
//----------
void Base::setName(const string name) {
this->name = name;
}
//----------
void Base::setNodeHost(Graph::Editor::NodeHost * nodeHost) {
this->nodeHost = nodeHost;
}
//----------
Graph::Editor::NodeHost * Base::getNodeHost() const {
return this->nodeHost;
}
//----------
const ofColor & Base::getColor() {
if (!this->color) {
this->color = make_shared<ofColor>(GraphicsManager::X().getColor(this->getTypeName()));
}
return * this->color;
}
//----------
void Base::setColor(const ofColor & color) {
this->color = make_shared<ofColor>(color);
}
//----------
const ofBaseDraws & Base::getIcon() {
if (this->customIcon) {
return * this->customIcon;
} else if (this->standardIcon) {
return this->standardIcon->get();
}
else {
this->standardIcon = GraphicsManager::X().getIcon(this->getTypeName());
return this->standardIcon->get();
}
}
//----------
void Base::setIcon(shared_ptr<ofBaseDraws> customIcon) {
this->customIcon = customIcon;
}
//----------
void Base::setIcon(shared_ptr<ofxAssets::Image> standardIcon) {
this->standardIcon = standardIcon;
}
//----------
const Graph::PinSet & Base::getInputPins() const {
return this->inputPins;
}
//----------
void Base::populateInspector(ofxCvGui::InspectArguments &inspectArguments) {
auto inspector = inspectArguments.inspector;
auto nameWidget = inspector->add(new Widgets::Title(this->getName(), ofxCvGui::Widgets::Title::Level::H1));
auto nameWidgetWeak = weak_ptr<Element>(nameWidget);
nameWidget->onDraw += [this](ofxCvGui::DrawArguments & args) {
ofxAssets::image("ofxCvGui::edit").draw(ofRectangle(args.localBounds.width - 20, 5, 15, 15));
};
nameWidget->onMouseReleased += [this, nameWidgetWeak](ofxCvGui::MouseArguments & args) {
auto nameWidget = nameWidgetWeak.lock();
if (nameWidget) {
auto result = ofSystemTextBoxDialog("Change name of [" + this->getTypeName() + "] node (" + this->getName() + ")");
if (result != "") {
this->setName(result);
nameWidget->setCaption(result);
}
}
};
inspector->addToggle("Lock selection"
, []() {
return ofxCvGui::InspectController::X().getInspectorLocked();
}, [](bool inspectorLocked) {
ofxCvGui::InspectController::X().setInspectorLocked(inspectorLocked);
});
inspector->add(new Widgets::Title("Type : " + this->getTypeName(), ofxCvGui::Widgets::Title::Level::H3));
{
auto widget = inspector->addMultipleChoice("Draw on World Stage", { "Always", "Selected", "Never" });
widget->setSelection(this->whenDrawOnWorldStage);
widget->onValueChange += [this](int value) {
this->whenDrawOnWorldStage= (WhenDrawOnWorldStage::Options) value;
};
}
//pin status
for (auto inputPin : this->getInputPins()) {
inspector->add(new Widgets::Indicator(inputPin->getName(), [inputPin]() {
return (Widgets::Indicator::Status) inputPin->isConnected();
}));
}
//node parameters
inspector->add(new Widgets::Spacer());
}
//----------
void Base::drawWorldStage() {
this->onDrawWorldStage.notifyListeners();
}
//----------
void Base::drawWorldAdvanced(DrawWorldAdvancedArgs & args) {
if (this->onDrawWorldAdvanced.empty()) {
if (args.useStandardDrawWhereCustomIsNotAvailable) {
this->drawWorldStage();
}
}
else {
this->onDrawWorldAdvanced.notifyListeners(args);
}
}
//----------
WhenDrawOnWorldStage::Options Base::getWhenDrawOnWorldStage() const {
return this->whenDrawOnWorldStage;
}
//----------
void Base::throwIfMissingAnyConnection() const {
const auto inputPins = this->getInputPins();
for(auto & inputPin : inputPins) {
if (!inputPin->isConnected()) {
stringstream message;
message << "Node [" << this->getTypeName() << "] is missing connection [" << inputPin->getName() << "]";
throw(Exception(message.str()));
}
}
}
//----------
void Base::manageParameters(ofParameterGroup & parameters, bool addToInspector) {
this->onSerialize += [¶meters](Json::Value & json) {
Utils::Serializable::serialize(json, parameters);
};
this->onDeserialize += [¶meters](const Json::Value & json) {
Utils::Serializable::deserialize(json, parameters);
};
if (addToInspector) {
this->onPopulateInspector += [¶meters](ofxCvGui::InspectArguments & args) {
args.inspector->addParameterGroup(parameters);
};
}
}
//----------
void Base::addInput(shared_ptr<Graph::AbstractPin> pin) {
//setup events to fire on this node for this pin
auto pinWeak = weak_ptr<Graph::AbstractPin>(pin);
pin->onNewConnectionUntyped += [this, pinWeak](shared_ptr<Base> &) {
auto pin = pinWeak.lock();
if (pin) {
this->onConnect(pin);
}
this->onAnyInputConnectionChanged.notifyListeners();
};
pin->onDeleteConnectionUntyped += [this, pinWeak](shared_ptr<Base> &) {
auto pin = pinWeak.lock();
if (pin) {
this->onDisconnect(pin);
}
this->onAnyInputConnectionChanged.notifyListeners();
};
this->inputPins.add(pin);
}
//----------
void Base::removeInput(shared_ptr<Graph::AbstractPin> pin) {
this->inputPins.remove(pin);
}
//----------
void Base::clearInputs() {
this->inputPins.clear();
}
//----------
void Base::setUpdateAllInputsFirst(bool updateAllInputsFirst) {
this->updateAllInputsFirst = updateAllInputsFirst;
}
//----------
bool Base::getUpdateAllInputsFirst() const {
return this->updateAllInputsFirst;
}
}
}