diff 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
line wrap: on
line diff
--- a/presetManager.h	Thu Dec 06 18:26:51 2012 +0000
+++ b/presetManager.h	Thu Jan 10 18:24:26 2013 +0000
@@ -6,65 +6,89 @@
 //
 //
 
+// defines:
+//      PresetManager
+// and Preset
+
 #ifndef __oscSenderExample__presetManager__
 #define __oscSenderExample__presetManager__
 
+#define PRESET_FILENAME "presets.json"
+
 #include <iostream>
+#include <string>
 #include "ofMain.h"
 #include "ofxiPhone.h"
 #include "ofxiPhoneExtras.h"
 #include "2dvector.h"
 #include "grid.h"
+#include "eventLogger.h"
+#include "json.h"
+
+#import "iViewController.h"
+
+
 
 
 //---------------------------------------------------------------------------
 class Preset{
 public:
-    string userName;
-    int presetID;
-    string name;
-    long long savetime;
-    vector<ofColor> pixVals;
-    double timemsd = [NSDate timeIntervalSinceReferenceDate];
-    
-    TwoVector coordinates;
+    // important details - these saved to file (uploaded?)
+    string          creatorUserName;
+    unsigned int    creatorDeviceID; // unique user device ID
+    string          name; // name of preset
+    long long       creationTime; // datetime that preset was created
+    vector<ofColor> pixVals; // hmmm
+    TwoVector       coordinates;  // position on grid
     
     // from save button press
-    Preset(TwoVector acoord, string aname,int aID, const vector<ofColor> & pixCols){
+    Preset(TwoVector acoord, string aname,int aID, string un, unsigned int uid){
         coordinates = acoord;
         name = aname;
-        presetID = aID;
+        creatorUserName = un;
+        creatorDeviceID = uid;
+        double timemsd = [NSDate timeIntervalSinceReferenceDate];
+        creationTime = (long long)(timemsd*1000);
+        pixVals = makePresetPicture(coordinates);
         
-        timemsd = [NSDate timeIntervalSinceReferenceDate];
-        savetime = (long long)(timemsd*1000);
-        pixVals = pixCols;
-        
-
-    };
-    Preset(int aID){
-        coordinates.setCoord(0.0,0.0);
-        name = "Blank";
-        presetID = aID;
-        timemsd = [NSDate timeIntervalSinceReferenceDate];
-        savetime = (long long)(timemsd*1000);
         
     };
+    // from json value
+    Preset(Json::Value jval){
+        
+        // CRAHSED!!!!!
+        name = jval["name"].asString();
+        creatorUserName = jval["creatorUserName"].asString();
+        creatorDeviceID = jval["creatorDeviceID"].asUInt();
+        coordinates.x = jval["coordinates"].get("x", 0.0).asFloat();
+        coordinates.y = jval["coordinates"].get("y", 0.0).asFloat();
+        creationTime = jval["creationTime"].asLargestInt();
+        pixVals = makePresetPicture(coordinates);
 
-    // from file load
-    Preset(TwoVector acoord, string aname,int aID, long long stime, const vector<ofColor> & pixCols){
+    }
+    // from preset file load
+    Preset(TwoVector acoord, string aname,int aID, long long stime){
         coordinates = acoord;
         name = aname;
-        presetID = aID;
-        savetime = stime;
-        pixVals = pixCols;
+        creationTime = stime;
+        pixVals = makePresetPicture(coordinates);
         
     };
     
+    // from download request??
+    
     void draw();
-
+    Json::Value presetToJson();
+    vector<ofColor> makePresetPicture(TwoVector coord);
+    
+    string displayTextDescription(){
+        stringstream ss;
+        ss << "Name: \t" << name << "\nCreation time: \t" << creationTime << "\nCreator: \t" << creatorUserName <<  "\nCreator ID: \t" << creatorDeviceID << '\n';
+        return ss.str();
+        
+    }
 };
 
-
 //---------------------------------------------------------------------------
 class PresetManager{
 public:
@@ -75,35 +99,42 @@
     // find and return all(?) presets within a certain coordinate range
     vector<Preset *> thePresets; // we want vector ? or list? pointers using new?
     
-    int addPreset(TwoVector coord, string name); // returns id or negative error number
+    int addPreset(string name); // returns id or negative error number
     int loadPreset(const TwoVector coord, const string name, long long stime);
     
     TwoVector recallPreset(int presetID); // by name ? id?
     TwoVector recallPreset(string name); // by name ? id?
     vector<Preset *>  getPresetsInRange(TwoVector min, TwoVector max);
     void drawPresetsInRange(const TwoVector min, const TwoVector max);
-    vector<ofColor> makePresetPicture(TwoVector coord);
     void printAll();
     
     void startupLoadAll();  // get stuff from XML
     void exitAndSaveAll();  // save to XML, delete presets array (?)
     void clearAll();
+    Json::Value  allPresetsToJson();
+    void readJsonToPresets(const string &jsonFile);
     
     PresetManager();
 };
 
 
 //---------------------------------------------------------------------------
+// this is the function that 'saves' a single preset as formatted text
+// replaced with presetToJson
 inline ostream& operator<<(ostream & os, const Preset& p){
     os.setf(ios_base::fixed,ios_base::floatfield);
     os.precision(1);
     
-    os << p.savetime << ',' << p.coordinates.x << ',' << p.coordinates.y << '\n';
+    // change this to JSON
+    
+    os << p.creationTime << p.coordinates.x << ',' << p.coordinates.y << '\n';
     
     return os;
 }
     
-    //---------------------------------------------------------------------------
+//---------------------------------------------------------------------------
+// this is the function that 'reads' all presets as formatted text
+    // replaced with jsonFromPreset or somesuch
     inline istream& operator>>(istream & is, PresetManager& p)
     {
         //um