From 860773932a67fa0a30c5943336bef6e0be0414a1 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Sat, 1 Feb 2025 22:11:28 +0500 Subject: feat: select a tile --- src/editor.cpp | 28 ++++++++++++++++++++++++++++ src/editor.hpp | 3 +++ src/tileset.cpp | 13 ++++++++----- src/tileset.hpp | 5 +++-- 4 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/editor.cpp b/src/editor.cpp index b78ec2a..65e9cf4 100644 --- a/src/editor.cpp +++ b/src/editor.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -51,6 +52,33 @@ namespace silly::editor { ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoTitleBar); + // --- TILE SELECTION --- + ImGui::BeginChild("TileSelectionRegion", ImVec2(0, 400), + ImGuiChildFlags_Border, + ImGuiWindowFlags_HorizontalScrollbar); + + float padding = 10.0f; + float imageSize = TILE_WIDTH * 2.5f; + int columns = ImGui::GetContentRegionAvail().x / (imageSize + padding); + int count = 0; + + for (auto it = this->tileset.get_tiles().begin(); + it != this->tileset.get_tiles().end(); ++it) { + if (ImGui::ImageButton(std::to_string(it->get()->id).c_str(), + it->get()->texture, + sf::Vector2f(imageSize, imageSize))) { + this->selectedTile = std::make_optional(*it); + } + + count++; + + if (count % columns != 0) { + ImGui::SameLine(); + } + } + + ImGui::EndChild(); + // --- "CREATING A NEW TILE" WINDOW --- if (ImGui::Button("Add tile") && !this->newTileState.has_value()) { this->newTileState = diff --git a/src/editor.hpp b/src/editor.hpp index eba7f14..97af5b4 100644 --- a/src/editor.hpp +++ b/src/editor.hpp @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -26,6 +27,8 @@ namespace silly::editor { private: Tileset &tileset; TileFloor &floor; + + std::optional> selectedTile; std::optional newTileState; }; } \ No newline at end of file diff --git a/src/tileset.cpp b/src/tileset.cpp index 8651a8f..5e5b47a 100644 --- a/src/tileset.cpp +++ b/src/tileset.cpp @@ -1,5 +1,7 @@ #include "tileset.hpp" +#include + namespace silly::editor { void Tileset::add_tile(const std::string &path, TilesetTileType type) { TilesetTile tile; @@ -11,18 +13,19 @@ namespace silly::editor { return; } - this->tiles.push_back(tile); + this->tiles.push_back(std::make_shared(tile)); } void Tileset::remove_tile(const TilesetTile &tile) { this->tiles.resize(std::distance( this->tiles.begin(), - std::remove_if( - this->tiles.begin(), this->tiles.end(), - [&tile](const TilesetTile &t) { return t.id == tile.id; }))); + std::remove_if(this->tiles.begin(), this->tiles.end(), + [&tile](const std::shared_ptr &t) { + return t.get()->id == tile.id; + }))); } - const std::vector &Tileset::get_tiles() const { + const std::vector> &Tileset::get_tiles() const { return this->tiles; } } \ No newline at end of file diff --git a/src/tileset.hpp b/src/tileset.hpp index 2b65356..58f5bec 100644 --- a/src/tileset.hpp +++ b/src/tileset.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include @@ -22,9 +23,9 @@ namespace silly::editor { void add_tile(const std::string &path, TilesetTileType type); void remove_tile(const TilesetTile &tile); - const std::vector &get_tiles() const; + const std::vector> &get_tiles() const; private: - std::vector tiles; + std::vector> tiles; }; } \ No newline at end of file -- cgit v1.2.3