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@46
|
89 presetVal["whichSequence"] = whichSequence;
|
rt300@8
|
90
|
rt300@8
|
91
|
rt300@8
|
92 return presetVal;
|
rt300@8
|
93 }
|
rt300@8
|
94 //---------------------------------------------------------------------------
|
rt300@0
|
95 PresetManager::PresetManager(){
|
rt300@3
|
96 timesOpened = 0;
|
rt300@4
|
97 nextID = 0;
|
rt300@8
|
98
|
rt300@7
|
99 string ts = ofGetTimestampString();
|
rt300@22
|
100
|
rt300@22
|
101 presetAlertShowing = false;
|
rt300@22
|
102
|
rt300@22
|
103
|
rt300@7
|
104 cout << "ofGetTimestampString: " << ts << '\n';
|
rt300@4
|
105 }
|
rt300@8
|
106 //---------------------------------------------------------------------------
|
rt300@8
|
107 Json::Value PresetManager::allPresetsToJson(){
|
rt300@8
|
108 Json::Value root;
|
rt300@8
|
109
|
rt300@8
|
110 // use jsoncpp
|
rt300@44
|
111 vector<Preset>::iterator presetIter;
|
rt300@8
|
112
|
rt300@8
|
113 int i = 0;
|
rt300@8
|
114 for(presetIter = thePresets.begin(); presetIter < thePresets.end(); presetIter++){
|
rt300@38
|
115 // only save the preset to file if it was ours... (other users and defaults saved elewhere)
|
rt300@44
|
116 if((*presetIter).creatorDeviceID == eventLogger.deviceID){
|
rt300@44
|
117 root["presets"][i] = (*presetIter).presetToJson();
|
rt300@38
|
118 i++;
|
rt300@38
|
119 }
|
rt300@8
|
120 }
|
rt300@27
|
121
|
rt300@8
|
122 return root;
|
rt300@8
|
123 }
|
rt300@8
|
124 //---------------------------------------------------------------------------
|
rt300@8
|
125 void PresetManager::readJsonToPresets(const string &jsonFile){
|
rt300@8
|
126 Json::Value root;
|
rt300@8
|
127 Json::Reader reader;
|
rt300@8
|
128
|
rt300@8
|
129
|
rt300@8
|
130 ifstream theFile(jsonFile.c_str());
|
rt300@8
|
131 stringstream fileText;
|
rt300@8
|
132 string line;
|
rt300@8
|
133 if(!theFile){
|
rt300@35
|
134 cout<<"can't find preset file: " << jsonFile.c_str() << "\n";
|
rt300@22
|
135 return;
|
rt300@8
|
136 }else{
|
rt300@8
|
137
|
rt300@8
|
138 while(theFile){
|
rt300@8
|
139 theFile >> line;
|
rt300@9
|
140 // cout << line << "\n"; // lots?
|
rt300@8
|
141 fileText << line;
|
rt300@8
|
142
|
rt300@8
|
143 }
|
rt300@8
|
144
|
rt300@8
|
145 theFile.close();
|
rt300@8
|
146 }
|
rt300@8
|
147
|
rt300@8
|
148 bool parsingSuccessful = reader.parse( fileText.str(), root );
|
rt300@8
|
149
|
rt300@8
|
150 if ( !parsingSuccessful )
|
rt300@8
|
151 {
|
rt300@8
|
152 // report to the user the failure and their locations in the document.
|
rt300@22
|
153 std::cout << "Failed to parse preset JSON: \n"
|
rt300@8
|
154 << reader.getFormattedErrorMessages();
|
rt300@8
|
155 return;
|
rt300@8
|
156 }
|
rt300@8
|
157
|
rt300@8
|
158 // now put into variables
|
rt300@8
|
159 const Json::Value jpresets = root["presets"];
|
rt300@8
|
160
|
rt300@44
|
161 for ( int index = 0; index < jpresets.size(); ++index ) thePresets.push_back(Preset(jpresets[index]));
|
rt300@8
|
162
|
rt300@8
|
163 //printAll();
|
rt300@8
|
164
|
rt300@8
|
165 }
|
rt300@4
|
166 //---------------------------------------------------------------------------
|
rt300@4
|
167 void PresetManager::printAll(){
|
rt300@30
|
168 cout << "----------------ALL PRESETS-------------: \n";
|
rt300@30
|
169 cout << allPresetsToJson() << "\n";
|
rt300@0
|
170 }
|
rt300@25
|
171 //---------------------------------------------------------------------------
|
rt300@9
|
172 void PresetManager::showNameDialog(){
|
rt300@29
|
173 if(!presetAlertViewController.alertShowing){ // this is to stop wierd infinite loop in ios5 (simulator)
|
rt300@22
|
174 presetAlertShowing = true;
|
rt300@22
|
175 [presetAlertViewController showPresetNamePrompt];
|
rt300@22
|
176
|
rt300@22
|
177 }
|
rt300@22
|
178
|
rt300@9
|
179 }
|
rt300@0
|
180 //---------------------------------------------------------------------------
|
rt300@8
|
181 // when save button pressed
|
rt300@8
|
182 int PresetManager::addPreset(const string name){
|
rt300@3
|
183
|
rt300@22
|
184 presetAlertShowing = false;
|
rt300@0
|
185 // check for same name
|
rt300@0
|
186 vector<Preset *>::iterator iter;
|
rt300@31
|
187 /*
|
rt300@0
|
188 for(iter = thePresets.begin(); iter < thePresets.end(); iter++){
|
rt300@0
|
189 if ((*iter)->name == name){
|
rt300@0
|
190 cout << " Preset by that name exists\n";
|
rt300@0
|
191
|
rt300@0
|
192 // use exceptions!
|
rt300@0
|
193 return -1;
|
rt300@0
|
194 }
|
rt300@0
|
195 }
|
rt300@31
|
196
|
rt300@0
|
197 if(name == ""){
|
rt300@0
|
198 cout << "Please name preset\n";
|
rt300@0
|
199 return -2;
|
rt300@0
|
200
|
rt300@0
|
201 }
|
rt300@31
|
202 */
|
rt300@31
|
203 // hmm shouldn't have to know about eventlogger and grid view...
|
rt300@37
|
204
|
rt300@38
|
205 // get the preset coord from the grid
|
rt300@39
|
206 TwoVector coord = theGridView.getCoordForPresetSave();
|
rt300@38
|
207
|
rt300@38
|
208
|
rt300@46
|
209 thePresets.push_back(Preset(coord, name,nextID, eventLogger.userName, eventLogger.deviceID, ((testApp*)ofGetAppPtr())->currentSequence));
|
rt300@38
|
210 eventLogger.logEvent(SAVE_PRESET, coord);
|
rt300@9
|
211 // poke grid view to get it to show details
|
rt300@9
|
212 theGridView.snapCheck();
|
rt300@0
|
213 // if ok
|
rt300@0
|
214 return nextID++;
|
rt300@0
|
215 }
|
rt300@8
|
216
|
rt300@5
|
217 //---------------------------------------------------------------------------
|
rt300@5
|
218 vector<Preset *> PresetManager::getPresetsInRange(const TwoVector min, const TwoVector max){
|
rt300@0
|
219 //return all the coordinates. oh and names (displayed at certain scales?).
|
rt300@3
|
220
|
rt300@3
|
221 // TODO INEFFICIENT FOR LOTS OF PRESETS. CALLED EVERY GRAPHICS UPDATE!
|
rt300@3
|
222 // so: put burden on saving rather than retrieving, make into list and index using coordinates
|
rt300@3
|
223
|
rt300@5
|
224 vector<Preset *> results;
|
rt300@44
|
225 vector<Preset>::iterator presetIter;
|
rt300@3
|
226 for(presetIter = thePresets.begin(); presetIter < thePresets.end(); presetIter++){
|
rt300@44
|
227 if( ((*presetIter).coordinates.x > min.x ) && ((*presetIter).coordinates.y > min.y ) && ((*presetIter).coordinates.x < max.x ) && ((*presetIter).coordinates.y < max.y )){
|
rt300@44
|
228 results.push_back(&(*presetIter));
|
rt300@3
|
229 }
|
rt300@3
|
230
|
rt300@3
|
231 }
|
rt300@3
|
232 return results;
|
rt300@0
|
233
|
rt300@0
|
234 }
|
rt300@5
|
235
|
rt300@5
|
236
|
rt300@5
|
237 //---------------------------------------------------------------------------
|
rt300@5
|
238 void PresetManager::drawPresetsInRange(const TwoVector min, const TwoVector max){
|
rt300@5
|
239 vector<Preset *> pInRange = getPresetsInRange(min, max);
|
rt300@5
|
240 vector<Preset *>::iterator piter;
|
rt300@5
|
241 int i = 0;
|
rt300@5
|
242 for(piter = pInRange.begin(); piter < pInRange.end(); piter++){
|
rt300@5
|
243
|
rt300@5
|
244 (*piter)->draw();
|
rt300@5
|
245 i++;
|
rt300@5
|
246 }
|
rt300@5
|
247
|
rt300@5
|
248 }
|
rt300@8
|
249 //----------------------------------------------cu-----------------------------
|
rt300@30
|
250 void PresetManager::startLoadAll(){
|
rt300@8
|
251 // get stuff from file
|
rt300@3
|
252 // load file
|
rt300@3
|
253
|
rt300@8
|
254 string fname = ofxiPhoneGetDocumentsDirectory() + PRESET_FILENAME;
|
rt300@35
|
255 readJsonToPresets(fname);
|
rt300@5
|
256
|
rt300@46
|
257 string fullpath = ofFilePath::getAbsolutePath(ofToDataPath(FACTORY_PRESET_FILENAME));
|
rt300@35
|
258 string file = ofFilePath::getFileName(fullpath);
|
rt300@35
|
259 string folder = ofFilePath::getEnclosingDirectory(fullpath);
|
rt300@35
|
260
|
rt300@35
|
261 readJsonToPresets(fullpath);
|
rt300@7
|
262
|
rt300@3
|
263 timesOpened++;
|
rt300@3
|
264 }
|
rt300@8
|
265
|
rt300@3
|
266 //---------------------------------------------------------------------------
|
rt300@3
|
267 void PresetManager::exitAndSaveAll(){
|
rt300@8
|
268 ofFile presetFile(ofxiPhoneGetDocumentsDirectory() +PRESET_FILENAME,ofFile::WriteOnly);
|
rt300@4
|
269
|
rt300@8
|
270 // stick all the stuff in a json value
|
rt300@8
|
271 Json::Value root = allPresetsToJson();
|
rt300@5
|
272
|
rt300@8
|
273 cout << root;
|
rt300@8
|
274 presetFile << root;
|
rt300@29
|
275
|
rt300@3
|
276 }
|
rt300@32
|
277 //---------------------------------------------------------------------------
|
rt300@32
|
278 void PresetManager::saveSessionToFile(string userName){ // for supervised newUser()
|
rt300@32
|
279 ofFile presetFile(ofxiPhoneGetDocumentsDirectory() + userName + '_' + PRESET_FILENAME,ofFile::WriteOnly);
|
rt300@32
|
280
|
rt300@32
|
281 // stick all the stuff in a json value
|
rt300@32
|
282 Json::Value root = allPresetsToJson();
|
rt300@32
|
283
|
rt300@32
|
284 cout << root;
|
rt300@32
|
285 presetFile << root;
|
rt300@32
|
286
|
rt300@32
|
287 }
|
rt300@35
|
288
|
rt300@5
|
289 //---------------------------------------------------------------------------
|
rt300@5
|
290 void PresetManager::clearAll(){
|
rt300@5
|
291 thePresets.clear();
|
rt300@5
|
292 }
|
rt300@3
|
293 //---------------------------------------------------------------------------
|
rt300@0
|
294 //---------------------------------------------------------------------------
|
rt300@0
|
295 //---------------------------------------------------------------------------
|
rt300@0
|
296 //---------------------------------------------------------------------------
|
rt300@0
|
297 //--------------------------------------------------------------------------- |