Skip to content

Commit 5540bb8

Browse files
committed
fixed proper manage of current nodes map for reloading patches
1 parent 32b6e9a commit 5540bb8

File tree

2 files changed

+129
-92
lines changed

2 files changed

+129
-92
lines changed

src/ofxVisualProgramming.cpp

Lines changed: 126 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ ofxVisualProgramming::ofxVisualProgramming(){
6161
lastAddedObjectID = -1;
6262
bLoadingNewObject = false;
6363
bLoadingNewPatch = false;
64+
clearingObjectsMap = false;
6465

6566
livePatchingObiID = -1;
6667

@@ -175,43 +176,16 @@ void ofxVisualProgramming::update(){
175176
}
176177

177178
// Sound Context
178-
unique_lock<std::mutex> lock(inputAudioMutex);
179+
/*unique_lock<std::mutex> lock(inputAudioMutex);
179180
{
180181
181-
}
182+
}*/
182183

183184
// Clear map from deleted objects
184-
if(ofGetElapsedTimeMillis()-resetTime > wait){
185-
resetTime = ofGetElapsedTimeMillis();
186-
eraseIndexes.clear();
187-
for(map<int,shared_ptr<PatchObject>>::iterator it = patchObjects.begin(); it != patchObjects.end(); it++ ){
188-
if(it->second->getWillErase()){
189-
eraseIndexes.push_back(it->first);
190-
191-
}
192-
}
193-
for(int x=0;x<static_cast<int>(eraseIndexes.size());x++){
194-
for(int p=0;p<static_cast<int>(patchObjects.at(eraseIndexes.at(x))->outPut.size());p++){
195-
patchObjects[patchObjects.at(eraseIndexes.at(x))->outPut.at(p)->toObjectID]->inletsConnected.at(patchObjects.at(eraseIndexes.at(x))->outPut.at(p)->toInletID) = false;
196-
}
197-
198-
// remove scripts objects filepath reference from scripts objects files map
199-
ofFile tempsofp(patchObjects.at(eraseIndexes.at(x))->getFilepath());
200-
string fileExt = ofToUpper(tempsofp.getExtension());
201-
if(fileExt == "LUA" || fileExt == "PY" || fileExt == "SH" || fileExt == "FRAG"){
202-
map<string,string>::iterator sofpIT = scriptsObjectsFilesPaths.find(tempsofp.getFileName());
203-
if (sofpIT != scriptsObjectsFilesPaths.end()){
204-
// found it, remove it
205-
scriptsObjectsFilesPaths.erase(sofpIT);
206-
}
207-
}
208-
209-
patchObjects.at(eraseIndexes.at(x))->removeObjectContent(true);
210-
patchObjects.erase(eraseIndexes.at(x));
211-
}
185+
clearObjectsMap();
212186

213-
}
214187

188+
// update patch objects
215189
if(!bLoadingNewPatch && !patchObjects.empty()){
216190
// left to right computing order
217191
leftToRightIndexOrder.clear();
@@ -255,6 +229,8 @@ void ofxVisualProgramming::updateCanvasViewport(){
255229
//--------------------------------------------------------------
256230
void ofxVisualProgramming::draw(){
257231

232+
if(bLoadingNewPatch) return;
233+
258234
// LIVE PATCHING SESSION
259235
drawLivePatchingSession();
260236

@@ -368,6 +344,7 @@ void ofxVisualProgramming::draw(){
368344
// Graphical Context
369345
canvas.update();
370346

347+
371348
}
372349

373350
//--------------------------------------------------------------
@@ -554,6 +531,8 @@ void ofxVisualProgramming::keyReleased(ofKeyEventArgs &e){
554531
//--------------------------------------------------------------
555532
void ofxVisualProgramming::audioProcess(float *input, int bufferSize, int nChannels){
556533

534+
if(bLoadingNewPatch) return;
535+
557536
if(audioSampleRate != 0 && dspON){
558537

559538
if(!bLoadingNewObject){
@@ -852,6 +831,48 @@ void ofxVisualProgramming::deleteObject(int id){
852831
}
853832
}
854833

834+
//--------------------------------------------------------------
835+
void ofxVisualProgramming::clearObjectsMap(){
836+
if(ofGetElapsedTimeMillis()-resetTime > wait){
837+
resetTime = ofGetElapsedTimeMillis();
838+
eraseIndexes.clear();
839+
for(map<int,shared_ptr<PatchObject>>::iterator it = patchObjects.begin(); it != patchObjects.end(); it++ ){
840+
if(it->second->getWillErase()){
841+
eraseIndexes.push_back(it->first);
842+
}
843+
}
844+
for(int x=0;x<static_cast<int>(eraseIndexes.size());x++){
845+
846+
if(!clearingObjectsMap){
847+
for(int p=0;p<static_cast<int>(patchObjects.at(eraseIndexes.at(x))->outPut.size());p++){
848+
patchObjects[patchObjects.at(eraseIndexes.at(x))->outPut.at(p)->toObjectID]->inletsConnected.at(patchObjects.at(eraseIndexes.at(x))->outPut.at(p)->toInletID) = false;
849+
}
850+
}
851+
852+
// remove scripts objects filepath reference from scripts objects files map
853+
ofFile tempsofp(patchObjects.at(eraseIndexes.at(x))->getFilepath());
854+
string fileExt = ofToUpper(tempsofp.getExtension());
855+
if(fileExt == "LUA" || fileExt == "PY" || fileExt == "SH" || fileExt == "FRAG"){
856+
map<string,string>::iterator sofpIT = scriptsObjectsFilesPaths.find(tempsofp.getFileName());
857+
if (sofpIT != scriptsObjectsFilesPaths.end()){
858+
// found it, remove it
859+
scriptsObjectsFilesPaths.erase(sofpIT);
860+
}
861+
}
862+
863+
patchObjects.at(eraseIndexes.at(x))->removeObjectContent(true);
864+
patchObjects.erase(eraseIndexes.at(x));
865+
std::this_thread::sleep_for(std::chrono::milliseconds(10));
866+
}
867+
868+
if(clearingObjectsMap){
869+
clearingObjectsMap = false;
870+
openPatch(currentPatchFile);
871+
}
872+
873+
}
874+
}
875+
855876
//--------------------------------------------------------------
856877
void ofxVisualProgramming::removeObject(int &id){
857878
resetTime = ofGetElapsedTimeMillis();
@@ -1066,12 +1087,10 @@ void ofxVisualProgramming::newPatch(){
10661087
ofFile fileToRead(ofToDataPath("empty_patch.xml",true));
10671088
ofFile newPatchFile(ofToDataPath("temp/"+newFileName,true));
10681089
ofFile::copyFromTo(fileToRead.getAbsolutePath(),newPatchFile.getAbsolutePath(),true,true);
1069-
newFileCounter++;
10701090

1071-
currentPatchFile = newPatchFile.getAbsolutePath();
1072-
openPatch(currentPatchFile);
1091+
newFileCounter++;
10731092

1074-
tempPatchFile = currentPatchFile;
1093+
preloadPatch(newPatchFile.getAbsolutePath());
10751094

10761095
}
10771096

@@ -1089,10 +1108,29 @@ void ofxVisualProgramming::newTempPatchFromFile(string patchFile){
10891108

10901109
newFileCounter++;
10911110

1092-
currentPatchFile = newPatchFile.getAbsolutePath();
1093-
openPatch(currentPatchFile);
1111+
preloadPatch(newPatchFile.getAbsolutePath());
10941112

1113+
}
1114+
1115+
//--------------------------------------------------------------
1116+
void ofxVisualProgramming::preloadPatch(string patchFile){
1117+
currentPatchFile = patchFile;
10951118
tempPatchFile = currentPatchFile;
1119+
// clear previous patch
1120+
for(map<int,shared_ptr<PatchObject>>::iterator it = patchObjects.begin(); it != patchObjects.end(); it++ ){
1121+
if(it->second->getName() != "audio device"){
1122+
it->second->setWillErase(true);
1123+
}else{
1124+
for(int in=0;in<it->second->getNumInlets();in++){
1125+
it->second->inletsConnected[in] = false;
1126+
it->second->pdspIn[in].disconnectIn();
1127+
}
1128+
1129+
it->second->outPut.clear();
1130+
}
1131+
}
1132+
resetTime = ofGetElapsedTimeMillis();
1133+
clearingObjectsMap = true;
10961134
}
10971135

10981136
//--------------------------------------------------------------
@@ -1109,13 +1147,6 @@ void ofxVisualProgramming::openPatch(string patchFile){
11091147
patchDataFolder.create();
11101148
}
11111149

1112-
// clear previous patch
1113-
for(map<int,shared_ptr<PatchObject>>::iterator it = patchObjects.begin(); it != patchObjects.end(); it++ ){
1114-
it->second->removeObjectContent();
1115-
}
1116-
1117-
patchObjects.clear();
1118-
11191150
// load new patch
11201151
loadPatch(currentPatchFile);
11211152

@@ -1243,67 +1274,70 @@ void ofxVisualProgramming::loadPatch(string patchFile){
12431274

12441275
int totalObjects = XML.getNumTags("object");
12451276

1246-
// Load all the patch objects
1247-
for(int i=0;i<totalObjects;i++){
1248-
if(XML.pushTag("object", i)){
1249-
string objname = XML.getValue("name","");
1250-
bool loaded = false;
1251-
1252-
shared_ptr<PatchObject> tempObj = selectObject(objname);
1253-
if(tempObj != nullptr){
1254-
loaded = tempObj->loadConfig(mainWindow,*engine,i,patchFile);
1255-
if(loaded){
1256-
tempObj->setPatchfile(currentPatchFile);
1257-
tempObj->setIsRetina(isRetina);
1258-
ofAddListener(tempObj->removeEvent ,this,&ofxVisualProgramming::removeObject);
1259-
ofAddListener(tempObj->resetEvent ,this,&ofxVisualProgramming::resetObject);
1260-
ofAddListener(tempObj->reconnectOutletsEvent ,this,&ofxVisualProgramming::reconnectObjectOutlets);
1261-
ofAddListener(tempObj->duplicateEvent ,this,&ofxVisualProgramming::duplicateObject);
1262-
// Insert the new patch into the map
1263-
patchObjects[tempObj->getId()] = tempObj;
1264-
actualObjectID = tempObj->getId();
1265-
lastAddedObjectID = tempObj->getId();
1266-
1267-
std::this_thread::sleep_for(std::chrono::milliseconds(10));
1277+
if(totalObjects > 0){
1278+
// Load all the patch objects
1279+
for(int i=0;i<totalObjects;i++){
1280+
if(XML.pushTag("object", i)){
1281+
string objname = XML.getValue("name","");
1282+
bool loaded = false;
1283+
1284+
shared_ptr<PatchObject> tempObj = selectObject(objname);
1285+
if(tempObj != nullptr){
1286+
ofLog(OF_LOG_NOTICE,"BEFORE LOADING OBJECT CONFIG....");
1287+
loaded = tempObj->loadConfig(mainWindow,*engine,i,patchFile);
1288+
ofLog(OF_LOG_NOTICE,"AFTER LOADING OBJECT CONFIG....");
1289+
if(loaded){
1290+
tempObj->setPatchfile(currentPatchFile);
1291+
tempObj->setIsRetina(isRetina);
1292+
ofAddListener(tempObj->removeEvent ,this,&ofxVisualProgramming::removeObject);
1293+
ofAddListener(tempObj->resetEvent ,this,&ofxVisualProgramming::resetObject);
1294+
ofAddListener(tempObj->reconnectOutletsEvent ,this,&ofxVisualProgramming::reconnectObjectOutlets);
1295+
ofAddListener(tempObj->duplicateEvent ,this,&ofxVisualProgramming::duplicateObject);
1296+
// Insert the new patch into the map
1297+
patchObjects[tempObj->getId()] = tempObj;
1298+
actualObjectID = tempObj->getId();
1299+
lastAddedObjectID = tempObj->getId();
1300+
1301+
std::this_thread::sleep_for(std::chrono::milliseconds(10));
1302+
}
12681303
}
1304+
XML.popTag();
12691305
}
1270-
XML.popTag();
12711306
}
1272-
}
12731307

1274-
// Load Links
1275-
for(int i=0;i<totalObjects;i++){
1276-
if(XML.pushTag("object", i)){
1277-
int fromID = XML.getValue("id", -1);
1278-
if (XML.pushTag("outlets")){
1279-
int totalOutlets = XML.getNumTags("link");
1280-
for(int j=0;j<totalOutlets;j++){
1281-
if (XML.pushTag("link",j)){
1282-
int linkType = XML.getValue("type", 0);
1283-
int totalLinks = XML.getNumTags("to");
1284-
for(int z=0;z<totalLinks;z++){
1285-
if(XML.pushTag("to",z)){
1286-
int toObjectID = XML.getValue("id", 0);
1287-
int toInletID = XML.getValue("inlet", 0);
1288-
1289-
if(connect(fromID,j,toObjectID,toInletID,linkType)){
1290-
//ofLog(OF_LOG_NOTICE,"Connected object %s, outlet %i TO object %s, inlet %i",patchObjects[fromID]->getName().c_str(),z,patchObjects[toObjectID]->getName().c_str(),toInletID);
1291-
std::this_thread::sleep_for(std::chrono::milliseconds(10));
1292-
}
1308+
// Load Links
1309+
for(int i=0;i<totalObjects;i++){
1310+
if(XML.pushTag("object", i)){
1311+
int fromID = XML.getValue("id", -1);
1312+
if (XML.pushTag("outlets")){
1313+
int totalOutlets = XML.getNumTags("link");
1314+
for(int j=0;j<totalOutlets;j++){
1315+
if (XML.pushTag("link",j)){
1316+
int linkType = XML.getValue("type", 0);
1317+
int totalLinks = XML.getNumTags("to");
1318+
for(int z=0;z<totalLinks;z++){
1319+
if(XML.pushTag("to",z)){
1320+
int toObjectID = XML.getValue("id", 0);
1321+
int toInletID = XML.getValue("inlet", 0);
1322+
1323+
if(connect(fromID,j,toObjectID,toInletID,linkType)){
1324+
//ofLog(OF_LOG_NOTICE,"Connected object %s, outlet %i TO object %s, inlet %i",patchObjects[fromID]->getName().c_str(),z,patchObjects[toObjectID]->getName().c_str(),toInletID);
1325+
std::this_thread::sleep_for(std::chrono::milliseconds(10));
1326+
}
12931327

1294-
XML.popTag();
1328+
XML.popTag();
1329+
}
12951330
}
1331+
XML.popTag();
12961332
}
1297-
XML.popTag();
12981333
}
1299-
}
13001334

1335+
XML.popTag();
1336+
}
13011337
XML.popTag();
13021338
}
1303-
XML.popTag();
13041339
}
13051340
}
1306-
13071341
}
13081342

13091343
bLoadingNewPatch = false;

src/ofxVisualProgramming.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,11 @@ class ofxVisualProgramming : public pdsp::Wrapper {
9595
void resetSpecificSystemObjects(string name);
9696
bool weAlreadyHaveObject(string name);
9797
void deleteObject(int id);
98+
void clearObjectsMap();
9899

99100
void newPatch();
100101
void newTempPatchFromFile(string patchFile);
102+
void preloadPatch(string patchFile);
101103
void openPatch(string patchFile);
102104
void loadPatch(string patchFile);
103105
void reloadPatch();
@@ -142,6 +144,7 @@ class ofxVisualProgramming : public pdsp::Wrapper {
142144
int lastAddedObjectID;
143145
bool bLoadingNewObject;
144146
bool bLoadingNewPatch;
147+
bool clearingObjectsMap;
145148

146149
// LOAD/SAVE
147150
string currentPatchFile;

0 commit comments

Comments
 (0)