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