annotate presetManager.mm @ 32:ab7c86d0f3d8

V0.3 SZBeta sent out. bristol tests.
author Robert Tubb <rt300@eecs.qmul.ac.uk>
date Fri, 08 Mar 2013 14:54:55 +0000
parents 23ef179c3748
children 790939017078
rev   line source
rt300@0 1 //
rt300@0 2 // presetManager.mm
rt300@0 3 // oscSenderExample
rt300@0 4 //
rt300@0 5 // Created by Robert Tubb on 07/11/2012.
rt300@0 6 //
rt300@0 7 //
rt300@0 8
rt300@0 9 #include "presetManager.h"
rt300@0 10
rt300@0 11 //---------------------------------------------------------------------------
rt300@3 12 extern Grid theGridView;
rt300@0 13 PresetManager presetManager;
rt300@8 14 extern EventLogger eventLogger;
rt300@16 15 extern PresetAlertViewController *presetAlertViewController;
rt300@8 16 //---------------------------------------------------------------------------
rt300@8 17 vector<ofColor> Preset::makePresetPicture(TwoVector coord){
rt300@8 18 // convert midi parameters to a nice piccy
rt300@8 19 vector<int> params = theGridView.calculateParamsFromCoord(coord);
rt300@8 20 vector<ofColor> pixCols;
rt300@8 21
rt300@8 22 ofColor col(255,0,0);
rt300@8 23 for(int i=0; i<9;i++){
rt300@8 24 col.setHue(params[i]*2);
rt300@8 25 pixCols.push_back(col);
rt300@8 26 }
rt300@8 27 return pixCols;
rt300@8 28 }
rt300@0 29 //---------------------------------------------------------------------------
rt300@5 30 void Preset::draw(){
rt300@8 31 // TODO bit stupid actually. make it a shaded ball / dot
rt300@8 32
rt300@5 33 int sz = 2;
rt300@5 34 TwoVector pos = theGridView.coordToPixel(coordinates); // euch -rely on grid view?
rt300@5 35
rt300@5 36 if(pixVals.size() < 9){
rt300@5 37 cout << "NO PRESET PIC\n";
rt300@5 38 return;
rt300@5 39 }
rt300@5 40 //middle row
rt300@5 41 // centre
rt300@5 42 ofSetColor(pixVals[0]);
rt300@5 43 ofRect(pos.x, pos.y,sz,sz);
rt300@5 44 // right
rt300@5 45 ofSetColor(pixVals[1]);
rt300@5 46 ofRect(pos.x+sz, pos.y,sz,sz);
rt300@5 47 // left
rt300@5 48 ofSetColor(pixVals[2]);
rt300@5 49 ofRect(pos.x-sz, pos.y,sz,sz);
rt300@5 50
rt300@5 51 //top
rt300@5 52 // centre
rt300@5 53 ofSetColor(pixVals[3]);
rt300@5 54 ofRect(pos.x, pos.y+sz,sz,sz);
rt300@5 55 // right
rt300@5 56 ofSetColor(pixVals[4]);
rt300@5 57 ofRect(pos.x+sz, pos.y+sz,sz,sz);
rt300@5 58 // left
rt300@5 59 ofSetColor(pixVals[5]);
rt300@5 60 ofRect(pos.x-sz, pos.y+sz,sz,sz);
rt300@5 61
rt300@5 62 // bottom
rt300@5 63 // centre
rt300@5 64 ofSetColor(pixVals[6]);
rt300@5 65 ofRect(pos.x, pos.y-sz,sz,sz);
rt300@5 66 // right
rt300@5 67 ofSetColor(pixVals[7]);
rt300@5 68 ofRect(pos.x+sz, pos.y-sz,sz,sz);
rt300@5 69 // left
rt300@5 70 ofSetColor(pixVals[8]);
rt300@5 71 ofRect(pos.x-sz, pos.y-sz,sz,sz);
rt300@5 72 };
rt300@5 73 //---------------------------------------------------------------------------
rt300@8 74 Json::Value Preset::presetToJson(){
rt300@8 75 // create the string for this instance of Preset object
rt300@8 76
rt300@8 77 Json::Value presetVal;
rt300@8 78
rt300@8 79 presetVal["creatorUserName"] = creatorUserName;
rt300@8 80 presetVal["creatorDeviceID"] = creatorDeviceID;
rt300@8 81 presetVal["creationTime"] = creationTime;
rt300@8 82 presetVal["name"] = name;
rt300@8 83 presetVal["coordinates"]["x"] = coordinates.x;
rt300@8 84 presetVal["coordinates"]["y"] = coordinates.y;
rt300@8 85
rt300@8 86
rt300@8 87 return presetVal;
rt300@8 88 }
rt300@8 89 //---------------------------------------------------------------------------
rt300@0 90 PresetManager::PresetManager(){
rt300@3 91 timesOpened = 0;
rt300@4 92 nextID = 0;
rt300@8 93
rt300@7 94 string ts = ofGetTimestampString();
rt300@22 95
rt300@22 96 presetAlertShowing = false;
rt300@22 97
rt300@22 98
rt300@7 99 cout << "ofGetTimestampString: " << ts << '\n';
rt300@4 100 }
rt300@8 101 //---------------------------------------------------------------------------
rt300@8 102 Json::Value PresetManager::allPresetsToJson(){
rt300@8 103 Json::Value root;
rt300@8 104
rt300@8 105 // use jsoncpp
rt300@8 106 vector<Preset *>::iterator presetIter;
rt300@8 107
rt300@8 108 int i = 0;
rt300@8 109 for(presetIter = thePresets.begin(); presetIter < thePresets.end(); presetIter++){
rt300@8 110 root["presets"][i] = (*presetIter)->presetToJson();
rt300@8 111 i++;
rt300@8 112 }
rt300@27 113
rt300@8 114 return root;
rt300@8 115 }
rt300@8 116 //---------------------------------------------------------------------------
rt300@8 117 void PresetManager::readJsonToPresets(const string &jsonFile){
rt300@8 118 Json::Value root;
rt300@8 119 Json::Reader reader;
rt300@8 120
rt300@8 121
rt300@8 122 ifstream theFile(jsonFile.c_str());
rt300@8 123 stringstream fileText;
rt300@8 124 string line;
rt300@8 125 if(!theFile){
rt300@8 126 cout<<"can't find preset file \n";
rt300@22 127 return;
rt300@8 128 }else{
rt300@8 129
rt300@8 130 while(theFile){
rt300@8 131 theFile >> line;
rt300@9 132 // cout << line << "\n"; // lots?
rt300@8 133 fileText << line;
rt300@8 134
rt300@8 135 }
rt300@8 136
rt300@8 137 theFile.close();
rt300@8 138 }
rt300@8 139
rt300@8 140 bool parsingSuccessful = reader.parse( fileText.str(), root );
rt300@8 141
rt300@8 142 if ( !parsingSuccessful )
rt300@8 143 {
rt300@8 144 // report to the user the failure and their locations in the document.
rt300@22 145 std::cout << "Failed to parse preset JSON: \n"
rt300@8 146 << reader.getFormattedErrorMessages();
rt300@8 147 return;
rt300@8 148 }
rt300@8 149
rt300@8 150 // now put into variables
rt300@8 151 const Json::Value jpresets = root["presets"];
rt300@8 152
rt300@8 153 for ( int index = 0; index < jpresets.size(); ++index ) thePresets.push_back(new Preset(jpresets[index]));
rt300@8 154
rt300@8 155 //printAll();
rt300@8 156
rt300@8 157 }
rt300@4 158 //---------------------------------------------------------------------------
rt300@4 159 void PresetManager::printAll(){
rt300@30 160 cout << "----------------ALL PRESETS-------------: \n";
rt300@30 161 cout << allPresetsToJson() << "\n";
rt300@0 162 }
rt300@25 163 //---------------------------------------------------------------------------
rt300@9 164 void PresetManager::showNameDialog(){
rt300@29 165 if(!presetAlertViewController.alertShowing){ // this is to stop wierd infinite loop in ios5 (simulator)
rt300@22 166 presetAlertShowing = true;
rt300@22 167 [presetAlertViewController showPresetNamePrompt];
rt300@22 168
rt300@22 169 }
rt300@22 170
rt300@9 171 }
rt300@0 172 //---------------------------------------------------------------------------
rt300@8 173 // when save button pressed
rt300@8 174 int PresetManager::addPreset(const string name){
rt300@3 175
rt300@22 176 presetAlertShowing = false;
rt300@0 177 // check for same name
rt300@0 178 vector<Preset *>::iterator iter;
rt300@31 179 /*
rt300@0 180 for(iter = thePresets.begin(); iter < thePresets.end(); iter++){
rt300@0 181 if ((*iter)->name == name){
rt300@0 182 cout << " Preset by that name exists\n";
rt300@0 183
rt300@0 184 // use exceptions!
rt300@0 185 return -1;
rt300@0 186 }
rt300@0 187 }
rt300@31 188
rt300@0 189 if(name == ""){
rt300@0 190 cout << "Please name preset\n";
rt300@0 191 return -2;
rt300@0 192
rt300@0 193 }
rt300@31 194 */
rt300@31 195 // hmm shouldn't have to know about eventlogger and grid view...
rt300@8 196 thePresets.push_back(new Preset(theGridView.getCoord(), name,nextID, eventLogger.userName, eventLogger.deviceID));
rt300@31 197 eventLogger.logEvent(SAVE_PRESET, theGridView.getCoord());
rt300@9 198 // poke grid view to get it to show details
rt300@9 199 theGridView.snapCheck();
rt300@0 200 // if ok
rt300@0 201 return nextID++;
rt300@0 202 }
rt300@8 203
rt300@5 204 //---------------------------------------------------------------------------
rt300@4 205
rt300@4 206 int PresetManager::loadPreset(const TwoVector coord, const string name, long long stime){
rt300@8 207 thePresets.push_back(new Preset(coord, name,nextID, stime));
rt300@5 208
rt300@4 209 // if ok
rt300@4 210 return nextID++;
rt300@4 211 }
rt300@4 212 //---------------------------------------------------------------------------
rt300@5 213 vector<Preset *> PresetManager::getPresetsInRange(const TwoVector min, const TwoVector max){
rt300@0 214 //return all the coordinates. oh and names (displayed at certain scales?).
rt300@3 215
rt300@3 216 // TODO INEFFICIENT FOR LOTS OF PRESETS. CALLED EVERY GRAPHICS UPDATE!
rt300@3 217 // so: put burden on saving rather than retrieving, make into list and index using coordinates
rt300@3 218
rt300@5 219 vector<Preset *> results;
rt300@3 220 vector<Preset *>::iterator presetIter;
rt300@3 221 for(presetIter = thePresets.begin(); presetIter < thePresets.end(); presetIter++){
rt300@3 222 if( ((*presetIter)->coordinates.x > min.x ) && ((*presetIter)->coordinates.y > min.y ) && ((*presetIter)->coordinates.x < max.x ) && ((*presetIter)->coordinates.y < max.y )){
rt300@5 223 results.push_back(*presetIter);
rt300@3 224 }
rt300@3 225
rt300@3 226 }
rt300@3 227 return results;
rt300@0 228
rt300@0 229 }
rt300@5 230
rt300@5 231
rt300@5 232 //---------------------------------------------------------------------------
rt300@5 233 void PresetManager::drawPresetsInRange(const TwoVector min, const TwoVector max){
rt300@5 234 vector<Preset *> pInRange = getPresetsInRange(min, max);
rt300@5 235 vector<Preset *>::iterator piter;
rt300@5 236 int i = 0;
rt300@5 237 for(piter = pInRange.begin(); piter < pInRange.end(); piter++){
rt300@5 238
rt300@5 239 (*piter)->draw();
rt300@5 240 i++;
rt300@5 241 }
rt300@5 242
rt300@5 243 }
rt300@8 244 //----------------------------------------------cu-----------------------------
rt300@30 245 void PresetManager::startLoadAll(){
rt300@8 246 // get stuff from file
rt300@3 247 // load file
rt300@3 248
rt300@8 249 string fname = ofxiPhoneGetDocumentsDirectory() + PRESET_FILENAME;
rt300@5 250
rt300@8 251 readJsonToPresets(fname);
rt300@7 252
rt300@3 253 timesOpened++;
rt300@3 254 }
rt300@8 255
rt300@3 256 //---------------------------------------------------------------------------
rt300@3 257 void PresetManager::exitAndSaveAll(){
rt300@8 258 ofFile presetFile(ofxiPhoneGetDocumentsDirectory() +PRESET_FILENAME,ofFile::WriteOnly);
rt300@4 259
rt300@8 260 // stick all the stuff in a json value
rt300@8 261 Json::Value root = allPresetsToJson();
rt300@5 262
rt300@8 263 cout << root;
rt300@8 264 presetFile << root;
rt300@29 265
rt300@3 266 }
rt300@32 267 //---------------------------------------------------------------------------
rt300@32 268 void PresetManager::saveSessionToFile(string userName){ // for supervised newUser()
rt300@32 269 ofFile presetFile(ofxiPhoneGetDocumentsDirectory() + userName + '_' + PRESET_FILENAME,ofFile::WriteOnly);
rt300@32 270
rt300@32 271 // stick all the stuff in a json value
rt300@32 272 Json::Value root = allPresetsToJson();
rt300@32 273
rt300@32 274 cout << root;
rt300@32 275 presetFile << root;
rt300@32 276
rt300@32 277 }
rt300@5 278 //---------------------------------------------------------------------------
rt300@5 279 void PresetManager::clearAll(){
rt300@5 280 thePresets.clear();
rt300@5 281 }
rt300@3 282 //---------------------------------------------------------------------------
rt300@0 283 //---------------------------------------------------------------------------
rt300@0 284 //---------------------------------------------------------------------------
rt300@0 285 //---------------------------------------------------------------------------
rt300@0 286 //---------------------------------------------------------------------------