selection_menu: Move it to it's dedicated class
This commit is contained in:
109
src/selection_menu.cpp
Normal file
109
src/selection_menu.cpp
Normal file
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
* File name: selection_menu.cpp
|
||||
* Author: lejulien
|
||||
* Date created: 13-01-2026 22:12:44
|
||||
// Date modified: 13-01-2026 22:18:58
|
||||
* ------
|
||||
*/
|
||||
|
||||
#include <unistd.h>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <filesystem>
|
||||
|
||||
#include <selection_menu.hpp>
|
||||
|
||||
namespace gol {
|
||||
|
||||
SelectionMenu::SelectionMenu(std::shared_ptr<ctx> context): context_(context) {
|
||||
selectionTexture_ = LoadRenderTexture(200, 200);
|
||||
patern_name_[0] = '\0';
|
||||
|
||||
}
|
||||
|
||||
SelectionMenu::~SelectionMenu() {
|
||||
UnloadRenderTexture(selectionTexture_);
|
||||
}
|
||||
|
||||
void SelectionMenu::update() {
|
||||
if (!sel_ctrl_) {
|
||||
patern_name_[0] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
void SelectionMenu::open() {
|
||||
sel_ctrl_ = true;
|
||||
}
|
||||
|
||||
void SelectionMenu::setSelection(std::vector<uint32_t> selection) {
|
||||
sel_data_ = selection;
|
||||
}
|
||||
|
||||
void SelectionMenu::display() {
|
||||
if (sel_ctrl_) {
|
||||
BeginTextureMode(selectionTexture_);
|
||||
ClearBackground(BLACK);
|
||||
auto max_size = (sel_data_[0] > sel_data_[1]) ? sel_data_[0] : sel_data_[1];
|
||||
int fitted_width = 200 / max_size;
|
||||
auto sel_it = sel_data_.begin();
|
||||
sel_it += 2; // skip dimensions
|
||||
for (int j = 0; j < sel_data_[1]; j++) {
|
||||
for (int i = 0; i < sel_data_[0]; i++) {
|
||||
if (*sel_it == 1) {
|
||||
DrawRectangle(i * fitted_width, (sel_data_[1] -j-1) * fitted_width, fitted_width, fitted_width, WHITE);
|
||||
}
|
||||
sel_it++;
|
||||
}
|
||||
}
|
||||
EndTextureMode();
|
||||
ImGuiWindowFlags settings_flags =
|
||||
ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoResize;
|
||||
ImGui::Begin("Selection", &sel_ctrl_, settings_flags);
|
||||
rlImGuiImageSize(&selectionTexture_.texture, 200, 200);
|
||||
ImGui::InputText("patern_name", patern_name_, 255);
|
||||
if (ImGui::Button("Save")) {
|
||||
char path_buf[1024];
|
||||
ssize_t len = readlink("/proc/self/exe", path_buf, sizeof(path_buf)-1);
|
||||
if (len != -1) {
|
||||
// Create paterns dir if not present
|
||||
std::filesystem::path paterns_dir = std::filesystem::path(path_buf).parent_path() / "paterns";
|
||||
if (!std::filesystem::exists(paterns_dir) && !std::filesystem::create_directory(paterns_dir)) {
|
||||
std::cerr << "Failed to create paterns directory" << std::endl;
|
||||
} else { // Could be optimized by early returning in a function
|
||||
std::ofstream patern_file;
|
||||
paterns_dir += '/';
|
||||
paterns_dir += patern_name_;
|
||||
patern_file.open(paterns_dir);
|
||||
if (!patern_file) {
|
||||
std::cerr << "Failed to create the patern file" << std::endl;
|
||||
} else {
|
||||
auto sel_it = sel_data_.begin();
|
||||
sel_it += 2; // skip dimensions
|
||||
patern_file << sel_data_[0];
|
||||
patern_file << "|"; // Separator needed to split as ascii values
|
||||
patern_file << sel_data_[1];
|
||||
patern_file << "|"; // Separator needed to split as ascii values
|
||||
for (int j = 0; j < sel_data_[1]; j++) {
|
||||
for (int i = 0; i < sel_data_[0]; i++) {
|
||||
patern_file << std::to_string(*sel_it);
|
||||
if (*sel_it == 1) {
|
||||
}
|
||||
sel_it++;
|
||||
}
|
||||
}
|
||||
patern_file << std::flush;
|
||||
patern_file.close();
|
||||
}
|
||||
}
|
||||
sel_ctrl_ = false;
|
||||
}
|
||||
}
|
||||
ImGui::SameLine();
|
||||
if (ImGui::Button("Discard")) {
|
||||
sel_ctrl_ = false;
|
||||
}
|
||||
ImGui::End();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace gol
|
||||
Reference in New Issue
Block a user