Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
EditorConfigManager.cpp
Go to the documentation of this file.
1/*
2 * Olympe Blueprint Editor - Configuration Manager Implementation
3 */
4
6#include "../third_party/imgui/imgui.h"
7#include "../third_party/imgui/imgui_internal.h"
8#include "../third_party/imnodes/imnodes.h"
9#include <fstream>
10#include <iostream>
11
13
14namespace Olympe
15{
21
22 bool EditorConfigManager::LoadConfig(const std::string& filepath)
23 {
24 try
25 {
26 std::ifstream file(filepath);
27 if (!file.is_open())
28 {
29 std::cout << "[EditorConfigManager] Config file not found, using defaults: " << filepath << std::endl;
30 return false;
31 }
32
33 json j;
34 file >> j;
35 file.close();
36
37 // Load panel visibility
38 if (j.contains("panelVisibility"))
39 {
40 auto& pv = j["panelVisibility"];
41 m_PanelVisibility.showAssetBrowser = pv.value("showAssetBrowser", true);
42 m_PanelVisibility.showInspector = pv.value("showInspector", true);
43 m_PanelVisibility.showNodeGraph = pv.value("showNodeGraph", true);
44 m_PanelVisibility.showTemplateBrowser = pv.value("showTemplateBrowser", false);
45 m_PanelVisibility.showHistory = pv.value("showHistory", false);
46 }
47
48 // Load editor preferences
49 if (j.contains("preferences"))
50 {
51 auto& prefs = j["preferences"];
52 m_Preferences.gridLinesEnabled = prefs.value("gridLinesEnabled", true);
53 m_Preferences.autoSaveEnabled = prefs.value("autoSaveEnabled", true);
54 m_Preferences.autoSaveIntervalSeconds = prefs.value("autoSaveIntervalSeconds", 300);
55 m_Preferences.panningSpeed = prefs.value("panningSpeed", 1.0f);
56 m_Preferences.zoomSpeed = prefs.value("zoomSpeed", 0.1f);
57 }
58
59 // Load ImGui window configurations
60 if (j.contains("imguiSettings") && j["imguiSettings"].contains("windows"))
61 {
63 json windows = j["imguiSettings"]["windows"];
64
65 for (json::iterator it = windows.begin(); it != windows.end(); ++it)
66 {
67 std::string key = it.key();
68 json value = it.value();
69
71 config.x = value.value("x", 0.0f);
72 config.y = value.value("y", 0.0f);
73 config.width = value.value("width", 800.0f);
74 config.height = value.value("height", 600.0f);
75 config.collapsed = value.value("collapsed", false);
76
78 }
79 }
80
81 std::cout << "[EditorConfigManager] Configuration loaded from: " << filepath << std::endl;
82 return true;
83 }
84 catch (const std::exception& e)
85 {
86 std::cerr << "[EditorConfigManager] Failed to load config: " << e.what() << std::endl;
87 return false;
88 }
89 }
90
91 bool EditorConfigManager::SaveConfig(const std::string& filepath)
92 {
93 try
94 {
95 json j;
96
97 // Save panel visibility
98 j["panelVisibility"] = {
99 {"showAssetBrowser", m_PanelVisibility.showAssetBrowser},
100 {"showInspector", m_PanelVisibility.showInspector},
101 {"showNodeGraph", m_PanelVisibility.showNodeGraph},
102 {"showTemplateBrowser", m_PanelVisibility.showTemplateBrowser},
103 {"showHistory", m_PanelVisibility.showHistory}
104 };
105
106 // Save editor preferences
107 j["preferences"] = {
108 {"gridLinesEnabled", m_Preferences.gridLinesEnabled},
109 {"autoSaveEnabled", m_Preferences.autoSaveEnabled},
110 {"autoSaveIntervalSeconds", m_Preferences.autoSaveIntervalSeconds},
111 {"panningSpeed", m_Preferences.panningSpeed},
112 {"zoomSpeed", m_Preferences.zoomSpeed}
113 };
114
115 // Save ImGui window configurations
116 json windowsJson = json::object();
117 for (std::map<std::string, WindowConfig>::const_iterator it = m_ImGuiSettings.windowConfigs.begin();
119 {
120 const std::string& name = it->first;
121 const WindowConfig& config = it->second;
122
123 json windowJson = json::object();
124 windowJson["x"] = config.x;
125 windowJson["y"] = config.y;
126 windowJson["width"] = config.width;
127 windowJson["height"] = config.height;
128 windowJson["collapsed"] = config.collapsed;
129
130 windowsJson[name] = windowJson;
131 }
132 j["imguiSettings"]["windows"] = windowsJson;
133
134 // Write to file with formatting
135 std::ofstream file(filepath);
136 if (!file.is_open())
137 {
138 std::cerr << "[EditorConfigManager] Failed to open config file for writing: " << filepath << std::endl;
139 return false;
140 }
141
142 file << j.dump(2); // Pretty print with 2 space indentation
143 file.close();
144
145 std::cout << "[EditorConfigManager] Configuration saved to: " << filepath << std::endl;
146 return true;
147 }
148 catch (const std::exception& e)
149 {
150 std::cerr << "[EditorConfigManager] Failed to save config: " << e.what() << std::endl;
151 return false;
152 }
153 }
154
159
161 {
163 if (it != m_ImGuiSettings.windowConfigs.end())
164 {
165 return it->second;
166 }
167 return WindowConfig(); // Return default
168 }
169
171 {
172 // Apply ImNodes grid settings
174 {
175 ImNodesStyle& style = ImNodes::GetStyle();
176 style.Flags |= ImNodesStyleFlags_GridLines;
177 }
178 else
179 {
180 ImNodesStyle& style = ImNodes::GetStyle();
181 style.Flags &= ~ImNodesStyleFlags_GridLines;
182 }
183
184 // Note: Window positions/sizes will be applied when windows are created
185 // by checking GetWindowConfig() for each window
186 }
187
189 {
190 // Capture all open ImGui window positions and sizes
191 ImGuiContext* context = ImGui::GetCurrentContext();
192 if (!context)
193 return;
194
195 for (int i = 0; i < context->Windows.Size; i++)
196 {
197 ImGuiWindow* window = context->Windows[i];
198 if (!window || window->Hidden)
199 continue;
200
201 // Skip internal ImGui windows (those starting with ##)
202 std::string windowName = window->Name;
203 if (windowName.empty() || windowName[0] == '#')
204 continue;
205
207 config.x = window->Pos.x;
208 config.y = window->Pos.y;
209 config.width = window->Size.x;
210 config.height = window->Size.y;
211 config.collapsed = window->Collapsed;
212
214 }
215 }
216}
nlohmann::json json
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
static SDL_Window * window
void SaveWindowConfig(const std::string &windowName, const WindowConfig &config)
bool LoadConfig(const std::string &filepath)
bool SaveConfig(const std::string &filepath)
static EditorConfigManager & Get()
WindowConfig GetWindowConfig(const std::string &windowName) const
< Provides AssetID and INVALID_ASSET_ID
nlohmann::json json
nlohmann::json json
std::map< std::string, WindowConfig > windowConfigs