Adding controls to control_menu
This commit is contained in:
@@ -70,6 +70,7 @@ set(SRC_CXX_FILES "./src/main.cpp"
|
||||
"./src/rules.cpp"
|
||||
"./src/world.cpp"
|
||||
"./src/render.cpp"
|
||||
"./src/control_menu.cpp"
|
||||
"${rlImGui_SOURCE_DIR}/rlImGui.cpp"
|
||||
"${imgui_SOURCE_DIR}/imgui.cpp"
|
||||
"${imgui_SOURCE_DIR}/imgui_draw.cpp"
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
* File name: control_menu.hpp
|
||||
* Author: lejulien
|
||||
* Date created: 10-01-2026 22:00:33
|
||||
// Date modified: 10-01-2026 22:18:25
|
||||
// Date modified: 10-01-2026 22:45:10
|
||||
* ------
|
||||
*/
|
||||
|
||||
@@ -20,6 +20,7 @@ public:
|
||||
void display();
|
||||
private:
|
||||
ctx context_;
|
||||
public: // Keep those public for easy access
|
||||
int fps_ctrl_ = false;
|
||||
int cell_size_ctrl_ = false;
|
||||
bool play_ctrl_ = true;
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
* File name: control_menu.cpp
|
||||
* Author: lejulien
|
||||
* Date created: 10-01-2026 22:12:44
|
||||
// Date modified: 10-01-2026 22:21:20
|
||||
// Date modified: 10-01-2026 22:43:36
|
||||
* ------
|
||||
*/
|
||||
|
||||
@@ -11,7 +11,20 @@
|
||||
namespace gol {
|
||||
|
||||
ControlMenu::ControlMenu(ctx context):context_(context) {
|
||||
// TODO: load content from json
|
||||
}
|
||||
fps_ctrl_ = context.config_json["fps"].get<int>();
|
||||
cell_size_ctrl_ = context.config_json["cell_size"].get<int>();
|
||||
play_ctrl_ = true;
|
||||
step_ctrl_ = false;
|
||||
step_back_ctrl_ = false;
|
||||
rand_ctrl_ = false;
|
||||
edit_ctrl_ = false;
|
||||
clear_ctrl_ = false;
|
||||
settings_window_ = false;
|
||||
paterns_ctrl_ = false;
|
||||
width_ctrl_ = context.config_json["screen_width"].get<int>();
|
||||
height_ctrl_ = context.config_json["screen_height"].get<int>();
|
||||
dark_theme_ctrl_ = context.config_json["dark_theme"].get<bool>();
|
||||
apply_ctrl_ = false;
|
||||
}
|
||||
|
||||
} // namespace gol
|
||||
|
||||
138
src/main.cpp
138
src/main.cpp
@@ -2,7 +2,7 @@
|
||||
* File name: main.cpp
|
||||
* Author: lejulien
|
||||
* Date created: 10-01-2026 21:59:32
|
||||
// Date modified: 10-01-2026 22:39:36
|
||||
// Date modified: 10-01-2026 22:55:11
|
||||
* ------
|
||||
*/
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
#include <world.hpp>
|
||||
#include <types.hpp>
|
||||
#include <context.hpp>
|
||||
#include <control_menu.hpp>
|
||||
|
||||
Vector2 snapToGrid(Vector2 screen, int cell_size) {
|
||||
return {static_cast<float>(round(screen.x / cell_size) * cell_size),
|
||||
@@ -109,20 +110,7 @@ int main(int ac, char **av) {
|
||||
Render render(context.config_json["cell_size"]);
|
||||
|
||||
// Imgui control menu
|
||||
int fps_ctrl = context.config_json["fps"].get<int>();
|
||||
int cell_size_ctrl = context.config_json["cell_size"].get<int>();
|
||||
bool play_ctrl = true;
|
||||
bool step_ctrl = false;
|
||||
bool step_back_ctrl = false;
|
||||
bool rand_ctrl = false;
|
||||
bool edit_ctrl = false;
|
||||
bool clear_ctrl = false;
|
||||
bool settings_window = false;
|
||||
bool paterns_ctrl = false;
|
||||
int width_ctrl = context.config_json["screen_width"].get<int>();
|
||||
int height_ctrl = context.config_json["screen_height"].get<int>();
|
||||
bool dark_theme_ctrl = context.config_json["dark_theme"].get<bool>();
|
||||
bool apply_ctrl = false;
|
||||
gol::ControlMenu control_menu(context);
|
||||
|
||||
// Speed handling values
|
||||
float sim_speed = 1.0f;
|
||||
@@ -143,54 +131,54 @@ int main(int ac, char **av) {
|
||||
// Diplay generations
|
||||
while (!WindowShouldClose()) {
|
||||
if (menu_state == MenuState::PLAY || menu_state == MenuState::EDIT) {
|
||||
if (!play_ctrl && !edit_ctrl) {
|
||||
if (!control_menu.play_ctrl_ && !control_menu.edit_ctrl_) {
|
||||
menu_state = MenuState::NONE;
|
||||
}
|
||||
}
|
||||
// Frames shinenigans
|
||||
float deltaTime = GetFrameTime();
|
||||
sim_speed = fps_ctrl / 10.0f;
|
||||
sim_speed = control_menu.fps_ctrl_ / 10.0f;
|
||||
timePerUpdate = (1.0f / 10.0f) / sim_speed;
|
||||
|
||||
auto gesture = GetGestureDetected();
|
||||
auto mousePos = GetMousePosition();
|
||||
if (rand_ctrl) {
|
||||
if (control_menu.rand_ctrl_) {
|
||||
context.world->randomize();
|
||||
rand_ctrl = false;
|
||||
control_menu.rand_ctrl_ = false;
|
||||
}
|
||||
if (clear_ctrl) {
|
||||
if (control_menu.clear_ctrl_) {
|
||||
context.world->clear();
|
||||
clear_ctrl = false;
|
||||
control_menu.clear_ctrl_ = false;
|
||||
}
|
||||
if (edit_ctrl && play_ctrl) {
|
||||
if (control_menu.edit_ctrl_ && control_menu.play_ctrl_) {
|
||||
if (menu_state == MenuState::PLAY) {
|
||||
menu_state = MenuState::EDIT;
|
||||
play_ctrl = false;
|
||||
control_menu.play_ctrl_ = false;
|
||||
} else if (menu_state == MenuState::EDIT) {
|
||||
menu_state = MenuState::PLAY;
|
||||
edit_ctrl = false;
|
||||
control_menu.edit_ctrl_ = false;
|
||||
}
|
||||
} else if (play_ctrl) {
|
||||
} else if (control_menu.play_ctrl_) {
|
||||
menu_state = MenuState::PLAY;
|
||||
} else if (edit_ctrl) {
|
||||
} else if (control_menu.edit_ctrl_) {
|
||||
menu_state = MenuState::EDIT;
|
||||
}
|
||||
if (edit_ctrl && IsMouseButtonPressed(0) &&
|
||||
if (control_menu.edit_ctrl_ && IsMouseButtonPressed(0) &&
|
||||
!ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow)) {
|
||||
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 (!edit_ctrl && !play_ctrl && !paterns_ctrl) {
|
||||
if (!control_menu.edit_ctrl_ && !control_menu.play_ctrl_ && !control_menu.paterns_ctrl_ && !control_menu.settings_window_) {
|
||||
if (gesture == GESTURE_TAP && !ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow)) {
|
||||
sel_pos = snapToGrid(mousePos, cell_size_ctrl);
|
||||
sel_pos = snapToGrid(mousePos, control_menu.cell_size_ctrl_);
|
||||
selecting = true;
|
||||
}
|
||||
if (ImGui::IsMouseReleased(0) && selecting == true && mousePos.x >=0 && mousePos.x < width_ctrl && mousePos.y >=0 && mousePos.y < height_ctrl ) {
|
||||
if (ImGui::IsMouseReleased(0) && selecting == true && mousePos.x >=0 && mousePos.x < control_menu.width_ctrl_ && mousePos.y >=0 && mousePos.y < control_menu.height_ctrl_ ) {
|
||||
selecting = false;
|
||||
Vector2i p1 = screenToGrid(sel_pos, cell_size_ctrl);
|
||||
Vector2i p2 = screenToGrid(mousePos, cell_size_ctrl);
|
||||
Vector2i p1 = screenToGrid(sel_pos, control_menu.cell_size_ctrl_);
|
||||
Vector2i p2 = screenToGrid(mousePos, control_menu.cell_size_ctrl_);
|
||||
// Get origin
|
||||
Vector2i orig = {
|
||||
(p1.x < p2.x)?p1.x:p2.x,
|
||||
@@ -211,43 +199,43 @@ int main(int ac, char **av) {
|
||||
if (!sel_ctrl) {
|
||||
patern_name[0] = '\0';
|
||||
}
|
||||
if (step_ctrl) {
|
||||
if (control_menu.step_ctrl_) {
|
||||
context.world->saveCompressed();
|
||||
rules.update();
|
||||
step_ctrl = false;
|
||||
control_menu.step_ctrl_ = false;
|
||||
}
|
||||
if (step_back_ctrl) {
|
||||
if (control_menu.step_back_ctrl_) {
|
||||
context.world->stepBack();
|
||||
step_back_ctrl = false;
|
||||
control_menu.step_back_ctrl_ = false;
|
||||
}
|
||||
|
||||
if (apply_ctrl) {
|
||||
if (control_menu.apply_ctrl_) {
|
||||
bool resize_needed = false;
|
||||
context.config_json["fps"] = fps_ctrl;
|
||||
if (context.config_json["screen_width"].get<int>() != width_ctrl ||
|
||||
context.config_json["screen_height"].get<int>() != height_ctrl) {
|
||||
context.config_json["screen_width"] = width_ctrl;
|
||||
context.config_json["screen_height"] = height_ctrl;
|
||||
context.config_json["fps"] = control_menu.fps_ctrl_;
|
||||
if (context.config_json["screen_width"].get<int>() != control_menu.width_ctrl_ ||
|
||||
context.config_json["screen_height"].get<int>() != control_menu.height_ctrl_) {
|
||||
context.config_json["screen_width"] = control_menu.width_ctrl_;
|
||||
context.config_json["screen_height"] = control_menu.height_ctrl_;
|
||||
rlImGuiShutdown();
|
||||
SetWindowSize(width_ctrl, height_ctrl);
|
||||
rlImGuiSetup(dark_theme_ctrl);
|
||||
SetWindowSize(control_menu.width_ctrl_, control_menu.height_ctrl_);
|
||||
rlImGuiSetup(control_menu.dark_theme_ctrl_);
|
||||
resize_needed = true;
|
||||
}
|
||||
if (cell_size_ctrl != context.config_json["cell_size"].get<int>() ||
|
||||
if (control_menu.cell_size_ctrl_ != context.config_json["cell_size"].get<int>() ||
|
||||
resize_needed) {
|
||||
context.world->resize(context.config_json["screen_width"].get<int>() / cell_size_ctrl,
|
||||
context.config_json["screen_height"].get<int>() / cell_size_ctrl);
|
||||
render.updateCellSize(cell_size_ctrl);
|
||||
context.world->resize(context.config_json["screen_width"].get<int>() / control_menu.cell_size_ctrl_,
|
||||
context.config_json["screen_height"].get<int>() / control_menu.cell_size_ctrl_);
|
||||
render.updateCellSize(control_menu.cell_size_ctrl_);
|
||||
rules.newWorld(&(*context.world));
|
||||
context.config_json["cell_size"] = cell_size_ctrl;
|
||||
context.config_json["cell_size"] = control_menu.cell_size_ctrl_;
|
||||
}
|
||||
if (dark_theme_ctrl != context.config_json["dark_theme"]) {
|
||||
if (control_menu.dark_theme_ctrl_ != context.config_json["dark_theme"]) {
|
||||
rlImGuiShutdown();
|
||||
rlImGuiSetup(dark_theme_ctrl);
|
||||
context.config_json["dark_theme"] = dark_theme_ctrl;
|
||||
rlImGuiSetup(control_menu.dark_theme_ctrl_);
|
||||
context.config_json["dark_theme"] = control_menu.dark_theme_ctrl_;
|
||||
}
|
||||
apply_ctrl = false;
|
||||
settings_window = false;
|
||||
control_menu.apply_ctrl_ = false;
|
||||
control_menu.settings_window_ = false;
|
||||
}
|
||||
|
||||
// Accumulate time and update simulation at the adjusted speed
|
||||
@@ -265,7 +253,7 @@ int main(int ac, char **av) {
|
||||
ClearBackground(BLACK);
|
||||
render.display(&(*context.world));
|
||||
if (selecting) {
|
||||
auto grid_mouse = snapToGrid(mousePos, cell_size_ctrl);
|
||||
auto grid_mouse = snapToGrid(mousePos, control_menu.cell_size_ctrl_);
|
||||
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),
|
||||
@@ -282,43 +270,43 @@ int main(int ac, char **av) {
|
||||
ImVec2 menu_pos(3, 3);
|
||||
ImGui::SetNextWindowPos(menu_pos, ImGuiCond_Always);
|
||||
ImGui::Begin("Control Panel", &control_panel_open, control_panel_flags);
|
||||
ImGui::Checkbox("Play", &play_ctrl);
|
||||
ImGui::Checkbox("Play", &control_menu.play_ctrl_);
|
||||
if (ImGui::Button("Step")) {
|
||||
step_ctrl = true;
|
||||
control_menu.step_ctrl_ = true;
|
||||
}
|
||||
if (ImGui::Button("Step Back")) {
|
||||
step_back_ctrl = true;
|
||||
control_menu.step_back_ctrl_ = true;
|
||||
}
|
||||
ImGui::Checkbox("Edit", &edit_ctrl);
|
||||
ImGui::Checkbox("Clear", &clear_ctrl);
|
||||
ImGui::Checkbox("Randomize", &rand_ctrl);
|
||||
if (ImGui::Button((paterns_ctrl) ? "Hide paterns" : "Show paterns")) {
|
||||
paterns_ctrl = !paterns_ctrl;
|
||||
ImGui::Checkbox("Edit", &control_menu.edit_ctrl_);
|
||||
ImGui::Checkbox("Clear", &control_menu.clear_ctrl_);
|
||||
ImGui::Checkbox("Randomize", &control_menu.rand_ctrl_);
|
||||
if (ImGui::Button((control_menu.paterns_ctrl_) ? "Hide paterns" : "Show paterns")) {
|
||||
control_menu.paterns_ctrl_ = !control_menu.paterns_ctrl_;
|
||||
}
|
||||
if (ImGui::Button((settings_window) ? "Hide settings" : "Show settings")) {
|
||||
settings_window = !settings_window;
|
||||
if (ImGui::Button((control_menu.settings_window_) ? "Hide settings" : "Show settings")) {
|
||||
control_menu.settings_window_ = !control_menu.settings_window_;
|
||||
}
|
||||
ImGui::Text("Generation: %zu", context.world->getCycle());
|
||||
ImGui::End();
|
||||
if (paterns_ctrl) {
|
||||
if (control_menu.paterns_ctrl_) {
|
||||
ImGuiWindowFlags paterns_flags =
|
||||
ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoResize;
|
||||
ImGui::SetNextWindowSize(ImVec2(150, 200), ImGuiCond_Always);
|
||||
ImGui::Begin("paterns", &paterns_ctrl, paterns_flags);
|
||||
ImGui::Begin("paterns", &control_menu.paterns_ctrl_, paterns_flags);
|
||||
ImGui::Button("refresh");
|
||||
ImGui::End();
|
||||
}
|
||||
if (settings_window) {
|
||||
if (control_menu.settings_window_) {
|
||||
ImGuiWindowFlags settings_flags =
|
||||
ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoResize;
|
||||
ImGui::Begin("Settings", &settings_window, settings_flags);
|
||||
ImGui::SliderInt("Window Width", &width_ctrl, 800, 1920);
|
||||
ImGui::SliderInt("Window Height", &height_ctrl, 600, 1200);
|
||||
ImGui::SliderInt("FPS", &fps_ctrl, 0, 30);
|
||||
ImGui::SliderInt("Cell Size", &cell_size_ctrl, 4, 100);
|
||||
ImGui::Checkbox("Dark Theme", &dark_theme_ctrl);
|
||||
ImGui::Begin("Settings", &control_menu.settings_window_, settings_flags);
|
||||
ImGui::SliderInt("Window Width", &control_menu.width_ctrl_, 800, 1920);
|
||||
ImGui::SliderInt("Window Height", &control_menu.height_ctrl_, 600, 1200);
|
||||
ImGui::SliderInt("FPS", &control_menu.fps_ctrl_, 0, 30);
|
||||
ImGui::SliderInt("Cell Size", &control_menu.cell_size_ctrl_, 4, 100);
|
||||
ImGui::Checkbox("Dark Theme", &control_menu.dark_theme_ctrl_);
|
||||
if (ImGui::Button("Save & Apply")) {
|
||||
apply_ctrl = true;
|
||||
control_menu.apply_ctrl_ = true;
|
||||
}
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user