annotate ExplorePresetManager.mm @ 32:75202498bee9

perform mode (no guides at all)
author Robert Tubb <rt300@eecs.qmul.ac.uk>
date Tue, 25 Nov 2014 17:03:33 +0000
parents a677c027e3a0
children 2bd658b44c2d
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@32 117 vector<string> drumPresetNames(){
rt300@32 118 vector<string> vs;
rt300@32 119
rt300@32 120 vs.push_back("boom kick");
rt300@32 121 vs.push_back("punch kick");
rt300@32 122 vs.push_back("snare");
rt300@32 123 vs.push_back("clap");
rt300@32 124
rt300@32 125 vs.push_back("closed hihat");
rt300@32 126 vs.push_back("open hihat");
rt300@32 127 vs.push_back("cowbell");
rt300@32 128 vs.push_back("bongo");
rt300@32 129
rt300@32 130
rt300@32 131 }
rt300@32 132 //-----------------------------------------------------------------------------
rt300@18 133
rt300@18 134 void ExplorePresetManager::fillSlotsWithLoadedPresets(){
rt300@18 135 for(auto pi = thePresets.begin(); pi < thePresets.end(); pi++){
rt300@18 136 string presetName = (*pi)->name;
rt300@18 137
rt300@18 138 for(auto psi = thePresetSlots.begin(); psi < thePresetSlots.end(); psi++){
rt300@18 139 string slotName = (*psi)->name;
rt300@18 140 if (slotName == presetName){
rt300@18 141 fillSlotFromLoadedPreset(psi, pi);
rt300@18 142 }
rt300@18 143 }
rt300@18 144 }
rt300@18 145 }
rt300@18 146 //-----------------------------------------------------------------------------
rt300@18 147
rt300@19 148 void ExplorePresetManager::fillSlotFromLoadedPreset(vector<PresetSlot*>::iterator slotToFill, vector<Preset*>::iterator preset){
rt300@19 149 (*slotToFill)->setValues((*preset)->CCValues);
rt300@19 150 (*slotToFill)->isFilled = true;
rt300@19 151 filledSlots++;
rt300@19 152
rt300@19 153 if(filledSlots == MAX_PRESETS){
rt300@19 154 cout << "ALL SLOTS ARE FILLED!" << endl;
rt300@19 155 }
rt300@19 156
rt300@19 157
rt300@18 158 }
rt300@18 159 //-----------------------------------------------------------------------------
rt300@18 160 void ExplorePresetManager::generatePresetSlot(const string name, const string imagefn){
rt300@18 161 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 162 thePresetSlots.push_back(new PresetSlot(values, name, nextID, eventLogger.userName, eventLogger.deviceID, imagefn));
rt300@18 163
rt300@18 164 }