|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- #pragma once
- #include "widgets/OpaqueWidget.hpp"
- #include "ui/common.hpp"
- #include "event.hpp"
-
-
- namespace rack {
-
-
- /** Parent must be a ScrollWidget */
- struct ScrollBar : OpaqueWidget {
- enum Orientation {
- VERTICAL,
- HORIZONTAL
- };
- Orientation orientation;
- BNDwidgetState state = BND_DEFAULT;
- float offset = 0.0;
- float size = 0.0;
-
- ScrollBar() {
- box.size = Vec(BND_SCROLLBAR_WIDTH, BND_SCROLLBAR_HEIGHT);
- }
-
- void draw(NVGcontext *vg) override {
- bndScrollBar(vg, 0.0, 0.0, box.size.x, box.size.y, state, offset, size);
- }
-
- void onDragStart(event::DragStart &e) override {
- state = BND_ACTIVE;
- windowCursorLock();
- }
-
- void onDragMove(event::DragMove &e) override;
-
- void onDragEnd(event::DragEnd &e) override {
- state = BND_DEFAULT;
- windowCursorUnlock();
- }
- };
-
-
- /** Handles a container with ScrollBar */
- struct ScrollWidget : OpaqueWidget {
- Widget *container;
- ScrollBar *horizontalScrollBar;
- ScrollBar *verticalScrollBar;
- Vec offset;
-
- ScrollWidget() {
- container = new Widget;
- addChild(container);
-
- horizontalScrollBar = new ScrollBar;
- horizontalScrollBar->orientation = ScrollBar::HORIZONTAL;
- horizontalScrollBar->visible = false;
- addChild(horizontalScrollBar);
-
- verticalScrollBar = new ScrollBar;
- verticalScrollBar->orientation = ScrollBar::VERTICAL;
- verticalScrollBar->visible = false;
- addChild(verticalScrollBar);
- }
-
- void scrollTo(Rect r) {
- Rect bound = Rect::fromMinMax(r.getBottomRight().minus(box.size), r.pos);
- offset = offset.clampBetween(bound);
- }
-
- void draw(NVGcontext *vg) override {
- nvgScissor(vg, 0, 0, box.size.x, box.size.y);
- Widget::draw(vg);
- nvgResetScissor(vg);
- }
-
- void step() override {
- Widget::step();
-
- // Clamp scroll offset
- Vec containerCorner = container->getChildrenBoundingBox().getBottomRight();
- Rect containerBox = Rect(Vec(0, 0), containerCorner.minus(box.size));
- offset = offset.clamp(containerBox);
- // Lock offset to top/left if no scrollbar will display
- if (containerBox.size.x < 0.0)
- offset.x = 0.0;
- if (containerBox.size.y < 0.0)
- offset.y = 0.0;
-
- // Update the container's positions from the offset
- container->box.pos = offset.neg().round();
-
- // Update scrollbar offsets and sizes
- Vec viewportSize = container->getChildrenBoundingBox().getBottomRight();
- Vec scrollbarOffset = offset.div(viewportSize.minus(box.size));
- Vec scrollbarSize = box.size.div(viewportSize);
-
- horizontalScrollBar->visible = (0.0 < scrollbarSize.x && scrollbarSize.x < 1.0);
- verticalScrollBar->visible = (0.0 < scrollbarSize.y && scrollbarSize.y < 1.0);
- horizontalScrollBar->offset = scrollbarOffset.x;
- verticalScrollBar->offset = scrollbarOffset.y;
- horizontalScrollBar->size = scrollbarSize.x;
- verticalScrollBar->size = scrollbarSize.y;
-
- // Resize scroll bars
- Vec inner = Vec(box.size.x - verticalScrollBar->box.size.x, box.size.y - horizontalScrollBar->box.size.y);
- horizontalScrollBar->box.pos.y = inner.y;
- verticalScrollBar->box.pos.x = inner.x;
- horizontalScrollBar->box.size.x = verticalScrollBar->visible ? inner.x : box.size.x;
- verticalScrollBar->box.size.y = horizontalScrollBar->visible ? inner.y : box.size.y;
- }
-
- void onHover(event::Hover &e) override {
- // Scroll with arrow keys
- if (!event::gContext->selectedWidget) {
- float arrowSpeed = 30.0;
- if (windowIsShiftPressed() && windowIsModPressed())
- arrowSpeed /= 16.0;
- else if (windowIsShiftPressed())
- arrowSpeed *= 4.0;
- else if (windowIsModPressed())
- arrowSpeed /= 4.0;
-
- if (glfwGetKey(gWindow, GLFW_KEY_LEFT) == GLFW_PRESS) {
- offset.x -= arrowSpeed;
- }
- if (glfwGetKey(gWindow, GLFW_KEY_RIGHT) == GLFW_PRESS) {
- offset.x += arrowSpeed;
- }
- if (glfwGetKey(gWindow, GLFW_KEY_UP) == GLFW_PRESS) {
- offset.y -= arrowSpeed;
- }
- if (glfwGetKey(gWindow, GLFW_KEY_DOWN) == GLFW_PRESS) {
- offset.y += arrowSpeed;
- }
- }
-
- OpaqueWidget::onHover(e);
- }
-
- void onHoverScroll(event::HoverScroll &e) override {
- offset = offset.minus(e.scrollDelta);
- e.target = this;
- }
-
- void onHoverKey(event::HoverKey &e) override {
- OpaqueWidget::onHoverKey(e);
- }
- };
-
-
- inline void ScrollBar::onDragMove(event::DragMove &e) {
- ScrollWidget *scrollWidget = dynamic_cast<ScrollWidget*>(parent);
- assert(scrollWidget);
- if (orientation == HORIZONTAL)
- scrollWidget->offset.x += e.mouseDelta.x;
- else
- scrollWidget->offset.y += e.mouseDelta.y;
- }
-
-
- } // namespace rack
|