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/editor.cpp | |
| parent | 51760ea4a4bdff7ff22d8baf18af8032b7a9256f (diff) | |
feat: camera movement and zoom
Diffstat (limited to 'src/editor.cpp')
| -rw-r--r-- | src/editor.cpp | 68 |
1 files changed, 62 insertions, 6 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 |
