Compare commits
1 Commits
3e7e7e2547
...
3c37d9aca4
| Author | SHA1 | Date | |
|---|---|---|---|
| 3c37d9aca4 |
@@ -75,6 +75,8 @@ set(SRC_CXX_FILES "./src/main.cpp"
|
||||
"./src/selection_menu.cpp"
|
||||
"./src/selection.cpp"
|
||||
"./src/paterns_menu.cpp"
|
||||
"./src/patern_preview.cpp"
|
||||
"./src/snapping.cpp"
|
||||
"${rlImGui_SOURCE_DIR}/rlImGui.cpp"
|
||||
"${imgui_SOURCE_DIR}/imgui.cpp"
|
||||
"${imgui_SOURCE_DIR}/imgui_draw.cpp"
|
||||
|
||||
@@ -22,6 +22,7 @@ class SelectionMenu;
|
||||
class ControlMenu;
|
||||
class Selection;
|
||||
class PaternsMenu;
|
||||
class PaternPreview;
|
||||
|
||||
typedef struct ctx {
|
||||
std::shared_ptr<World> world = nullptr;
|
||||
@@ -32,6 +33,7 @@ typedef struct ctx {
|
||||
std::shared_ptr<ControlMenu> control_menu = nullptr;
|
||||
std::shared_ptr<Selection> selection = nullptr;
|
||||
std::shared_ptr<PaternsMenu> paterns_menu = nullptr;
|
||||
std::shared_ptr<PaternPreview> patern_preview = nullptr;
|
||||
nlohmann::json config_json;
|
||||
std::filesystem::path program_dir;
|
||||
} ctx;
|
||||
|
||||
32
includes/patern_preview.hpp
Normal file
32
includes/patern_preview.hpp
Normal file
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* File name: patern_preview.hpp
|
||||
* Author: lejulien
|
||||
* Date created: 01-01-1970 00:59:59
|
||||
// Date modified: 12-01-2026 21:30:10
|
||||
* ------
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <raylib.h>
|
||||
|
||||
#include <context.hpp>
|
||||
|
||||
namespace gol {
|
||||
|
||||
class PaternPreview {
|
||||
public:
|
||||
PaternPreview(std::shared_ptr<ctx>);
|
||||
~PaternPreview() = default;
|
||||
void update();
|
||||
void display();
|
||||
void start();
|
||||
private:
|
||||
bool is_started = false;
|
||||
std::shared_ptr<ctx> context_;
|
||||
Vector2 mouse_pos_ = {0, 0};
|
||||
};
|
||||
|
||||
} // namespace gol
|
||||
@@ -30,6 +30,7 @@ private:
|
||||
std::shared_ptr<ctx> context_ = nullptr;
|
||||
std::map<std::string,std::string> paterns_paths_list_;
|
||||
std::vector<std::string> paterns_name_list_;
|
||||
public:
|
||||
int patern_width_ = 0;
|
||||
int patern_height_ = 0;
|
||||
std::vector<uint32_t> loaded_patern_;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* File name: selection.cpp
|
||||
* File name: snapping.hpp
|
||||
* Author: lejulien
|
||||
* Date created: 01-01-1970 00:59:59
|
||||
// Date modified: 12-01-2026 21:30:10
|
||||
@@ -13,12 +13,6 @@
|
||||
#include <raylib.h>
|
||||
#include <cmath>
|
||||
|
||||
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)};
|
||||
}
|
||||
Vector2 snapToGrid(Vector2 screen, int 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))};
|
||||
}
|
||||
Vector2i screenToGrid(Vector2 screen, int cell_size);
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
#include <selection_menu.hpp>
|
||||
#include <selection.hpp>
|
||||
#include <paterns_menu.hpp>
|
||||
#include <patern_preview.hpp>
|
||||
|
||||
int main(int ac, char **av) {
|
||||
std::shared_ptr<gol::ctx> context = std::make_shared<gol::ctx>();
|
||||
@@ -107,6 +108,7 @@ int main(int ac, char **av) {
|
||||
context->selection_menu = std::make_shared<gol::SelectionMenu>(context);
|
||||
context->selection = std::make_shared<gol::Selection>(context);
|
||||
context->paterns_menu = std::make_shared<gol::PaternsMenu>(context);
|
||||
context->patern_preview = std::make_shared<gol::PaternPreview>(context);
|
||||
|
||||
// Speed handling values
|
||||
float sim_speed = 1.0f;
|
||||
@@ -136,6 +138,7 @@ int main(int ac, char **av) {
|
||||
|
||||
// Selection behaviour
|
||||
context->selection->update();
|
||||
context->patern_preview->update();
|
||||
context->selection_menu->update();
|
||||
context->settings_menu->update();
|
||||
|
||||
@@ -154,6 +157,7 @@ int main(int ac, char **av) {
|
||||
ClearBackground(BLACK);
|
||||
context->render->display(context->world);
|
||||
context->selection->display();
|
||||
context->patern_preview->display();
|
||||
// Start ImGui frame
|
||||
rlImGuiBegin();
|
||||
context->control_menu->display();
|
||||
|
||||
65
src/patern_preview.cpp
Normal file
65
src/patern_preview.cpp
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* 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 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());
|
||||
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 =
|
||||
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
|
||||
@@ -14,6 +14,7 @@
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <paterns_menu.hpp>
|
||||
#include <patern_preview.hpp>
|
||||
#include <sstream>
|
||||
|
||||
namespace gol {
|
||||
@@ -63,7 +64,7 @@ bool PaternsMenu::loadPatern(std::string &path) {
|
||||
}
|
||||
} catch (std::exception &e) {
|
||||
std::cerr << "Failure in loading patern : " << path << std::endl;
|
||||
return 1;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -78,8 +79,7 @@ void PaternsMenu::display() {
|
||||
ImGui::PushID(patern_name.c_str());
|
||||
if (ImGui::Button(patern_name.c_str()) &&
|
||||
loadPatern(paterns_paths_list_[patern_name])) {
|
||||
// TODO: If patern is loaded successfuly, start the preview in the
|
||||
// editor
|
||||
context_->patern_preview->start();
|
||||
}
|
||||
ImGui::PopID();
|
||||
ImGui::SameLine(ImGui::GetWindowWidth() - 57.);
|
||||
|
||||
19
src/snapping.cpp
Normal file
19
src/snapping.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
* File name: snapping.hpp
|
||||
* Author: lejulien
|
||||
* Date created: 01-01-1970 00:59:59
|
||||
// Date modified: 12-01-2026 21:30:10
|
||||
* ------
|
||||
*/
|
||||
|
||||
#include <snapping.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))};
|
||||
}
|
||||
Reference in New Issue
Block a user