70 lines
2.1 KiB
C++
70 lines
2.1 KiB
C++
/*
|
|
* File name: patern_preview.cpp
|
|
* Author: lejulien
|
|
* Date created: 01-01-1970 00:59:59
|
|
// Date modified: 12-01-2026 21:30:10
|
|
* ------
|
|
*/
|
|
|
|
#include <imgui.h>
|
|
#include <raylib.h>
|
|
#include <rlImGui.h>
|
|
|
|
#include <patern_preview.hpp>
|
|
#include <paterns_menu.hpp>
|
|
#include <settings_menu.hpp>
|
|
#include <snapping.hpp>
|
|
#include <world.hpp>
|
|
|
|
namespace gol {
|
|
|
|
PaternPreview::PaternPreview(std::shared_ptr<ctx> 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 (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;
|
|
auto cell_size = context_->settings_menu->getCellSize();
|
|
auto mouse_in_grid =
|
|
screenToGrid(mouse_pos_, context_->settings_menu->getCellSize());
|
|
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()));
|
|
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
|