From 07ac0fe25109717d4d7f9bcc7ae313a25b839cb0 Mon Sep 17 00:00:00 2001
From: mafaldo <mafaldo@heavyhammer.home>
Date: Mon, 12 Feb 2018 19:27:49 +0100
Subject: add pan

---
 src/main/cpp/Subconscious.cpp | 18 ++++++++++++++++++
 src/main/cpp/WorldScene.cpp   | 14 ++++++++++++--
 src/main/headers/Scene.hpp    |  1 +
 3 files changed, 31 insertions(+), 2 deletions(-)

(limited to 'src')

diff --git a/src/main/cpp/Subconscious.cpp b/src/main/cpp/Subconscious.cpp
index b53a38b..542e580 100644
--- a/src/main/cpp/Subconscious.cpp
+++ b/src/main/cpp/Subconscious.cpp
@@ -32,6 +32,22 @@ void Subconscious::run()
             switch (event.type) {
             /*** Keyboard Events ***/
             case sf::Event::KeyPressed:
+                switch (event.key.code) {
+                case sf::Keyboard::Up:
+                    _currentScene->pan(0, 10);
+                    break;
+                case sf::Keyboard::Down:
+                    _currentScene->pan(0, -10);
+                    break;
+                case sf::Keyboard::Left:
+                    _currentScene->pan(10, 0);
+                    break;
+                case sf::Keyboard::Right:
+                    _currentScene->pan(-10, 0);
+                    break;
+                default:
+                    break;
+                }
                 break;
 
             case sf::Event::KeyReleased:
@@ -66,6 +82,8 @@ void Subconscious::run()
             case sf::Event::Resized:
                 _currentScene->resize(event.size);
                 break;
+            default:
+                break;
             }
         }
 
diff --git a/src/main/cpp/WorldScene.cpp b/src/main/cpp/WorldScene.cpp
index 973c6e6..cefeda4 100644
--- a/src/main/cpp/WorldScene.cpp
+++ b/src/main/cpp/WorldScene.cpp
@@ -16,7 +16,7 @@ void WorldScene::render()
     rect.setOutlineColor(sf::Color::Black);
 
     for (const Tile &tile : map.tiles()) {
-        rect.setPosition(tile.x * _tileSize, tile.y * _tileSize);
+        rect.setPosition(tile.x * _tileSize + _panX, tile.y * _tileSize + _panY);
         _window.draw(rect);
     }
 }
@@ -52,4 +52,14 @@ void WorldScene::zoom(float factor)
 }
 
 void WorldScene::pan(int dx, int dy)
-{}
+{
+    _panX = _panX + dx;
+    _panY = _panY + dy;
+
+    sf::View view = _window.getView();
+
+    view.setCenter(
+        view.getCenter().x + _panX,
+        view.getCenter().y + _panY
+    );
+}
diff --git a/src/main/headers/Scene.hpp b/src/main/headers/Scene.hpp
index 757202c..19a32dd 100644
--- a/src/main/headers/Scene.hpp
+++ b/src/main/headers/Scene.hpp
@@ -15,6 +15,7 @@ public:
     virtual void click(const sf::Event::MouseButtonEvent &click) {}
     virtual void keyPress(const sf::Event::KeyEvent &event) {}
     virtual void zoom(float factor) {}
+    virtual void pan(int dx, int dy) {};
 
 protected:
     float _zoom = 1;
-- 
cgit v1.2.1