//
//  buttron.cpp
//  emptyExample
//
//  Created by Robert Tubb on 30/04/2013.
//
//

#include "buttron.h"

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

Buttron::Buttron(){
    //
    //cout << " buttron default constructur\n";
}
//------------------------------------------------------------------
Buttron::Buttron(
        float ax,
        float ay,
        float awidth,
        float aheight,
        float athickness,
        float aradius,
        ofColor aforegroundHi,
        ofColor abackgroundHi,
        ofColor aforegroundLo,
        ofColor abackgroundLo) :
        UIElement(ax,ay,awidth,aheight,abackgroundLo),
        thickness(athickness),
        radius(aradius),
        foregroundHi(aforegroundHi),
        backgroundHi(abackgroundHi),
        foregroundLo(aforegroundLo),
        backgroundLo(abackgroundLo) {
    //cout << "phew, buttron big constructur\n";
    on = false;
}
//------------------------------------------------------------------
Buttron::Buttron(float ax,
                 float ay,
                 const UIProps& props):
        UIElement(ax,ay,props.buttonWidth,props.buttonHeight, props)

{

    thickness = props.borderThickness;
    radius = props.cornerRadius;
    foregroundHi = props.buttonHi;
    backgroundHi = props.generalBackground;
    foregroundLo = props.buttonLo;
    backgroundLo = props.generalBackground;
    
    on = false;
}
//------------------------------------------------------------------
Buttron::Buttron(float ax,
        float ay,
        float awidth,
        float aheight,
        const UIProps& props):
    UIElement(ax,ay,awidth,aheight, props)

{
    //cout << "slider (meh) recommended constructor\n";
    
    thickness = props.borderThickness;
    radius = props.cornerRadius;
    foregroundHi = props.buttonHi;
    backgroundHi = props.generalBackground;
    foregroundLo = props.buttonLo;
    backgroundLo = props.generalBackground;
    fgInactive = props.inactiveGreyedOut;
    
    on = false;
}
//------------------------------------------------------------------
void Buttron::draw(){
    if(hidden) return;
    //cout << "drawing button" << endl;
    ofFill();
    UIElement::draw(); // should do background
    drawOutline();
    drawTextLabel();
}
//------------------------------------------------------------------
void Buttron::drawTextLabel(){
    ofColor fg,bg;
    
    if(on){
        fg = foregroundHi;
        bg = backgroundHi;
    }else{
        fg = foregroundLo;
        bg = backgroundLo;
    }
    if(inactive){
        fg = fgInactive;
    }
    ofSetColor(fg);
    verdana16.drawString(labelName, x + thickness*2, y + 35);
    
}
//------------------------------------------------------------------
void Buttron::drawOutline(){
    
    // draw bars
    ofColor fg,bg;
    
    if(on){
        fg = foregroundHi;
        bg = backgroundHi;
    }else{
        fg = foregroundLo;
        bg = backgroundLo;
    }
    if(inactive){
        ofSetColor(fgInactive);
    }
    ofSetColor(fg);
    
    float cornerSize = thickness + radius;
    // left
    ofRect(x, y+cornerSize, thickness, height - 2*(cornerSize));
    //right
    ofRect(x + width - thickness, y+cornerSize, thickness, height - 2*(cornerSize));
    // top
    ofRect(x+cornerSize, y, width-2*(cornerSize), thickness);
    // bottom
    ofRect(x+cornerSize, y+height-thickness, width-2*(cornerSize), thickness);
    
    // draw corner  foreground circles
    
    //tl
    ofCircle(x+cornerSize, y+cornerSize, cornerSize);

    
    //tr

    ofCircle(x+width-cornerSize, y+cornerSize, cornerSize);

    //bl

    ofCircle(x+cornerSize, y+height-cornerSize, cornerSize);

    //br

    ofCircle(x+width-cornerSize, y+height-cornerSize, cornerSize);

    // draw corner inner bg circles
    ofSetColor(bg);
    ofCircle(x+cornerSize, y+cornerSize, radius);

    ofCircle(x+width-cornerSize, y+cornerSize, radius);

    ofCircle(x+cornerSize, y+height-cornerSize, radius);

    ofCircle(x+width-cornerSize, y+height-cornerSize, radius);
    
    // fill in background
    ofRect(x+cornerSize,y+thickness,width-2*cornerSize,height-2*thickness);
    ofRect(x+thickness, y+cornerSize, radius, height-2*cornerSize);
    ofRect(x+width-cornerSize, y+cornerSize, radius, height-2*cornerSize);
}
//------------------------------------------------------------------------------
bool Buttron::handleMyTouch(int tx, int ty, touchType ttype, int touchID){
    
    //cout << "buttron handling touch\n";
    if(ttype == TOUCH_DOWN){
        on = true;
        if(callback) callback(myParamID,1);
        
    }else if(ttype == TOUCH_MOVED){
        
    }else if(ttype == TOUCH_UP){
        on = false;
    }
    return true; // necessary?
    
}

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