/* void mouseReleased(){ s.release_slider(); } void mouseDragged(){ s.drag_slider(); } void mousePressed(){ s.jump_slider(); } */ class slider{ float x; float y; float l; float w; float value; float min; float max; color background; color slide; float slidel; boolean grabbed; String label; int fontsize; slider(){ x = y = 50; l = 100; w = 10; min = 0; max = 100; fontsize = 14; value = (max-min)/2; background = color(0); slide = color(0,255,0); slidel = 4; grabbed = false; label = "Slider"; } void set_label(String s){ label = s; } void set_values(float mi, float ma){ if(mi < ma && mi >= 0){ min = mi; max = ma; } } void set_value(float vi){ if(vi >= min && vi <= max){ value = vi; } } void draw(){ float ratio = value/( max-min); float p = l*(1.0-ratio); fill(background); rect(x,y-slidel/2.0,w,l+slidel); // extend background by slider line(x,y,x-5,y); // tic line(x,y+l,x-5,y+l); // tic textSize(fontsize); textLeading(fontsize+2); textAlign(CENTER,BOTTOM); text(label,x,y-5); textSize(fontsize-2); textAlign(RIGHT,CENTER); if((max-min) > 10){ // write ints text(int(max),x-5,y); text(int(min),x-5,y+l); } else{ // write floats text((max),x-5,y); text((min),x-5,y+l); } textAlign(LEFT,CENTER); if((max-min) > 10){ // write ints text(int(value),x+w+4,y+p); //-slidel/2); }else{ text((value),x+w+4,y+p); //-slidel/2); } fill(slide); rect(x,y+p-slidel/2.0,w,slidel); } void set_position(int xp, int yp){ x = xp; y = yp; } void release_slider(){ grabbed = false; } void drag_slider(){ if(grabbed == true){ float ratio = (mouseY-y)/(l); value = min + (1-ratio)*(max-min); if(value < min) value = min; if(value > max) value = max; } } void jump_slider(){ if(mouseX > x && mouseX < x+w){ if(mouseY > y && mouseY < y+l){ float ratio = (mouseY-y)/(l); value = min + (1-ratio)*(max-min); if(value < min) value = min; if(value > max) value = max; grabbed = true; } } } float get(){ return value; } };