changeset 5:213df0baed47

presets file is updated when one is saved.
author Robert Tubb <rt300@eecs.qmul.ac.uk>
date Thu, 16 Oct 2014 15:52:53 +0100
parents 60b54ba87f6a
children 92850a2b099c
files MessageOrganiser.h TestController.h presetManager.h presetManager.mm testApp.mm
diffstat 5 files changed, 70 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/MessageOrganiser.h	Thu Oct 16 14:34:14 2014 +0100
+++ b/MessageOrganiser.h	Thu Oct 16 15:52:53 2014 +0100
@@ -668,8 +668,12 @@
     void loadPreset(string pname){
         
         vector<int> values = presetManager.recallPreset(pname);
+        if (values.size()){
         candidateSynth.setAllParams(values);
         setAllSlidersToValues(values);
+        }else{
+            cout << "ERROR, no preset found" << endl;
+        }
     }
     // called from UI
     void paramChangeCallback(int mappingID, int value){
--- a/TestController.h	Thu Oct 16 14:34:14 2014 +0100
+++ b/TestController.h	Thu Oct 16 15:52:53 2014 +0100
@@ -4,7 +4,9 @@
 //
 //  Created by Robert Tubb on 16/01/2014.
 //
-//
+
+
+// controller for as-long-as-it-takes, arbitrary target tests
 
 #ifndef __tweakathlon__TestController__
 #define __tweakathlon__TestController__
--- a/presetManager.h	Thu Oct 16 14:34:14 2014 +0100
+++ b/presetManager.h	Thu Oct 16 15:52:53 2014 +0100
@@ -33,11 +33,12 @@
     unsigned int    creatorDeviceID; // unique user device ID
     string          name; // name of preset
     unsigned long long       creationTime; // datetime that preset was created milliseconds
-
+    string imageFileName;
     vector<int> CCValues; // the actual data
+    ofImage presetImage;
     
     // from save button press
-    Preset(vector<int> aCCValues, string aname, int aID, string un, unsigned int uid){
+    Preset(vector<int> aCCValues, string aname, int aID, string un, unsigned int uid, string imageFile = ""){
         CCValues = aCCValues;
 
         name = aname;
@@ -49,6 +50,10 @@
         cout << "Create preset sys time: " << creationTime << "\n";
         
         //TODO color / texture?
+        imageFileName = imageFile;
+        if (imageFile != ""){
+            presetImage.loadImage(imageFile);
+        }
     };
     // contruct from json value
     Preset(Json::Value jval){
@@ -59,9 +64,10 @@
 
 
         creationTime = jval["creationTime"].asLargestInt(); // dodgy?
-
+        imageFileName = jval["imageFileName"].asString();
         
         Json::Value JArray = jval["CCValues"];
+        
         if(JArray.size() < 1){
             cout << "No Presets";
         }
@@ -69,7 +75,10 @@
         {
             CCValues.push_back(JArray[i].asInt());
         }
-
+        
+        if (imageFileName != ""){
+            presetImage.loadImage(imageFileName);
+        }
 
     }
     
@@ -98,28 +107,28 @@
 //---------------------------------------------------------------------------
 class PresetManager{
 public:
+    PresetManager();
+    void savePreset(string name, vector<int> stuff);
+    vector<int> recallPreset(int presetID);
+    vector<int> recallPreset(string name);
+    void startLoadAll(); // load everything from the json file
+    void exitAndSaveAll(); // save stuff to the json file
+    void printAll();
+    void clearAll();
+protected:
+    string presetFileName;
     int nextID;
     int timesOpened;
     bool presetAlertShowing;
 
     vector<Preset *> thePresets; 
+
+    Json::Value  allPresetsToJson();
+    void loadPresetsFromJsonFile(const string &jsonFile);
+    void updatePresetFile();
+    void showNameDialog();
     
-    int savePreset(string name, vector<int> stuff);
-  
-    vector<int> recallPreset(int presetID); // by name ? id?
-    vector<int> recallPreset(string name); // by name ? id?
-
-    void printAll();
-    
-    void startLoadAll(); // load everything from the json file
-    void exitAndSaveAll(); // save stuff to the json file
-    void clearAll();
-    Json::Value  allPresetsToJson();
-    void readJsonToPresets(const string &jsonFile);
-    void showNameDialog();
-    PresetManager();
 };
 
-
     
 #endif /* defined(__oscSenderExample__presetManager__) */
--- a/presetManager.mm	Thu Oct 16 14:34:14 2014 +0100
+++ b/presetManager.mm	Thu Oct 16 15:52:53 2014 +0100
@@ -23,13 +23,15 @@
     
     Json::Value presetVal;
     
-    presetVal["creatorUserName"] = creatorUserName;
-    presetVal["creatorDeviceID"] = creatorDeviceID;
+    presetVal["creatorUserName"] =  creatorUserName;
+    presetVal["creatorDeviceID"] =  creatorDeviceID;
     presetVal["creationTime"] =     creationTime;
     presetVal["name"] =             name;
-    
-    for(vector<int>::iterator i; i < CCValues.end(); i++){
-        presetVal["CCValues"].append(*i);
+    presetVal["imageFileName"] =    imageFileName;
+
+    for(vector<int>::iterator i = CCValues.begin(); i < CCValues.end(); i++){
+        int c = *i;
+        presetVal["CCValues"].append(c);
     }
 
     return presetVal;
@@ -62,7 +64,7 @@
     return root;
 }
 //---------------------------------------------------------------------------
-void PresetManager::readJsonToPresets(const string &jsonFile){
+void PresetManager::loadPresetsFromJsonFile(const string &jsonFile){
     Json::Value root;
     Json::Reader reader;
 
@@ -100,7 +102,7 @@
     
     for ( int index = 0; index < jpresets.size(); ++index ) thePresets.push_back(new Preset(jpresets[index]));
     
-    //printAll();
+    printAll();
      
 }
 //---------------------------------------------------------------------------
@@ -119,7 +121,7 @@
 }
 //---------------------------------------------------------------------------
 // when save button pressed
-int PresetManager::savePreset(const string name, vector<int> values){
+void PresetManager::savePreset(const string name, vector<int> values){
 
     presetAlertShowing = false;
     // check for same name
@@ -130,6 +132,9 @@
             cout << "WARNING Preset by that name exists, overwriting\n";
             // overwrite it
             (*iter)->CCValues = values;
+            updatePresetFile();
+            eventLogger.logEvent(SAVE_PRESET); // TODO need to log details?
+            return;
         }
     }
      
@@ -138,43 +143,47 @@
         
     }
 
-
     thePresets.push_back(new Preset(values, name, nextID, eventLogger.userName, eventLogger.deviceID));
     eventLogger.logEvent(SAVE_PRESET); // TODO need to log details?
+    updatePresetFile();
 
-    return nextID++;
 }
