diff ExplorePresetManager.mm @ 18:36cdb73691da

PIMPL speed compile? eventlogger now just saves as it goes more refactoring
author Robert Tubb <rt300@eecs.qmul.ac.uk>
date Wed, 22 Oct 2014 15:00:14 +0100
parents 92850a2b099c
children bd23c1b922be
line wrap: on
line diff
--- a/ExplorePresetManager.mm	Tue Oct 21 18:58:25 2014 +0100
+++ b/ExplorePresetManager.mm	Wed Oct 22 15:00:14 2014 +0100
@@ -8,3 +8,133 @@
 
 #include "ExplorePresetManager.h"
 ExplorePresetManager expPresetManager;
+
+//-----------------------------------------------------------------------------
+
+void ExplorePresetManager::onAppLoad(){
+    // check for already saved stuff
+    startLoadAll();
+    
+    if (thePresets.size() != MAX_PRESETS){
+        
+        initPresetSlots();
+    }
+    
+}
+
+//----------------------------------------------------------------------------
+void ExplorePresetManager::goToFirstEmptySlot(){
+    currentPresetSlotIndex = 0;
+    
+    // loop thru slots, find first empty slot
+}
+//----------------------------------------------------------------------------
+bool ExplorePresetManager::writeValuesToSlot(vector<int> values){
+    getPresetSlotAtIndex(currentPresetSlotIndex)->setValues(values);
+    
+    // now put it into a real preset, so it will be saved to file
+    savePreset(getPresetSlotAtIndex(currentPresetSlotIndex));
+    
+    filledSlots++;
+    currentPresetSlotIndex++;
+    if(filledSlots == MAX_PRESETS){
+        cout << "FINISHED EXP STAGE!" << endl;
+        return true;
+    }
+    return false;
+}
+//--------------------------------------------------------------------------------
+Preset* ExplorePresetManager::getCurrentPresetSlot(){
+    
+    return getPresetSlotAtIndex(currentPresetSlotIndex);
+}
+//----------------------------------------------------------------------------
+
+PresetSlot* ExplorePresetManager::getPresetSlotAtIndex(int index){
+    
+    if (index >= thePresetSlots.size()){
+        cout << "ERROR: index " << index << " exceeds number of presetslots " << thePresets.size() << endl;
+        return NULL;
+    }else{
+        return thePresetSlots[index];
+        
+    }
+};
+//---------------------------------------------------------------------------
+void ExplorePresetManager::initPresetSlots(){
+    
+    presetSlotFilename = ofFilePath::getAbsolutePath(ofToDataPath("presetSlots.json"));
+    
+    // set up preset slots with names and images
+    Json::Value root;
+    Json::Reader reader;
+    
+    
+    ifstream theFile(presetSlotFilename.c_str());
+    stringstream fileText;
+    string line;
+    if(!theFile){
+        cout<<"can't find presetSlot file \n";
+        return;
+    }else{
+        
+        while(theFile){
+            theFile >> line;
+            // cout << line << "\n"; // lots?
+            fileText << line;
+            
+        }
+        
+        theFile.close();
+    }
+    
+    bool parsingSuccessful = reader.parse( fileText.str(), root );
+    
+    if ( !parsingSuccessful )
+    {
+        // report to the user the failure and their locations in the document.
+        std::cout  << "Failed to parse preset slot JSON: \n"
+        << reader.getFormattedErrorMessages();
+        return;
+    }
+    
+    // now put into variables
+    const Json::Value jv = root["presetSlots"];
+    
+    for ( int index = 0; index < jv.size(); ++index ){
+        string name = jv[index]["name"].asString();
+        string imageFileName = jv[index]["imageFileName"].asString();
+        vector<int> empty;
+        generatePresetSlot(name, imageFileName);
+    }
+    
+    // now look at existing presets to see if slots are filled
+    fillSlotsWithLoadedPresets();
+    
+    filledSlots = 0;
+}
+//-----------------------------------------------------------------------------
+
+void ExplorePresetManager::fillSlotsWithLoadedPresets(){
+    for(auto pi = thePresets.begin(); pi < thePresets.end(); pi++){
+        string presetName = (*pi)->name;
+        
+        for(auto psi = thePresetSlots.begin(); psi < thePresetSlots.end(); psi++){
+            string slotName = (*psi)->name;
+            if (slotName == presetName){
+                fillSlotFromLoadedPreset(psi, pi);
+            }
+        }
+    }
+}
+//-----------------------------------------------------------------------------
+
+void ExplorePresetManager::fillSlotFromLoadedPreset(vector<PresetSlot*>::iterator psi, vector<Preset*>::iterator pi){
+    (*pi) = (*psi); // dodgy?
+}
+//-----------------------------------------------------------------------------
+void ExplorePresetManager::generatePresetSlot(const string name, const string imagefn){
+    vector<int> values = makeVector8(int(rand() % 128),int(rand() % 128),int(rand() % 128),int(rand() % 128),int(rand() % 128),0,0,0); // empty
+    thePresetSlots.push_back(new PresetSlot(values, name, nextID, eventLogger.userName, eventLogger.deviceID, imagefn));
+    
+}