From 4d6e94ad9b905df16c0c67ea9043395a1f19400a Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Sun, 2 Feb 2025 01:02:51 +0500 Subject: feat: camera movement and zoom --- src/editor.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 62 insertions(+), 6 deletions(-) (limited to 'src/editor.cpp') 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 #include +#include #include #include #include +#include #include #include #include @@ -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()) { 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()) { + if (e->button == sf::Mouse::Button::Middle) { + isDragging = true; + lastMousePosition = + window.mapPixelToCoords(sf::Mouse::getPosition(window)); + } + } + + if (const auto *e = event.getIf()) { + if (e->button == sf::Mouse::Button::Middle) { + isDragging = false; + lastMousePosition = {}; + } + } + + // world zoom + if (const auto *e = event.getIf()) { + 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 -- cgit v1.2.3