diff options
| author | ilotterytea <iltsu@alright.party> | 2025-02-01 23:19:16 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-02-01 23:19:16 +0500 |
| commit | 91ffa8637cddddae07c42115788e5104a5faca7f (patch) | |
| tree | 69b392ec7ecdc82402b79a5bac3e11db50ddf81d | |
| parent | 79c7edc335fc162e9d2ef9acc5cdf41fbd1bac76 (diff) | |
feat: tile rotation
| -rw-r--r-- | src/editor.cpp | 25 | ||||
| -rw-r--r-- | src/editor.hpp | 3 | ||||
| -rw-r--r-- | src/floor.cpp | 21 | ||||
| -rw-r--r-- | src/floor.hpp | 5 | ||||
| -rw-r--r-- | src/main.cpp | 1 |
5 files changed, 50 insertions, 5 deletions
diff --git a/src/editor.cpp b/src/editor.cpp index 198620c..97979fb 100644 --- a/src/editor.cpp +++ b/src/editor.cpp @@ -2,6 +2,8 @@ #include <SFML/Graphics/RenderWindow.hpp> #include <SFML/System/Vector2.hpp> +#include <SFML/Window/Event.hpp> +#include <SFML/Window/Keyboard.hpp> #include <memory> #include <optional> #include <string> @@ -13,6 +15,17 @@ #include "tileset.hpp" namespace silly::editor { + void Editor::update(const sf::Event &event) { + if (const auto *e = event.getIf<sf::Event::KeyPressed>()) { + if (e->code == sf::Keyboard::Key::R) { + this->rotation += 90.0f; + if (this->rotation >= 360.0f) { + this->rotation = 0.0f; + } + } + } + } + void Editor::update(const sf::RenderWindow &window) { if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) || sf::Mouse::isButtonPressed(sf::Mouse::Button::Right)) { @@ -28,7 +41,8 @@ namespace silly::editor { if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) && this->selectedTile.has_value()) { - this->floor.place_tile(this->selectedTile.value(), pos); + this->floor.place_tile(this->selectedTile.value(), pos, + this->rotation); } else if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Right)) { this->floor.remove_tile(pos); } @@ -54,6 +68,7 @@ namespace silly::editor { ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoTitleBar); // --- SELECTED TILE --- + ImGui::BeginChild("SelectedTileRegion", ImVec2(200, 100)); if (this->selectedTile.has_value()) { TilesetTile *t = this->selectedTile->get(); @@ -70,6 +85,14 @@ namespace silly::editor { ImGui::Text("NO TILE SELECTED!"); ImGui::Dummy(ImVec2(64, 64)); } + ImGui::EndChild(); + + ImGui::SameLine(); + + // --- EDITOR SETTINGS --- + ImGui::BeginChild("EditorSettingsRegion", ImVec2(170, 100)); + ImGui::Text("Rotation is %.0f degrees", this->rotation); + ImGui::EndChild(); // --- TILE SELECTION --- ImGui::BeginChild("TileSelectionRegion", ImVec2(0, 400), diff --git a/src/editor.hpp b/src/editor.hpp index 97af5b4..666694f 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/Window/Event.hpp> #include <memory> #include <optional> #include <string> @@ -22,11 +23,13 @@ namespace silly::editor { : tileset(tileset), floor(floor) {} void update(const sf::RenderWindow &window); + void update(const sf::Event &event); void render(const sf::RenderWindow &window); private: Tileset &tileset; TileFloor &floor; + float rotation; std::optional<std::shared_ptr<TilesetTile>> selectedTile; std::optional<NewTileState> newTileState; diff --git a/src/floor.cpp b/src/floor.cpp index 8482b48..b8503ec 100644 --- a/src/floor.cpp +++ b/src/floor.cpp @@ -3,6 +3,7 @@ #include <SFML/Graphics/RectangleShape.hpp> #include <SFML/Graphics/RenderWindow.hpp> #include <SFML/Graphics/Sprite.hpp> +#include <SFML/System/Angle.hpp> #include <SFML/System/Vector2.hpp> #include <algorithm> #include <iterator> @@ -16,6 +17,21 @@ namespace silly::editor { sf::Sprite s(t->tile->texture); s.setPosition({t->position.x * (float)TILE_WIDTH, t->position.y * (float)TILE_HEIGHT}); + s.setRotation(sf::degrees(t->rotation)); + + sf::Vector2f origin; + sf::Vector2u size = t->tile->texture.getSize(); + + if (t->rotation == 90.0f) { + origin.y = size.y; + } else if (t->rotation == 180.0f) { + origin.x = size.x; + origin.y = size.y; + } else if (t->rotation == 270.0f) { + origin.x = size.x; + } + + s.setOrigin(origin); window.draw(s); } @@ -23,13 +39,14 @@ namespace silly::editor { } void TileFloor::place_tile(std::shared_ptr<TilesetTile> &tile, - const sf::Vector2i &position) { + const sf::Vector2i &position, + const float &rotation) { TileLayer &layer = this->layers.at(tile->type); if (!std::any_of( layer.tiles.begin(), layer.tiles.end(), [&position](const Tile &t) { return t.position == position; })) { - layer.tiles.push_back({position, tile}); + layer.tiles.push_back({tile, position, rotation}); } } void TileFloor::remove_tile(const sf::Vector2i &position) { diff --git a/src/floor.hpp b/src/floor.hpp index 1a6fe47..dcb9fa7 100644 --- a/src/floor.hpp +++ b/src/floor.hpp @@ -9,8 +9,9 @@ namespace silly::editor { struct Tile { - sf::Vector2i position; std::shared_ptr<TilesetTile> tile; + sf::Vector2i position; + float rotation; }; struct TileLayer { @@ -31,7 +32,7 @@ namespace silly::editor { void render(sf::RenderWindow &window) const; void place_tile(std::shared_ptr<TilesetTile> &tile, - const sf::Vector2i &position); + const sf::Vector2i &position, const float &rotation); void remove_tile(const sf::Vector2i &position); const int get_width() const; diff --git a/src/main.cpp b/src/main.cpp index 6c09c7b..d889b0c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -36,6 +36,7 @@ int main() { if (event.has_value()) { sf::Event e = event.value(); ImGui::SFML::ProcessEvent(window, e); + editor.update(e); } // (cv pasted from |
