annotate presetManager.h @ 35:790939017078

V0.5. Now uses new continuous hilbert curve. HOORAY.
author Robert Tubb <rt300@eecs.qmul.ac.uk>
date Mon, 08 Apr 2013 17:23:13 +0100
parents ab7c86d0f3d8
children 0dfe9e0c01aa
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@35 17 #define PILOT_PRESET_FILENAME "pilot_presets.json"
rt300@8 18
rt300@0 19 #include <iostream>
rt300@8 20 #include <string>
rt300@0 21 #include "ofMain.h"
rt300@0 22 #include "ofxiPhone.h"
rt300@0 23 #include "ofxiPhoneExtras.h"
rt300@0 24 #include "2dvector.h"
rt300@3 25 #include "grid.h"
rt300@8 26 #include "eventLogger.h"
rt300@8 27 #include "json.h"
rt300@9 28 #include <ctime>
rt300@8 29
rt300@16 30 #import "presetAlertViewController.h"
rt300@8 31
rt300@0 32 //---------------------------------------------------------------------------
rt300@0 33 class Preset{
rt300@3 34 public:
rt300@8 35 // important details - these saved to file (uploaded?)
rt300@8 36 string creatorUserName;
rt300@8 37 unsigned int creatorDeviceID; // unique user device ID
rt300@8 38 string name; // name of preset
rt300@9 39 unsigned long long creationTime; // datetime that preset was created milliseconds
rt300@8 40 vector<ofColor> pixVals; // hmmm
rt300@8 41 TwoVector coordinates; // position on grid
rt300@3 42
rt300@5 43 // from save button press
rt300@8 44 Preset(TwoVector acoord, string aname,int aID, string un, unsigned int uid){
rt300@0 45 coordinates = acoord;
rt300@0 46 name = aname;
rt300@8 47 creatorUserName = un;
rt300@8 48 creatorDeviceID = uid;
rt300@8 49 double timemsd = [NSDate timeIntervalSinceReferenceDate];
rt300@9 50 creationTime = (unsigned long long)(timemsd*1000);
rt300@9 51
rt300@9 52 cout << "Create preset sys time: " << creationTime << "\n";
rt300@8 53 pixVals = makePresetPicture(coordinates);
rt300@9 54
rt300@4 55 };
rt300@8 56 // from json value
rt300@8 57 Preset(Json::Value jval){
rt300@9 58
rt300@8 59 name = jval["name"].asString();
rt300@8 60 creatorUserName = jval["creatorUserName"].asString();
rt300@8 61 creatorDeviceID = jval["creatorDeviceID"].asUInt();
rt300@8 62 coordinates.x = jval["coordinates"].get("x", 0.0).asFloat();
rt300@8 63 coordinates.y = jval["coordinates"].get("y", 0.0).asFloat();
rt300@9 64 creationTime = jval["creationTime"].asLargestInt(); // dodgy?
rt300@8 65 pixVals = makePresetPicture(coordinates);
rt300@4 66
rt300@8 67 }
rt300@8 68 // from preset file load
rt300@8 69 Preset(TwoVector acoord, string aname,int aID, long long stime){
rt300@4 70 coordinates = acoord;
rt300@4 71 name = aname;
rt300@8 72 creationTime = stime;
rt300@8 73 pixVals = makePresetPicture(coordinates);
rt300@0 74
rt300@0 75 };
rt300@5 76
rt300@8 77 // from download request??
rt300@8 78
rt300@5 79 void draw();
rt300@8 80 Json::Value presetToJson();
rt300@8 81 vector<ofColor> makePresetPicture(TwoVector coord);
rt300@8 82
rt300@9 83 string displayTextDescription(){ // eg: for grid hover
rt300@9 84 // objC to C to C++ bleurgh
rt300@9 85 NSDate *ocdate = [NSDate dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)(creationTime/1000.0)];
rt300@9 86 NSString *dateText = [ocdate description];
rt300@9 87 const char *date_str = [dateText cStringUsingEncoding:NSASCIIStringEncoding];
rt300@8 88 stringstream ss;
rt300@26 89 ss << "Name: \t" << name << "\nCreation time: \t" << date_str << "\nCreator: \t" << creatorUserName << '\n';
rt300@8 90 return ss.str();
rt300@8 91
rt300@8 92 }
rt300@0 93 };
rt300@3 94
rt300@3 95 //---------------------------------------------------------------------------
rt300@0 96 class PresetManager{
rt300@0 97 public:
rt300@0 98 int nextID;
rt300@3 99 int timesOpened;
rt300@22 100 bool presetAlertShowing;
rt300@22 101
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@27 105 vector<Preset *> thePresets;
rt300@0 106
rt300@27 107 int addPreset(string name);
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@30 116 void startLoadAll(); // 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@32 123 void saveSessionToFile(string userName);
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__) */