diff options
| author | ilotterytea <iltsu@alright.party> | 2025-02-01 21:39:43 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-02-01 21:39:43 +0500 |
| commit | c89db32c69c2ca3db059193591ec910d45fbbcc3 (patch) | |
| tree | fc47fd9296273a8087f250af434f78387c092d5a | |
| parent | 50e4d4c490782d878cb8777348c3c285f60cd592 (diff) | |
feat: tile creation + editor panel
| -rw-r--r-- | src/editor.cpp | 133 | ||||
| -rw-r--r-- | src/editor.hpp | 31 | ||||
| -rw-r--r-- | src/floor.cpp | 25 | ||||
| -rw-r--r-- | src/floor.hpp | 1 | ||||
| -rw-r--r-- | src/main.cpp | 9 |
5 files changed, 172 insertions, 27 deletions
diff --git a/src/editor.cpp b/src/editor.cpp new file mode 100644 index 0000000..b78ec2a --- /dev/null +++ b/src/editor.cpp @@ -0,0 +1,133 @@ +#include "editor.hpp" + +#include <SFML/Graphics/RenderWindow.hpp> +#include <SFML/System/Vector2.hpp> +#include <optional> +#include <string> + +#include "imgui-SFML.h" +#include "imgui.h" +#include "nfd.h" +#include "nfd.hpp" +#include "tileset.hpp" + +namespace silly::editor { + void Editor::update(const sf::RenderWindow &window) { + if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) || + sf::Mouse::isButtonPressed(sf::Mouse::Button::Right)) { + sf::Vector2i mousePosition = 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; + + if ((rx < mousePosition.x && mousePosition.x < rx + 16) && + (ry < mousePosition.y && mousePosition.y < ry + 16)) { + sf::Vector2i pos(x, y); + + if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) { + this->floor.place_tile(pos); + } else if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Right)) { + this->floor.remove_tile(pos); + } + } + } + } + } + } + + void Editor::render(const sf::RenderWindow &window) { + sf::Vector2u windowSize = window.getSize(); + + int width = 400; + int height = windowSize.y; + + int x = windowSize.x - width; + int y = 0; + + ImGui::SetNextWindowPos(ImVec2(x, y)); + ImGui::SetNextWindowSize(ImVec2(width, height)); + ImGui::Begin("Editor", NULL, + ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | + ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoTitleBar); + + // --- "CREATING A NEW TILE" WINDOW --- + if (ImGui::Button("Add tile") && !this->newTileState.has_value()) { + this->newTileState = + std::make_optional((NewTileState){"", {}, TILE_FLOOR}); + } + + if (this->newTileState.has_value()) { + NewTileState &state = this->newTileState.value(); + + ImGui::SetNextWindowPos( + ImVec2(windowSize.x / 2.0f - 200, windowSize.y / 2.0f - 250)); + ImGui::SetNextWindowSize(ImVec2(400, 500)); + ImGui::Begin("Creating a new tile", NULL, + ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize); + + // -- Tile type -- + ImGui::Text("Tile type"); + + if (ImGui::RadioButton("Floor", state.type == TILE_FLOOR)) { + state.type = TILE_FLOOR; + } + + ImGui::SameLine(); + + if (ImGui::RadioButton("Wall", state.type == TILE_WALL)) { + state.type = TILE_WALL; + } + + // -- Texture loading -- + ImGui::Text("2D"); + + if (!state.path.empty()) { + ImGui::Image(state.texture); + ImGui::SameLine(); + } + + if (ImGui::Button("Load texture file")) { + NFD::UniquePath outPath; + + nfdfilteritem_t filterItem[1] = {{"Images", "png"}}; + + nfdresult_t result = NFD::OpenDialog(outPath, filterItem, 1); + if (result == NFD_OKAY) { + state.path = outPath.get(); + if (!state.texture.loadFromFile(state.path)) { + state.path = ""; + } + } + } + + // -- Tile preview -- + if (!state.path.empty()) { + for (int y = 0; y <= 5; y++) { + for (int x = 0; x <= 10; x++) { + ImGui::Image(state.texture); + + if (x < 10) { + ImGui::SameLine(0, 0); + } + } + } + } + + // -- Tile creation -- + if (ImGui::Button("Create a new tile")) { + this->tileset.add_tile(state.path, state.type); + ImGui::SetWindowCollapsed(true); + } + + if (ImGui::IsWindowCollapsed()) { + ImGui::SetWindowCollapsed(false); + this->newTileState = std::nullopt; + } + + ImGui::End(); + } + + ImGui::End(); + } +}
\ No newline at end of file diff --git a/src/editor.hpp b/src/editor.hpp new file mode 100644 index 0000000..eba7f14 --- /dev/null +++ b/src/editor.hpp @@ -0,0 +1,31 @@ +#pragma once + +#include <SFML/Graphics/RenderWindow.hpp> +#include <SFML/Graphics/Texture.hpp> +#include <optional> +#include <string> + +#include "floor.hpp" +#include "tileset.hpp" + +namespace silly::editor { + struct NewTileState { + std::string path; + sf::Texture texture; + TilesetTileType type; + }; + + class Editor { + public: + Editor(Tileset &tileset, TileFloor &floor) + : tileset(tileset), floor(floor) {} + + void update(const sf::RenderWindow &window); + void render(const sf::RenderWindow &window); + + private: + Tileset &tileset; + TileFloor &floor; + std::optional<NewTileState> newTileState; + }; +}
\ No newline at end of file diff --git a/src/floor.cpp b/src/floor.cpp index f081008..9f94ad3 100644 --- a/src/floor.cpp +++ b/src/floor.cpp @@ -3,35 +3,10 @@ #include <SFML/Graphics/RectangleShape.hpp> #include <SFML/Graphics/RenderWindow.hpp> #include <SFML/System/Vector2.hpp> -#include <SFML/Window/Mouse.hpp> #include <algorithm> #include <iterator> namespace silly::editor { - void TileFloor::update(const sf::RenderWindow &window) { - if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) || - sf::Mouse::isButtonPressed(sf::Mouse::Button::Right)) { - sf::Vector2i mousePosition = sf::Mouse::getPosition(window); - - for (int x = 0; x < this->get_width(); x++) { - for (int y = 0; y < this->get_height(); y++) { - int rx = x * 16, ry = y * 16; - - if ((rx < mousePosition.x && mousePosition.x < rx + 16) && - (ry < mousePosition.y && mousePosition.y < ry + 16)) { - sf::Vector2i pos(x, y); - - if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) { - this->place_tile(pos); - } else if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Right)) { - this->remove_tile(pos); - } - } - } - } - } - } - void TileFloor::render(sf::RenderWindow &window) const { std::for_each( this->tiles.begin(), this->tiles.end(), [&window](const Tile &t) { diff --git a/src/floor.hpp b/src/floor.hpp index cf91ac5..e0bdcc4 100644 --- a/src/floor.hpp +++ b/src/floor.hpp @@ -14,7 +14,6 @@ namespace silly::editor { TileFloor(int width, int height) : width(width), height(height) {} ~TileFloor() = default; - void update(const sf::RenderWindow &window); void render(sf::RenderWindow &window) const; void place_tile(const sf::Vector2i &position); void remove_tile(const sf::Vector2i &position); diff --git a/src/main.cpp b/src/main.cpp index 2d2b148..6c09c7b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,8 +10,10 @@ #include <SFML/Window/VideoMode.hpp> #include <optional> +#include "editor.hpp" #include "floor.hpp" #include "nfd.hpp" +#include "tileset.hpp" int main() { NFD::Guard nfdGuard; @@ -20,7 +22,10 @@ int main() { window.setFramerateLimit(60); ImGui::SFML::Init(window); + silly::editor::Tileset tileset; + silly::editor::TileFloor tileFloor(30, 30); + silly::editor::Editor editor(tileset, tileFloor); sf::Clock deltaClock; while (window.isOpen()) { @@ -47,7 +52,7 @@ int main() { } } - tileFloor.update(window); + editor.update(window); ImGui::SFML::Update(window, deltaClock.restart()); @@ -67,6 +72,8 @@ int main() { } } + editor.render(window); + ImGui::SFML::Render(window); window.display(); } |
