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; HScrollbar (int xp, int yp, int sw, int sh, int l) { swidth = sw; sheight = sh; if(sw >= 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; } else{ int heighttowidth = sh - sw; ratio = (float)sh / (float)heighttowidth; xpos = xp - swidth/2; ypos = yp; spos = ypos + sheight/2 - swidth/2; newspos = spos; sposMin = ypos; sposMax = ypos - swidth + sheight; } loose = l; } void update() { if(over()) { over = true; } else { over = false; } if(mousePressed && over) { locked = true; } if(!mousePressed) { locked = false; } if(locked) { if(swidth >=sheight) newspos = constrain(mouseX-sheight/2, sposMin, sposMax); else newspos = constrain(mouseY-swidth/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 display() { noStroke(); rectMode(CORNER); fill(225,0); rect(xpos, ypos, swidth, sheight); stroke(1); if(over || locked) { fill(255); } else { fill(102, 102, 102); } if(swidth >=sheight) rect(spos, ypos, sheight, 2*sheight); else{ rectMode(CENTER); rect(xpos, spos, 2*swidth, swidth); } } float getPos() { // Convert spos to be values between // 0 and the total width of the scrollbar //if(swidth <=sheight) // println(spos+"\t"+ratio+"\n"); return spos * ratio; } }