rt300@0: // rt300@0: // buttron.cpp rt300@0: // emptyExample rt300@0: // rt300@0: // Created by Robert Tubb on 30/04/2013. rt300@0: // rt300@0: // rt300@0: rt300@0: #include "buttron.h" rt300@0: rt300@0: //------------------------------------------------------------------ rt300@0: rt300@0: Buttron::Buttron(){ rt300@0: // rt300@0: //cout << " buttron default constructur\n"; rt300@0: } rt300@0: //------------------------------------------------------------------ rt300@0: Buttron::Buttron( rt300@38: float ax, rt300@38: float ay, rt300@38: float awidth, rt300@38: float aheight, rt300@38: float athickness, rt300@38: float aradius, rt300@38: ofColor aforegroundHi, rt300@38: ofColor abackgroundHi, rt300@38: ofColor aforegroundLo, rt300@38: ofColor abackgroundLo) : rt300@38: UIElement(ax,ay,awidth,aheight,abackgroundLo), rt300@38: thickness(athickness), rt300@38: radius(aradius), rt300@38: foregroundHi(aforegroundHi), rt300@38: backgroundHi(abackgroundHi), rt300@38: foregroundLo(aforegroundLo), rt300@38: backgroundLo(abackgroundLo) { rt300@0: //cout << "phew, buttron big constructur\n"; rt300@38: behaviourMode = MOMENTARY; rt300@0: on = false; rt300@0: } rt300@0: //------------------------------------------------------------------ rt300@0: Buttron::Buttron(float ax, rt300@0: float ay, rt300@0: const UIProps& props): rt300@38: UIElement(ax,ay,props.buttonWidth,props.buttonHeight, props) rt300@0: rt300@0: { rt300@38: rt300@0: thickness = props.borderThickness; rt300@0: radius = props.cornerRadius; rt300@0: foregroundHi = props.buttonHi; rt300@38: backgroundHi = props.backgroundHi; rt300@0: foregroundLo = props.buttonLo; rt300@0: backgroundLo = props.generalBackground; rt300@38: behaviourMode = MOMENTARY; rt300@0: on = false; rt300@44: bgImage = NULL; rt300@0: } rt300@0: //------------------------------------------------------------------ rt300@0: Buttron::Buttron(float ax, rt300@38: float ay, rt300@38: float awidth, rt300@38: float aheight, rt300@38: const UIProps& props): rt300@38: UIElement(ax,ay,awidth,aheight, props) rt300@0: rt300@0: { rt300@0: //cout << "slider (meh) recommended constructor\n"; rt300@0: rt300@0: thickness = props.borderThickness; rt300@0: radius = props.cornerRadius; rt300@0: foregroundHi = props.buttonHi; rt300@38: backgroundHi = props.backgroundHi; rt300@0: foregroundLo = props.buttonLo; rt300@0: backgroundLo = props.generalBackground; rt300@0: fgInactive = props.inactiveGreyedOut; rt300@0: rt300@0: on = false; rt300@38: behaviourMode = MOMENTARY; rt300@0: } rt300@0: //------------------------------------------------------------------ rt300@0: void Buttron::draw(){ rt300@0: if(hidden) return; rt300@26: rt300@26: ofDisableDepthTest(); rt300@0: //cout << "drawing button" << endl; rt300@0: ofFill(); rt300@0: UIElement::draw(); // should do background rt300@44: //drawBackground(); rt300@0: drawOutline(); rt300@0: drawTextLabel(); rt300@26: rt300@26: ofEnableDepthTest(); rt300@0: } rt300@0: //------------------------------------------------------------------ rt300@0: void Buttron::drawTextLabel(){ rt300@0: ofColor fg,bg; rt300@0: rt300@0: if(on){ rt300@0: fg = foregroundHi; rt300@0: bg = backgroundHi; rt300@0: }else{ rt300@0: fg = foregroundLo; rt300@0: bg = backgroundLo; rt300@0: } rt300@0: if(inactive){ rt300@0: fg = fgInactive; rt300@0: } rt300@0: ofSetColor(fg); rt300@0: verdana16.drawString(labelName, x + thickness*2, y + 35); rt300@0: rt300@26: rt300@0: } rt300@0: //------------------------------------------------------------------ rt300@0: void Buttron::drawOutline(){ rt300@0: rt300@0: // draw bars rt300@0: ofColor fg,bg; rt300@0: rt300@0: if(on){ rt300@0: fg = foregroundHi; rt300@0: bg = backgroundHi; rt300@0: }else{ rt300@0: fg = foregroundLo; rt300@0: bg = backgroundLo; rt300@0: } rt300@22: if(inactive){ rt300@22: ofSetColor(fgInactive); rt300@22: } rt300@0: ofSetColor(fg); rt300@0: rt300@0: float cornerSize = thickness + radius; rt300@0: // left rt300@0: ofRect(x, y+cornerSize, thickness, height - 2*(cornerSize)); rt300@0: //right rt300@0: ofRect(x + width - thickness, y+cornerSize, thickness, height - 2*(cornerSize)); rt300@0: // top rt300@0: ofRect(x+cornerSize, y, width-2*(cornerSize), thickness); rt300@0: // bottom rt300@0: ofRect(x+cornerSize, y+height-thickness, width-2*(cornerSize), thickness); rt300@0: rt300@0: // draw corner foreground circles rt300@0: rt300@0: //tl rt300@0: ofCircle(x+cornerSize, y+cornerSize, cornerSize); rt300@38: rt300@0: rt300@0: //tr rt300@38: rt300@0: ofCircle(x+width-cornerSize, y+cornerSize, cornerSize); rt300@38: rt300@0: //bl rt300@38: rt300@0: ofCircle(x+cornerSize, y+height-cornerSize, cornerSize); rt300@38: rt300@0: //br rt300@38: rt300@0: ofCircle(x+width-cornerSize, y+height-cornerSize, cornerSize); rt300@38: rt300@0: // draw corner inner bg circles rt300@0: ofSetColor(bg); rt300@0: ofCircle(x+cornerSize, y+cornerSize, radius); rt300@38: rt300@0: ofCircle(x+width-cornerSize, y+cornerSize, radius); rt300@38: rt300@0: ofCircle(x+cornerSize, y+height-cornerSize, radius); rt300@38: rt300@0: ofCircle(x+width-cornerSize, y+height-cornerSize, radius); rt300@0: rt300@0: // fill in background rt300@44: rt300@0: ofRect(x+cornerSize,y+thickness,width-2*cornerSize,height-2*thickness); rt300@0: ofRect(x+thickness, y+cornerSize, radius, height-2*cornerSize); rt300@0: ofRect(x+width-cornerSize, y+cornerSize, radius, height-2*cornerSize); rt300@26: rt300@26: rt300@0: } rt300@0: //------------------------------------------------------------------------------ rt300@0: bool Buttron::handleMyTouch(int tx, int ty, touchType ttype, int touchID){ rt300@0: rt300@0: //cout << "buttron handling touch\n"; rt300@38: if(behaviourMode == MOMENTARY){ rt300@38: if(ttype == TOUCH_DOWN){ rt300@38: on = true; rt300@38: if(callback) callback(myParamID,1); rt300@38: rt300@38: }else if(ttype == TOUCH_MOVED){ rt300@38: rt300@38: }else if(ttype == TOUCH_UP){ rt300@38: on = false; rt300@38: } rt300@0: } rt300@38: if(behaviourMode == TOGGLE) rt300@38: if(ttype == TOUCH_DOWN){ rt300@38: on = !on; rt300@38: if(callback) callback(myParamID,1); rt300@38: } rt300@0: return true; // necessary? rt300@0: rt300@0: } rt300@0: rt300@0: //------------------------------------------------------------------------------