TeZ LIFE/FORMS workshop info + code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.7 KiB

3 years ago
  1. /* Scrollbar class */
  2. class HScrollbar {
  3. int swidth, sheight; // width and height of bar
  4. float xpos, ypos; // x and y position of bar
  5. float spos, newspos; // x position of slider
  6. float sposMin, sposMax; // max and min values of slider
  7. int loose; // how loose/heavy
  8. boolean over; // is the mouse over the slider?
  9. boolean locked;
  10. float ratio;
  11. HScrollbar (float xp, float yp, int sw, int sh, int l) {
  12. swidth = sw;
  13. sheight = sh;
  14. int widthtoheight = sw - sh;
  15. ratio = (float)sw / (float)widthtoheight;
  16. xpos = xp;
  17. ypos = yp-sheight/2;
  18. spos = xpos + swidth/2 - sheight/2;
  19. newspos = spos;
  20. sposMin = xpos;
  21. sposMax = xpos + swidth - sheight;
  22. loose = l;
  23. }
  24. boolean update(boolean scroll_lock) {
  25. if (mousePressed && overEvent() && ! scroll_lock) {
  26. locked = true;
  27. scroll_lock=true;
  28. }
  29. if (!mousePressed) {
  30. locked = false;
  31. scroll_lock=false;
  32. }
  33. if (locked) {
  34. newspos = constrain(mouseX-sheight/2, sposMin, sposMax);
  35. if (abs(newspos - spos) > 1) {
  36. spos = newspos;
  37. }
  38. }
  39. return scroll_lock;
  40. }
  41. float constrain(float val, float minv, float maxv) {
  42. return min(max(val, minv), maxv);
  43. }
  44. boolean overEvent() {
  45. return (mouseX > xpos && mouseX < xpos+swidth && mouseY > ypos && mouseY < ypos+sheight);
  46. }
  47. void display() {
  48. noStroke();
  49. fill(204);
  50. rect(xpos, ypos, swidth, sheight);
  51. if (over || locked) {
  52. fill(0, 0, 0);
  53. } else {
  54. fill(102, 102, 102);
  55. }
  56. rect(spos, ypos, sheight, sheight);
  57. }
  58. float getPos() {
  59. // Convert spos to be values between
  60. // 0 and the total width of the scrollbar
  61. return spos * ratio;
  62. }
  63. }