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.

88 lines
2.3KB

  1. #include <ui/Scrollbar.hpp>
  2. #include <ui/ScrollWidget.hpp>
  3. #include <context.hpp>
  4. #include <window/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. nvgAlpha(args.vg, 0.5);
  24. bndScrollBar(args.vg, 0.0, 0.0, box.size.x, box.size.y, state, handleOffset, handleSize);
  25. }
  26. void Scrollbar::onButton(const ButtonEvent& e) {
  27. if (e.button == GLFW_MOUSE_BUTTON_LEFT && e.action == GLFW_PRESS) {
  28. ScrollWidget* sw = dynamic_cast<ScrollWidget*>(parent);
  29. assert(sw);
  30. float pos = e.pos[vertical];
  31. pos /= box.size[vertical];
  32. float handleOffset = sw->getHandleOffset()[vertical];
  33. float handleSize = sw->getHandleSize()[vertical];
  34. float handlePos = math::rescale(handleOffset, 0.f, 1.f, handleSize / 2.f, 1.f - handleSize / 2.f);
  35. // Check if user clicked on handle
  36. if (std::fabs(pos - handlePos) > handleSize / 2.f) {
  37. // Jump to absolute position of the handle
  38. float offset = math::rescale(pos, handleSize / 2.f, 1.f - handleSize / 2.f, 0.f, 1.f);
  39. sw->offset[vertical] = sw->containerBox.pos[vertical] + offset * (sw->containerBox.size[vertical] - sw->box.size[vertical]);
  40. }
  41. }
  42. OpaqueWidget::onButton(e);
  43. }
  44. void Scrollbar::onDragStart(const DragStartEvent& e) {
  45. }
  46. void Scrollbar::onDragEnd(const DragEndEvent& e) {
  47. }
  48. void Scrollbar::onDragMove(const DragMoveEvent& e) {
  49. ScrollWidget* sw = dynamic_cast<ScrollWidget*>(parent);
  50. assert(sw);
  51. // Move handle absolutely.
  52. float mouseDelta = e.mouseDelta[vertical];
  53. mouseDelta /= getAbsoluteZoom();
  54. float handleSize = sw->getHandleSize()[vertical];
  55. float handleBound = (1.f - handleSize) * box.size[vertical];
  56. float offsetBound = sw->getContainerOffsetBound().size[vertical];
  57. float offsetDelta = mouseDelta * offsetBound / handleBound;
  58. sw->offset[vertical] += offsetDelta;
  59. }
  60. } // namespace ui
  61. } // namespace rack