Compare commits

..

1 Commits

Author SHA1 Message Date
3c37d9aca4 wippatern_preview: Adding patern preview
This adds a preview of the patern placement, checking out of bound
2026-01-14 16:38:25 +01:00
4 changed files with 8 additions and 22 deletions

View File

@@ -27,7 +27,6 @@ private:
bool is_started = false;
std::shared_ptr<ctx> context_;
Vector2 mouse_pos_ = {0, 0};
bool is_unplacable_ = false;
};
} // namespace gol

View File

@@ -37,7 +37,6 @@ public:
void setCell(int x, int y);
void resize(int width, int height); // destructive
std::vector<uint32_t> getSelection(Vector2i &origin, Vector2i &size);
void setSelection(Vector2i &origin, Vector2i &size, std::vector<uint32_t> &data);
// Private members
private:

View File

@@ -30,28 +30,24 @@ void PaternPreview::update() {
is_started = false;
return;
}
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;
}
// 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());
is_unplacable_ =
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 =
@@ -60,7 +56,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);
}
}
}

View File

@@ -143,6 +143,7 @@ void World::resize(int width, int height) {
}
std::vector<uint32_t> World::getSelection(Vector2i &origin, Vector2i &size) {
// We assume the selection is in the grid for now
std::vector<uint32_t> data = {static_cast<uint32_t>(size.x),
static_cast<uint32_t>(size.y)};
for (int y = origin.y; y < origin.y + size.y; y++) {
@@ -152,12 +153,3 @@ std::vector<uint32_t> World::getSelection(Vector2i &origin, Vector2i &size) {
}
return data;
}
void World::setSelection(Vector2i &origin, Vector2i &size, std::vector<uint32_t> &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];
}
}
}