class HScrollbar { 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; color overColor; String title; int maxim; // the maximum that the scrollbar should display HScrollbar (){ this("nameless", 0,0,width, 10, 16,width); } HScrollbar (String name, int xp, int yp, int sw, int sh, int l, int top) { 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 = l; textFont(loadFont("CenturyGothic-15.vlw"), 12.0); overColor = color(255); title=name; maxim = top; } 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() { //info("xpos =" + xpos + ", ypos = " + ypos +", swidth = " + swidth); if(mouseX > xpos && mouseX < xpos+swidth && mouseY > ypos && mouseY < ypos+ (2 * sheight)) { return true; } else { return false; } } void display() { //fill(255); fill(overColor); rect(xpos, ypos, swidth, sheight); if(over || locked) { fill(153, 102, 0); } else { fill(102, 102, 102); } rect(spos, ypos, sheight, sheight); fill(0); //noStroke(); rect(xpos,ypos+sheight,swidth, sheight); fill(0); text(str(getVal()) ,spos + sheight + 3, ypos+sheight); fill(255); //lets have a name for the value showing text(title,spos,ypos+ (2* sheight)); } //get the position as a portion of the max float getVal(){ return spos/swidth * maxim; } float getPos() { // Convert spos to be values between // 0 and the total width of the scrollbar return spos * ratio; } } //balanced bar between max and -max class BHScrollbar extends HScrollbar{ BHScrollbar(String name, int xp, int yp, int sw, int sh, int l, int top){ super(name, xp, yp, sw,sh,l,top); } float getVal(){ return (2*(spos - (swidth/2)))/swidth * maxim; } } //returns a value between 0 and max class IHScrollbar extends HScrollbar{ IHScrollbar(String name, int xp, int yp, int sw, int sh, int l, int top){ super(name, xp, yp, sw,sh,l,top); } float getVal(){ return floor(spos/swidth * maxim); } }