joemckay

slider_class

Jul 30th, 2017
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. /*
  2. Needs a global boolean called MP and two scripts to catch the mousepress and release.
  3. void mousePressed() {
  4. MP = true;
  5. }
  6. void mouseReleased() {
  7. MP = false;
  8. }
  9. */
  10.  
  11. class SliderClass {
  12. float x, y, xEnd, xStart, xDif;
  13. float sliderValue, startValue, endValue, interval, totalLength, screenInterval;
  14. color sliderColor = color(100, 120, 160, 255);
  15. int knobWidth = 12;
  16. int knobHeight = 20;
  17. float fade = 255; // for knob color
  18. boolean clicked, floats, sticky;
  19. int sliderValueInt;
  20. // x, y, length of slider, start value, end value, step value, int or float, sticky
  21. SliderClass(float _x, float _y, int _length, int _startValue, int _endValue, int _interval, boolean _floats, boolean _sticky) {
  22. x = _x;
  23. y = _y;
  24. xEnd = x + _length;
  25. xStart = x;
  26. startValue = _startValue;
  27. endValue = _endValue;
  28. floats = _floats;
  29. interval = _interval;
  30. totalLength = _length;
  31. sticky = _sticky;
  32. }
  33.  
  34. void move() {
  35. // check to see if it's clicked
  36. if (dist(mouseX, mouseY, x, y) < knobWidth) { // use this if you want a round knob on the slider
  37. //if (mouseX > x - (knobWidth/2) && mouseX < x + knobWidth/2
  38. // && mouseY > y - (knobHeight/2) && mouseY < y + knobHeight /2) {
  39. if (MP && clicked == false) { // if mouse was pressed
  40. xDif = mouseX - x;
  41. clicked = true;
  42. }
  43. fade = 100; // mouseOver but not clicked
  44. } else {
  45. fade = 0;
  46. }
  47. if (clicked == true) {
  48. fade = 255;
  49. }
  50. if (MP == false && clicked == true) { // if the mouse was just released
  51. clicked = false;
  52. if (sticky) { // if you set it to jump to the closest line
  53. float modDif = sliderValue % interval; // value to show how close it is to last line
  54. int div = int(sliderValue/interval); // vlaue to show how MANY intervals it's passed
  55. if (modDif < interval/2) { // so it goes to closest line not just lower
  56. x = xStart + (div * screenInterval);
  57. } else {
  58. x = xStart + ((div + 1) * screenInterval);
  59. }
  60. }
  61. }
  62. if (clicked) {
  63. x = mouseX - xDif; // so the slider doens't "jump" to the mouse x.
  64. x = constrain(x, xStart, xEnd); // keep the knob on the slider
  65. }
  66. sliderValue = map(x, xStart, xEnd, startValue, endValue); // get the slider position relative to the values
  67. sliderValueInt = int(sliderValue); // make that number an int
  68. }
  69.  
  70. void display() {
  71. stroke(1);
  72. screenInterval = map(interval, 0, endValue - startValue, 0, totalLength); // get the size of the gaps for the screen relative to the intervals
  73. for (int i = int(screenInterval); i < totalLength; i += screenInterval) { // go from the first gap to the end of slider by the screenInterval
  74. line(i + xStart, y+12, i + xStart, y-12); // draw lines
  75. }
  76. line(xStart, y+12, xStart, y-12); // draw first line (optional)
  77. line(xEnd, y+12, xEnd, y-12); // draw last line (optional)
  78. line(xStart, y, xEnd, y); // draw center line
  79. sliderColor = color(red(sliderColor), green(sliderColor), blue(sliderColor), fade);
  80. fill(255);
  81. //ellipse (x, y, knobWidth, knobWidth);
  82. rect(x, y, knobWidth, knobHeight); // so it's opaque
  83. fill(sliderColor);
  84. //ellipse (x, y, knobWidth, knobWidth);
  85. rect(x, y, knobWidth, knobHeight); // add color
  86. fill(1);
  87. if (floats) {
  88. text(sliderValue, x, y+30);
  89. } else {
  90. text(sliderValueInt, x, y+30);
  91. }
  92. text(int(startValue), xStart, y+30);
  93. text(int(endValue), xEnd, y+30);
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment