diff --git a/includes/paterns_menu.hpp b/includes/paterns_menu.hpp index e82f043..5518a25 100644 --- a/includes/paterns_menu.hpp +++ b/includes/paterns_menu.hpp @@ -25,7 +25,7 @@ public: void display(); void refresh(); private: - void loadPatern(std::string &path); + bool loadPatern(std::string &path); bool is_open_ = false; std::shared_ptr context_ = nullptr; std::map paterns_paths_list_; diff --git a/src/paterns_menu.cpp b/src/paterns_menu.cpp index 4d62428..d90212c 100644 --- a/src/paterns_menu.cpp +++ b/src/paterns_menu.cpp @@ -6,34 +6,34 @@ * ------ */ -#include - -#include -#include - #include #include #include +#include +#include +#include +#include +#include + namespace gol { -PaternsMenu::PaternsMenu(std::shared_ptr ctx): context_(ctx) {} +PaternsMenu::PaternsMenu(std::shared_ptr ctx) : context_(ctx) {} -void PaternsMenu::Toogle() { - is_open_ = !is_open_; -} +void PaternsMenu::Toogle() { is_open_ = !is_open_; } -bool PaternsMenu::isOpen() { - return is_open_; -} +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()) { + 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()); } @@ -41,19 +41,45 @@ void PaternsMenu::refresh() { } } -void PaternsMenu::loadPatern(std::string &path) { - +bool PaternsMenu::loadPatern(std::string &path) { + std::ifstream file(path); + std::string file_data; + if (!file.is_open()) { + std::cerr << "Failure in opening patern : " << path << std::endl; + return false; + } + loaded_patern_.clear(); + try { + std::getline(file, file_data); + std::stringstream ss(file_data); + std::string width, height, data; + std::getline(ss, width, '|'); + std::getline(ss, height, '|'); + std::getline(ss, data, '|'); + patern_width_ = std::stoi(width); + patern_height_ = std::stoi(height); + for (int i = 0; i < patern_width_ * patern_height_; i++) { + loaded_patern_.push_back((data[i] == '1') ? 1 : 0); + } + } catch (std::exception &e) { + std::cerr << "Failure in loading patern : " << path << std::endl; + return 1; + } + return true; } void PaternsMenu::display() { if (is_open_) { - ImGuiWindowFlags paterns_flags = ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoResize; + ImGuiWindowFlags paterns_flags = + ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoResize; ImGui::SetNextWindowSize(ImVec2(200, 250), ImGuiCond_Always); ImGui::Begin("paterns", &is_open_, paterns_flags); - for (auto patern_name: paterns_name_list_) { + 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]); + if (ImGui::Button(patern_name.c_str()) && + loadPatern(paterns_paths_list_[patern_name])) { + // TODO: If patern is loaded successfuly, start the preview in the + // editor } ImGui::PopID(); ImGui::SameLine(ImGui::GetWindowWidth() - 57.); @@ -70,4 +96,4 @@ void PaternsMenu::display() { } } -} // namespace gol +} // namespace gol