Mercurial > hg > soniczoomios
diff presetManager.mm @ 8:e2c6cfe8c6b7
JSON logs and presets.
author | Robert Tubb <rt300@eecs.qmul.ac.uk> |
---|---|
date | Thu, 10 Jan 2013 18:24:26 +0000 |
parents | 845ea04f8e33 |
children | 346807b47860 |
line wrap: on
line diff
--- a/presetManager.mm Thu Dec 06 18:26:51 2012 +0000 +++ b/presetManager.mm Thu Jan 10 18:24:26 2013 +0000 @@ -11,9 +11,25 @@ //--------------------------------------------------------------------------- extern Grid theGridView; PresetManager presetManager; +extern EventLogger eventLogger; +extern IViewController *iViewController; +//--------------------------------------------------------------------------- +vector<ofColor> Preset::makePresetPicture(TwoVector coord){ + // convert midi parameters to a nice piccy + vector<int> params = theGridView.calculateParamsFromCoord(coord); + vector<ofColor> pixCols; + + ofColor col(255,0,0); + for(int i=0; i<9;i++){ + col.setHue(params[i]*2); + pixCols.push_back(col); + } + return pixCols; +} //--------------------------------------------------------------------------- void Preset::draw(){ - // + // TODO bit stupid actually. make it a shaded ball / dot + int sz = 2; TwoVector pos = theGridView.coordToPixel(coordinates); // euch -rely on grid view? @@ -55,14 +71,88 @@ ofRect(pos.x-sz, pos.y-sz,sz,sz); }; //--------------------------------------------------------------------------- +Json::Value Preset::presetToJson(){ + // create the string for this instance of Preset object + + Json::Value presetVal; + + presetVal["creatorUserName"] = creatorUserName; + presetVal["creatorDeviceID"] = creatorDeviceID; + presetVal["creationTime"] = creationTime; + presetVal["name"] = name; + presetVal["coordinates"]["x"] = coordinates.x; + presetVal["coordinates"]["y"] = coordinates.y; + + + return presetVal; +} +//--------------------------------------------------------------------------- PresetManager::PresetManager(){ timesOpened = 0; nextID = 0; - + string ts = ofGetTimestampString(); cout << "ofGetTimestampString: " << ts << '\n'; } +//--------------------------------------------------------------------------- +Json::Value PresetManager::allPresetsToJson(){ + Json::Value root; + + // use jsoncpp + vector<Preset *>::iterator presetIter; + root["something"] = 23; + + int i = 0; + for(presetIter = thePresets.begin(); presetIter < thePresets.end(); presetIter++){ + root["presets"][i] = (*presetIter)->presetToJson(); + i++; + } + + return root; +} +//--------------------------------------------------------------------------- +void PresetManager::readJsonToPresets(const string &jsonFile){ + Json::Value root; + Json::Reader reader; + + + ifstream theFile(jsonFile.c_str()); + stringstream fileText; + string line; + if(!theFile){ + cout<<"can't find preset file \n"; + + }else{ + + while(theFile){ + theFile >> line; + cout << line; + 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 JSON\n" + << reader.getFormattedErrorMessages(); + return; + } + + // now put into variables + const Json::Value jpresets = root["presets"]; + + for ( int index = 0; index < jpresets.size(); ++index ) thePresets.push_back(new Preset(jpresets[index])); + + //printAll(); + +} //--------------------------------------------------------------------------- void PresetManager::printAll(){ cout << "ALL PRESETS: \n"; @@ -73,8 +163,8 @@ } } //--------------------------------------------------------------------------- - -int PresetManager::addPreset(const TwoVector coord, const string name){ +// when save button pressed +int PresetManager::addPreset(const string name){ // check for same name @@ -92,32 +182,17 @@ return -2; } - - // make piccy - vector<ofColor> presetPicture = makePresetPicture(coord); - thePresets.push_back(new Preset(coord, name,nextID, presetPicture)); + // yuk shouldn't have to know about eventlogger and grid view... + thePresets.push_back(new Preset(theGridView.getCoord(), name,nextID, eventLogger.userName, eventLogger.deviceID)); // if ok return nextID++; } -//--------------------------------------------------------------------------- -vector<ofColor> PresetManager::makePresetPicture(TwoVector coord){ - // convert midi parameters to a nice piccy - vector<int> params = theGridView.calculateParamsFromCoord(coord); - vector<ofColor> pixCols; - - ofColor col(255,0,0); - for(int i=0; i<9;i++){ - col.setHue(params[i]*2); - pixCols.push_back(col); - } - return pixCols; -} + //--------------------------------------------------------------------------- int PresetManager::loadPreset(const TwoVector coord, const string name, long long stime){ - vector<ofColor> presetPicture = makePresetPicture(coord); // or just store it?? - thePresets.push_back(new Preset(coord, name,nextID, stime, presetPicture)); + thePresets.push_back(new Preset(coord, name,nextID, stime)); // if ok return nextID++; @@ -154,45 +229,29 @@ } } -//--------------------------------------------------------------------------- +//----------------------------------------------cu----------------------------- void PresetManager::startupLoadAll(){ - // get stuff from file + // get stuff from file // load file - - string fname = ofxiPhoneGetDocumentsDirectory() + "presets.dat"; - ifstream presetFile(fname.c_str()); - // simply prints file - - if(!presetFile){ - cout<<"no preset file"; - return; - } - while(presetFile >> *this); + string fname = ofxiPhoneGetDocumentsDirectory() + PRESET_FILENAME; - //TODO get presets from online database... + readJsonToPresets(fname); - presetFile.close(); - timesOpened++; } + //--------------------------------------------------------------------------- void PresetManager::exitAndSaveAll(){ - ofFile presetFile(ofxiPhoneGetDocumentsDirectory() +"presets.dat",ofFile::WriteOnly); + ofFile presetFile(ofxiPhoneGetDocumentsDirectory() +PRESET_FILENAME,ofFile::WriteOnly); - cout << "Exit and save presets\n"; - vector<Preset *>::iterator presetIter; - - for(presetIter = thePresets.begin(); presetIter < thePresets.end(); presetIter++){ - //vector<int> params; - //params = theGridView.calculateParamsFromCoord((*presetIter)->coordinates); - presetFile << **presetIter << "\n"; - } - presetFile.close(); + // stick all the stuff in a json value + Json::Value root = allPresetsToJson(); - // TODO dleete all the new events?? + cout << root; + presetFile << root; + - } //---------------------------------------------------------------------------