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.

87 lines
2.3KB

  1. #include <ui/Scrollbar.hpp>
  2. #include <ui/ScrollWidget.hpp>
  3. #include <context.hpp>
  4. #include <Window.hpp>
  5. namespace rack {
  6. namespace ui {
  7. // Internal not currently used
  8. Scrollbar::Scrollbar() {
  9. box.size = math::Vec(BND_SCROLLBAR_WIDTH, BND_SCROLLBAR_HEIGHT);
  10. }
  11. Scrollbar::~Scrollbar() {
  12. }
  13. void Scrollbar::draw(const DrawArgs& args) {
  14. ScrollWidget* sw = dynamic_cast<ScrollWidget*>(parent);
  15. assert(sw);
  16. BNDwidgetState state = BND_DEFAULT;
  17. if (APP->event->getHoveredWidget() == this)
  18. state = BND_HOVER;
  19. if (APP->event->getDraggedWidget() == this)
  20. state = BND_ACTIVE;
  21. float handleOffset = sw->getHandleOffset()[vertical];
  22. float handleSize = sw->getHandleSize()[vertical];
  23. bndScrollBar(args.vg, 0.0, 0.0, box.size.x, box.size.y, state, handleOffset, handleSize);
  24. }
  25. void Scrollbar::onButton(const ButtonEvent& e) {
  26. if (e.button == GLFW_MOUSE_BUTTON_LEFT && e.action == GLFW_PRESS) {
  27. ScrollWidget* sw = dynamic_cast<ScrollWidget*>(parent);
  28. assert(sw);
  29. float pos = e.pos[vertical];
  30. pos /= box.size[vertical];
  31. float handleOffset = sw->getHandleOffset()[vertical];
  32. float handleSize = sw->getHandleSize()[vertical];
  33. float handlePos = math::rescale(handleOffset, 0.f, 1.f, handleSize / 2.f, 1.f - handleSize / 2.f);
  34. // Check if user clicked on handle
  35. if (std::fabs(pos - handlePos) > handleSize / 2.f) {
  36. // Jump to absolute position of the handle
  37. float offset = math::rescale(pos, handleSize / 2.f, 1.f - handleSize / 2.f, 0.f, 1.f);
  38. sw->offset[vertical] = sw->containerBox.pos[vertical] + offset * (sw->containerBox.size[vertical] - sw->box.size[vertical]);
  39. }
  40. }
  41. OpaqueWidget::onButton(e);
  42. }
  43. void Scrollbar::onDragStart(const DragStartEvent& e) {
  44. }
  45. void Scrollbar::onDragEnd(const DragEndEvent& e) {
  46. }
  47. void Scrollbar::onDragMove(const DragMoveEvent& e) {
  48. ScrollWidget* sw = dynamic_cast<ScrollWidget*>(parent);
  49. assert(sw);
  50. // Move handle absolutely.
  51. float mouseDelta = e.mouseDelta[vertical];
  52. mouseDelta /= getAbsoluteZoom();
  53. float handleSize = sw->getHandleSize()[vertical];
  54. float handleBound = (1.f - handleSize) * box.size[vertical];
  55. float offsetBound = sw->getContainerOffsetBound().size[vertical];
  56. float offsetDelta = mouseDelta * offsetBound / handleBound;
  57. sw->offset[vertical] += offsetDelta;
  58. }
  59. } // namespace ui
  60. } // namespace rack