Skip to content

Commit

Permalink
📜 Implement view scroll with mouse wheel; (#163)
Browse files Browse the repository at this point in the history
* implement view scroll with mouse wheel;

* oops
  • Loading branch information
zethon authored May 22, 2022
1 parent b32b0f2 commit 1997b5b
Showing 1 changed file with 32 additions and 35 deletions.
67 changes: 32 additions & 35 deletions src/Scenes/Scene.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <lua.hpp>
#include <boost/range/adaptor/indexed.hpp>

#include <fmt/core.h>

Expand Down Expand Up @@ -723,7 +723,7 @@ void Scene::createItems()
// default info for the item
ItemInstanceInfo groupinfo = data.get<ItemInstanceInfo>();
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<ItemInstanceInfo>();
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
}
}

Expand Down

0 comments on commit 1997b5b

Please sign in to comment.