i wanna save some struct variables with imgui::SaveIniSettingsToDisk and nlohmann json…
- User
i wanna save some struct variables with imgui::SaveIniSettingsToDisk and nlohmann json library, show snippet how to do it
- Assistant
Here's an example of how you could save a struct using the
imgui::SaveIniSettingsToDiskfunction and the nlohmann json library:#include <imgui.h> #include <nlohmann/json.hpp> // Define your struct here struct MyStruct { int myInt; float myFloat; bool myBool; char myChar[16]; }; // Define a global instance of your struct MyStruct myStruct = { 42, 3.14f, true, "Hello, world!" }; int main() { ImGui::CreateContext(); // ... // Call imgui::SaveIniSettingsToDisk with a filename to save your settings ImGui::SaveIniSettingsToDisk("settings.ini"); // Create an instance of a JSON object nlohmann::json j; // Add your struct's values to the JSON object j["myInt"] = myStruct.myInt; j["myFloat"] = myStruct.myFloat; j["myBool"] = myStruct.myBool; j["myChar"] = myStruct.myChar; // Use the dump() function to convert the JSON object to a string std::string jsonString = j.dump(); // Save the JSON string to a file std::ofstream outFile("settings.json"); outFile << jsonString; outFile.close(); return 0; } - User
std::ofstream outFile("settings.json"); is marked as error
- Assistant
I apologize for the mistake. The
std::ofstreamline should be modified to create the file in binary mode, like this:std::ofstream outFile("settings.json", std::ios::binary);This will ensure that no newline translation is performed on the data when writing to the file. Please let me know if you have any further questions or issues.
- User
okay, and now i need to load them back
- Assistant
Sure! Here's an example of how you could use the nlohmann json library to load your saved JSON file back into your struct:
int main() { ImGui::CreateContext(); // ... // Load settings from JSON file std::ifstream inFile("settings.json"); if (inFile.is_open()) { nlohmann::json j; inFile >> j; myStruct.myInt = j["myInt"]; myStruct.myFloat = j["myFloat"]; myStruct.myBool = j["myBool"]; std::strcpy(myStruct.myChar, j["myChar"].get<std::string>().c_str()); } while (!glfwWindowShouldClose(window)) { // ... // Call imgui::SaveIniSettingsToDisk with a filename to save your settings ImGui::SaveIniSettingsToDisk("settings.ini"); } ImGui::DestroyContext(); return 0; }This code creates an input stream and opens the "settings.json" file that you saved earlier. It then uses the
nlohmann::json::operator[]function to access the values from the JSON object and assign them to the fields of the struct. Note that themyCharfield needs to be converted from a to a using . - User
i wanna save ImVec4 color with your json snippet, but i can't just call j["color"] = color;
- Assistant
To save an
ImVec4color value to JSON using the nlohmann json library, you could convert theImVec4value to astd::vectororstd::arrayoffloatsbefore adding it to the JSON object. Here's an example:// Save an ImVec4 color value to JSON ImVec4 color(1.0f, 0.5f, 0.2f, 1.0f); std::vector<float> colorVec = { color.x, color.y, color.z, color.w }; j["color"] = colorVec;This code creates an
ImVec4color value and then converts it to astd::vectorof fourfloats. It then adds this vector to thenlohmann::jsonobject using theoperator[].To load the color value back from JSON, you would extract the vector of
floatsfrom the JSON object and create a newImVec4value from it:// Load an ImVec4 color value from JSON nlohmann::json j; j["color"] = { 1.0f, 0.5f, 0.2f, 1.0f }; std::vector<float> colorVec = j["color"]; ImVec4 color(colorVec[0], colorVec[1], colorVec[2], colorVec[3]);This code loads the JSON object and assigns a vector of to the key. It then extracts this vector from the JSON object and uses it to create a new value.
- User
std::vector colorVec = j[“color”]; has error
- Assistant
I apologize for my mistake. When reading an
ImVec4color value back from JSON, you need to first extract the array offloatsfrom the JSON object and then use it to construct a newImVec4. Here is an example of how you can read a color value from a JSON file:// Load an ImVec4 color value from JSON std::ifstream inFile("settings.json"); if (inFile.is_open()) { nlohmann::json j; inFile >> j; std::vector<float> colorVec = j["color"]; ImVec4 color(colorVec[0], colorVec[1], colorVec[2], colorVec[3]); }This code reads the contents of the "settings.json" file into a
nlohmann::jsonobject, extracts the"color"key from the object, and assigns its value to astd::vector<float> colorVec. It then constructs a newImVec4value using the values stored in thecolorVecvector.I hope this helps! Let me know if you have any further questions.
- User
Error (active) E0350 more than one operator "=" matches these operands: imgui_trk_final E:\projects\mine\2023TRK_imgui2\imgui_trk_final\imgui_trk_final\imguiSettings.cpp 105 still error
- Assistant
I'm sorry for the confusion. The error message you're seeing is due to having multiple
operator=functions available for the same operation. You can work around this issue by casting the vector to an array, or by using thestd::arraycontainer instead. Here's how you can modify the code to use a vector cast to an array:// Save an ImVec4 color value to JSON ImVec4 color(1.0f, 0.5f, 0.2f, 1.0f); std::vector<float> colorVec = { color.x, color.y, color.z, color.w }; j["color"] = nlohmann::json::array({ colorVec.begin(), colorVec.end() });This code creates an
ImVec4color value and then converts it to astd::vectorof fourfloats. It then casts this vector to astd::arrayand adds it to thenlohmann::jsonobject by wrapping it in anarrayconstructor.To read the color value back from JSON, you would extract the vector of floats from the JSON object and convert it to an :
- User
std::vector colorVec = j[“color”]; gives an error
Error (active) E0350 more than one operator "=" matches these operands: imgui_trk_final E:\projects\mine\2023TRK_imgui2\imgui_trk_final\imgui_trk_final\imguiSettings.cpp 105
- Assistant
I apologize for the mistake. When reading a color value back from JSON, you need to first extract the array of
floatsfrom the JSON object and then use it to construct a newImVec4instance.Here is an example of how you can read a color value from a JSON file:
// Load an ImVec4 color value from JSON std::ifstream inFile("settings.json"); if (inFile.is_open()) { nlohmann::json j; inFile >> j; std::vector<float> colorVec = j["color"].get<std::vector<float>>(); ImVec4 color(colorVec[0], colorVec[1], colorVec[2], colorVec[3]); }This code reads the contents of the "settings.json" file into a
nlohmann::jsonobject, extracts the"color"key from the object, and then uses theget()function to convert the value stored there to astd::vector<float>. It then constructs a newImVec4instance using the values from the vector.I hope this helps you! Please let me know if you have any other questions.
- User
why when i move / resize my imgui window it resets to previous location / size
- Assistant
When you create a new
ImGuiContext, by default, an INI file called "imgui.ini" is loaded and used to store persistent settings and preferences, including window positions and sizes.To prevent your ImGui window from resetting to the previous location and size, you can either:
- Disable INI file loading by passing a
nullptrvalue instead of a filename as theini_filenameparameter ofImGui::CreateContext(). This will prevent ImGui from attempting to load any INI files and will prevent it from overwriting window positions and sizes.
ImGui::CreateContext(nullptr);- If you're using an INI file to store your settings, make sure to call
ImGui::SaveIniSettingsToDisk()after making any changes to the window location or size.SaveIniSettingsToDisk()writes the current state of the ImGui context to the INI file. If you don't call this function, any changes you make to window positions and sizes will be lost the next time the application runs.
while (!glfwWindowShouldClose(window)) { // ... // Save ImGui settings to INI file ImGui::SaveIniSettingsToDisk("imgui.ini"); } - Disable INI file loading by passing a
- User
i mean it resets when launched
- Assistant
I see! In that case, it's likely that your application is attempting to load the default "imgui.ini" file when it starts up, which is overwriting the settings that you saved previously. To prevent this from happening, you can either delete the default "imgui.ini" file before your application runs, or you can specify a custom INI file name using
ImGui::CreateContext().- Deleting the default "imgui.ini" file:
std::remove("imgui.ini"); ImGui::CreateContext();This code deletes the default "imgui.ini" file before creating a new
ImGuiContextinstance. This ensures that there are no existing INI files to overwrite your window settings.- Using a custom INI file name:
ImGui::CreateContext(nullptr, "mysettings.ini");This code creates a new
ImGuiContextinstance and specifies "mysettings.ini" as the INI file name. This will prevent ImGui from attempting to use the default "imgui.ini" file and ensures that your window settings are saved to a file with a custom name that you can choose.I hope this helps! Let me know if you have any further questions.
- User
how can i stick buttons to bottom line of imgui window
- Assistant
To stick buttons to the bottom line of an ImGui window, you can make use of the
ImGui::GetWindowDrawList()andImGui::GetWindowPos()functions to calculate the position of the bottom edge of the window.Here's an example of how you can create a button at the bottom of an ImGui window:
bool showButton = true; while (!glfwWindowShouldClose(window)) { ImGui::Begin("My Window"); // ... // Get the window position and size ImVec2 windowPos = ImGui::GetWindowPos(); ImVec2 windowSize = ImGui::GetWindowSize(); // Calculate the position of the button ImVec2 buttonPos(windowPos.x + windowSize.x / 2, windowPos.y + windowSize.y - 40); // Display the button if (showButton) { if (ImGui::Button("My Button", ImVec2(100, 30))) { // ... } } // ... ImGui::End(); }This code uses
ImGui::GetWindowPos()andImGui::GetWindowSize()to retrieve the position and size of the current window. It then subtracts the height of the button (30 pixels in this case) from the window height and adds it to the window's Y coordinate. This gives us the Y coordinate of the button's bottom edge. - User
buttonPos is not used anywhere
- Assistant
I apologize for the confusion. You can use the
ImGui::SetCursorPos()function to set the cursor position to the desired location before displaying the button, like this:while (!glfwWindowShouldClose(window)) { // ... ImGui::Begin("My Window"); // ... // Get the window position and size ImVec2 windowPos = ImGui::GetWindowPos(); ImVec2 windowSize = ImGui::GetWindowSize(); // Calculate the position of the button ImVec2 buttonPos(windowPos.x + windowSize.x / 2 - 50, windowPos.y + windowSize.y - 40); // Set the cursor position to the button location ImGui::SetCursorPos(buttonPos); // Display the button if (ImGui::Button("My Button", ImVec2(100, 30))) { // Button action } // ... ImGui::End(); // ... }This code calculates the position of the button and then sets the cursor position with
ImGui::SetCursorPos(buttonPos)before displaying the button. The button will then be displayed at the desired location.I hope this helps! Let me know if you have any further questions.
- User
i need this button to stick on left bottom corner, not right bottom
- Assistant
To stick a button to the left bottom corner of an ImGui window, you can use a combination of
ImGui::GetWindowDrawList(),ImGui::GetWindowPos(), andImGui::GetWindowContentRegionMin()functions to calculate the position of the bottom-left corner of the window. Here's an example:bool showButton = true; while (!glfwWindowShouldClose(window)) { ImGui::Begin("My Window"); // ... // Get the window position and content region ImVec2 windowPos = ImGui::GetWindowPos(); ImVec2 contentRegionMin = ImGui::GetWindowContentRegionMin(); // Calculate the position of the button ImVec2 buttonPos(windowPos.x, windowPos.y + contentRegionMin.y + ImGui::GetFontSize() + ImGui::GetStyle().ItemSpacing.y); // Set the cursor position to the button location ImGui::SetCursorPos(buttonPos); // Display the button if (showButton) { if (ImGui::Button("My Button", ImVec2(100, 30))) { // ... } } // ... ImGui::End(); } - User
ImVec2 windowPos = ImGui::GetWindowPos(); ImVec2 windowSize = ImGui::GetWindowSize();
// Calculate the position of the button ImVec2 buttonPos(windowPos.x + windowSize.x / 2 - 50, windowPos.y + windowSize.y - 40);does this snippet will place button on bottom-left side of window?
- Assistant
No, the snippet you posted earlier will place the button on the bottom center of the window, as it is calculating the X coordinate using
windowSize.x/2 - 50and the Y coordinate usingwindowPos.y + windowSize.y - 40.To place the button on the bottom left corner of the window, you need to use