view presetManager.h @ 45:c2fffc8ea84d

10 timbre params.
author Robert Tubb <rt300@eecs.qmul.ac.uk>
date Tue, 23 Apr 2013 18:29:55 +0100
parents a1e75b94c505
children 1e266647840d
line wrap: on
line source
//
//  presetManager.h
//  oscSenderExample
//
//  Created by Robert Tubb on 07/11/2012.
//
//

// defines:
//      PresetManager
// and Preset

#ifndef __oscSenderExample__presetManager__
#define __oscSenderExample__presetManager__

#define PRESET_FILENAME "presets.json"
#define PILOT_PRESET_FILENAME "pilot_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"
#include <ctime>

#import "presetAlertViewController.h"

//---------------------------------------------------------------------------
class Preset{
public:
    // important details - these saved to file (uploaded?)
    string          creatorUserName;
    unsigned int    creatorDeviceID; // unique user device ID
    string          name; // name of preset
    unsigned long long       creationTime; // datetime that preset was created milliseconds
    vector<ofColor> pixVals; // hmmm
    TwoVector       coordinates;  // position on grid
    vector<int>     params;         // actual parameters???
    int whichSequence;
    // not so important - not uploaded
    
    // from save button press
    Preset(TwoVector acoord, string aname,int aID, string un, unsigned int uid){
        coordinates = acoord;
        name = aname;
        creatorUserName = un;
        creatorDeviceID = uid;
        double timemsd = [NSDate timeIntervalSinceReferenceDate];
        creationTime = (unsigned long long)(timemsd*1000);

        cout << "Create preset sys time: " << creationTime << "\n";
        pixVals = makePresetPicture(coordinates);
        //whichSequence = seq;
    
    };
    // from json value
    Preset(Json::Value jval){

        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(); // dodgy?
        pixVals = makePresetPicture(coordinates);

    }
    // from preset file load
    Preset(TwoVector acoord, string aname,int aID, long long stime){
        coordinates = acoord;
        name = aname;
        creationTime = stime;
        pixVals = makePresetPicture(coordinates);
        
    };
    
    // from download request??
    
    void draw();
    Json::Value presetToJson();
    vector<ofColor> makePresetPicture(TwoVector coord);
    
    string displayTextDescription(){ // eg: for grid hover
        // objC to C to C++ bleurgh
        NSDate *ocdate = [NSDate dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)(creationTime/1000.0)];
        NSString *dateText = [ocdate description];
        const char *date_str = [dateText cStringUsingEncoding:NSASCIIStringEncoding];
        stringstream ss;
        ss << "Name: \t" << name << "\nCreation time: \t" << date_str << "\nCreator: \t" << creatorUserName << '\n';
        return ss.str();
        
    }
};

//---------------------------------------------------------------------------
class PresetManager{
public:
    int nextID;
    int timesOpened;
    bool presetAlertShowing;
    
    // names values
    // check if already there
    // find and return all(?) presets within a certain coordinate range
    vector<Preset> thePresets; 
    
    int addPreset(string name);
    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);
    void printAll();
    
    void startLoadAll();  // get stuff from XML
    void exitAndSaveAll();  // save to XML, delete presets array (?)
    void clearAll();
    Json::Value  allPresetsToJson();
    void readJsonToPresets(const string &jsonFile);
    void showNameDialog();
    PresetManager();
    void saveSessionToFile(string userName);
};


//---------------------------------------------------------------------------
// 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);
    
    // 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
        string pname = "BLANK";
        char delim;
        double px, py;
        long long stime;
        
        
        is.setf(ios_base::fixed,ios_base::floatfield);
        is.precision(1);
        
        is >> stime >> delim >> px >> delim >> py;
        if(!is){
            return(is);
        }
        TwoVector pcoord(px,py);
        p.loadPreset(pcoord, pname, stime);
        return(is);
    }
    
#endif /* defined(__oscSenderExample__presetManager__) */