12. November 2010
Slider bearbeiten VSlider sliderR = null; VSlider sliderG = null; VSlider sliderB = null; void setup() { size(300, 600); sliderR = new VSlider(100,50,20,500,.5); sliderG = new VSlider(150,50,20,500,.5); sliderB = new VSlider(200,50,20,500,.5); } void draw() { background(sliderR.pos() * 255,sliderG.pos() * 255,sliderB.pos() * 255); sliderR.draw(); sliderG.draw(); sliderB.draw(); } void mousePressed() { sliderR.mousePos(mouseX,mouseY,true); sliderG.mousePos(mouseX,mouseY,true); sliderB.mousePos(mouseX,mouseY,true); } void mouseDragged() { sliderR.mousePos(mouseX,mouseY,true); sliderG.mousePos(mouseX,mouseY,true); sliderB.mousePos(mouseX,mouseY,true);} } klasse: class VSlider { PVector _p1; PVector _p2; int _w; float _pos; VSlider(int x,int y,int w,int h,float pos) { _p1 = new PVector(x+ w*.5,y); _p2 = new PVector(x+ w*.5,y+h); _w = w; _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) //WO IST DIE maus? { if(y > _p1.y && y < _p2.y && x > _p1.x -10 && x < _p1.x +10) return true; else return false; } void draw() { pushStyle(); drawSlider(); drawKnob(); popStyle(); } void drawSlider() { stroke(225); line(_p1.x,_p1.y,_p2.x,_p2.y); line(_p1.x-0.5*_w,_p1.y,_p1.x+0.5*_w,_p1.y); line(_p2.x-0.5*_w,_p2.y,_p2.x+0.5*_w,_p2.y); } void drawKnob() { stroke(255); fill(0); PVector dir = PVector.sub(_p2,_p1); PVector pos = PVector.add( _p1 , PVector.mult(dir,_pos)); rect(pos.x-10,pos.y-30,20,60); } void mousePos(int x,int y,boolean pressed) //boolean = wert entweder 0 und 1 { if(pressed) { if(isHit(x,y)) { // move knob PVector dir = PVector.sub(_p2,_p1); _pos = 1.0 / dir.mag() * (y - _p1.y); } } else { // mouse over } } }