comparison 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
comparison
equal deleted inserted replaced
7:845ea04f8e33 8:e2c6cfe8c6b7
9 #include "presetManager.h" 9 #include "presetManager.h"
10 10
11 //--------------------------------------------------------------------------- 11 //---------------------------------------------------------------------------
12 extern Grid theGridView; 12 extern Grid theGridView;
13 PresetManager presetManager; 13 PresetManager presetManager;
14 extern EventLogger eventLogger;
15 extern IViewController *iViewController;
16 //---------------------------------------------------------------------------
17 vector<ofColor> Preset::makePresetPicture(TwoVector coord){
18 // convert midi parameters to a nice piccy
19 vector<int> params = theGridView.calculateParamsFromCoord(coord);
20 vector<ofColor> pixCols;
21
22 ofColor col(255,0,0);
23 for(int i=0; i<9;i++){
24 col.setHue(params[i]*2);
25 pixCols.push_back(col);
26 }
27 return pixCols;
28 }
14 //--------------------------------------------------------------------------- 29 //---------------------------------------------------------------------------
15 void Preset::draw(){ 30 void Preset::draw(){
16 // 31 // TODO bit stupid actually. make it a shaded ball / dot
32
17 int sz = 2; 33 int sz = 2;
18 TwoVector pos = theGridView.coordToPixel(coordinates); // euch -rely on grid view? 34 TwoVector pos = theGridView.coordToPixel(coordinates); // euch -rely on grid view?
19 35
20 if(pixVals.size() < 9){ 36 if(pixVals.size() < 9){
21 cout << "NO PRESET PIC\n"; 37 cout << "NO PRESET PIC\n";
53 // left 69 // left
54 ofSetColor(pixVals[8]); 70 ofSetColor(pixVals[8]);
55 ofRect(pos.x-sz, pos.y-sz,sz,sz); 71 ofRect(pos.x-sz, pos.y-sz,sz,sz);
56 }; 72 };
57 //--------------------------------------------------------------------------- 73 //---------------------------------------------------------------------------
74 Json::Value Preset::presetToJson(){
75 // create the string for this instance of Preset object
76
77 Json::Value presetVal;
78
79 presetVal["creatorUserName"] = creatorUserName;
80 presetVal["creatorDeviceID"] = creatorDeviceID;
81 presetVal["creationTime"] = creationTime;
82 presetVal["name"] = name;
83 presetVal["coordinates"]["x"] = coordinates.x;
84 presetVal["coordinates"]["y"] = coordinates.y;
85
86
87 return presetVal;
88 }
89 //---------------------------------------------------------------------------
58 PresetManager::PresetManager(){ 90 PresetManager::PresetManager(){
59 timesOpened = 0; 91 timesOpened = 0;
60 nextID = 0; 92 nextID = 0;
61 93
62 string ts = ofGetTimestampString(); 94 string ts = ofGetTimestampString();
63 cout << "ofGetTimestampString: " << ts << '\n'; 95 cout << "ofGetTimestampString: " << ts << '\n';
64 } 96 }
65 97 //---------------------------------------------------------------------------
98 Json::Value PresetManager::allPresetsToJson(){
99 Json::Value root;
100
101 // use jsoncpp
102 vector<Preset *>::iterator presetIter;
103
104 root["something"] = 23;
105
106 int i = 0;
107 for(presetIter = thePresets.begin(); presetIter < thePresets.end(); presetIter++){
108 root["presets"][i] = (*presetIter)->presetToJson();
109 i++;
110 }
111
112 return root;
113 }
114 //---------------------------------------------------------------------------
115 void PresetManager::readJsonToPresets(const string &jsonFile){
116 Json::Value root;
117 Json::Reader reader;
118
119
120 ifstream theFile(jsonFile.c_str());
121 stringstream fileText;
122 string line;
123 if(!theFile){
124 cout<<"can't find preset file \n";
125
126 }else{
127
128 while(theFile){
129 theFile >> line;
130 cout << line;
131 fileText << line;
132
133 }
134
135 theFile.close();
136 }
137
138 bool parsingSuccessful = reader.parse( fileText.str(), root );
139
140 if ( !parsingSuccessful )
141 {
142 // report to the user the failure and their locations in the document.
143 std::cout << "Failed to parse preset JSON\n"
144 << reader.getFormattedErrorMessages();
145 return;
146 }
147
148 // now put into variables
149 const Json::Value jpresets = root["presets"];
150
151 for ( int index = 0; index < jpresets.size(); ++index ) thePresets.push_back(new Preset(jpresets[index]));
152
153 //printAll();
154
155 }
66 //--------------------------------------------------------------------------- 156 //---------------------------------------------------------------------------
67 void PresetManager::printAll(){ 157 void PresetManager::printAll(){
68 cout << "ALL PRESETS: \n"; 158 cout << "ALL PRESETS: \n";
69 vector<Preset *>::iterator presetIter; 159 vector<Preset *>::iterator presetIter;
70 for(presetIter = thePresets.begin(); presetIter < thePresets.end(); presetIter++){ 160 for(presetIter = thePresets.begin(); presetIter < thePresets.end(); presetIter++){
71 cout << **presetIter; 161 cout << **presetIter;
72 162
73 } 163 }
74 } 164 }
75 //--------------------------------------------------------------------------- 165 //---------------------------------------------------------------------------
76 166 // when save button pressed
77 int PresetManager::addPreset(const TwoVector coord, const string name){ 167 int PresetManager::addPreset(const string name){
78 168
79 169
80 // check for same name 170 // check for same name
81 vector<Preset *>::iterator iter; 171 vector<Preset *>::iterator iter;
82 for(iter = thePresets.begin(); iter < thePresets.end(); iter++){ 172 for(iter = thePresets.begin(); iter < thePresets.end(); iter++){
90 if(name == ""){ 180 if(name == ""){
91 cout << "Please name preset\n"; 181 cout << "Please name preset\n";
92 return -2; 182 return -2;
93 183
94 } 184 }
95 185 // yuk shouldn't have to know about eventlogger and grid view...
96 // make piccy 186 thePresets.push_back(new Preset(theGridView.getCoord(), name,nextID, eventLogger.userName, eventLogger.deviceID));
97 vector<ofColor> presetPicture = makePresetPicture(coord);
98 thePresets.push_back(new Preset(coord, name,nextID, presetPicture));
99 187
100 // if ok 188 // if ok
101 return nextID++; 189 return nextID++;
102 } 190 }
103 //--------------------------------------------------------------------------- 191
104 vector<ofColor> PresetManager::makePresetPicture(TwoVector coord){
105 // convert midi parameters to a nice piccy
106 vector<int> params = theGridView.calculateParamsFromCoord(coord);
107 vector<ofColor> pixCols;
108
109 ofColor col(255,0,0);
110 for(int i=0; i<9;i++){
111 col.setHue(params[i]*2);
112 pixCols.push_back(col);
113 }
114 return pixCols;
115 }
116 //--------------------------------------------------------------------------- 192 //---------------------------------------------------------------------------
117 193
118 int PresetManager::loadPreset(const TwoVector coord, const string name, long long stime){ 194 int PresetManager::loadPreset(const TwoVector coord, const string name, long long stime){
119 vector<ofColor> presetPicture = makePresetPicture(coord); // or just store it?? 195 thePresets.push_back(new Preset(coord, name,nextID, stime));
120 thePresets.push_back(new Preset(coord, name,nextID, stime, presetPicture));
121 196
122 // if ok 197 // if ok
123 return nextID++; 198 return nextID++;
124 } 199 }
125 //--------------------------------------------------------------------------- 200 //---------------------------------------------------------------------------
152 (*piter)->draw(); 227 (*piter)->draw();
153 i++; 228 i++;
154 } 229 }
155 230
156 } 231 }
157 //--------------------------------------------------------------------------- 232 //----------------------------------------------cu-----------------------------
158 void PresetManager::startupLoadAll(){ 233 void PresetManager::startupLoadAll(){
159 // get stuff from file 234 // get stuff from file
160 // load file 235 // load file
161 236
162 string fname = ofxiPhoneGetDocumentsDirectory() + "presets.dat"; 237 string fname = ofxiPhoneGetDocumentsDirectory() + PRESET_FILENAME;
163 ifstream presetFile(fname.c_str()); 238
164 239 readJsonToPresets(fname);
165 // simply prints file 240
166
167 if(!presetFile){
168 cout<<"no preset file";
169 return;
170 }
171 while(presetFile >> *this);
172
173 //TODO get presets from online database...
174
175 presetFile.close();
176
177 timesOpened++; 241 timesOpened++;
178 } 242 }
243
179 //--------------------------------------------------------------------------- 244 //---------------------------------------------------------------------------
180 void PresetManager::exitAndSaveAll(){ 245 void PresetManager::exitAndSaveAll(){
181 ofFile presetFile(ofxiPhoneGetDocumentsDirectory() +"presets.dat",ofFile::WriteOnly); 246 ofFile presetFile(ofxiPhoneGetDocumentsDirectory() +PRESET_FILENAME,ofFile::WriteOnly);
182 247
183 cout << "Exit and save presets\n"; 248 // stick all the stuff in a json value
184 vector<Preset *>::iterator presetIter; 249 Json::Value root = allPresetsToJson();
185 250
186 for(presetIter = thePresets.begin(); presetIter < thePresets.end(); presetIter++){ 251 cout << root;
187 //vector<int> params; 252 presetFile << root;
188 //params = theGridView.calculateParamsFromCoord((*presetIter)->coordinates); 253
189 presetFile << **presetIter << "\n"; 254
190 }
191 presetFile.close();
192
193 // TODO dleete all the new events??
194
195
196 } 255 }
197 256
198 //--------------------------------------------------------------------------- 257 //---------------------------------------------------------------------------
199 void PresetManager::clearAll(){ 258 void PresetManager::clearAll(){
200 thePresets.clear(); 259 thePresets.clear();