selection: Create it's dedicated class
This commit is contained in:
73
src/main.cpp
73
src/main.cpp
@@ -23,16 +23,7 @@
|
||||
#include <control_menu.hpp>
|
||||
#include <settings_menu.hpp>
|
||||
#include <selection_menu.hpp>
|
||||
|
||||
Vector2 snapToGrid(Vector2 screen, int cell_size) {
|
||||
return {static_cast<float>(round(screen.x / cell_size) * cell_size),
|
||||
static_cast<float>(round(screen.y / cell_size) * cell_size)};
|
||||
}
|
||||
|
||||
Vector2i screenToGrid(Vector2 screen, int cell_size) {
|
||||
return {static_cast<int>(round(screen.x / cell_size)),
|
||||
static_cast<int>(round(screen.y / cell_size))};
|
||||
}
|
||||
#include <selection.hpp>
|
||||
|
||||
int main(int ac, char **av) {
|
||||
std::shared_ptr<gol::ctx> context = std::make_shared<gol::ctx>();
|
||||
@@ -98,71 +89,41 @@ int main(int ac, char **av) {
|
||||
rlImGuiSetup(context->config_json["dark_theme"]);
|
||||
|
||||
// Initialize objects
|
||||
context->control_menu = std::make_shared<gol::ControlMenu>(context);
|
||||
context->world = std::make_shared<World>(context);
|
||||
context->rules = std::make_shared<Rules>();
|
||||
context->settings_menu = std::make_shared<gol::SettingsMenu>(context);
|
||||
context->render = std::make_shared<Render>(context->settings_menu->getCellSize());
|
||||
context->selection_menu = std::make_shared<gol::SelectionMenu>(context);
|
||||
|
||||
// Imgui control menu
|
||||
gol::ControlMenu control_menu(context);
|
||||
context->selection = std::make_shared<gol::Selection>(context);
|
||||
|
||||
// Speed handling values
|
||||
float sim_speed = 1.0f;
|
||||
float deltaTimeAccumulator = 0.0f;
|
||||
float timePerUpdate = 1.0f / 10.0f;
|
||||
|
||||
// Selection window
|
||||
Vector2 sel_pos = {0., 0.};
|
||||
bool selecting = false;
|
||||
|
||||
// Setups
|
||||
context->rules->setup(context->world);
|
||||
// Diplay generations
|
||||
while (!WindowShouldClose()) {
|
||||
// Frames shinenigans
|
||||
float deltaTime = GetFrameTime();
|
||||
sim_speed = context->settings_menu->getFPS() / 10.0f;
|
||||
sim_speed = context->settings_menu->getFPS() / 10.0f;
|
||||
timePerUpdate = (1.0f / 10.0f) / sim_speed;
|
||||
|
||||
control_menu.update();
|
||||
context->control_menu->update();
|
||||
|
||||
auto gesture = GetGestureDetected();
|
||||
auto mousePos = GetMousePosition();
|
||||
if (control_menu.edit_ctrl_ && IsMouseButtonPressed(0) &&
|
||||
if (context->control_menu->edit_ctrl_ && IsMouseButtonPressed(0) &&
|
||||
!ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow)) {
|
||||
control_menu.menu_state_ = MenuState::EDIT;
|
||||
context->control_menu->menu_state_ = MenuState::EDIT;
|
||||
context->world->setCell(mousePos.x / context->config_json["cell_size"].get<int>(),
|
||||
mousePos.y / context->config_json["cell_size"].get<int>());
|
||||
}
|
||||
|
||||
// Selection behaviour
|
||||
if (!control_menu.edit_ctrl_ && !control_menu.play_ctrl_ && !control_menu.paterns_ctrl_ && !context->settings_menu->isOpen()) {
|
||||
if (gesture == GESTURE_TAP && !ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow)) {
|
||||
sel_pos = snapToGrid(mousePos, context->settings_menu->getCellSize());
|
||||
selecting = true;
|
||||
}
|
||||
if (ImGui::IsMouseReleased(0) && selecting == true && mousePos.x >=0 && mousePos.x < context->settings_menu->getWidth() && mousePos.y >=0 && mousePos.y < context->settings_menu->getHeight() ) {
|
||||
selecting = false;
|
||||
Vector2i p1 = screenToGrid(sel_pos, context->settings_menu->getCellSize());
|
||||
Vector2i p2 = screenToGrid(mousePos, context->settings_menu->getCellSize());
|
||||
// Get origin
|
||||
Vector2i orig = {
|
||||
(p1.x < p2.x)?p1.x:p2.x,
|
||||
(p1.y < p2.y)?p1.y:p2.y,
|
||||
};
|
||||
// Get selection size
|
||||
Vector2i sel_size = {
|
||||
(p1.x > p2.x)?p1.x-p2.x:p2.x-p1.x,
|
||||
(p1.y > p2.y)?p1.y-p2.y:p2.y-p1.y,
|
||||
};
|
||||
// Ensure there is at least one cell selected
|
||||
if (!(sel_size.x == 0 || sel_size.y == 0)) {
|
||||
context->selection_menu->setSelection(context->world->getSelection(orig, sel_size));
|
||||
context->selection_menu->open();
|
||||
}
|
||||
}
|
||||
}
|
||||
context->selection->update();
|
||||
context->selection_menu->update();
|
||||
context->settings_menu->update();
|
||||
|
||||
@@ -172,7 +133,7 @@ int main(int ac, char **av) {
|
||||
if (deltaTimeAccumulator >= timePerUpdate) {
|
||||
// Reset accumulator
|
||||
deltaTimeAccumulator -= timePerUpdate;
|
||||
if (control_menu.menu_state_ == MenuState::PLAY) {
|
||||
if (context->control_menu->menu_state_ == MenuState::PLAY) {
|
||||
context->world->saveCompressed();
|
||||
context->rules->update();
|
||||
}
|
||||
@@ -180,23 +141,15 @@ int main(int ac, char **av) {
|
||||
BeginDrawing();
|
||||
ClearBackground(BLACK);
|
||||
context->render->display(context->world);
|
||||
if (selecting) {
|
||||
auto grid_mouse = snapToGrid(mousePos, context->settings_menu->getCellSize());
|
||||
bool sel_x_less = (sel_pos.x < grid_mouse.x);
|
||||
bool sel_y_less = (sel_pos.y < grid_mouse.y);
|
||||
DrawRectangleLines(((sel_x_less)? sel_pos.x: grid_mouse.x), ((sel_y_less)? sel_pos.y: grid_mouse.y),
|
||||
((sel_x_less)? grid_mouse.x - sel_pos.x : sel_pos.x - grid_mouse.x),
|
||||
((sel_y_less)? grid_mouse.y - sel_pos.y : sel_pos.y - grid_mouse.y),
|
||||
RED);
|
||||
}
|
||||
context->selection->display();
|
||||
// Start ImGui frame
|
||||
rlImGuiBegin();
|
||||
control_menu.display();
|
||||
if (control_menu.paterns_ctrl_) {
|
||||
context->control_menu->display();
|
||||
if (context->control_menu->paterns_ctrl_) {
|
||||
ImGuiWindowFlags paterns_flags =
|
||||
ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoResize;
|
||||
ImGui::SetNextWindowSize(ImVec2(150, 200), ImGuiCond_Always);
|
||||
ImGui::Begin("paterns", &control_menu.paterns_ctrl_, paterns_flags);
|
||||
ImGui::Begin("paterns", &context->control_menu->paterns_ctrl_, paterns_flags);
|
||||
ImGui::Button("refresh");
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
67
src/selection.cpp
Normal file
67
src/selection.cpp
Normal file
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* File name: selection.cpp
|
||||
* Author: lejulien
|
||||
* Date created: 01-01-1970 00:59:59
|
||||
// Date modified: 12-01-2026 21:30:10
|
||||
* ------
|
||||
*/
|
||||
|
||||
#include <types.hpp>
|
||||
|
||||
#include <selection.hpp>
|
||||
#include <selection_menu.hpp>
|
||||
#include <control_menu.hpp>
|
||||
#include <settings_menu.hpp>
|
||||
#include <world.hpp>
|
||||
#include <snapping.hpp>
|
||||
|
||||
namespace gol {
|
||||
|
||||
Selection::Selection(std::shared_ptr<ctx> context): context_(context) {
|
||||
|
||||
}
|
||||
|
||||
void Selection::update() {
|
||||
auto gesture = GetGestureDetected();
|
||||
mouse_pos_ = GetMousePosition();
|
||||
if (!context_->control_menu->edit_ctrl_ && !context_->control_menu->play_ctrl_ && !context_->control_menu->paterns_ctrl_ && !context_->settings_menu->isOpen()) {
|
||||
if (gesture == GESTURE_TAP && !ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow)) {
|
||||
sel_pos_ = snapToGrid(mouse_pos_, context_->settings_menu->getCellSize());
|
||||
selecting_ = true;
|
||||
}
|
||||
if (ImGui::IsMouseReleased(0) && selecting_ == true && mouse_pos_.x >=0 && mouse_pos_.x < context_->settings_menu->getWidth() && mouse_pos_.y >=0 && mouse_pos_.y < context_->settings_menu->getHeight() ) {
|
||||
selecting_ = false;
|
||||
Vector2i p1 = screenToGrid(sel_pos_, context_->settings_menu->getCellSize());
|
||||
Vector2i p2 = screenToGrid(mouse_pos_, context_->settings_menu->getCellSize());
|
||||
// Get origin
|
||||
Vector2i orig = {
|
||||
(p1.x < p2.x)?p1.x:p2.x,
|
||||
(p1.y < p2.y)?p1.y:p2.y,
|
||||
};
|
||||
// Get selection size
|
||||
Vector2i sel_size = {
|
||||
(p1.x > p2.x)?p1.x-p2.x:p2.x-p1.x,
|
||||
(p1.y > p2.y)?p1.y-p2.y:p2.y-p1.y,
|
||||
};
|
||||
// Ensure there is at least one cell selected
|
||||
if (!(sel_size.x == 0 || sel_size.y == 0)) {
|
||||
context_->selection_menu->setSelection(context_->world->getSelection(orig, sel_size));
|
||||
context_->selection_menu->open();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Selection::display() {
|
||||
if (selecting_) {
|
||||
auto grid_mouse = snapToGrid(mouse_pos_, context_->settings_menu->getCellSize());
|
||||
bool sel_x_less = (sel_pos_.x < grid_mouse.x);
|
||||
bool sel_y_less = (sel_pos_.y < grid_mouse.y);
|
||||
DrawRectangleLines(((sel_x_less)? sel_pos_.x: grid_mouse.x), ((sel_y_less)? sel_pos_.y: grid_mouse.y),
|
||||
((sel_x_less)? grid_mouse.x - sel_pos_.x : sel_pos_.x - grid_mouse.x),
|
||||
((sel_y_less)? grid_mouse.y - sel_pos_.y : sel_pos_.y - grid_mouse.y),
|
||||
RED);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace gol
|
||||
Reference in New Issue
Block a user