-
+//----------------------------------------------cu-----------------------------
 vector<int> PresetManager::recallPreset(string name){
     vector<Preset *>::iterator p;
-        for(p = thePresets.begin(); p < thePresets.end(); p++){
-            if ( (*p)->getName() == name){
-                return (*p)->getValues();
-            }
+    for(p = thePresets.begin(); p < thePresets.end(); p++){
+        if ( (*p)->getName() == name){
+            return (*p)->getValues();
+        }
     }
-    
+    vector<int> empty;
+    return empty;
 }
 //----------------------------------------------cu-----------------------------
 void PresetManager::startLoadAll(){
-    // get stuff from file
-    // load file
 
-    string fname = ofxiPhoneGetDocumentsDirectory() + PRESET_FILENAME;
+    presetFileName = ofxiPhoneGetDocumentsDirectory() + PRESET_FILENAME;
     
-    readJsonToPresets(fname);
+    loadPresetsFromJsonFile(presetFileName);
     
     timesOpened++;
 }
+//---------------------------------------------------------------------------
+void PresetManager::updatePresetFile(){
+    
+    // TODO really inefficient
+    
+    ofFile presetFile(presetFileName,ofFile::WriteOnly);
 
-//---------------------------------------------------------------------------
-void PresetManager::exitAndSaveAll(){
-    ofFile presetFile(ofxiPhoneGetDocumentsDirectory() +PRESET_FILENAME,ofFile::WriteOnly);
-    
     // stick all the stuff in a json value
     Json::Value root = allPresetsToJson();
     
     cout << root;
     presetFile << root;
+}
+//---------------------------------------------------------------------------
+void PresetManager::exitAndSaveAll(){
+    updatePresetFile();
 
 }
 
--- a/testApp.mm	Thu Oct 16 14:34:14 2014 +0100
+++ b/testApp.mm	Thu Oct 16 15:52:53 2014 +0100
@@ -2,6 +2,7 @@
 #include "ofAppiOSWindow.h"
 
 extern EventLogger eventLogger;
+extern PresetManager presetManager;
 
 // static members inited here. not my choice.
 int SynthParam::mappingUID = 88000;
@@ -20,6 +21,7 @@
     
     
     testController = new TestController;
+    presetManager.startLoadAll();
     
     messageOrganiser.init(&core, testController);
     timeController.init();