18. November 2010
class Slider { PVector _p1; PVector _p2; int _h; float _pos; Slider(int x,int y,int w,int h,float pos) { _p1 = new PVector(x,y +h * .5); _p2 = new PVector(x + w,y + h* .5); _h = h; _pos = pos; if(_pos > 1.0) _pos = 1.0; else if(_pos < 0.0) _pos = 0.0; } float pos() { return _pos; } boolean isHit(int x,int y) { if(x > _p1.x && x < _p2.x && y > _p1.y - 5 && y < _p1.y + 5) return true; else return false; } void draw() { pushStyle(); drawSlider(); drawKnob(); popStyle(); } void drawSlider() { stroke(0); line(_p1.y,_p1.x,_p2.y,_p2.x); line(_p1.y + _h* .5,_p1.x,_p1.y - _h* .5,_p1.x); line(_p1.y + _h* .5,_p2.x,_p1.y - _h* .5,_p2.x); } void drawKnob() { stroke(0); fill(0); PVector dir = PVector.sub(_p2,_p1); PVector pos = PVector.add(PVector.mult(dir,_pos), _p1); ellipse(pos.y,pos.x-3,10,10); } void mousePos(int y,int x,boolean pressed) { if(pressed) { if(isHit(x,y)) { // move knob PVector dir = PVector.sub(_p2,_p1); _pos = 1.0 / dir.mag() * (x - _p1.x); } } else { // mouse over } } }