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