-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathsettings_handler.cpp
More file actions
80 lines (73 loc) · 2.73 KB
/
settings_handler.cpp
File metadata and controls
80 lines (73 loc) · 2.73 KB
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
/*
* Copyright (c) 2023-2025, NVIDIA CORPORATION. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-FileCopyrightText: Copyright (c) 2023-2025, NVIDIA CORPORATION.
* SPDX-License-Identifier: Apache-2.0
*/
#include <unordered_map>
#include <sstream>
#include <imgui/imgui.h>
#include <imgui/imgui_internal.h>
#include "settings_handler.hpp"
nvgui::SettingsHandler::SettingsHandler(const std::string& name)
{
setHandlerName(name);
}
nvgui::SettingsHandler::SettingsHandler() {}
// This is reading the section in the .ini file and for each entry read or write the value
// ex:
// [Application][State]
// WindowWidth=1513
// WindowHeight=871
//
void nvgui::SettingsHandler::addImGuiHandler()
{
assert(!handlerName.empty());
ImGuiSettingsHandler ini_handler{};
ini_handler.TypeName = handlerName.c_str();
ini_handler.TypeHash = ImHashStr(handlerName.c_str());
ini_handler.ReadOpenFn = [](ImGuiContext*, ImGuiSettingsHandler*, const char*) -> void* { return (void*)1; };
// Read line by line and send the string after the `=` as value
ini_handler.ReadLineFn = [](ImGuiContext*, ImGuiSettingsHandler* handler, void*, const char* line) {
SettingsHandler* s = static_cast<SettingsHandler*>(handler->UserData);
char key[64], value[256];
key[63] = 0; // zero terminate, protection
value[255] = 0; // zero terminate, protection
#ifdef _MSC_VER
if(sscanf_s(line, "%63[^=]=%255[^\n]", key, (unsigned)_countof(key), value, (unsigned)_countof(value)) == 2)
#else
if(sscanf(line, "%63[^=]=%255[^\n]", key, value) == 2)
#endif
{
auto it = s->settings.find(key);
if(it != s->settings.end())
{
it->second.fromString(value);
}
}
};
// Write the ["name"][State], then one line for each setting
ini_handler.WriteAllFn = [](ImGuiContext* ctx, ImGuiSettingsHandler* handler, ImGuiTextBuffer* buf) {
SettingsHandler* s = static_cast<SettingsHandler*>(handler->UserData);
buf->appendf("[%s][State]\n", handler->TypeName);
for(const auto& [key, entry] : s->settings)
{
buf->appendf("%s=%s\n", key.c_str(), entry.toString().c_str());
}
buf->appendf("\n");
};
ini_handler.UserData = this;
ImGui::AddSettingsHandler(&ini_handler);
}