Mercurial > hg > tweakathon2ios
view ExplorePresetManager.mm @ 30:78b51f924ec1
twiddles
author | Robert Tubb <rt300@eecs.qmul.ac.uk> |
---|---|
date | Tue, 04 Nov 2014 14:37:35 +0000 |
parents | bd23c1b922be |
children | a677c027e3a0 |
line wrap: on
line source
// // ExplorePresetManager.mm // riftathon // // Created by Robert Tubb on 16/10/2014. // // #include "ExplorePresetManager.h" ExplorePresetManager expPresetManager; //----------------------------------------------------------------------------- void ExplorePresetManager::onAppLoad(){ // check for already saved stuff startLoadAll(); 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; currentPresetSlotIndex = 0; return true; } return false; } //-------------------------------------------------------------------------------- PresetSlot* 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 slotToFill, vector<Preset*>::iterator preset){ (*slotToFill)->setValues((*preset)->CCValues); (*slotToFill)->isFilled = true; filledSlots++; if(filledSlots == MAX_PRESETS){ cout << "ALL SLOTS ARE FILLED!" << endl; } } //----------------------------------------------------------------------------- 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)); }