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@7
|
95 cout << "ofGetTimestampString: " << ts << '\n';
|
rt300@4
|
96 }
|
rt300@8
|
97 //---------------------------------------------------------------------------
|
rt300@8
|
98 Json::Value PresetManager::allPresetsToJson(){
|
rt300@8
|
99 Json::Value root;
|
rt300@8
|
100
|
rt300@8
|
101 // use jsoncpp
|
rt300@8
|
102 vector<Preset *>::iterator presetIter;
|
rt300@5
|
103
|
rt300@8
|
104 root["something"] = 23;
|
rt300@8
|
105
|
rt300@8
|
106 int i = 0;
|
rt300@8
|
107 for(presetIter = thePresets.begin(); presetIter < thePresets.end(); presetIter++){
|
rt300@8
|
108 root["presets"][i] = (*presetIter)->presetToJson();
|
rt300@8
|
109 i++;
|
rt300@8
|
110 }
|
rt300@8
|
111
|
rt300@8
|
112 return root;
|
rt300@8
|
113 }
|
rt300@8
|
114 //---------------------------------------------------------------------------
|
rt300@8
|
115 void PresetManager::readJsonToPresets(const string &jsonFile){
|
rt300@8
|
116 Json::Value root;
|
rt300@8
|
117 Json::Reader reader;
|
rt300@8
|
118
|
rt300@8
|
119
|
rt300@8
|
120 ifstream theFile(jsonFile.c_str());
|
rt300@8
|
121 stringstream fileText;
|
rt300@8
|
122 string line;
|
rt300@8
|
123 if(!theFile){
|
rt300@8
|
124 cout<<"can't find preset file \n";
|
rt300@8
|
125
|
rt300@8
|
126 }else{
|
rt300@8
|
127
|
rt300@8
|
128 while(theFile){
|
rt300@8
|
129 theFile >> line;
|
rt300@9
|
130 // cout << line << "\n"; // lots?
|
rt300@8
|
131 fileText << line;
|
rt300@8
|
132
|
rt300@8
|
133 }
|
rt300@8
|
134
|
rt300@8
|
135 theFile.close();
|
rt300@8
|
136 }
|
rt300@8
|
137
|
rt300@8
|
138 bool parsingSuccessful = reader.parse( fileText.str(), root );
|
rt300@8
|
139
|
rt300@8
|
140 if ( !parsingSuccessful )
|
rt300@8
|
141 {
|
rt300@8
|
142 // report to the user the failure and their locations in the document.
|
rt300@8
|
143 std::cout << "Failed to parse preset JSON\n"
|
rt300@8
|
144 << reader.getFormattedErrorMessages();
|
rt300@8
|
145 return;
|
rt300@8
|
146 }
|
rt300@8
|
147
|
rt300@8
|
148 // now put into variables
|
rt300@8
|
149 const Json::Value jpresets = root["presets"];
|
rt300@8
|
150
|
rt300@8
|
151 for ( int index = 0; index < jpresets.size(); ++index ) thePresets.push_back(new Preset(jpresets[index]));
|
rt300@8
|
152
|
rt300@8
|
153 //printAll();
|
rt300@8
|
154
|
rt300@8
|
155 }
|
rt300@4
|
156 //---------------------------------------------------------------------------
|
rt300@4
|
157 void PresetManager::printAll(){
|
rt300@4
|
158 cout << "ALL PRESETS: \n";
|
rt300@4
|
159 vector<Preset *>::iterator presetIter;
|
rt300@4
|
160 for(presetIter = thePresets.begin(); presetIter < thePresets.end(); presetIter++){
|
rt300@5
|
161 cout << **presetIter;
|
rt300@4
|
162
|
rt300@4
|
163 }
|
rt300@0
|
164 }
|
rt300@9
|
165 void PresetManager::showNameDialog(){
|
rt300@16
|
166 [presetAlertViewController showPresetNamePrompt];
|
rt300@9
|
167 }
|
rt300@0
|
168 //---------------------------------------------------------------------------
|
rt300@8
|
169 // when save button pressed
|
rt300@8
|
170 int PresetManager::addPreset(const string name){
|
rt300@3
|
171
|
rt300@3
|
172
|
rt300@0
|
173 // check for same name
|
rt300@0
|
174 vector<Preset *>::iterator iter;
|
rt300@0
|
175 for(iter = thePresets.begin(); iter < thePresets.end(); iter++){
|
rt300@0
|
176 if ((*iter)->name == name){
|
rt300@0
|
177 cout << " Preset by that name exists\n";
|
rt300@0
|
178
|
rt300@0
|
179 // use exceptions!
|
rt300@0
|
180 return -1;
|
rt300@0
|
181 }
|
rt300@0
|
182 }
|
rt300@0
|
183 if(name == ""){
|
rt300@0
|
184 cout << "Please name preset\n";
|
rt300@0
|
185 return -2;
|
rt300@0
|
186
|
rt300@0
|
187 }
|
rt300@8
|
188 // yuk shouldn't have to know about eventlogger and grid view...
|
rt300@8
|
189 thePresets.push_back(new Preset(theGridView.getCoord(), name,nextID, eventLogger.userName, eventLogger.deviceID));
|
rt300@0
|
190
|
rt300@9
|
191 // poke grid view to get it to show details
|
rt300@9
|
192 theGridView.snapCheck();
|
rt300@0
|
193 // if ok
|
rt300@0
|
194 return nextID++;
|
rt300@0
|
195 }
|
rt300@8
|
196
|
rt300@5
|
197 //---------------------------------------------------------------------------
|
rt300@4
|
198
|
rt300@4
|
199 int PresetManager::loadPreset(const TwoVector coord, const string name, long long stime){
|
rt300@8
|
200 thePresets.push_back(new Preset(coord, name,nextID, stime));
|
rt300@5
|
201
|
rt300@4
|
202 // if ok
|
rt300@4
|
203 return nextID++;
|
rt300@4
|
204 }
|
rt300@4
|
205 //---------------------------------------------------------------------------
|
rt300@5
|
206 vector<Preset *> PresetManager::getPresetsInRange(const TwoVector min, const TwoVector max){
|
rt300@0
|
207 //return all the coordinates. oh and names (displayed at certain scales?).
|
rt300@3
|
208
|
rt300@3
|
209 // TODO INEFFICIENT FOR LOTS OF PRESETS. CALLED EVERY GRAPHICS UPDATE!
|
rt300@3
|
210 // so: put burden on saving rather than retrieving, make into list and index using coordinates
|
rt300@3
|
211
|
rt300@5
|
212 vector<Preset *> results;
|
rt300@3
|
213 vector<Preset *>::iterator presetIter;
|
rt300@3
|
214 for(presetIter = thePresets.begin(); presetIter < thePresets.end(); presetIter++){
|
rt300@3
|
215 if( ((*presetIter)->coordinates.x > min.x ) && ((*presetIter)->coordinates.y > min.y ) && ((*presetIter)->coordinates.x < max.x ) && ((*presetIter)->coordinates.y < max.y )){
|
rt300@5
|
216 results.push_back(*presetIter);
|
rt300@3
|
217 }
|
rt300@3
|
218
|
rt300@3
|
219 }
|
rt300@3
|
220 return results;
|
rt300@0
|
221
|
rt300@0
|
222 }
|
rt300@5
|
223
|
rt300@5
|
224
|
rt300@5
|
225 //---------------------------------------------------------------------------
|
rt300@5
|
226 void PresetManager::drawPresetsInRange(const TwoVector min, const TwoVector max){
|
rt300@5
|
227 vector<Preset *> pInRange = getPresetsInRange(min, max);
|
rt300@5
|
228 vector<Preset *>::iterator piter;
|
rt300@5
|
229 int i = 0;
|
rt300@5
|
230 for(piter = pInRange.begin(); piter < pInRange.end(); piter++){
|
rt300@5
|
231
|
rt300@5
|
232 (*piter)->draw();
|
rt300@5
|
233 i++;
|
rt300@5
|
234 }
|
rt300@5
|
235
|
rt300@5
|
236 }
|
rt300@8
|
237 //----------------------------------------------cu-----------------------------
|
rt300@3
|
238 void PresetManager::startupLoadAll(){
|
rt300@8
|
239 // get stuff from file
|
rt300@3
|
240 // load file
|
rt300@3
|
241
|
rt300@8
|
242 string fname = ofxiPhoneGetDocumentsDirectory() + PRESET_FILENAME;
|
rt300@5
|
243
|
rt300@8
|
244 readJsonToPresets(fname);
|
rt300@7
|
245
|
rt300@3
|
246 timesOpened++;
|
rt300@3
|
247 }
|
rt300@8
|
248
|
rt300@3
|
249 //---------------------------------------------------------------------------
|
rt300@3
|
250 void PresetManager::exitAndSaveAll(){
|
rt300@8
|
251 ofFile presetFile(ofxiPhoneGetDocumentsDirectory() +PRESET_FILENAME,ofFile::WriteOnly);
|
rt300@4
|
252
|
rt300@8
|
253 // stick all the stuff in a json value
|
rt300@8
|
254 Json::Value root = allPresetsToJson();
|
rt300@5
|
255
|
rt300@8
|
256 cout << root;
|
rt300@8
|
257 presetFile << root;
|
rt300@8
|
258
|
rt300@5
|
259
|
rt300@3
|
260 }
|
rt300@5
|
261
|
rt300@5
|
262 //---------------------------------------------------------------------------
|
rt300@5
|
263 void PresetManager::clearAll(){
|
rt300@5
|
264 thePresets.clear();
|
rt300@5
|
265 }
|
rt300@3
|
266 //---------------------------------------------------------------------------
|
rt300@0
|
267 //---------------------------------------------------------------------------
|
rt300@0
|
268 //---------------------------------------------------------------------------
|
rt300@0
|
269 //---------------------------------------------------------------------------
|
rt300@0
|
270 //--------------------------------------------------------------------------- |