diff --git a/includes/patern_preview.hpp b/includes/patern_preview.hpp index a96664c..218afb5 100644 --- a/includes/patern_preview.hpp +++ b/includes/patern_preview.hpp @@ -27,6 +27,7 @@ private: bool is_started = false; std::shared_ptr context_; Vector2 mouse_pos_ = {0, 0}; + bool is_unplacable_ = false; }; } // namespace gol diff --git a/includes/world.hpp b/includes/world.hpp index c61e373..b21e93f 100644 --- a/includes/world.hpp +++ b/includes/world.hpp @@ -37,6 +37,7 @@ public: void setCell(int x, int y); void resize(int width, int height); // destructive std::vector getSelection(Vector2i &origin, Vector2i &size); + void setSelection(Vector2i &origin, Vector2i &size, std::vector &data); // Private members private: diff --git a/src/patern_preview.cpp b/src/patern_preview.cpp index 26a87b6..0de2d63 100644 --- a/src/patern_preview.cpp +++ b/src/patern_preview.cpp @@ -30,24 +30,28 @@ void PaternPreview::update() { is_started = false; return; } - // if right click stop the preview - // if left click, apply patern to current world + if (ImGui::IsMouseClicked(0) && !is_unplacable_) { + auto selection_pos = + screenToGrid(mouse_pos_, context_->settings_menu->getCellSize()); + Vector2i size = {context_->paterns_menu->patern_width_, + context_->paterns_menu->patern_height_}; + context_->world->setSelection(selection_pos, size, + context_->paterns_menu->loaded_patern_); + is_started = false; + } // 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 = + 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 = @@ -56,7 +60,7 @@ void PaternPreview::display() { 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); + (is_unplacable_) ? RED : BLUE); } } } diff --git a/src/world.cpp b/src/world.cpp index ae653ba..a66dc14 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -143,7 +143,6 @@ void World::resize(int width, int height) { } std::vector World::getSelection(Vector2i &origin, Vector2i &size) { - // We assume the selection is in the grid for now std::vector data = {static_cast(size.x), static_cast(size.y)}; for (int y = origin.y; y < origin.y + size.y; y++) { @@ -153,3 +152,12 @@ std::vector World::getSelection(Vector2i &origin, Vector2i &size) { } return data; } + +void World::setSelection(Vector2i &origin, Vector2i &size, std::vector &data) { + + for (int y = 0; y < size.y; y++) { + for (int x = 0; x < size.x; x++) { + (*_data)[(x + origin.x) + (y + origin.y) * _width] = data[x + y * size.x]; + } + } +}