settings_menu: Create a dedicated class for the settings
This commit is contained in:
122
src/settings_menu.cpp
Normal file
122
src/settings_menu.cpp
Normal file
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
* File name: settings_menu.cpp
|
||||
* Author: lejulien
|
||||
* Date created: 01-01-1970 00:59:59
|
||||
// Date modified: 12-01-2026 22:21:49
|
||||
* ------
|
||||
*/
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include <imgui.h>
|
||||
#include <raylib.h>
|
||||
#include <rlImGui.h>
|
||||
|
||||
#include <settings_menu.hpp>
|
||||
#include <context.hpp>
|
||||
#include <world.hpp>
|
||||
#include <rules.hpp>
|
||||
#include <render.hpp>
|
||||
|
||||
namespace gol {
|
||||
|
||||
SettingsMenu::SettingsMenu(std::shared_ptr<ctx> context): context_(context) {
|
||||
fps_ctrl_ = context->config_json["fps"].get<int>();
|
||||
cell_size_ctrl_ = context->config_json["cell_size"].get<int>();
|
||||
settings_window_ = 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;
|
||||
}
|
||||
|
||||
void SettingsMenu::update() {
|
||||
if (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_;
|
||||
rlImGuiShutdown();
|
||||
SetWindowSize(width_ctrl_, height_ctrl_);
|
||||
rlImGuiSetup(dark_theme_ctrl_);
|
||||
resize_needed = true;
|
||||
}
|
||||
if (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_);
|
||||
context_->render->updateCellSize(cell_size_ctrl_);
|
||||
context_->rules->newWorld(context_->world);
|
||||
context_->config_json["cell_size"] = cell_size_ctrl_;
|
||||
}
|
||||
if (dark_theme_ctrl_ != context_->config_json["dark_theme"]) {
|
||||
rlImGuiShutdown();
|
||||
rlImGuiSetup(dark_theme_ctrl_);
|
||||
context_->config_json["dark_theme"] = dark_theme_ctrl_;
|
||||
}
|
||||
apply_ctrl_ = false;
|
||||
settings_window_ = false;
|
||||
}
|
||||
}
|
||||
|
||||
void SettingsMenu::display() {
|
||||
if (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_);
|
||||
if (ImGui::Button("Save & Apply")) {
|
||||
apply_ctrl_ = true;
|
||||
}
|
||||
ImGui::End();
|
||||
}
|
||||
}
|
||||
|
||||
bool SettingsMenu::isOpen() {
|
||||
return settings_window_;
|
||||
}
|
||||
|
||||
void SettingsMenu::Toogle() {
|
||||
settings_window_ = !settings_window_;
|
||||
}
|
||||
|
||||
// Getters/Setters
|
||||
int SettingsMenu::getFPS() {
|
||||
return fps_ctrl_;
|
||||
}
|
||||
|
||||
void SettingsMenu::setFPS(int new_fps) {
|
||||
fps_ctrl_ = new_fps;
|
||||
}
|
||||
|
||||
int SettingsMenu::getCellSize() {
|
||||
return cell_size_ctrl_;
|
||||
}
|
||||
|
||||
void SettingsMenu::setCellSize(int new_cell_size) {
|
||||
cell_size_ctrl_ = new_cell_size;
|
||||
}
|
||||
|
||||
int SettingsMenu::getWidth() {
|
||||
return width_ctrl_;
|
||||
}
|
||||
|
||||
void SettingsMenu::setWidth(int new_width) {
|
||||
width_ctrl_ = new_width;
|
||||
}
|
||||
|
||||
int SettingsMenu::getHeight() {
|
||||
return height_ctrl_;
|
||||
}
|
||||
|
||||
void SettingsMenu::setHeight(int new_height) {
|
||||
height_ctrl_ = new_height;
|
||||
}
|
||||
|
||||
} // namespace gol
|
||||
Reference in New Issue
Block a user