@@ -9,10 +9,6 @@ void cyrilApp::setup(){
9
9
10
10
// Switch back to external data folder
11
11
ofSetDataPathRoot (" ../../../data/" );
12
- // Directory watcher for data folder
13
- codeWatcher.registerAllEvents (this );
14
- std::string folderToWatch = ofToDataPath (" code" , true );
15
- codeWatcher.addPath (folderToWatch, true , &fileFilter);
16
12
17
13
ofBackground (0 );
18
14
pauseProg = false ;
@@ -75,7 +71,15 @@ void cyrilApp::setup(){
75
71
_state.parent = NULL ;
76
72
_state.light = new ofLight ();
77
73
// _state.light = NULL;
78
-
74
+
75
+
76
+ // Directory watcher for data folder
77
+ codeWatcher.registerAllEvents (this );
78
+ spriteWatcher.registerAllEvents (this );
79
+ codeWatcher.addPath (ofToDataPath (" code" , true ), true , &fileFilter);
80
+ spriteWatcher.addPath (ofToDataPath (" sprites" , true ), true , &fileFilter);
81
+
82
+
79
83
(*_state.sym )[REG_X_MAX] = 640 ;
80
84
(*_state.sym )[REG_Y_MAX] = 480 ;
81
85
(*_state.sym )[REG_X_MID] = (*_state.sym )[REG_X_MAX] / 2.0 ;
@@ -274,13 +278,13 @@ void cyrilApp::toggleLights(void * _o) {
274
278
((cyrilApp *)_o)->_state .light ->setAttenuation (1 .f ,0 .f ,0 .f );
275
279
}
276
280
void cyrilApp::loadFile (void * _o) {
277
- // int whichEditor = ((cyrilApp *)_o)->editor.currentBuffer;
278
- // ((cyrilApp *)_o)->editor.loadFile(ofToString(whichEditor)+".txt ", whichEditor);
279
- // ((cyrilApp *)_o)->editor.update();
281
+ int whichEditor = ((cyrilApp *)_o)->editor .currentBuffer ;
282
+ ((cyrilApp *)_o)->editor .loadFile (" code/ " + ofToString (whichEditor)+" .cy " , whichEditor);
283
+ ((cyrilApp *)_o)->editor .update ();
280
284
}
281
285
void cyrilApp::saveFile (void * _o) {
282
- // int whichEditor = ((cyrilApp *)_o)->editor.currentBuffer;
283
- // ((cyrilApp *)_o)->editor.saveFile(ofToString(whichEditor)+".txt ", whichEditor);
286
+ int whichEditor = ((cyrilApp *)_o)->editor .currentBuffer ;
287
+ ((cyrilApp *)_o)->editor .saveFile (" code/ " + ofToString (whichEditor)+" .cy " , whichEditor);
284
288
}
285
289
void cyrilApp::resetTimers (void * _o) {
286
290
((cyrilApp *)_o)->doResetTimers = true ;
@@ -312,28 +316,41 @@ void cyrilApp::runScript(void * _o) {
312
316
((cyrilApp *)_o)->reportError = true ;
313
317
}
314
318
319
+ std::string removeExtension (const std::string filename) {
320
+ size_t lastdot = filename.find_last_of (" ." );
321
+ if (lastdot == std::string::npos) return filename;
322
+ return filename.substr (0 , lastdot);
323
+ }
315
324
316
- void cyrilApp::onDirectoryWatcherItemAdded (const DirectoryWatcherManager::DirectoryEvent& evt) {
317
- ofFile file = ofFile (evt.item .path ());
325
+ void cyrilApp::reloadFileBuffer (std::string filePath) {
326
+ // cout << "File = " << filePath << endl;
327
+ ofFile file = ofFile (filePath);
318
328
if (file.getExtension () == " cy" ) {
319
- cout << " Load: " << file.getFileName () << endl;
320
- int whichEditor = editor.currentBuffer ;
321
- editor.loadFile (evt.item .path (), whichEditor);
322
- editor.update ();
329
+ int whichEditor = ofToInt (removeExtension (file.getFileName ()));
330
+ if (whichEditor >= 0 && whichEditor <= 9 ) {
331
+ editor.loadFile (filePath, whichEditor);
332
+ // editor.update();
333
+ if (running[whichEditor]) {
334
+ editor.currentBuffer = whichEditor;
335
+ runScript (this );
336
+ }
337
+ }
338
+ }
339
+ if (file.getExtension () == " png" ) {
340
+ // cout << "Loading image " << file.getFileName() << endl;
341
+ int whichImg = ofToInt (removeExtension (file.getFileName ()));
342
+ (*_state.img )[whichImg] = new ofImage (filePath);
323
343
}
324
344
}
345
+
346
+ void cyrilApp::onDirectoryWatcherItemAdded (const DirectoryWatcherManager::DirectoryEvent& evt) {
347
+ reloadFileBuffer (evt.item .path ());
348
+ }
325
349
void cyrilApp::onDirectoryWatcherItemRemoved (const DirectoryWatcherManager::DirectoryEvent& evt) {
326
- cout << " Unload: " << evt.item .path () << endl;
350
+ // cout << "Unload: " << evt.item.path() << endl;
327
351
}
328
352
void cyrilApp::onDirectoryWatcherItemModified (const DirectoryWatcherManager::DirectoryEvent& evt) {
329
- ofFile file = ofFile (evt.item .path ());
330
- if (file.getExtension () == " cy" ) {
331
- cout << " Reload: " << evt.item .path () << endl;
332
- int whichEditor = editor.currentBuffer ;
333
- editor.loadFile (evt.item .path (), whichEditor);
334
- // editor.update();
335
- runScript (this );
336
- }
353
+ reloadFileBuffer (evt.item .path ());
337
354
}
338
355
void cyrilApp::onDirectoryWatcherItemMovedFrom (const DirectoryWatcherManager::DirectoryEvent& evt) {
339
356
0 commit comments