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.

64 lines
1.7KB

  1. #include "widgets.hpp"
  2. #include "gui.hpp"
  3. namespace rack {
  4. ScrollWidget::ScrollWidget() {
  5. container = new Widget();
  6. addChild(container);
  7. horizontalScrollBar = new ScrollBar();
  8. horizontalScrollBar->orientation = ScrollBar::HORIZONTAL;
  9. addChild(horizontalScrollBar);
  10. verticalScrollBar = new ScrollBar();
  11. verticalScrollBar->orientation = ScrollBar::VERTICAL;
  12. addChild(verticalScrollBar);
  13. }
  14. void ScrollWidget::step() {
  15. // Clamp scroll offset
  16. Vec containerCorner = container->getChildrenBoundingBox().getBottomRight();
  17. offset = offset.clamp(Rect(Vec(0, 0), containerCorner.minus(box.size)));
  18. // Resize scroll bars
  19. Vec inner = Vec(box.size.x - verticalScrollBar->box.size.x, box.size.y - horizontalScrollBar->box.size.y);
  20. horizontalScrollBar->box.pos.y = inner.y;
  21. horizontalScrollBar->box.size.x = inner.x;
  22. verticalScrollBar->box.pos.x = inner.x;
  23. verticalScrollBar->box.size.y = inner.y;
  24. // Update the container's positions from the offset
  25. container->box.pos = offset.neg().round();
  26. Widget::step();
  27. }
  28. Widget *ScrollWidget::onMouseMove(Vec pos, Vec mouseRel) {
  29. if (!gFocusedWidget) {
  30. const float arrowSpeed = 50.0;
  31. if (glfwGetKey(gWindow, GLFW_KEY_LEFT) == GLFW_PRESS) {
  32. offset = offset.minus(Vec(1, 0).mult(arrowSpeed));
  33. }
  34. if (glfwGetKey(gWindow, GLFW_KEY_RIGHT) == GLFW_PRESS) {
  35. offset = offset.minus(Vec(-1, 0).mult(arrowSpeed));
  36. }
  37. if (glfwGetKey(gWindow, GLFW_KEY_UP) == GLFW_PRESS) {
  38. offset = offset.minus(Vec(0, 1).mult(arrowSpeed));
  39. }
  40. if (glfwGetKey(gWindow, GLFW_KEY_DOWN) == GLFW_PRESS) {
  41. offset = offset.minus(Vec(0, -1).mult(arrowSpeed));
  42. }
  43. }
  44. return OpaqueWidget::onMouseMove(pos, mouseRel);
  45. }
  46. bool ScrollWidget::onScrollOpaque(Vec scrollRel) {
  47. offset = offset.minus(scrollRel);
  48. return true;
  49. }
  50. } // namespace rack