patern_preview/world: Place selection into the world
This commit is contained in:
@@ -27,6 +27,7 @@ private:
|
|||||||
bool is_started = false;
|
bool is_started = false;
|
||||||
std::shared_ptr<ctx> context_;
|
std::shared_ptr<ctx> context_;
|
||||||
Vector2 mouse_pos_ = {0, 0};
|
Vector2 mouse_pos_ = {0, 0};
|
||||||
|
bool is_unplacable_ = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace gol
|
} // namespace gol
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ public:
|
|||||||
void setCell(int x, int y);
|
void setCell(int x, int y);
|
||||||
void resize(int width, int height); // destructive
|
void resize(int width, int height); // destructive
|
||||||
std::vector<uint32_t> getSelection(Vector2i &origin, Vector2i &size);
|
std::vector<uint32_t> getSelection(Vector2i &origin, Vector2i &size);
|
||||||
|
void setSelection(Vector2i &origin, Vector2i &size, std::vector<uint32_t> &data);
|
||||||
|
|
||||||
// Private members
|
// Private members
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -30,24 +30,28 @@ void PaternPreview::update() {
|
|||||||
is_started = false;
|
is_started = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// if right click stop the preview
|
if (ImGui::IsMouseClicked(0) && !is_unplacable_) {
|
||||||
// if left click, apply patern to current world
|
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
|
// mouse should pass through any present windows
|
||||||
}
|
}
|
||||||
|
|
||||||
void PaternPreview::display() {
|
void PaternPreview::display() {
|
||||||
if (!is_started) return;
|
if (!is_started) return;
|
||||||
std::cout << "preview display started " << std::endl;
|
|
||||||
auto cell_size = context_->settings_menu->getCellSize();
|
auto cell_size = context_->settings_menu->getCellSize();
|
||||||
auto mouse_in_grid =
|
auto mouse_in_grid =
|
||||||
screenToGrid(mouse_pos_, context_->settings_menu->getCellSize());
|
screenToGrid(mouse_pos_, context_->settings_menu->getCellSize());
|
||||||
bool is_unplacable =
|
is_unplacable_ =
|
||||||
((mouse_in_grid.x + context_->paterns_menu->patern_width_ >
|
((mouse_in_grid.x + context_->paterns_menu->patern_width_ >
|
||||||
context_->world->getWidth()) ||
|
context_->world->getWidth()) ||
|
||||||
(mouse_in_grid.y + context_->paterns_menu->patern_height_ >
|
(mouse_in_grid.y + context_->paterns_menu->patern_height_ >
|
||||||
context_->world->getHeight()));
|
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 j = 0; j < context_->paterns_menu->patern_height_; j++) {
|
||||||
for (int i = 0; i < context_->paterns_menu->patern_width_; i++) {
|
for (int i = 0; i < context_->paterns_menu->patern_width_; i++) {
|
||||||
auto cell =
|
auto cell =
|
||||||
@@ -56,7 +60,7 @@ void PaternPreview::display() {
|
|||||||
if (cell) {
|
if (cell) {
|
||||||
DrawRectangle((i + mouse_in_grid.x) * cell_size,
|
DrawRectangle((i + mouse_in_grid.x) * cell_size,
|
||||||
(j + mouse_in_grid.y) * cell_size, cell_size, cell_size,
|
(j + mouse_in_grid.y) * cell_size, cell_size, cell_size,
|
||||||
(is_unplacable) ? RED : BLUE);
|
(is_unplacable_) ? RED : BLUE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -143,7 +143,6 @@ void World::resize(int width, int height) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::vector<uint32_t> World::getSelection(Vector2i &origin, Vector2i &size) {
|
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),
|
std::vector<uint32_t> data = {static_cast<uint32_t>(size.x),
|
||||||
static_cast<uint32_t>(size.y)};
|
static_cast<uint32_t>(size.y)};
|
||||||
for (int y = origin.y; y < origin.y + size.y; 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;
|
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];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user