aboutsummaryrefslogtreecommitdiff
path: root/src/Core/SettingsManager.cpp
blob: 9543787f223e13181f7e9290cca909d29e32ab31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "SettingsManager.h"
#include "Log.h"
#include "ThemeManager.h"
#include "Rendering/Renderer.h"

#include <fstream>
#include <filesystem>
#include <algorithm>

namespace Donut
{
    Settings SettingsManager::s_Settings;
    bool     SettingsManager::s_Initialized = false;

    void SettingsManager::Initialize()
    {
        if (s_Initialized)
            return;
            
        LoadSettings();
        s_Initialized = true;
        DONUT_INFO("Settings Manager initialized");
    }

    void SettingsManager::Shutdown()
    {
        if (!s_Initialized)
            return;
            
        SaveSettings();
        s_Initialized = false;
        DONUT_INFO("Settings Manager shutdown");
    }

    void SettingsManager::LoadSettings()
    {
        std::string filePath = GetSettingsFilePath();
        
        try
        {
            if (std::filesystem::exists(filePath))
            {
                auto config = toml::parse(filePath);
                
                if (config.contains("simulation"))
                {
                    auto sim = config["simulation"];
                    s_Settings.simulation.targetFPS         = toml::find_or(sim, "target_fps",          60);
                    s_Settings.simulation.computeHeight     = toml::find_or(sim, "compute_height",      512);
                    s_Settings.simulation.maxStepsMoving    = toml::find_or(sim, "max_steps_moving",    30000);
                    s_Settings.simulation.maxStepsStatic    = toml::find_or(sim, "max_steps_static",    15000);
                    s_Settings.simulation.earlyExitDistance = toml::find_or(sim, "early_exit_distance", 5e12f);
                    s_Settings.simulation.gravityEnabled    = toml::find_or(sim, "gravity_enabled",     true);
                    s_Settings.simulation.diskThickness     = toml::find_or(sim, "disk_thickness",      0.1f);
                    s_Settings.simulation.diskDensity       = toml::find_or(sim, "disk_density",        0.1f);
                    s_Settings.simulation.rotationSpeed     = toml::find_or(sim, "rotation_speed",      1.0f);
                    s_Settings.simulation.blurStrength      = toml::find_or(sim, "blur_strength",       2.0f);
                    s_Settings.simulation.glowIntensity     = toml::find_or(sim, "glow_intensity",      0.1f);
                    
                    s_Settings.simulation.targetFPS         = std::max(30,    std::min(120,   s_Settings.simulation.targetFPS));
                    s_Settings.simulation.computeHeight     = std::max(64,    std::min(2048,  s_Settings.simulation.computeHeight));
                    s_Settings.simulation.maxStepsMoving    = std::max(1000,  std::min(60000, s_Settings.simulation.maxStepsMoving));
                    s_Settings.simulation.maxStepsStatic    = std::max(1000,  std::min(30000, s_Settings.simulation.maxStepsStatic));
                    s_Settings.simulation.earlyExitDistance = std::max(1e11f, std::min(1e13f, s_Settings.simulation.earlyExitDistance));
                    s_Settings.simulation.diskThickness     = std::max(0.01f, std::min(5.0f,  s_Settings.simulation.diskThickness));
                    s_Settings.simulation.diskDensity       = std::max(0.01f, std::min(5.0f,  s_Settings.simulation.diskDensity));
                    s_Settings.simulation.rotationSpeed     = std::max(0.0f,  std::min(5.0f,  s_Settings.simulation.rotationSpeed));
                    s_Settings.simulation.blurStrength      = std::max(0.1f,  std::min(10.0f, s_Settings.simulation.blurStrength));
                    s_Settings.simulation.glowIntensity     = std::max(0.01f, std::min(5.0f,  s_Settings.simulation.glowIntensity));
                }
                
                if (config.contains("graphics"))
                {
                    auto gfx = config["graphics"];
                    s_Settings.graphics.renderAPI              = toml::find_or(gfx, "render_api",               std::string("OpenGL"));
                    s_Settings.graphics.vSyncEnabled           = toml::find_or(gfx, "vsync_enabled",            true);
                    s_Settings.graphics.showFPS                = toml::find_or(gfx, "show_fps",                 true);
                    s_Settings.graphics.showPerformanceMetrics = toml::find_or(gfx, "show_performance_metrics", true);
                    s_Settings.graphics.showDebugInfo          = toml::find_or(gfx, "show_debug_info",          false);
                    s_Settings.graphics.enableAntiAliasing     = toml::find_or(gfx, "enable_anti_aliasing",     true);
                    s_Settings.graphics.selectedTheme          = toml::find_or(gfx, "selected_theme",           std::string("Dark"));
                    
                    if (s_Settings.graphics.renderAPI != "OpenGL" && 
                        s_Settings.graphics.renderAPI != "Vulkan")
                        s_Settings.graphics.renderAPI = "OpenGL";
                    if (s_Settings.graphics.selectedTheme != "Dark" && 
                        s_Settings.graphics.selectedTheme != "Light" && 
                        s_Settings.graphics.selectedTheme != "Blue")
                        s_Settings.graphics.selectedTheme = "Dark";
                }
                
                DONUT_INFO("Settings loaded from {}", filePath);
            }
            else
            {
                LoadDefaultSettings();
                SaveSettings();
                DONUT_INFO("No settings file found, created default settings");
            }
        }
        catch (const std::exception& e)
        {
            DONUT_ERROR("Failed to load settings: {}", e.what());
            LoadDefaultSettings();
        }
    }

