//
//  ButtronXY.cpp
//  tweakathlon
//
//  Created by Robert Tubb on 25/06/2013.
//
//

#include "ButtronXY.h"

ButtronXY::ButtronXY(float ax,
                             float ay,
                             float awidth, // width same as height
                             float athickness,
                             float aradius,
                             ofColor aforegroundHi,
                             ofColor abackgroundHi,
                             ofColor aforegroundLo,
                             ofColor abackgroundLo) :
Buttron(ax,ay,awidth,awidth,athickness,aradius,aforegroundHi,abackgroundHi,aforegroundLo,abackgroundLo)

{
    
    cout << "ButtronXY constructor\n";
    init();
}

ButtronXY::ButtronXY(float ax,
          float ay,
          const UIProps& props) :
Buttron(ax,ay,props.XYsize ,props.XYsize,props)
{
    cout << "ButtronXY constructor\n";
    init();
};
void ButtronXY::init(){
    xvalue = 0.6;
    yvalue = 0.7;
    minVal = 0.;
    maxVal = 127.;
    myType = XYPAD;
    labelNamex = "xxx";
    labelNamey = "yyy";
}
//---------------------------------------------------------------------
void ButtronXY::drawIndicator(){
    if(hintShowing) drawHintIndicator();
    
    if(on){
        ofSetColor(foregroundHi);
    }else{
        ofSetColor(foregroundLo);
        
    }
    if(inactive){
        ofSetColor(fgInactive);
    }
    
    // circle and cross hairs
    ofFill();

    double left = x + thickness + radius;
    double top = y + radius + thickness;
    double activeWidth = width - 2*(radius + thickness);
    double cx = left + xvalue*activeWidth;
    double cy = top + (1-yvalue)*activeWidth;
    
    ofLine(cx,y,cx,y+height);
    ofLine(x, cy, x+ width, cy);
    
    
    ofEllipse(cx,cy , 2*radius,2*radius);

    
}
//---------------------------------------------------------------------
void ButtronXY::drawHintIndicator(){
    double left = x + thickness + radius;
    double top = y + radius + thickness;
    double activeWidth = width - 2*(radius + thickness);

    // loc = y + hthick +  (1 - hintValue)*(height-3*hthick);
    ofSetColor(255,255,255);
    float hthick = 1;
    double locx = left + hintValuex*activeWidth;
    double locy = top + (1 - hintValuey)*activeWidth;
    ofEllipse(locx,locy, hthick,hthick);
    
    // draw target dart board thingy
    ofNoFill();
    ofSetLineWidth(2);
    // yellow red blue
    ofSetColor(255,255,0,166);
    
    float rband = activeWidth*TARGET_SCORE_CC_BAND*sqrt(2.0)/127.;
    ofCircle(locx, locy, rband);
    ofSetColor(255,0,0,166);
    ofCircle(locx, locy, rband*2);
    ofSetColor(45,45,255,166);
    ofCircle(locx, locy, rband*3);
    ofSetColor(0,255,0,166);
    ofCircle(locx, locy, rband*4); // within 20 cc vals?
    ofFill();
    
}
//---------------------------------------------------------------------


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

    double lx = tx - x - thickness - radius;
    double ly = ty - y - thickness - radius;
    
    double propx,propy;
    propx = lx/(width - 2 * (thickness + radius));
    propy = 1 - ly/(height - 2 * (thickness + radius));
    if(propx > 1.) propx = 1.;
    if(propx < 0.) propx = 0.;
    if(propy > 1.) propy = 1.;
    if(propy < 0.) propy = 0.;
    //cout << "propx: " << propx << endl;
    setValue(propx,propy);
    
    int scaleValx = int((maxVal - minVal)*propx + minVal);
    int scaleValy = int((maxVal - minVal)*propy + minVal);

    if(callback) callback(myParamIDX,scaleValx);
    if(callback) callback(myParamIDY,scaleValy);
    return true;
}

//---------------------------------------------------------------------