view MessageOrganiser.h @ 8:d59de9fd3496

Refactored messageController, ready for instroduction of other stages and tests.
author Robert Tubb <rt300@eecs.qmul.ac.uk>
date Fri, 17 Oct 2014 16:35:22 +0100
parents 92850a2b099c
children d5e928887f51
line wrap: on
line source
//
//  MessageOrganiser.h
//  tweakathlon
//
//  Created by Robert Tubb on 10/12/2013.
//
// This object handles the mapping from GUI to params
//
// and sends their messages to PD and eventLogger
#pragma once
#include "eventLogger.h"
#include <map.h>
#include <vector>
#include <string>
#include "boost/bind.hpp"
#include "boost/function.hpp"

#include <UIElement.h>
#include <Buttron.h>
#include <ButtronSlider.h>
#include <ButtronXY.h>
#include "TestController.h"
#include "timeController.h"
#include "PDSynthWrapper.h"
#include "ofxTimer.h"
#include "sliderPanel.h"
//#include "testApp.h"
#include "targetSymbol.h"
#include "3Dbox.h"
#include "TextPanel.h"
#include "CountdownText.h"
#include "buttonPanel.h"
#include "ExplorePresetManager.h"


// should be called TIMED TEST MESSAGE ORGANISER ?

// event logger needs to know
// which controls were showing in what mode
// which controls were mapped to what param
// what was the target sound params
// all the updates of control movements, submit, quit etc

// this is the bit that handles mapping from UI elements to synth i.e testApp DOESNT DO THAT

// has links to panel sliders can show hide them

// controls flow of stuff

//---------------------------------------------------------------------
//---------------------------------------------------------------------
extern TimeController timeController;

extern EventLogger eventLogger;

extern ExplorePresetManager expPresetManager;

typedef boost::function<void(void)> AppModeChangeFunction;

class MessageOrganiser {
    
public:
    void init(PDSynthWrapper& cs, PDSynthWrapper& ts){
        candidateSynth = cs;
        targetSynth = ts;
    }
    // could template for ui element type??
    void mapButtonToAction(UIElement* control, int mappingID){
        UICallbackFunction callbackF;
        callbackF = boost::bind(&MessageOrganiser::buttonPressCallback, this, _1,_2);
        control->addHandler(callbackF, mappingID);
        currentMapping.insert(std::pair<int,UIElement*>(mappingID,control));
    }
protected:

    PDSynthWrapper candidateSynth;
    PDSynthWrapper targetSynth;
    

    map<int,UIElement*> currentMapping; // could get more sophisticated if not 1-1 ?
    
    SliderPanel* panel;
    

    void sendSynthValuesAgain(){
        candidateSynth.sendAllParams();
        targetSynth.sendAllParams();
    };
 
    
    void setAllSlidersToValues(vector<int> values){
        for(int i = 0; i < values.size(); i++){
            setUIToParam(i, values[i]);
        }
    }
    // we want to set UI object
    void setUIToParam(int index, int value){ // e.g. from MIDI incoming, will handle both box and sliders...
        // theXY->setValueAndScale(candidateSynth.getParamValueForID(mids[i]), candidateSynth.getParamValueForID(mids[i+1]));
        UIElement* elem;
        // get the element
        if(panel->subElements.size() <= index){
            cout << "ERROR: index out of range for num sliders" << endl;
            return;
        }
        elem = panel->subElements[index];
        if ( elem->getType() == SLIDER){
            ButtronSlider* theSlider = (ButtronSlider*)elem;
            theSlider->setValueAndScale(value);
            
        }else{
            cout << "ERROR ERROR: ui type not handled by setUIToParam!" << endl;
        }
        
    };
    
    
    void mapControlToParam(UIElement* control, int mappingID){
        
        UICallbackFunction callbackF;
        callbackF = boost::bind(&MessageOrganiser::paramChangeCallback, this, _1,_2);
        control->addHandler(callbackF, mappingID);
        // put in our map so we can send param values to gui
        currentMapping.insert(std::pair<int,UIElement*>(mappingID,control));
        cout << " Mapped control to ID: " << mappingID << "Name: " << candidateSynth.getNameForMappingID(mappingID) << endl;
        control->setLabel(candidateSynth.getNameForMappingID(mappingID));
    };
    
    void mapXYToParams(ButtronXY* control, int mappingIDX, int mappingIDY){
        UICallbackFunction callback;
        
        callback = boost::bind(&MessageOrganiser::paramChangeCallback, this, _1,_2);
        
        control->addHandler(callback, mappingIDX, mappingIDY);
        
        // put in our map so we can send param values to gui
        //currentMapping.insert(std::pair<int,UIElement*>(mappingID,control));
        
        
        cout << " Mapped control to XID: " << mappingIDX << "Name: " << candidateSynth.getNameForMappingID(mappingIDX) << endl;
        cout << " Mapped control to YID: " << mappingIDY << "Name: " << candidateSynth.getNameForMappingID(mappingIDY) << endl;
        control->setLabel(candidateSynth.getNameForMappingID(mappingIDX), candidateSynth.getNameForMappingID(mappingIDY));
        
    };

    
    void mapControlToParam(UIElement* control, string paramName){
        // get mapping ID from synth
        int mappingID = candidateSynth.getMappingIDForName(paramName);
        mapControlToParam(control, mappingID);
        control->setLabel(paramName);
    };

    virtual void paramChangeCallback(int mappingID, int value){
        // virtual?
    };
    virtual void buttonPressCallback(int mappingID, int value){
        
    };

    
    void setSlidersToTarget(){
        // this will actually show sliders with target vals - for "memorisation" purposes mwa heh heh
        // get target values
        // set ui
        vector<int> vals = targetSynth.getAllParamValues();
        for(int i=1; i < vals.size(); i++){
            setUIToParam(i, vals[i]);
        }
    }

};