    void SettingsManager::SaveSettings()
    {
        std::string filePath = GetSettingsFilePath();
        
        try
        {
            std::filesystem::path path(filePath);
            std::filesystem::create_directories(path.parent_path());
            
            toml::value simulation = toml::table
            {
                {"target_fps",          s_Settings.simulation.targetFPS        },
                {"compute_height",      s_Settings.simulation.computeHeight    },
                {"max_steps_moving",    s_Settings.simulation.maxStepsMoving   },
                {"max_steps_static",    s_Settings.simulation.maxStepsStatic   },
                {"early_exit_distance", s_Settings.simulation.earlyExitDistance},
                {"gravity_enabled",     s_Settings.simulation.gravityEnabled   },
                {"disk_thickness",      s_Settings.simulation.diskThickness    },
                {"disk_density",        s_Settings.simulation.diskDensity      },
                {"rotation_speed",      s_Settings.simulation.rotationSpeed    },
                {"blur_strength",       s_Settings.simulation.blurStrength     },
                {"glow_intensity",      s_Settings.simulation.glowIntensity    }
            };
            
            toml::value graphics = toml::table
            {
                {"render_api",               s_Settings.graphics.renderAPI             },
                {"vsync_enabled",            s_Settings.graphics.vSyncEnabled          },
                {"show_fps",                 s_Settings.graphics.showFPS               },
                {"show_performance_metrics", s_Settings.graphics.showPerformanceMetrics},
                {"show_debug_info",          s_Settings.graphics.showDebugInfo         },
                {"enable_anti_aliasing",     s_Settings.graphics.enableAntiAliasing    },
                {"selected_theme",           s_Settings.graphics.selectedTheme         }
            };
            
            toml::value config = toml::table
            {
                {"simulation", simulation},
                {"graphics",   graphics}
            };
            
            std::ofstream file(filePath);
            file << config;
            file.close();
            
            DONUT_INFO("Settings saved to {}", filePath);
        }
        catch (const std::exception& e)
        {
            DONUT_ERROR("Failed to save settings: {}", e.what());
        }
    }

    void SettingsManager::SetSimulationSettings(const SimulationSettings& settings)
    {
        s_Settings.simulation = settings;
        SaveSettings();
    }

    void SettingsManager::SetGraphicsSettings(const GraphicsSettings& settings)
    {
        s_Settings.graphics = settings;
        SaveSettings();
    }

    std::string SettingsManager::GetSettingsFilePath()
    {
        return "config/settings.toml";
    }

    void SettingsManager::LoadDefaultSettings()
    {
        s_Settings.simulation.targetFPS         = 60;
        s_Settings.simulation.computeHeight     = 512;
        s_Settings.simulation.maxStepsMoving    = 30000;
        s_Settings.simulation.maxStepsStatic    = 15000;
        s_Settings.simulation.earlyExitDistance = 5e12f;
        s_Settings.simulation.gravityEnabled    = true;
        s_Settings.simulation.diskThickness     = 0.1f;
        s_Settings.simulation.diskDensity       = 0.1f;
        s_Settings.simulation.rotationSpeed     = 1.0f;
        s_Settings.simulation.blurStrength      = 2.0f;
        s_Settings.simulation.glowIntensity     = 0.1f;
        
        s_Settings.graphics.renderAPI              = "OpenGL";
        s_Settings.graphics.vSyncEnabled           = true;
        s_Settings.graphics.showFPS                = true;
        s_Settings.graphics.showPerformanceMetrics = true;
        s_Settings.graphics.showDebugInfo          = false;
        s_Settings.graphics.enableAntiAliasing     = true;
        s_Settings.graphics.selectedTheme          = "Dark";
    }
}