annotate presetManager.h @ 8:e2c6cfe8c6b7

JSON logs and presets.
author Robert Tubb <rt300@eecs.qmul.ac.uk>
date Thu, 10 Jan 2013 18:24:26 +0000
parents 845ea04f8e33
children 346807b47860
rev   line source
rt300@0 1 //
rt300@0 2 // presetManager.h
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@8 9 // defines:
rt300@8 10 // PresetManager
rt300@8 11 // and Preset
rt300@8 12
rt300@0 13 #ifndef __oscSenderExample__presetManager__
rt300@0 14 #define __oscSenderExample__presetManager__
rt300@0 15
rt300@8 16 #define PRESET_FILENAME "presets.json"
rt300@8 17
rt300@0 18 #include <iostream>
rt300@8 19 #include <string>
rt300@0 20 #include "ofMain.h"
rt300@0 21 #include "ofxiPhone.h"
rt300@0 22 #include "ofxiPhoneExtras.h"
rt300@0 23 #include "2dvector.h"
rt300@3 24 #include "grid.h"
rt300@8 25 #include "eventLogger.h"
rt300@8 26 #include "json.h"
rt300@8 27
rt300@8 28 #import "iViewController.h"
rt300@8 29
rt300@8 30
rt300@3 31
rt300@0 32
rt300@0 33 //---------------------------------------------------------------------------
rt300@0 34 class Preset{
rt300@3 35 public:
rt300@8 36 // important details - these saved to file (uploaded?)
rt300@8 37 string creatorUserName;
rt300@8 38 unsigned int creatorDeviceID; // unique user device ID
rt300@8 39 string name; // name of preset
rt300@8 40 long long creationTime; // datetime that preset was created
rt300@8 41 vector<ofColor> pixVals; // hmmm
rt300@8 42 TwoVector coordinates; // position on grid
rt300@3 43
rt300@5 44 // from save button press
rt300@8 45 Preset(TwoVector acoord, string aname,int aID, string un, unsigned int uid){
rt300@0 46 coordinates = acoord;
rt300@0 47 name = aname;
rt300@8 48 creatorUserName = un;
rt300@8 49 creatorDeviceID = uid;
rt300@8 50 double timemsd = [NSDate timeIntervalSinceReferenceDate];
rt300@8 51 creationTime = (long long)(timemsd*1000);
rt300@8 52 pixVals = makePresetPicture(coordinates);
rt300@4 53
rt300@4 54
rt300@4 55 };
rt300@8 56 // from json value
rt300@8 57 Preset(Json::Value jval){
rt300@8 58
rt300@8 59 // CRAHSED!!!!!
rt300@8 60 name = jval["name"].asString();
rt300@8 61 creatorUserName = jval["creatorUserName"].asString();
rt300@8 62 creatorDeviceID = jval["creatorDeviceID"].asUInt();
rt300@8 63 coordinates.x = jval["coordinates"].get("x", 0.0).asFloat();
rt300@8 64 coordinates.y = jval["coordinates"].get("y", 0.0).asFloat();
rt300@8 65 creationTime = jval["creationTime"].asLargestInt();
rt300@8 66 pixVals = makePresetPicture(coordinates);
rt300@4 67
rt300@8 68 }
rt300@8 69 // from preset file load
rt300@8 70 Preset(TwoVector acoord, string aname,int aID, long long stime){
rt300@4 71 coordinates = acoord;
rt300@4 72 name = aname;
rt300@8 73 creationTime = stime;
rt300@8 74 pixVals = makePresetPicture(coordinates);
rt300@0 75
rt300@0 76 };
rt300@5 77
rt300@8 78 // from download request??
rt300@8 79
rt300@5 80 void draw();
rt300@8 81 Json::Value presetToJson();
rt300@8 82 vector<ofColor> makePresetPicture(TwoVector coord);
rt300@8 83
rt300@8 84 string displayTextDescription(){
rt300@8 85 stringstream ss;
rt300@8 86 ss << "Name: \t" << name << "\nCreation time: \t" << creationTime << "\nCreator: \t" << creatorUserName << "\nCreator ID: \t" << creatorDeviceID << '\n';
rt300@8 87 return ss.str();
rt300@8 88
rt300@8 89 }
rt300@0 90 };
rt300@3 91
rt300@3 92 //---------------------------------------------------------------------------
rt300@0 93 class PresetManager{
rt300@0 94 public:
rt300@0 95 int nextID;
rt300@3 96 int timesOpened;
rt300@0 97 // names values
rt300@0 98 // check if already there
rt300@0 99 // find and return all(?) presets within a certain coordinate range
rt300@0 100 vector<Preset *> thePresets; // we want vector ? or list? pointers using new?
rt300@0 101
rt300@8 102 int addPreset(string name); // returns id or negative error number
rt300@4 103 int loadPreset(const TwoVector coord, const string name, long long stime);
rt300@4 104
rt300@0 105 TwoVector recallPreset(int presetID); // by name ? id?
rt300@0 106 TwoVector recallPreset(string name); // by name ? id?
rt300@5 107 vector<Preset *> getPresetsInRange(TwoVector min, TwoVector max);
rt300@5 108 void drawPresetsInRange(const TwoVector min, const TwoVector max);
rt300@4 109 void printAll();
rt300@0 110
rt300@0 111 void startupLoadAll(); // get stuff from XML
rt300@0 112 void exitAndSaveAll(); // save to XML, delete presets array (?)
rt300@5 113 void clearAll();
rt300@8 114 Json::Value allPresetsToJson();
rt300@8 115 void readJsonToPresets(const string &jsonFile);
rt300@0 116
rt300@0 117 PresetManager();
rt300@0 118 };
rt300@0 119
rt300@4 120
rt300@4 121 //---------------------------------------------------------------------------
rt300@8 122 // this is the function that 'saves' a single preset as formatted text
rt300@8 123 // replaced with presetToJson
rt300@4 124 inline ostream& operator<<(ostream & os, const Preset& p){
rt300@4 125 os.setf(ios_base::fixed,ios_base::floatfield);
rt300@4 126 os.precision(1);
rt300@4 127
rt300@8 128 // change this to JSON
rt300@8 129
rt300@8 130 os << p.creationTime << p.coordinates.x << ',' << p.coordinates.y << '\n';
rt300@4 131
rt300@4 132 return os;
rt300@4 133 }
rt300@4 134
rt300@8 135 //---------------------------------------------------------------------------
rt300@8 136 // this is the function that 'reads' all presets as formatted text
rt300@8 137 // replaced with jsonFromPreset or somesuch
rt300@4 138 inline istream& operator>>(istream & is, PresetManager& p)
rt300@4 139 {
rt300@4 140 //um
rt300@4 141 string pname = "BLANK";
rt300@4 142 char delim;
rt300@4 143 double px, py;
rt300@4 144 long long stime;
rt300@4 145
rt300@4 146
rt300@4 147 is.setf(ios_base::fixed,ios_base::floatfield);
rt300@4 148 is.precision(1);
rt300@4 149
rt300@4 150 is >> stime >> delim >> px >> delim >> py;
rt300@4 151 if(!is){
rt300@4 152 return(is);
rt300@4 153 }
rt300@4 154 TwoVector pcoord(px,py);
rt300@7 155 p.loadPreset(pcoord, pname, stime);
rt300@4 156 return(is);
rt300@4 157 }
rt300@4 158
rt300@0 159 #endif /* defined(__oscSenderExample__presetManager__) */