diff options
| author | ilotterytea <iltsu@alright.party> | 2025-02-02 01:02:51 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-02-02 01:02:51 +0500 |
| commit | 4d6e94ad9b905df16c0c67ea9043395a1f19400a (patch) | |
| tree | 8bbcabad05d352049cccdd2089112d2f6b6e76d5 /src | |
| parent | 51760ea4a4bdff7ff22d8baf18af8032b7a9256f (diff) | |
feat: camera movement and zoom
Diffstat (limited to 'src')
| -rw-r--r-- | src/editor.cpp | 68 | ||||
| -rw-r--r-- | src/editor.hpp | 11 | ||||
| -rw-r--r-- | src/main.cpp | 7 |
3 files changed, 76 insertions, 10 deletions
diff --git a/src/editor.cpp b/src/editor.cpp index 3efc1ad..f19cf65 100644 --- a/src/editor.cpp +++ b/src/editor.cpp @@ -1,9 +1,12 @@ #include "editor.hpp" +#include <SFML/Graphics/Rect.hpp> #include <SFML/Graphics/RenderWindow.hpp> +#include <SFML/Graphics/View.hpp> #include <SFML/System/Vector2.hpp> #include <SFML/Window/Event.hpp> #include <SFML/Window/Keyboard.hpp> +#include <SFML/Window/Mouse.hpp> #include <memory> #include <optional> #include <string> @@ -15,7 +18,8 @@ #include "tileset.hpp" namespace silly::editor { - void Editor::update(const sf::Event &event) { + void Editor::update(const sf::Event &event, sf::RenderWindow &window) { + // tile rotation if (const auto *e = event.getIf<sf::Event::KeyPressed>()) { if (e->code == sf::Keyboard::Key::R) { this->rotation += 90.0f; @@ -24,19 +28,57 @@ namespace silly::editor { } } } + + // world movement + if (const auto *e = event.getIf<sf::Event::MouseButtonPressed>()) { + if (e->button == sf::Mouse::Button::Middle) { + isDragging = true; + lastMousePosition = + window.mapPixelToCoords(sf::Mouse::getPosition(window)); + } + } + + if (const auto *e = event.getIf<sf::Event::MouseButtonReleased>()) { + if (e->button == sf::Mouse::Button::Middle) { + isDragging = false; + lastMousePosition = {}; + } + } + + // world zoom + if (const auto *e = event.getIf<sf::Event::MouseWheelScrolled>()) { + sf::FloatRect visibleArea({0.f, 0.f}, sf::Vector2f(window.getSize())); + sf::View view(visibleArea); + + this->zoom -= e->delta; + + this->zoom = (this->zoom * 10.0f) / 10.0f; + + if (this->zoom >= 1.0f) { + this->zoom = 1.0f; + } else if (this->zoom <= 0.3f) { + this->zoom = 0.3f; + } + + view.zoom(this->zoom); + view.setCenter(window.getView().getCenter()); + window.setView(view); + } } - void Editor::update(const sf::RenderWindow &window) { + void Editor::update(sf::RenderWindow &window) { + // tile placement if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) || sf::Mouse::isButtonPressed(sf::Mouse::Button::Right)) { - sf::Vector2i mousePosition = sf::Mouse::getPosition(window); + sf::Vector2f mousePosition = + window.mapPixelToCoords(sf::Mouse::getPosition(window)); for (int x = 0; x < this->floor.get_width(); x++) { for (int y = 0; y < this->floor.get_height(); y++) { - int rx = x * 16, ry = y * 16; + int rx = x * TILE_WIDTH, ry = y * TILE_HEIGHT; - if ((rx < mousePosition.x && mousePosition.x < rx + 16) && - (ry < mousePosition.y && mousePosition.y < ry + 16) && + if ((rx < mousePosition.x && mousePosition.x < rx + TILE_WIDTH) && + (ry < mousePosition.y && mousePosition.y < ry + TILE_HEIGHT) && // editor related mousePosition.x < window.getSize().x - 400.0f && !this->newTileState.has_value()) { @@ -54,6 +96,18 @@ namespace silly::editor { } } } + + // world movement + if (isDragging) { + sf::Vector2f mousePosition = + window.mapPixelToCoords(sf::Mouse::getPosition(window)); + + sf::Vector2f deltaPosition = lastMousePosition - mousePosition; + + sf::View view = window.getView(); + view.move(deltaPosition); + window.setView(view); + } } void Editor::render(const sf::RenderWindow &window) { @@ -204,4 +258,6 @@ namespace silly::editor { ImGui::End(); } + + const float Editor::get_zoom() const { return this->zoom; } }
\ No newline at end of file diff --git a/src/editor.hpp b/src/editor.hpp index 666694f..13f9cac 100644 --- a/src/editor.hpp +++ b/src/editor.hpp @@ -2,6 +2,7 @@ #include <SFML/Graphics/RenderWindow.hpp> #include <SFML/Graphics/Texture.hpp> +#include <SFML/System/Vector2.hpp> #include <SFML/Window/Event.hpp> #include <memory> #include <optional> @@ -22,15 +23,21 @@ namespace silly::editor { Editor(Tileset &tileset, TileFloor &floor) : tileset(tileset), floor(floor) {} - void update(const sf::RenderWindow &window); - void update(const sf::Event &event); + void update(sf::RenderWindow &window); + void update(const sf::Event &event, sf::RenderWindow &window); void render(const sf::RenderWindow &window); + const float get_zoom() const; private: Tileset &tileset; TileFloor &floor; float rotation; + // need for world movement and zoom + sf::Vector2f lastMousePosition; + float zoom = 1.0f; + bool isDragging = false; + std::optional<std::shared_ptr<TilesetTile>> selectedTile; std::optional<NewTileState> newTileState; }; diff --git a/src/main.cpp b/src/main.cpp index d889b0c..084671a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -36,7 +36,7 @@ int main() { if (event.has_value()) { sf::Event e = event.value(); ImGui::SFML::ProcessEvent(window, e); - editor.update(e); + editor.update(e, window); } // (cv pasted from @@ -45,7 +45,10 @@ int main() { if (const auto* resized = event->getIf<sf::Event::Resized>()) { // update the view to the new size of the window sf::FloatRect visibleArea({0.f, 0.f}, sf::Vector2f(resized->size)); - window.setView(sf::View(visibleArea)); + sf::View view(visibleArea); + view.setCenter(window.getView().getCenter()); + view.zoom(editor.get_zoom()); + window.setView(view); } if (event->is<sf::Event::Closed>()) { |
