Skip to content

Instantly share code, notes, and snippets.

@vampjaz
Created July 30, 2018 21:18
Show Gist options
  • Save vampjaz/73c516b9a28efd209878f6af0f7327a1 to your computer and use it in GitHub Desktop.
Save vampjaz/73c516b9a28efd209878f6af0f7327a1 to your computer and use it in GitHub Desktop.
A nice scrollbar implementation for Processing
class ScrollBar {
float x,y,w,h;
float min,max,val;
String name;
ScrollBar(float xp, float yp, float sw, float sh, float lo, float hi, float v, String n) {
x=xp;
y=yp;
w=sw;
h=sh;
min=lo;
max=hi;
val=v;
name=n;
}
void run() {
float tempval = map(mouseX - x,0,w,min,max);
boolean mouseover = mouseX > x && mouseY > y && mouseX < x+w && mouseY < y+h;
if (mousePressed && mouseover) {
val = tempval;
}
float computed = x + map(val,min,max,0,w);
float tempcomputed = 0;
if (mouseover) {tempcomputed = x + map(tempval,min,max,0,w);}
noStroke();
fill(60);
rect(x,y,w,h);
strokeWeight(2);
stroke(255,255,0);
line(computed,y,computed,y+h);
if (mouseover) {
stroke(0,255,255);
line(tempcomputed,y,tempcomputed,y+h);
fill(255,0,0);
text(name+" - "+val+" ("+tempval+")",x+10,y+12);
} else {
fill(255,0,0);
text(name+" - "+val,x+10,y+12);
}
}
float getVal() {
return val;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment