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