package lilWorld; import processing.core.PApplet; public 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; PApplet parent; HScrollbar (int xp, int yp, int sw, int sh, int l, PApplet p) { parent = p; swidth = sw; sheight = sh; int widthtoheight = sw - sh; ratio = (float)sw / (float)widthtoheight; xpos = xp; ypos = yp-sheight/2; spos = xpos; newspos = spos; sposMin = xpos; sposMax = xpos + swidth - sheight; loose = l; } void update() { if(over()) { over = true; } else { over = false; } if(parent.mousePressed && over) { locked = true; } if(!parent.mousePressed) { locked = false; } if(locked) { newspos = constrain2(parent.mouseX-sheight/2, sposMin, sposMax); } if(parent.abs(newspos - spos) > 0) { spos = spos + (newspos-spos)/loose; } } public int constrain2(int val, int minv, int maxv) { return parent.min(parent.max(val, minv), maxv); } boolean over() { if(parent.mouseX > xpos && parent.mouseX < xpos+swidth && parent.mouseY > ypos && parent.mouseY < ypos+sheight) { return true; } else { return false; } } public void draw() { parent.fill(255); parent.rectMode(parent.CORNER); parent.rect(xpos, ypos, swidth, sheight); if(over || locked) { parent.fill(153, 102, 0); } else { parent.fill(102, 102, 102); } parent.rect(spos, ypos, sheight, sheight); } void setPos(float s) { spos = xpos + s*(sposMax-sposMin); newspos = spos; } float getPos() { // convert spos to be values between // 0 and the total width of the scrollbar return ((spos-xpos))/(sposMax-sposMin);// * ratio; } }