diff --git a/includes/paterns_menu.hpp b/includes/paterns_menu.hpp index 9af513a..e82f043 100644 --- a/includes/paterns_menu.hpp +++ b/includes/paterns_menu.hpp @@ -8,17 +8,31 @@ #pragma once +#include +#include +#include +#include +#include + namespace gol { class PaternsMenu { public: - PaternsMenu() = default; + PaternsMenu(std::shared_ptr); ~PaternsMenu() = default; void Toogle(); bool isOpen(); void display(); + void refresh(); private: + void loadPatern(std::string &path); bool is_open_ = false; + std::shared_ptr context_ = nullptr; + std::map paterns_paths_list_; + std::vector paterns_name_list_; + int patern_width_ = 0; + int patern_height_ = 0; + std::vector loaded_patern_; }; } // namespace gol diff --git a/src/main.cpp b/src/main.cpp index 1c64630..6ecc7b5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -106,7 +106,7 @@ int main(int ac, char **av) { context->render = std::make_shared(context->settings_menu->getCellSize()); context->selection_menu = std::make_shared(context); context->selection = std::make_shared(context); - context->paterns_menu = std::make_shared(); + context->paterns_menu = std::make_shared(context); // Speed handling values float sim_speed = 1.0f; @@ -115,6 +115,7 @@ int main(int ac, char **av) { // Setups context->rules->setup(context->world); + context->paterns_menu->refresh(); // Diplay generations while (!WindowShouldClose()) { // Frames shinenigans diff --git a/src/paterns_menu.cpp b/src/paterns_menu.cpp index ad450fa..4d62428 100644 --- a/src/paterns_menu.cpp +++ b/src/paterns_menu.cpp @@ -8,12 +8,17 @@ #include +#include +#include + #include #include #include namespace gol { +PaternsMenu::PaternsMenu(std::shared_ptr ctx): context_(ctx) {} + void PaternsMenu::Toogle() { is_open_ = !is_open_; } @@ -22,12 +27,45 @@ bool PaternsMenu::isOpen() { return is_open_; } +void PaternsMenu::refresh() { + paterns_name_list_.clear(); + paterns_paths_list_.clear(); + auto paterns_path = context_->program_dir / "paterns"; + if (std::filesystem::exists(paterns_path) && std::filesystem::is_directory(paterns_path)) { + for (const auto& entry : std::filesystem::directory_iterator(paterns_path)) { + if (!std::filesystem::is_directory(entry) && entry.path().has_filename()) { + paterns_paths_list_[entry.path().filename()] = entry.path(); + paterns_name_list_.push_back(entry.path().filename()); + } + } + } +} + +void PaternsMenu::loadPatern(std::string &path) { + +} + void PaternsMenu::display() { if (is_open_) { ImGuiWindowFlags paterns_flags = ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoResize; - ImGui::SetNextWindowSize(ImVec2(150, 200), ImGuiCond_Always); + ImGui::SetNextWindowSize(ImVec2(200, 250), ImGuiCond_Always); ImGui::Begin("paterns", &is_open_, paterns_flags); - ImGui::Button("refresh"); + for (auto patern_name: paterns_name_list_) { + ImGui::PushID(patern_name.c_str()); + if (ImGui::Button(patern_name.c_str())) { + loadPatern(paterns_paths_list_[patern_name]); + } + ImGui::PopID(); + ImGui::SameLine(ImGui::GetWindowWidth() - 57.); + std::string del_id = patern_name.c_str(); + del_id.append("_del"); + ImGui::PushID(del_id.c_str()); + if (ImGui::Button("delete")) { + std::filesystem::remove(paterns_paths_list_[patern_name]); + refresh(); + } + ImGui::PopID(); + } ImGui::End(); } } diff --git a/src/selection_menu.cpp b/src/selection_menu.cpp index 17b6879..8cbb881 100644 --- a/src/selection_menu.cpp +++ b/src/selection_menu.cpp @@ -12,6 +12,7 @@ #include #include +#include namespace gol { @@ -90,6 +91,7 @@ void SelectionMenu::display() { } patern_file << std::flush; patern_file.close(); + context_->paterns_menu->refresh(); } } sel_ctrl_ = false;