From 1997b5b07dea2eb8708a68f9d3383f1aa3f9385b Mon Sep 17 00:00:00 2001 From: Addy Date: Sun, 22 May 2022 07:48:06 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=9C=20Implement=20view=20scroll=20with?= =?UTF-8?q?=20mouse=20wheel;=20(#163)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * implement view scroll with mouse wheel; * oops --- src/Scenes/Scene.cpp | 67 +++++++++++++++++++++----------------------- 1 file changed, 32 insertions(+), 35 deletions(-) diff --git a/src/Scenes/Scene.cpp b/src/Scenes/Scene.cpp index 05e349c5..1aadcfa2 100644 --- a/src/Scenes/Scene.cpp +++ b/src/Scenes/Scene.cpp @@ -1,4 +1,4 @@ -#include +#include #include @@ -723,7 +723,7 @@ void Scene::createItems() // default info for the item ItemInstanceInfo groupinfo = data.get(); groupinfo.objid = itemid; // we don't want to require the objid to be set in json - + for (const auto& instance : data["instances"]) { auto instanceinfo = instance.get(); @@ -888,7 +888,36 @@ void Scene::adjustView() PollResult Scene::privatePollHandler(const sf::Event& e) { - if (e.type == sf::Event::KeyPressed) + if (e.type == sf::Event::MouseWheelScrolled) + { + constexpr float ZOOM_AMOUNT = 0.035f; + if ((_background->minzoom() == 0.f && _background->maxzoom() == 0.f) + || e.mouseWheelScroll.delta == 0) + { + return {}; + } + + auto viewSize = _gameView.getSize(); + const auto defaultSize = _window.getDefaultView().getSize(); + + if (e.mouseWheelScroll.delta > 0) + { + viewSize.x -= defaultSize.x * ZOOM_AMOUNT; + viewSize.y -= defaultSize.y * ZOOM_AMOUNT; + } + else + { + viewSize.x += defaultSize.x * ZOOM_AMOUNT; + viewSize.y += defaultSize.y * ZOOM_AMOUNT; + } + + if (float ratio = (viewSize.x / defaultSize.x); + ratio >= _background->minzoom() && ratio <= _background->maxzoom()) + { + _gameView.setSize(viewSize); + } + } + else if (e.type == sf::Event::KeyPressed) { switch (e.key.code) { @@ -1143,38 +1172,6 @@ PollResult Scene::privatePollHandler(const sf::Event& e) _player->dance(); } break; - - case sf::Keyboard::Equal: - { - constexpr float ZOOM_AMOUNT = 0.05f; - if (_background->minzoom() == 0.f - && _background->maxzoom() == 0.f) - { - break; - } - - auto viewSize = _gameView.getSize(); - const auto defaultSize = _window.getDefaultView().getSize(); - - if (sf::Keyboard::isKeyPressed(sf::Keyboard::LShift) - || sf::Keyboard::isKeyPressed(sf::Keyboard::RShift)) - { - viewSize.x += defaultSize.x * ZOOM_AMOUNT; - viewSize.y += defaultSize.y * ZOOM_AMOUNT; - } - else - { - viewSize.x -= defaultSize.x * ZOOM_AMOUNT; - viewSize.y -= defaultSize.y * ZOOM_AMOUNT; - } - - if (float ratio = (viewSize.x / defaultSize.x); - ratio >= _background->minzoom() && ratio <= _background->maxzoom()) - { - _gameView.setSize(viewSize); - } - } - break; } }