annotate ExplorePresetManager.mm @ 31:a677c027e3a0

Demo to Simon, kinda works. 4 reps of sequence.
author Robert Tubb <rt300@eecs.qmul.ac.uk>
date Fri, 21 Nov 2014 17:07:56 +0000
parents bd23c1b922be
children 75202498bee9
rev   line source
rt300@6 1 //
rt300@6 2 // ExplorePresetManager.mm
rt300@6 3 // riftathon
rt300@6 4 //
rt300@6 5 // Created by Robert Tubb on 16/10/2014.
rt300@6 6 //
rt300@6 7 //
rt300@6 8
rt300@6 9 #include "ExplorePresetManager.h"
rt300@6 10 ExplorePresetManager expPresetManager;
rt300@18 11
rt300@18 12 //-----------------------------------------------------------------------------
rt300@18 13
rt300@18 14 void ExplorePresetManager::onAppLoad(){
rt300@18 15 // check for already saved stuff
rt300@18 16 startLoadAll();
rt300@19 17 initPresetSlots();
rt300@19 18
rt300@18 19
rt300@31 20 makeNeutralPreset();
rt300@31 21
rt300@18 22 }
rt300@18 23
rt300@18 24 //----------------------------------------------------------------------------
rt300@18 25 void ExplorePresetManager::goToFirstEmptySlot(){
rt300@18 26 currentPresetSlotIndex = 0;
rt300@18 27
rt300@18 28 // loop thru slots, find first empty slot
rt300@18 29 }
rt300@18 30 //----------------------------------------------------------------------------
rt300@18 31 bool ExplorePresetManager::writeValuesToSlot(vector<int> values){
rt300@18 32 getPresetSlotAtIndex(currentPresetSlotIndex)->setValues(values);
rt300@18 33
rt300@18 34 // now put it into a real preset, so it will be saved to file
rt300@18 35 savePreset(getPresetSlotAtIndex(currentPresetSlotIndex));
rt300@18 36
rt300@18 37 filledSlots++;
rt300@18 38 currentPresetSlotIndex++;
rt300@18 39 if(filledSlots == MAX_PRESETS){
rt300@18 40 cout << "FINISHED EXP STAGE!" << endl;
rt300@19 41 currentPresetSlotIndex = 0;
rt300@18 42 return true;
rt300@18 43 }
rt300@18 44 return false;
rt300@18 45 }
rt300@18 46 //--------------------------------------------------------------------------------
rt300@19 47 PresetSlot* ExplorePresetManager::getCurrentPresetSlot(){
rt300@18 48
rt300@18 49 return getPresetSlotAtIndex(currentPresetSlotIndex);
rt300@18 50 }
rt300@18 51 //----------------------------------------------------------------------------
rt300@18 52
rt300@18 53 PresetSlot* ExplorePresetManager::getPresetSlotAtIndex(int index){
rt300@18 54
rt300@18 55 if (index >= thePresetSlots.size()){
rt300@18 56 cout << "ERROR: index " << index << " exceeds number of presetslots " << thePresets.size() << endl;
rt300@18 57 return NULL;
rt300@18 58 }else{
rt300@18 59 return thePresetSlots[index];
rt300@18 60
rt300@18 61 }
rt300@18 62 };
rt300@18 63 //---------------------------------------------------------------------------
rt300@18 64 void ExplorePresetManager::initPresetSlots(){
rt300@18 65
rt300@18 66 presetSlotFilename = ofFilePath::getAbsolutePath(ofToDataPath("presetSlots.json"));
rt300@18 67
rt300@18 68 // set up preset slots with names and images
rt300@18 69 Json::Value root;
rt300@18 70 Json::Reader reader;
rt300@18 71
rt300@18 72
rt300@18 73 ifstream theFile(presetSlotFilename.c_str());
rt300@18 74 stringstream fileText;
rt300@18 75 string line;
rt300@18 76 if(!theFile){
rt300@18 77 cout<<"can't find presetSlot file \n";
rt300@18 78 return;
rt300@18 79 }else{
rt300@18 80
rt300@18 81 while(theFile){
rt300@18 82 theFile >> line;
rt300@18 83 // cout << line << "\n"; // lots?
rt300@18 84 fileText << line;
rt300@18 85
rt300@18 86 }
rt300@18 87
rt300@18 88 theFile.close();
rt300@18 89 }
rt300@18 90
rt300@18 91 bool parsingSuccessful = reader.parse( fileText.str(), root );
rt300@18 92
rt300@18 93 if ( !parsingSuccessful )
rt300@18 94 {
rt300@18 95 // report to the user the failure and their locations in the document.
rt300@18 96 std::cout << "Failed to parse preset slot JSON: \n"
rt300@18 97 << reader.getFormattedErrorMessages();
rt300@18 98 return;
rt300@18 99 }
rt300@18 100
rt300@18 101 // now put into variables
rt300@18 102 const Json::Value jv = root["presetSlots"];
rt300@18 103
rt300@18 104 for ( int index = 0; index < jv.size(); ++index ){
rt300@18 105 string name = jv[index]["name"].asString();
rt300@18 106 string imageFileName = jv[index]["imageFileName"].asString();
rt300@18 107 vector<int> empty;
rt300@18 108 generatePresetSlot(name, imageFileName);
rt300@18 109 }
rt300@18 110
rt300@18 111 // now look at existing presets to see if slots are filled
rt300@18 112 fillSlotsWithLoadedPresets();
rt300@18 113
rt300@18 114 filledSlots = 0;
rt300@18 115 }
rt300@18 116 //-----------------------------------------------------------------------------
rt300@18 117
rt300@18 118 void ExplorePresetManager::fillSlotsWithLoadedPresets(){
rt300@18 119 for(auto pi = thePresets.begin(); pi < thePresets.end(); pi++){
rt300@18 120 string presetName = (*pi)->name;
rt300@18 121
rt300@18 122 for(auto psi = thePresetSlots.begin(); psi < thePresetSlots.end(); psi++){
rt300@18 123 string slotName = (*psi)->name;
rt300@18 124 if (slotName == presetName){
rt300@18 125 fillSlotFromLoadedPreset(psi, pi);
rt300@18 126 }
rt300@18 127 }
rt300@18 128 }
rt300@18 129 }
rt300@18 130 //-----------------------------------------------------------------------------
rt300@18 131
rt300@19 132 void ExplorePresetManager::fillSlotFromLoadedPreset(vector<PresetSlot*>::iterator slotToFill, vector<Preset*>::iterator preset){
rt300@19 133 (*slotToFill)->setValues((*preset)->CCValues);
rt300@19 134 (*slotToFill)->isFilled = true;
rt300@19 135 filledSlots++;
rt300@19 136
rt300@19 137 if(filledSlots == MAX_PRESETS){
rt300@19 138 cout << "ALL SLOTS ARE FILLED!" << endl;
rt300@19 139 }
rt300@19 140
rt300@19 141
rt300@18 142 }
rt300@18 143 //-----------------------------------------------------------------------------
rt300@18 144 void ExplorePresetManager::generatePresetSlot(const string name, const string imagefn){
rt300@18 145 vector<int> values = makeVector8(int(rand() % 128),int(rand() % 128),int(rand() % 128),int(rand() % 128),int(rand() % 128),0,0,0); // empty
rt300@18 146 thePresetSlots.push_back(new PresetSlot(values, name, nextID, eventLogger.userName, eventLogger.deviceID, imagefn));
rt300@18 147
rt300@18 148 }