rt300@0: // rt300@0: // presetManager.h rt300@0: // oscSenderExample rt300@0: // rt300@0: // Created by Robert Tubb on 07/11/2012. rt300@0: // rt300@0: // rt300@0: rt300@8: // defines: rt300@8: // PresetManager rt300@8: // and Preset rt300@8: rt300@0: #ifndef __oscSenderExample__presetManager__ rt300@0: #define __oscSenderExample__presetManager__ rt300@0: rt300@8: #define PRESET_FILENAME "presets.json" rt300@8: rt300@0: #include rt300@8: #include rt300@0: #include "ofMain.h" rt300@0: #include "ofxiPhone.h" rt300@0: #include "ofxiPhoneExtras.h" rt300@0: #include "2dvector.h" rt300@3: #include "grid.h" rt300@8: #include "eventLogger.h" rt300@8: #include "json.h" rt300@8: rt300@8: #import "iViewController.h" rt300@8: rt300@8: rt300@3: rt300@0: rt300@0: //--------------------------------------------------------------------------- rt300@0: class Preset{ rt300@3: public: rt300@8: // important details - these saved to file (uploaded?) rt300@8: string creatorUserName; rt300@8: unsigned int creatorDeviceID; // unique user device ID rt300@8: string name; // name of preset rt300@8: long long creationTime; // datetime that preset was created rt300@8: vector pixVals; // hmmm rt300@8: TwoVector coordinates; // position on grid rt300@3: rt300@5: // from save button press rt300@8: Preset(TwoVector acoord, string aname,int aID, string un, unsigned int uid){ rt300@0: coordinates = acoord; rt300@0: name = aname; rt300@8: creatorUserName = un; rt300@8: creatorDeviceID = uid; rt300@8: double timemsd = [NSDate timeIntervalSinceReferenceDate]; rt300@8: creationTime = (long long)(timemsd*1000); rt300@8: pixVals = makePresetPicture(coordinates); rt300@4: rt300@4: rt300@4: }; rt300@8: // from json value rt300@8: Preset(Json::Value jval){ rt300@8: rt300@8: // CRAHSED!!!!! rt300@8: name = jval["name"].asString(); rt300@8: creatorUserName = jval["creatorUserName"].asString(); rt300@8: creatorDeviceID = jval["creatorDeviceID"].asUInt(); rt300@8: coordinates.x = jval["coordinates"].get("x", 0.0).asFloat(); rt300@8: coordinates.y = jval["coordinates"].get("y", 0.0).asFloat(); rt300@8: creationTime = jval["creationTime"].asLargestInt(); rt300@8: pixVals = makePresetPicture(coordinates); rt300@4: rt300@8: } rt300@8: // from preset file load rt300@8: Preset(TwoVector acoord, string aname,int aID, long long stime){ rt300@4: coordinates = acoord; rt300@4: name = aname; rt300@8: creationTime = stime; rt300@8: pixVals = makePresetPicture(coordinates); rt300@0: rt300@0: }; rt300@5: rt300@8: // from download request?? rt300@8: rt300@5: void draw(); rt300@8: Json::Value presetToJson(); rt300@8: vector makePresetPicture(TwoVector coord); rt300@8: rt300@8: string displayTextDescription(){ rt300@8: stringstream ss; rt300@8: ss << "Name: \t" << name << "\nCreation time: \t" << creationTime << "\nCreator: \t" << creatorUserName << "\nCreator ID: \t" << creatorDeviceID << '\n'; rt300@8: return ss.str(); rt300@8: rt300@8: } rt300@0: }; rt300@3: rt300@3: //--------------------------------------------------------------------------- rt300@0: class PresetManager{ rt300@0: public: rt300@0: int nextID; rt300@3: int timesOpened; rt300@0: // names values rt300@0: // check if already there rt300@0: // find and return all(?) presets within a certain coordinate range rt300@0: vector thePresets; // we want vector ? or list? pointers using new? rt300@0: rt300@8: int addPreset(string name); // returns id or negative error number rt300@4: int loadPreset(const TwoVector coord, const string name, long long stime); rt300@4: rt300@0: TwoVector recallPreset(int presetID); // by name ? id? rt300@0: TwoVector recallPreset(string name); // by name ? id? rt300@5: vector getPresetsInRange(TwoVector min, TwoVector max); rt300@5: void drawPresetsInRange(const TwoVector min, const TwoVector max); rt300@4: void printAll(); rt300@0: rt300@0: void startupLoadAll(); // get stuff from XML rt300@0: void exitAndSaveAll(); // save to XML, delete presets array (?) rt300@5: void clearAll(); rt300@8: Json::Value allPresetsToJson(); rt300@8: void readJsonToPresets(const string &jsonFile); rt300@0: rt300@0: PresetManager(); rt300@0: }; rt300@0: rt300@4: rt300@4: //--------------------------------------------------------------------------- rt300@8: // this is the function that 'saves' a single preset as formatted text rt300@8: // replaced with presetToJson rt300@4: inline ostream& operator<<(ostream & os, const Preset& p){ rt300@4: os.setf(ios_base::fixed,ios_base::floatfield); rt300@4: os.precision(1); rt300@4: rt300@8: // change this to JSON rt300@8: rt300@8: os << p.creationTime << p.coordinates.x << ',' << p.coordinates.y << '\n'; rt300@4: rt300@4: return os; rt300@4: } rt300@4: rt300@8: //--------------------------------------------------------------------------- rt300@8: // this is the function that 'reads' all presets as formatted text rt300@8: // replaced with jsonFromPreset or somesuch rt300@4: inline istream& operator>>(istream & is, PresetManager& p) rt300@4: { rt300@4: //um rt300@4: string pname = "BLANK"; rt300@4: char delim; rt300@4: double px, py; rt300@4: long long stime; rt300@4: rt300@4: rt300@4: is.setf(ios_base::fixed,ios_base::floatfield); rt300@4: is.precision(1); rt300@4: rt300@4: is >> stime >> delim >> px >> delim >> py; rt300@4: if(!is){ rt300@4: return(is); rt300@4: } rt300@4: TwoVector pcoord(px,py); rt300@7: p.loadPreset(pcoord, pname, stime); rt300@4: return(is); rt300@4: } rt300@4: rt300@0: #endif /* defined(__oscSenderExample__presetManager__) */