class Scroll { int swidth, sheight; // width and height of bar int xpos, ypos; // x and y position of bar float spos, newspos; // x position of slider int sposMin, sposMax; // max and min values of slider int loose; // how loose/heavy boolean over; // is the mouse over the slider? boolean locked; float ratio; Scroll (int xp, int yp, int sw, int sh, int l) { swidth = sw; sheight = sh; int widthtoheight = sw - sh; ratio = (float)sw / (float)widthtoheight; xpos = xp; ypos = yp-sheight/2; spos = xpos + swidth/2 - sheight/2; newspos = spos; sposMin = xpos; sposMax = xpos + swidth - sheight; loose = 1; } void update() { if(over()) { over = true; } else { over = false; } if(mousePressed && over) { locked = true; } if(!mousePressed) { locked = false; } if(locked) { newspos = constrain(mouseX-sheight/2, sposMin, sposMax); } if(abs(newspos - spos) > 1) { spos = spos + (newspos-spos)/loose; } } int constrain(int val, int minv, int maxv) { return min(max(val, minv), maxv); } boolean over() { if(mouseX > xpos && mouseX < xpos+swidth && mouseY > ypos && mouseY < ypos+sheight) { return true; } else { return false; } } void draw() { if(xpos==s1x && ypos+sheight/2==s1y) { s1p = ((spos-sposMin)/(sposMax-sposMin)); } else if (xpos==s2x && ypos+sheight/2==s2y) { s2p = ((spos-sposMin)/(sposMax-sposMin)); } else if (xpos==s3x && ypos+sheight/2==s3y) { s3p = ((spos-sposMin)/(sposMax-sposMin)); } else if (xpos==s4x && ypos+sheight/2==s4y) { s4p = ((spos-sposMin)/(sposMax-sposMin)); } else if (xpos==s5x && ypos+sheight/2==s5y) { s5p = ((spos-sposMin)/(sposMax-sposMin)); } else if (xpos==s6x && ypos+sheight/2==s6y) { s6p = ((spos-sposMin)/(sposMax-sposMin)); } fill(0,0,100); stroke(0,0,50); rect(xpos, ypos, swidth, sheight); rect(spos, ypos, sheight, sheight); } float getPos() { // convert spos to be values between // 0 and the total width of the scrollbar return spos * ratio; } }