From 6e6aa6ef94e0ce5d705869119c842600e9478ab1 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Fri, 1 Mar 2019 06:21:57 -0500 Subject: [PATCH] Add zooming with Ctrl+Scroll to RackScrollWidget. --- include/app/RackScrollWidget.hpp | 6 ++++-- src/app/RackScrollWidget.cpp | 31 ++++++++++++++++++++++--------- src/app/Scene.cpp | 2 +- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/include/app/RackScrollWidget.hpp b/include/app/RackScrollWidget.hpp index 25f103da..14ed7b44 100644 --- a/include/app/RackScrollWidget.hpp +++ b/include/app/RackScrollWidget.hpp @@ -10,8 +10,10 @@ namespace app { struct RackScrollWidget : ui::ScrollWidget { - widget::ZoomWidget *zoom; - RackWidget *rack; + widget::ZoomWidget *zoomWidget; + RackWidget *rackWidget; + /** The pivot point for zooming */ + math::Vec zoomPos; RackScrollWidget(); void step() override; diff --git a/src/app/RackScrollWidget.cpp b/src/app/RackScrollWidget.cpp index b3891884..6b5243ac 100644 --- a/src/app/RackScrollWidget.cpp +++ b/src/app/RackScrollWidget.cpp @@ -10,29 +10,37 @@ namespace app { RackScrollWidget::RackScrollWidget() { - zoom = new widget::ZoomWidget; - container->addChild(zoom); + zoomWidget = new widget::ZoomWidget; + container->addChild(zoomWidget); - rack = new RackWidget; - zoom->addChild(rack); + rackWidget = new RackWidget; + zoomWidget->addChild(rackWidget); } void RackScrollWidget::step() { - zoom->setZoom(std::round(settings.zoom * 100) / 100); + float zoom = std::round(settings.zoom * 100) / 100; + if (zoom != zoomWidget->zoom) { + // Set offset based on zoomPos + offset = offset.plus(zoomPos).div(zoomWidget->zoom).mult(zoom).minus(zoomPos); + // Set zoom + zoomWidget->setZoom(zoom); + } + + zoomPos = box.size.div(2); // Resize RackWidget to be a bit larger than the viewport - rack->box.size = box.size + rackWidget->box.size = box.size .minus(container->box.pos) .plus(math::Vec(500, 500)) - .div(zoom->zoom); + .div(zoomWidget->zoom); // Resize ZoomWidget - zoom->box.size = rack->box.size.mult(zoom->zoom); + zoomWidget->box.size = rackWidget->box.size.mult(zoomWidget->zoom); // Scroll rack if dragging cable near the edge of the screen math::Vec pos = APP->window->mousePos; math::Rect viewport = getViewport(box.zeroPos()); - if (rack->incompleteCable) { + if (rackWidget->incompleteCable) { float margin = 20.0; float speed = 15.0; if (pos.x <= viewport.pos.x + margin) @@ -83,6 +91,11 @@ void RackScrollWidget::onHover(const widget::HoverEvent &e) { void RackScrollWidget::onHoverScroll(const widget::HoverScrollEvent &e) { if ((APP->window->getMods() & WINDOW_MOD_MASK) == WINDOW_MOD_CTRL) { + // Increase zoom + float zoomDelta = e.scrollDelta.y / 50 / 4; + settings.zoom *= std::pow(2, zoomDelta); + settings.zoom = math::clamp(settings.zoom, 0.25f, 2.f); + zoomPos = e.pos; e.consume(this); return; } diff --git a/src/app/Scene.cpp b/src/app/Scene.cpp index ac61c9a5..7af96948 100644 --- a/src/app/Scene.cpp +++ b/src/app/Scene.cpp @@ -19,7 +19,7 @@ Scene::Scene() { rackScroll = new RackScrollWidget; addChild(rackScroll); - rack = rackScroll->rack; + rack = rackScroll->rackWidget; toolbar = new Toolbar; addChild(toolbar);