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, sposRange; // max and min values of slider int loose; // how loose/heavy boolean over; // is the mouse over the slider? boolean locked; float minval, maxval, range; String caption; PFont font; HScrollbar (int xp, int yp, int sw, int sh, int l, String cap, PFont f) { swidth = sw; sheight = sh; xpos = xp; ypos = yp-sheight/2; spos = xpos + swidth/2 - sheight/2; newspos = spos; sposMin = xpos; sposMax = xpos + swidth - sheight; sposRange = sposMax - sposMin; loose = l; caption = cap; font = f; } void setRange(float _min, float _max) { minval = _min; maxval = _max; range = maxval - minval; } 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() { fill(#EEEEEE); stroke(#666666); rect(xpos, ypos, swidth, sheight); stroke(#999999); if(over || locked) { fill(#AAAAAA); } else { fill(#BBBBBB); } rect(spos, ypos, sheight, sheight); textFont(font); fill(#666666); text(caption, xpos+2, ypos+sheight-1); } int getValue() { return (int)(minval + ((spos-sposMin)/sposRange) * range); } void setValue(float v) { v = min(max(v, minval), maxval); spos = newspos = sposMin + (v/range) * sposRange; } }