patern_preview/world: Place selection into the world

This commit is contained in:
2026-01-14 17:34:15 +01:00
parent 5c9b60163a
commit 3e7e7e2547
4 changed files with 22 additions and 8 deletions

View File

@@ -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);
}
}
}

View File

@@ -143,7 +143,6 @@ 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++) {
@@ -153,3 +152,12 @@ 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];
}
}
}