From 3c37d9aca46e974ca928fa2c1d48ca547349a7d5 Mon Sep 17 00:00:00 2001 From: lejulien Date: Wed, 14 Jan 2026 13:33:48 +0100 Subject: [PATCH] wippatern_preview: Adding patern preview This adds a preview of the patern placement, checking out of bound --- CMakeLists.txt | 2 ++ includes/context.hpp | 2 ++ includes/patern_preview.hpp | 32 ++++++++++++++++++ includes/paterns_menu.hpp | 1 + includes/snapping.hpp | 12 ++----- src/main.cpp | 4 +++ src/patern_preview.cpp | 65 +++++++++++++++++++++++++++++++++++++ src/paterns_menu.cpp | 8 ++--- src/snapping.cpp | 19 +++++++++++ 9 files changed, 132 insertions(+), 13 deletions(-) create mode 100644 includes/patern_preview.hpp create mode 100644 src/patern_preview.cpp create mode 100644 src/snapping.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f03cca0..d2ebdad 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -75,6 +75,8 @@ set(SRC_CXX_FILES "./src/main.cpp" "./src/selection_menu.cpp" "./src/selection.cpp" "./src/paterns_menu.cpp" + "./src/patern_preview.cpp" + "./src/snapping.cpp" "${rlImGui_SOURCE_DIR}/rlImGui.cpp" "${imgui_SOURCE_DIR}/imgui.cpp" "${imgui_SOURCE_DIR}/imgui_draw.cpp" diff --git a/includes/context.hpp b/includes/context.hpp index 7e3c99d..6cd2293 100644 --- a/includes/context.hpp +++ b/includes/context.hpp @@ -22,6 +22,7 @@ class SelectionMenu; class ControlMenu; class Selection; class PaternsMenu; +class PaternPreview; typedef struct ctx { std::shared_ptr world = nullptr; @@ -32,6 +33,7 @@ typedef struct ctx { std::shared_ptr control_menu = nullptr; std::shared_ptr selection = nullptr; std::shared_ptr paterns_menu = nullptr; + std::shared_ptr patern_preview = nullptr; nlohmann::json config_json; std::filesystem::path program_dir; } ctx; diff --git a/includes/patern_preview.hpp b/includes/patern_preview.hpp new file mode 100644 index 0000000..a96664c --- /dev/null +++ b/includes/patern_preview.hpp @@ -0,0 +1,32 @@ +/* +* File name: patern_preview.hpp +* Author: lejulien +* Date created: 01-01-1970 00:59:59 +// Date modified: 12-01-2026 21:30:10 +* ------ +*/ + +#pragma once + +#include + +#include + +#include + +namespace gol { + +class PaternPreview { +public: + PaternPreview(std::shared_ptr); + ~PaternPreview() = default; + void update(); + void display(); + void start(); +private: + bool is_started = false; + std::shared_ptr context_; + Vector2 mouse_pos_ = {0, 0}; +}; + +} // namespace gol diff --git a/includes/paterns_menu.hpp b/includes/paterns_menu.hpp index 5518a25..bea2c85 100644 --- a/includes/paterns_menu.hpp +++ b/includes/paterns_menu.hpp @@ -30,6 +30,7 @@ private: std::shared_ptr context_ = nullptr; std::map paterns_paths_list_; std::vector paterns_name_list_; +public: int patern_width_ = 0; int patern_height_ = 0; std::vector loaded_patern_; diff --git a/includes/snapping.hpp b/includes/snapping.hpp index dfcc0fb..7192209 100644 --- a/includes/snapping.hpp +++ b/includes/snapping.hpp @@ -1,5 +1,5 @@ /* -* File name: selection.cpp +* File name: snapping.hpp * Author: lejulien * Date created: 01-01-1970 00:59:59 // Date modified: 12-01-2026 21:30:10 @@ -13,12 +13,6 @@ #include #include -Vector2 snapToGrid(Vector2 screen, int cell_size) { - return {static_cast(round(screen.x / cell_size) * cell_size), - static_cast(round(screen.y / cell_size) * cell_size)}; -} +Vector2 snapToGrid(Vector2 screen, int cell_size); -Vector2i screenToGrid(Vector2 screen, int cell_size) { - return {static_cast(round(screen.x / cell_size)), - static_cast(round(screen.y / cell_size))}; -} +Vector2i screenToGrid(Vector2 screen, int cell_size); diff --git a/src/main.cpp b/src/main.cpp index 6ecc7b5..80fdaf4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -25,6 +25,7 @@ #include #include #include +#include int main(int ac, char **av) { std::shared_ptr context = std::make_shared(); @@ -107,6 +108,7 @@ int main(int ac, char **av) { context->selection_menu = std::make_shared(context); context->selection = std::make_shared(context); context->paterns_menu = std::make_shared(context); + context->patern_preview = std::make_shared(context); // Speed handling values float sim_speed = 1.0f; @@ -136,6 +138,7 @@ int main(int ac, char **av) { // Selection behaviour context->selection->update(); + context->patern_preview->update(); context->selection_menu->update(); context->settings_menu->update(); @@ -154,6 +157,7 @@ int main(int ac, char **av) { ClearBackground(BLACK); context->render->display(context->world); context->selection->display(); + context->patern_preview->display(); // Start ImGui frame rlImGuiBegin(); context->control_menu->display(); diff --git a/src/patern_preview.cpp b/src/patern_preview.cpp new file mode 100644 index 0000000..26a87b6 --- /dev/null +++ b/src/patern_preview.cpp @@ -0,0 +1,65 @@ +/* +* File name: patern_preview.cpp +* Author: lejulien +* Date created: 01-01-1970 00:59:59 +// Date modified: 12-01-2026 21:30:10 +* ------ +*/ + +#include +#include +#include + +#include +#include +#include +#include +#include + +namespace gol { + +PaternPreview::PaternPreview(std::shared_ptr context) + : context_(context) {} + +void PaternPreview::start() { is_started = true; } + +void PaternPreview::update() { + if (!is_started) return; + mouse_pos_ = GetMousePosition(); + if (ImGui::IsMouseClicked(1)) { + is_started = false; + return; + } + // if right click stop the preview + // if left click, apply patern to current world + // mouse should pass through any present windows +} + +void PaternPreview::display() { + if (!is_started) return; + std::cout << "preview display started " << std::endl; + auto cell_size = context_->settings_menu->getCellSize(); + auto mouse_in_grid = + screenToGrid(mouse_pos_, context_->settings_menu->getCellSize()); + bool is_unplacable = + ((mouse_in_grid.x + context_->paterns_menu->patern_width_ > + context_->world->getWidth()) || + (mouse_in_grid.y + context_->paterns_menu->patern_height_ > + context_->world->getHeight())); + std::cout << "mx:" << mouse_in_grid.x << ", my:" << mouse_in_grid.y + << ", cs:" << context_->settings_menu->getCellSize() << std::endl; + for (int j = 0; j < context_->paterns_menu->patern_height_; j++) { + for (int i = 0; i < context_->paterns_menu->patern_width_; i++) { + auto cell = + context_->paterns_menu + ->loaded_patern_[i + (j * context_->paterns_menu->patern_width_)]; + if (cell) { + DrawRectangle((i + mouse_in_grid.x) * cell_size, + (j + mouse_in_grid.y) * cell_size, cell_size, cell_size, + (is_unplacable) ? RED : BLUE); + } + } + } +} + +}; // namespace gol diff --git a/src/paterns_menu.cpp b/src/paterns_menu.cpp index d90212c..1bcee0e 100644 --- a/src/paterns_menu.cpp +++ b/src/paterns_menu.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include namespace gol { @@ -63,7 +64,7 @@ bool PaternsMenu::loadPatern(std::string &path) { } } catch (std::exception &e) { std::cerr << "Failure in loading patern : " << path << std::endl; - return 1; + return false; } return true; } @@ -77,9 +78,8 @@ void PaternsMenu::display() { 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])) { - // TODO: If patern is loaded successfuly, start the preview in the - // editor + loadPatern(paterns_paths_list_[patern_name])) { + context_->patern_preview->start(); } ImGui::PopID(); ImGui::SameLine(ImGui::GetWindowWidth() - 57.); diff --git a/src/snapping.cpp b/src/snapping.cpp new file mode 100644 index 0000000..9210b64 --- /dev/null +++ b/src/snapping.cpp @@ -0,0 +1,19 @@ +/* +* File name: snapping.hpp +* Author: lejulien +* Date created: 01-01-1970 00:59:59 +// Date modified: 12-01-2026 21:30:10 +* ------ +*/ + +#include + +Vector2 snapToGrid(Vector2 screen, int cell_size) { + return {static_cast(round(screen.x / cell_size) * cell_size), + static_cast(round(screen.y / cell_size) * cell_size)}; +} + +Vector2i screenToGrid(Vector2 screen, int cell_size) { + return {static_cast(round(screen.x / cell_size)), + static_cast(round(screen.y / cell_size))}; +}