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@16
|
29 #import "presetAlertViewController.h"
|
rt300@8
|
30
|
rt300@8
|
31
|
rt300@3
|
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@9
|
40 unsigned long long creationTime; // datetime that preset was created milliseconds
|
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@9
|
51 creationTime = (unsigned long long)(timemsd*1000);
|
rt300@9
|
52
|
rt300@9
|
53 cout << "Create preset sys time: " << creationTime << "\n";
|
rt300@8
|
54 pixVals = makePresetPicture(coordinates);
|
rt300@9
|
55
|
rt300@4
|
56 };
|
rt300@8
|
57 // from json value
|
rt300@8
|
58 Preset(Json::Value jval){
|
rt300@9
|
59
|
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@9
|
65 creationTime = jval["creationTime"].asLargestInt(); // dodgy?
|
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@9
|
84 string displayTextDescription(){ // eg: for grid hover
|
rt300@9
|
85 // objC to C to C++ bleurgh
|
rt300@9
|
86 NSDate *ocdate = [NSDate dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)(creationTime/1000.0)];
|
rt300@9
|
87 NSString *dateText = [ocdate description];
|
rt300@9
|
88 const char *date_str = [dateText cStringUsingEncoding:NSASCIIStringEncoding];
|
rt300@8
|
89 stringstream ss;
|
rt300@26
|
90 ss << "Name: \t" << name << "\nCreation time: \t" << date_str << "\nCreator: \t" << creatorUserName << '\n';
|
rt300@8
|
91 return ss.str();
|
rt300@8
|
92
|
rt300@8
|
93 }
|
rt300@0
|
94 };
|
rt300@3
|
95
|
rt300@3
|
96 //---------------------------------------------------------------------------
|
rt300@0
|
97 class PresetManager{
|
rt300@0
|
98 public:
|
rt300@0
|
99 int nextID;
|
rt300@3
|
100 int timesOpened;
|
rt300@22
|
101 bool presetAlertShowing;
|
rt300@22
|
102
|
rt300@0
|
103 // names values
|
rt300@0
|
104 // check if already there
|
rt300@0
|
105 // find and return all(?) presets within a certain coordinate range
|
rt300@27
|
106 vector<Preset *> thePresets;
|
rt300@0
|
107
|
rt300@27
|
108 int addPreset(string name);
|
rt300@4
|
109 int loadPreset(const TwoVector coord, const string name, long long stime);
|
rt300@4
|
110
|
rt300@0
|
111 TwoVector recallPreset(int presetID); // by name ? id?
|
rt300@0
|
112 TwoVector recallPreset(string name); // by name ? id?
|
rt300@5
|
113 vector<Preset *> getPresetsInRange(TwoVector min, TwoVector max);
|
rt300@5
|
114 void drawPresetsInRange(const TwoVector min, const TwoVector max);
|
rt300@4
|
115 void printAll();
|
rt300@0
|
116
|
rt300@30
|
117 void startLoadAll(); // get stuff from XML
|
rt300@0
|
118 void exitAndSaveAll(); // save to XML, delete presets array (?)
|
rt300@5
|
119 void clearAll();
|
rt300@8
|
120 Json::Value allPresetsToJson();
|
rt300@8
|
121 void readJsonToPresets(const string &jsonFile);
|
rt300@9
|
122 void showNameDialog();
|
rt300@0
|
123 PresetManager();
|
rt300@0
|
124 };
|
rt300@0
|
125
|
rt300@4
|
126
|
rt300@4
|
127 //---------------------------------------------------------------------------
|
rt300@8
|
128 // this is the function that 'saves' a single preset as formatted text
|
rt300@8
|
129 // replaced with presetToJson
|
rt300@4
|
130 inline ostream& operator<<(ostream & os, const Preset& p){
|
rt300@4
|
131 os.setf(ios_base::fixed,ios_base::floatfield);
|
rt300@4
|
132 os.precision(1);
|
rt300@4
|
133
|
rt300@8
|
134 // change this to JSON
|
rt300@8
|
135
|
rt300@8
|
136 os << p.creationTime << p.coordinates.x << ',' << p.coordinates.y << '\n';
|
rt300@4
|
137
|
rt300@4
|
138 return os;
|
rt300@4
|
139 }
|
rt300@4
|
140
|
rt300@8
|
141 //---------------------------------------------------------------------------
|
rt300@8
|
142 // this is the function that 'reads' all presets as formatted text
|
rt300@8
|
143 // replaced with jsonFromPreset or somesuch
|
rt300@4
|
144 inline istream& operator>>(istream & is, PresetManager& p)
|
rt300@4
|
145 {
|
rt300@4
|
146 //um
|
rt300@4
|
147 string pname = "BLANK";
|
rt300@4
|
148 char delim;
|
rt300@4
|
149 double px, py;
|
rt300@4
|
150 long long stime;
|
rt300@4
|
151
|
rt300@4
|
152
|
rt300@4
|
153 is.setf(ios_base::fixed,ios_base::floatfield);
|
rt300@4
|
154 is.precision(1);
|
rt300@4
|
155
|
rt300@4
|
156 is >> stime >> delim >> px >> delim >> py;
|
rt300@4
|
157 if(!is){
|
rt300@4
|
158 return(is);
|
rt300@4
|
159 }
|
rt300@4
|
160 TwoVector pcoord(px,py);
|
rt300@7
|
161 p.loadPreset(pcoord, pname, stime);
|
rt300@4
|
162 return(is);
|
rt300@4
|
163 }
|
rt300@4
|
164
|
rt300@0
|
165 #endif /* defined(__oscSenderExample__presetManager__) */
|