rt300@0: // rt300@0: // ButtronSlider.cpp rt300@0: // emptyExample rt300@0: // rt300@0: // Created by Robert Tubb on 22/05/2013. rt300@0: // rt300@0: // rt300@0: rt300@0: #include "ButtronSlider.h" rt300@0: rt300@0: rt300@0: ButtronSlider::ButtronSlider(float ax, rt300@0: float ay, rt300@0: float awidth, rt300@0: float aheight, rt300@0: float athickness, rt300@0: float aradius, rt300@0: ofColor aforegroundHi, rt300@0: ofColor abackgroundHi, rt300@0: ofColor aforegroundLo, rt300@0: ofColor abackgroundLo, rt300@0: SliderType type) : rt300@0: Buttron(ax,ay,awidth,aheight,athickness,aradius,aforegroundHi,abackgroundHi,aforegroundLo,abackgroundLo), rt300@0: sliderType(type) rt300@0: rt300@0: { rt300@0: rt300@0: init(); rt300@0: } rt300@0: ButtronSlider::ButtronSlider(float ax, rt300@0: float ay, rt300@0: float awidth, rt300@0: float aheight, rt300@0: SliderType type, rt300@0: const UIProps& props) : rt300@0: Buttron(ax,ay,awidth,aheight,props), rt300@0: sliderType(type) rt300@0: rt300@0: { rt300@0: init(); rt300@0: } rt300@0: void ButtronSlider::init(){ rt300@0: myType = SLIDER; rt300@0: minVal = 0.; rt300@0: maxVal = 127.; rt300@0: value = 0.6; rt300@0: hintShowing = false; rt300@37: rt300@37: string fname = ofFilePath::getAbsolutePath(ofToDataPath("buttron.png")); rt300@37: defaultImage.loadImage(fname); rt300@37: setHandTexture(&defaultImage); rt300@38: showScoreForFrames = 0; rt300@38: hintPosAnimIncr = 0; rt300@41: posAnimIncr = 0; rt300@41: onlyOneTouchAllowed = true; rt300@44: indicatorShowing = true; rt300@0: } rt300@37: // rt300@0: //--------------------------------------------------------------------- rt300@0: void ButtronSlider::drawIndicator(double proportion){ rt300@27: if(!indicatorShowing) return; rt300@0: if(on){ rt300@0: ofSetColor(foregroundHi); rt300@0: }else{ rt300@0: ofSetColor(foregroundLo); rt300@0: rt300@0: } rt300@0: if(inactive){ rt300@0: ofSetColor(fgInactive); rt300@0: } rt300@37: if(showScoreForFrames > 0){ rt300@37: ofSetColor(flashColor); rt300@37: showScoreForFrames--; rt300@37: } rt300@0: if(sliderType == FILL){ rt300@0: rt300@0: double maxH = height - 2 * thickness; // rt300@0: double w = width - 2 * thickness;// rt300@0: double barheight = value*maxH; rt300@0: double top = y + height - thickness - barheight; rt300@0: ofRect(x+thickness, top, w, barheight); rt300@38: rt300@37: }else if(sliderType == TEXTURE_FILL){ rt300@38: rt300@37: double maxH = height - 2 * thickness; // rt300@37: double w = width - 2 * thickness;// rt300@37: double barheight = value*maxH; rt300@37: double top = y + height - thickness - barheight; rt300@37: //ofRect(x+thickness, top, w, barheight); rt300@37: ofSetColor(255,255, 255); rt300@37: (*handTextureRef).draw(x+thickness, top, w, barheight); rt300@38: rt300@0: }else if(sliderType == LINE){ rt300@38: rt300@0: double loc = y + thickness + (1 -value)*(height-3*thickness); rt300@0: ofRect(x+thickness,loc, width-2*thickness,thickness); rt300@0: } rt300@0: rt300@0: rt300@0: } rt300@0: rt300@0: void ButtronSlider::drawHintIndicator(){ rt300@0: rt300@38: //ofSetColor(hintColor); rt300@38: float hthick = 16; rt300@38: double loc = y + thickness + (1 - hintValue)*(height-3*thickness); rt300@38: //ofRect(x-thickness,loc, width+2*thickness,hthick); rt300@38: ofSetColor(255, 255, 255); rt300@44: (*handTextureRef).draw(x+thickness,loc-hthick/2, width-4*thickness,hthick); rt300@0: } rt300@0: rt300@29: void ButtronSlider::animateHintToNewValue(int newVal, float timeToTake){ rt300@31: hintTargVal = (newVal - minVal)/(maxVal - minVal); rt300@29: rt300@29: float amtPerFrame = 1000./(ofGetFrameRate() * timeToTake); rt300@31: hintPosAnimIncr = (hintTargVal - hintValue )*amtPerFrame; rt300@29: animating = true; rt300@29: } rt300@31: void ButtronSlider::animateToNewValue(int newVal, float timeToTake){ rt300@31: targVal = (newVal - minVal)/(maxVal - minVal); rt300@31: rt300@31: float amtPerFrame = 1000./(ofGetFrameRate() * timeToTake); rt300@31: posAnimIncr = (targVal - value )*amtPerFrame; rt300@47: rt300@47: animFrac = pow( 0.05, 1000./(ofGetFrameRate() * timeToTake)); rt300@31: animating = true; rt300@31: } rt300@29: void ButtronSlider::update(){ rt300@29: if (!animating) return; rt300@47: float mov = (1-animFrac)*(targVal - value); rt300@47: value += mov; rt300@38: //hintValue += hintPosAnimIncr; rt300@31: rt300@47: // when we've hit the target stop. rt300@47: if (abs(targVal - value) < 0.01){ rt300@47: animating = false; rt300@47: } rt300@47: if (abs(hintTargVal - hintValue) < 0.01){ rt300@47: animating = false; rt300@47: } rt300@29: } rt300@0: //--------------------------------------------------------------------- rt300@0: rt300@0: bool ButtronSlider::handleMyTouch(int tx, int ty, touchType ttype, int touchID){ rt300@0: rt300@0: double ly = ty - y - thickness - radius; rt300@0: rt300@0: double prop; rt300@0: rt300@0: prop = 1 - ly/(height - 2 * (thickness + radius)); rt300@0: rt300@0: if(prop > 1.) prop = 1.; rt300@0: if(prop < 0.) prop = 0.; rt300@0: rt300@0: setValue(prop); rt300@0: int scaleVal = int((maxVal - minVal)*prop + minVal); rt300@0: rt300@0: vector pass; rt300@0: if(callback) callback(myParamID,scaleVal); rt300@0: rt300@0: return true; rt300@29: } rt300@37: //-------------------------------------------------------------------- rt300@39: void ButtronSlider::flashScore(int score, ofColor col, int howLong){ rt300@39: showScoreForFrames = howLong; rt300@37: flashColor = col; rt300@37: }