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.

95 lines
2.6KB

  1. #include "ui.hpp"
  2. #include "window.hpp"
  3. namespace rack {
  4. ScrollWidget::ScrollWidget() {
  5. container = new Widget();
  6. addChild(container);
  7. horizontalScrollBar = new ScrollBar();
  8. horizontalScrollBar->orientation = ScrollBar::HORIZONTAL;
  9. horizontalScrollBar->visible = false;
  10. addChild(horizontalScrollBar);
  11. verticalScrollBar = new ScrollBar();
  12. verticalScrollBar->orientation = ScrollBar::VERTICAL;
  13. verticalScrollBar->visible = false;
  14. addChild(verticalScrollBar);
  15. }
  16. void ScrollWidget::draw(NVGcontext *vg) {
  17. nvgScissor(vg, 0, 0, box.size.x, box.size.y);
  18. Widget::draw(vg);
  19. nvgResetScissor(vg);
  20. }
  21. void ScrollWidget::step() {
  22. // Clamp scroll offset
  23. Vec containerCorner = container->getChildrenBoundingBox().getBottomRight();
  24. offset = offset.clamp(Rect(Vec(0, 0), containerCorner.minus(box.size)));
  25. // Resize scroll bars
  26. Vec inner = Vec(box.size.x - verticalScrollBar->box.size.x, box.size.y - horizontalScrollBar->box.size.y);
  27. horizontalScrollBar->box.pos.y = inner.y;
  28. horizontalScrollBar->box.size.x = inner.x;
  29. verticalScrollBar->box.pos.x = inner.x;
  30. verticalScrollBar->box.size.y = inner.y;
  31. // Update the container's positions from the offset
  32. container->box.pos = offset.neg().round();
  33. // Update scrollbar offsets and sizes
  34. Vec viewportSize = container->getChildrenBoundingBox().getBottomRight();
  35. Vec scrollbarOffset = offset.div(viewportSize.minus(box.size));
  36. Vec scrollbarSize = box.size.div(viewportSize);
  37. horizontalScrollBar->offset = scrollbarOffset.x;
  38. horizontalScrollBar->size = scrollbarSize.x;
  39. horizontalScrollBar->visible = (0.0 < scrollbarSize.x && scrollbarSize.x < 1.0);
  40. verticalScrollBar->offset = scrollbarOffset.y;
  41. verticalScrollBar->size = scrollbarSize.y;
  42. verticalScrollBar->visible = (0.0 < scrollbarSize.y && scrollbarSize.y < 1.0);
  43. Widget::step();
  44. }
  45. void ScrollWidget::onMouseMove(EventMouseMove &e) {
  46. // Scroll with arrow keys
  47. if (!gFocusedWidget) {
  48. float arrowSpeed = 30.0;
  49. if (windowIsShiftPressed() && windowIsModPressed())
  50. arrowSpeed /= 16.0;
  51. else if (windowIsShiftPressed())
  52. arrowSpeed *= 4.0;
  53. else if (windowIsModPressed())
  54. arrowSpeed /= 4.0;
  55. if (glfwGetKey(gWindow, GLFW_KEY_LEFT) == GLFW_PRESS) {
  56. offset.x -= arrowSpeed;
  57. }
  58. if (glfwGetKey(gWindow, GLFW_KEY_RIGHT) == GLFW_PRESS) {
  59. offset.x += arrowSpeed;
  60. }
  61. if (glfwGetKey(gWindow, GLFW_KEY_UP) == GLFW_PRESS) {
  62. offset.y -= arrowSpeed;
  63. }
  64. if (glfwGetKey(gWindow, GLFW_KEY_DOWN) == GLFW_PRESS) {
  65. offset.y += arrowSpeed;
  66. }
  67. }
  68. Widget::onMouseMove(e);
  69. }
  70. void ScrollWidget::onScroll(EventScroll &e) {
  71. offset = offset.minus(e.scrollRel);
  72. e.consumed = true;
  73. }
  74. } // namespace rack