//
//  ButtronSlider.cpp
//  emptyExample
//
//  Created by Robert Tubb on 22/05/2013.
//
//

#include "ButtronSlider.h"


ButtronSlider::ButtronSlider(float ax,
                             float ay,
                             float awidth,
                             float aheight,
                             float athickness,
                             float aradius,
                             ofColor aforegroundHi,
                             ofColor abackgroundHi,
                             ofColor aforegroundLo,
                             ofColor abackgroundLo,
                             SliderType type) :
Buttron(ax,ay,awidth,aheight,athickness,aradius,aforegroundHi,abackgroundHi,aforegroundLo,abackgroundLo),
sliderType(type)

{

    init();
}
ButtronSlider::ButtronSlider(float ax,
                             float ay,
                             float awidth,
                             float aheight,
                             SliderType type,
                             const UIProps& props) :
Buttron(ax,ay,awidth,aheight,props),
sliderType(type)

{
    init();
}
void  ButtronSlider::init(){
    myType = SLIDER;
    minVal = 0.;
    maxVal = 127.;
    value = 0.6;
    hintShowing = false;
}
// 
//---------------------------------------------------------------------
void ButtronSlider::drawIndicator(double proportion){
    if(!indicatorShowing) return;
    if(on){
        ofSetColor(foregroundHi);
    }else{
        ofSetColor(foregroundLo);
        
    }
    if(inactive){
        ofSetColor(fgInactive);
    }
    if(sliderType == FILL){

        double maxH = height - 2 * thickness; //
        double w = width - 2 * thickness;//
        double barheight = value*maxH;
        double top = y + height - thickness - barheight;
        ofRect(x+thickness, top, w, barheight);
        
    }else if(sliderType == LINE){
        double loc = y + thickness +  (1 -value)*(height-3*thickness);
        ofRect(x+thickness,loc, width-2*thickness,thickness);
    }
    

}

void ButtronSlider::drawHintIndicator(){

    ofSetColor(hintColor);
    float hthick = 6;
    double loc = y + hthick +  (1 - hintValue)*(height-3*hthick);
    ofRect(x-thickness,loc, width+2*thickness,hthick);

}

void ButtronSlider::animateHintToNewValue(int newVal, float timeToTake){
    float targVal = (newVal - minVal)/(maxVal - minVal);
    
    float amtPerFrame = 1000./(ofGetFrameRate() * timeToTake);
    posAnimIncr = (targVal - hintValue )*amtPerFrame;
    animating = true;
}

void ButtronSlider::update(){
    if (!animating) return;
    hintValue += posAnimIncr;
}
//---------------------------------------------------------------------

bool ButtronSlider::handleMyTouch(int tx, int ty, touchType ttype, int touchID){

    double ly = ty - y - thickness - radius;
    
    double prop;
    
    prop = 1 - ly/(height - 2 * (thickness + radius));

    if(prop > 1.) prop = 1.;                                                           
    if(prop < 0.) prop = 0.;

    setValue(prop);
    int scaleVal = int((maxVal - minVal)*prop + minVal);

    vector<int> pass;
    if(callback) callback(myParamID,scaleVal);
    
    return true;
}

