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