view TrainingMessageOrganiser.h @ 14:f83635861187

rewrote sequence controller. ticks show relevant UI modes. images show for preset textures (but greenish?)
author Robert Tubb <rt300@eecs.qmul.ac.uk>
date Tue, 21 Oct 2014 16:39:39 +0100
parents ab3e0e980c82
children 2da0350a4aa2
line wrap: on
line source
//
//  TrainingMessageOrganiser.h
//  riftathon
//
//  Created by Robert Tubb on 17/10/2014.
//
//

#ifndef __riftathon__TrainingMessageOrganiser__
#define __riftathon__TrainingMessageOrganiser__

#include <iostream>
#include "MessageOrganiser.h"
#include "SequenceController.h"
#include "globalVariables.h"
#include "IconPanel.h"

extern ExplorePresetManager expPresetManager;

class TrainingMessageOrganiser : public MessageOrganiser {
public:
    int numParamsToUse;
    void init( PDSynthWrapper& cs, PDSynthWrapper& ts){
        
        MessageOrganiser::init(cs,ts);
        
        TickListenerFunction callback;
        callback = boost::bind(&TrainingMessageOrganiser::onNextTick, this, _1);
        candidateSynth.registerForTicks(callback);
        
        numParamsToUse = 3;
        
    }
    
    void setupDefaultMapping(){
        vector<int> mappingIDsForChangeableParams = getMappingIDsFromSynths();
        
        controlPanelType cpt = REVISITABLE;
        vector<controllerType> elemList;
        for(int i = 0; i < numParamsToUse; i++){
            elemList.push_back(SLIDER);
        }
        
        vector<UIElement*> UIElemHandles = panel->generateControls(elemList, cpt);
        
        mapSlidersToParams(UIElemHandles, mappingIDsForChangeableParams);
        
        bottomPanel->show();
    }
    
    vector<int> getMappingIDsFromSynths(){
        vector<int> index;
        for(int i = 0; i < numParamsToUse; i++){
            index.push_back(i);
        }
        vector<int> mids = candidateSynth.getMappingIDForIndices(index);
        
        return mids;
    }
    
    void setupNewTest(){
        
        
    };
    
    void setIconPanel(IconPanel * ip){
        presetIconPanel = ip;
    }
    void setInstructionPanel(TextPanel * ip){
        instructionPanel = ip;
        instructionPanel->show();
    }
//-----------------------------------------------------------------------
    void displayInstructions(Step s){
        

        if (s.type == Step::COUNT_IN){
            instructionPanel->setText("GET READY");
        }
        if (s.type == Step::PREVIEW_DISPLAY){
            instructionPanel->setText("MEMORISE");
            panel->setColor(ofColor(0,0,0));
        }
        if (s.type == Step::PREVIEW_PLAY){
            instructionPanel->setText("MEMORISE");
            panel->setColor(ofColor(0,0,0));
        }
        if (s.type == Step::MATCHING_INTERACTION){
            instructionPanel->setText("MATCH:");
            panel->setColor(ofColor(100,0,0));
        }
        if (s.type == Step::MATCHING_RESULT){
            instructionPanel->setText("RESULT");
            panel->setColor(ofColor(0,100,0));
        }
    }
//-----------------------------------------------------------------------
    void onNextTick(int tickNumber){
        cout << "TICK " << tickNumber << endl;

        // only first beat in bar is active one
        if ( tickNumber % 4  != 0){
            return;
        }
        // load next target preset
        
        Step newStep = sequenceController.getNextStep();
        displayInstructions(newStep);
        
        if(newStep.isLastOfAll){
            // do finished run stuff, show summary
            candidateSynth.stopMetronome();
            cout << "FINISHED BLOCK" << endl;
        }
        if(newStep.isLastOfRun){
            // do finished run stuff, show summary
            candidateSynth.stopMetronome();
            cout << "FINISHED RUN" << endl;
        }
        
        float t = newStep.getTimeBetweenTicks();
        candidateSynth.setMetroTime(t);
        
        if(newStep.type == Step::COUNT_IN){
            // count in
            return;
        }
        
        if(newStep.presetIndex >= 0 && newStep.presetIndex <= 8){
            currentTargetPreset =  expPresetManager.getPreset(newStep.presetIndex);
        }
        
        if(newStep.showsTargetIcon){
            presetIconPanel->setTextAndImage(currentTargetPreset->name, currentTargetPreset->getImage());
            presetIconPanel->show();
        }else{
            presetIconPanel->hide();
        }
        
        if(newStep.showsControlSettings){
            targetSynth.setAllParams(currentTargetPreset->getValues());
            setSlidersToTarget();
            
        }else{
            //setSlidersToDefault();
            
        }
        
        if(newStep.playsTarget){
            targetSynth.setAllParams(currentTargetPreset->getValues());
            targetSynth.trigger();
        }
        
        if(newStep.playsCandidate){
            triggerCandidateSound();
        }
        
        if(newStep.allowsCandidateControl){
            panel->setActive(true);
        }else{
            panel->setActive(false);
        }
        
        if(newStep.showsMatchResults){
            // do something
        }
        
        if(newStep.showsControlGuides){
            
            panel->setHintValues(currentTargetPreset->getValues());
            panel->showHint(true);
        }else{
            panel->showHint(false);
        }
        
    }
    
