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.

161 lines
4.2KB

  1. #pragma once
  2. #include "widgets.hpp"
  3. namespace rack {
  4. /** Parent must be a ScrollWidget */
  5. struct ScrollBar : OpaqueWidget {
  6. enum Orientation {
  7. VERTICAL,
  8. HORIZONTAL
  9. };
  10. Orientation orientation;
  11. BNDwidgetState state = BND_DEFAULT;
  12. float offset = 0.0;
  13. float size = 0.0;
  14. ScrollBar() {
  15. box.size = math::Vec(BND_SCROLLBAR_WIDTH, BND_SCROLLBAR_HEIGHT);
  16. }
  17. void draw(NVGcontext *vg) override {
  18. bndScrollBar(vg, 0.0, 0.0, box.size.x, box.size.y, state, offset, size);
  19. }
  20. void on(event::DragStart &e) override {
  21. state = BND_ACTIVE;
  22. windowCursorLock();
  23. }
  24. void on(event::DragMove &e) override;
  25. void on(event::DragEnd &e) override {
  26. state = BND_DEFAULT;
  27. windowCursorUnlock();
  28. }
  29. };
  30. /** Handles a container with ScrollBar */
  31. struct ScrollWidget : OpaqueWidget {
  32. Widget *container;
  33. ScrollBar *horizontalScrollBar;
  34. ScrollBar *verticalScrollBar;
  35. math::Vec offset;
  36. ScrollWidget() {
  37. container = new Widget();
  38. addChild(container);
  39. horizontalScrollBar = new ScrollBar();
  40. horizontalScrollBar->orientation = ScrollBar::HORIZONTAL;
  41. horizontalScrollBar->visible = false;
  42. addChild(horizontalScrollBar);
  43. verticalScrollBar = new ScrollBar();
  44. verticalScrollBar->orientation = ScrollBar::VERTICAL;
  45. verticalScrollBar->visible = false;
  46. addChild(verticalScrollBar);
  47. }
  48. void scrollTo(math::Rect r) {
  49. math::Rect bound = math::Rect::fromMinMax(r.getBottomRight().minus(box.size), r.pos);
  50. offset = offset.clampBetween(bound);
  51. }
  52. void draw(NVGcontext *vg) override {
  53. nvgScissor(vg, 0, 0, box.size.x, box.size.y);
  54. Widget::draw(vg);
  55. nvgResetScissor(vg);
  56. }
  57. void step() override {
  58. Widget::step();
  59. // Clamp scroll offset
  60. math::Vec containerCorner = container->getChildrenBoundingBox().getBottomRight();
  61. math::Rect containerBox = math::Rect(math::Vec(0, 0), containerCorner.minus(box.size));
  62. offset = offset.clamp(containerBox);
  63. // Lock offset to top/left if no scrollbar will display
  64. if (containerBox.size.x < 0.0)
  65. offset.x = 0.0;
  66. if (containerBox.size.y < 0.0)
  67. offset.y = 0.0;
  68. // Update the container's positions from the offset
  69. container->box.pos = offset.neg().round();
  70. // Update scrollbar offsets and sizes
  71. math::Vec viewportSize = container->getChildrenBoundingBox().getBottomRight();
  72. math::Vec scrollbarOffset = offset.div(viewportSize.minus(box.size));
  73. math::Vec scrollbarSize = box.size.div(viewportSize);
  74. horizontalScrollBar->visible = (0.0 < scrollbarSize.x && scrollbarSize.x < 1.0);
  75. verticalScrollBar->visible = (0.0 < scrollbarSize.y && scrollbarSize.y < 1.0);
  76. horizontalScrollBar->offset = scrollbarOffset.x;
  77. verticalScrollBar->offset = scrollbarOffset.y;
  78. horizontalScrollBar->size = scrollbarSize.x;
  79. verticalScrollBar->size = scrollbarSize.y;
  80. // Resize scroll bars
  81. math::Vec inner = math::Vec(box.size.x - verticalScrollBar->box.size.x, box.size.y - horizontalScrollBar->box.size.y);
  82. horizontalScrollBar->box.pos.y = inner.y;
  83. verticalScrollBar->box.pos.x = inner.x;
  84. horizontalScrollBar->box.size.x = verticalScrollBar->visible ? inner.x : box.size.x;
  85. verticalScrollBar->box.size.y = horizontalScrollBar->visible ? inner.y : box.size.y;
  86. }
  87. void on(event::Hover &e) override {
  88. // Scroll with arrow keys
  89. if (!gSelectedWidget) {
  90. float arrowSpeed = 30.0;
  91. if (windowIsShiftPressed() && windowIsModPressed())
  92. arrowSpeed /= 16.0;
  93. else if (windowIsShiftPressed())
  94. arrowSpeed *= 4.0;
  95. else if (windowIsModPressed())
  96. arrowSpeed /= 4.0;
  97. if (glfwGetKey(gWindow, GLFW_KEY_LEFT) == GLFW_PRESS) {
  98. offset.x -= arrowSpeed;
  99. }
  100. if (glfwGetKey(gWindow, GLFW_KEY_RIGHT) == GLFW_PRESS) {
  101. offset.x += arrowSpeed;
  102. }
  103. if (glfwGetKey(gWindow, GLFW_KEY_UP) == GLFW_PRESS) {
  104. offset.y -= arrowSpeed;
  105. }
  106. if (glfwGetKey(gWindow, GLFW_KEY_DOWN) == GLFW_PRESS) {
  107. offset.y += arrowSpeed;
  108. }
  109. }
  110. OpaqueWidget::on(e);
  111. }
  112. void on(event::HoverScroll &e) override {
  113. offset = offset.minus(e.scrollDelta);
  114. e.target = this;
  115. }
  116. void on(event::HoverKey &e) override {
  117. OpaqueWidget::on(e);
  118. }
  119. };
  120. inline void ScrollBar::on(event::DragMove &e) {
  121. ScrollWidget *scrollWidget = dynamic_cast<ScrollWidget*>(parent);
  122. assert(scrollWidget);
  123. if (orientation == HORIZONTAL)
  124. scrollWidget->offset.x += e.mouseDelta.x;
  125. else
  126. scrollWidget->offset.y += e.mouseDelta.y;
  127. }
  128. } // namespace rack