galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
ConfigPanel.cpp
Go to the documentation of this file.
1
7
11
12#include "ConfigPanel.hpp"
13
14#define INDENT_PIXELS 16.0f
15
16namespace galaxy
17{
19 : m_counter {0}
20 {
21 }
22
24 {
25 }
26
28 {
29 if (ImGui::Begin("Config Editor", &m_show, ImGuiWindowFlags_MenuBar))
30 {
31 if (ImGui::BeginMenuBar())
32 {
33 if (ImGui::MenuItem("Save"))
34 {
35 entt::locator<Config>::value().save();
36 ui::imgui_notify_success("Config changed, a restart is needed.");
37 }
38
39 if (ImGui::MenuItem("Reset"))
40 {
42 ui::imgui_notify_success("Config restored to default.");
43 }
44
45 if (ImGui::MenuItem("Reload"))
46 {
48 ui::imgui_notify_success("Settings restored from updated config.");
49 }
50
51 ImGui::EndMenuBar();
52 }
53
54 m_counter = 0;
55
56 nlohmann::json& root = entt::locator<Config>::value().raw();
57 if (root.is_object())
58 {
59 do_object(root);
60 }
61 else if (root.is_array())
62 {
63 do_array(root);
64 }
65 }
66
67 ImGui::End();
68 }
69
70 void ConfigPanel::do_object(nlohmann::json& json)
71 {
72 m_counter++;
73 ImGui::PushID(m_counter);
74
75 ImGui::Indent(INDENT_PIXELS);
76
77 for (auto& [key, value] : json.items())
78 {
79 if (value.is_array())
80 {
81 if (!key.empty())
82 {
83 ImGui::Text(key.c_str());
84 }
85
86 do_array(value);
87 }
88 else if (value.is_boolean())
89 {
90 ImGui::Checkbox(key.c_str(), value.get<bool*>());
91 }
92 else if (value.is_number_integer())
93 {
94 auto data = value.get<int>();
95 if (ImGui::InputInt(key.c_str(), &data))
96 {
97 value = data;
98 }
99 }
100 else if (value.is_number_float())
101 {
102 ImGui::InputDouble(key.c_str(), value.get<double*>(), 0.1f, 1.0f, "%.1f");
103 }
104 else if (value.is_string())
105 {
106 ImGui::InputText(key.c_str(), value.get<std::string*>(), ImGuiInputTextFlags_EnterReturnsTrue);
107 }
108 else if (value.is_object())
109 {
110 if (!key.empty())
111 {
112 ImGui::Text(key.c_str());
113 }
114
115 do_object(value);
116 }
117 }
118
119 ImGui::Unindent(INDENT_PIXELS);
120
121 ImGui::PopID();
122 }
123
124 void ConfigPanel::do_array(nlohmann::json& json)
125 {
126 m_counter++;
127 ImGui::PushID(m_counter);
128
129 ImGui::Indent(INDENT_PIXELS);
130
131 unsigned int counter = 0;
132 std::string name;
133 for (auto& elem : json)
134 {
135 name = std::format("[{0}]", counter);
136 if (elem.is_array())
137 {
138 do_array(elem);
139 }
140 else if (elem.is_boolean())
141 {
142 ImGui::Checkbox(name.c_str(), elem.get<bool*>());
143
144 counter++;
145 }
146 else if (elem.is_number_integer())
147 {
148 ImGui::InputScalar(name.c_str(), ImGuiDataType_S64, reinterpret_cast<void*>(elem.get<std::int64_t*>()));
149 counter++;
150 }
151 else if (elem.is_number_unsigned())
152 {
153 ImGui::InputScalar(name.c_str(), ImGuiDataType_U64, reinterpret_cast<void*>(elem.get<std::uint64_t*>()));
154 counter++;
155 }
156 else if (elem.is_number_float())
157 {
158 ImGui::InputDouble(name.c_str(), elem.get<double*>(), 0.1f, 1.0f, "%.1f");
159 counter++;
160 }
161 else if (elem.is_string())
162 {
163 ImGui::InputText(name.c_str(), elem.get<std::string*>(), ImGuiInputTextFlags_EnterReturnsTrue);
164 counter++;
165 }
166 else if (elem.is_object())
167 {
168 do_object(elem);
169 }
170 }
171
172 ImGui::Unindent(INDENT_PIXELS);
173 ImGui::PopID();
174 }
175} // namespace galaxy
#define INDENT_PIXELS
ConfigPanel.cpp galaxy.
ConfigPanel() noexcept
Constructor.
virtual ~ConfigPanel() noexcept
Destructor.
int m_counter
Internal level counter for rendering.
void draw() override
Draw config panel.
void do_array(nlohmann::json &json)
Handles rendering array objects.
void do_object(nlohmann::json &json)
Handles rendering json objects.
bool m_show
Should the panel be shown.
Definition Panel.hpp:71
void imgui_notify_success(const char *msg)
ImGui success notification.
Application.hpp galaxy.
static auto set_settings_from_config() -> void
Set all our settings using the provided config file.
Definition Settings.cpp:82
static auto set_config_to_default() -> void
Restore all config settings to default.
Definition Settings.cpp:23