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