view UI code/Buttron.mm @ 28:953db6518738

leap version more or less there, needs btter results feedback but thats detail. "no movement" bit is stupid cos peopel can move their hand. light flash not work.
author Robert Tubb <rt300@eecs.qmul.ac.uk>
date Thu, 30 Oct 2014 18:35:00 +0000
parents 8d7ae43b2edd
children fea11c3d1d94
line wrap: on
line source
//
//  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;
    
    ofDisableDepthTest();
    //cout << "drawing button" << endl;
    ofFill();
    UIElement::draw(); // should do background
    drawOutline();
    drawTextLabel();
    
    ofEnableDepthTest();
}
//------------------------------------------------------------------
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?
    
}

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