    //-----------------------------------------------------------------------
protected:
    void showUserHowTheyDid(){
        // colour flash
        // distance ?
        // score
        
    }
    void loadPreset(string pname){
        
        vector<int> values = expPresetManager.recallPreset(pname);
        if (values.size()){
            candidateSynth.setAllParams(values);
            setAllSlidersToValues(values);
        }else{
            cout << "ERROR, no preset found" << endl;
        }
    }
    
    
    //-----------------------------------------------------------------------
    void showATargetPresetWithGuide(Preset * p){ // this is when demoing the sequence to the user
        // p.show icon
        vector<int> values = p->getValues();
        if (values.size()){
            candidateSynth.setAllParams(values);
            setAllSlidersToValues(values);
        }else{
            cout << "WARNING, preset to show had no values" << endl;
        }
    }
    //-----------------------------------------------------------------------------
    
    void mapSlidersToParams(vector<UIElement*> elems, vector<int> mids){
        
        vector<UIElement*>::iterator elit;
        vector<int> typeListLog;
        int i = 0;
        for(elit=elems.begin(); elit<elems.end();elit++){
            if ( (*elit)->getType() == SLIDER){
                if(i >= mids.size()){
                    
                    cout << "ERROR ERROR: too many controls for mapping IDs: " << mids.size() << endl;
                }
                
                ButtronSlider* theSlider = (ButtronSlider*)(*elit);
                mapControlToParam((*elit), mids[i]);
                theSlider->setValueAndScale(candidateSynth.getParamValueForID(mids[i]));
                cout << "Hint Value " << targetSynth.getParamValueFromName(candidateSynth.getNameForMappingID(mids[i])) << endl;
                theSlider->setHintValue(targetSynth.getParamValueFromName(candidateSynth.getNameForMappingID(mids[i])));
                i++;
                typeListLog.push_back(int(SLIDER));
                
            }else{
                cout << "ERROR ERROR: ui type not handled my mapping function !" << endl;
            }
        }
        
        eventLogger.logEvent(CONTROL_LIST,typeListLog);
    };
//-----------------------------------------------------------------------------
    
    void buttonPressCallback(int mappingID, int value){
        if(mappingID == VOLUME_CHANGE_ID){
            targetSynth.sendVolume(value);
            candidateSynth.sendVolume(value);
            return;
        }
        
        if (mappingID == TRIGGER_CANDIDATE_ID){
            //triggerCandidateSound();
            // compare to target
            candidateSynth.setMetroTime(sequenceController.getStartTickTime());
            candidateSynth.startMetronome();
            return;
        }
        if(mappingID == SAVE_PRESET_HIT){
            expPresetManager.savePreset("blah", candidateSynth.getAllParamValues());
            return;
        }
        if(mappingID == RECALL_PRESET_HIT){
            
            loadPreset("blah");
            return;
        }
    }
    //-----------------------------------------------------------------------------
    
    SequenceController sequenceController;
    bool sequencePreview;
    Preset * currentTargetPreset;
    IconPanel* presetIconPanel;
    TextPanel* instructionPanel;
};
#endif /* defined(__riftathon__TrainingMessageOrganiser__) */