Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persist window size via .ini file #36

Merged
merged 4 commits into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ add_executable(${PROJECT_NAME} src/rig_reconfigure.cpp src/service_wrapper.cpp s
#target_compile_options(${PROJECT_NAME} PRIVATE -g -fsanitize=thread)
#target_link_options(${PROJECT_NAME} PRIVATE -fsanitize=thread)

target_include_directories(${PROJECT_NAME} PRIVATE include external/imspinner external/lodepng)
target_include_directories(${PROJECT_NAME} PRIVATE include external/lodepng)
target_link_libraries(${PROJECT_NAME} imgui rclcpp::rclcpp ament_index_cpp::ament_index_cpp)

install(TARGETS ${PROJECT_NAME}
Expand Down
2 changes: 1 addition & 1 deletion src/node_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct TreeNode {
std::string name; // node name (leaf node) / namespace (other)
std::string fullName; // full node name for easier usage

std::vector<std::shared_ptr<TreeNode>> children;
std::vector<std::shared_ptr<TreeNode>> children = {};
};

class NodeTree {
Expand Down
51 changes: 34 additions & 17 deletions src/rig_reconfigure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
#include <imgui_internal.h>
#include <vector>

#include <lodepng.h>

#include "service_wrapper.hpp"
#include "utils.hpp"
#include "lodepng.h"
#include "node_window.hpp"
#include "parameter_window.hpp"

Expand All @@ -27,6 +28,8 @@ constexpr auto STATUS_WARNING_COLOR = ImVec4(1, 0, 0, 1);
constexpr auto NODES_AUTO_REFRESH_INTERVAL = 1s; // unit: seconds
constexpr auto DESIRED_FRAME_RATE = 30;
constexpr std::chrono::duration<float> DESIRED_FRAME_DURATION_MS = 1000ms / DESIRED_FRAME_RATE;
constexpr auto DEFAULT_WINDOW_WIDTH = 600;
constexpr auto DEFAULT_WINDOW_HEIGHT = 800;

// window names
constexpr auto NODE_WINDOW_NAME = "Nodes";
Expand Down Expand Up @@ -59,36 +62,50 @@ int main(int argc, char *argv[]) {
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);

// Create window with graphics context
GLFWwindow *window = glfwCreateWindow(600, 800, "Parameter modification editor",
nullptr, nullptr);
if (window == nullptr) {
return 1;
}
glfwMakeContextCurrent(window);
glfwSwapInterval(1); // Enable vsync

const auto resourcePath = findResourcePath(argv[0]);

loadWindowIcon(window, resourcePath);

// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();

// place the imgui.ini config file within the users home directory (instead of current working directory)
const std::filesystem::path config_file_dir(std::string(std::getenv("HOME")) + "/.config/rig_reconfigure");
const std::string config_file_path = config_file_dir.string() + "/imgui.ini";

if (!std::filesystem::exists(config_file_dir)) {
std::filesystem::create_directory(config_file_dir);
}

const std::string config_file_path = config_file_dir.string() + "/imgui.ini";
ImGui::GetIO().IniFilename = config_file_path.c_str();
ImGui::GetIO().IniFilename = config_file_path.c_str(); // important for automatic saving of .ini file
ImGui::GetIO().ConfigFlags |= ImGuiConfigFlags_DockingEnable;

// load window size if already stored from last iteration
bool configFileExisting = std::filesystem::exists(config_file_path);
auto window_width = DEFAULT_WINDOW_WIDTH;
auto window_height = DEFAULT_WINDOW_HEIGHT;

if (configFileExisting) {
// manual loading of the .ini file since we require the window size before creating the glfw window
ImGui::LoadIniSettingsFromDisk(config_file_path.c_str());
ImGuiID id = ImHashStr("Root window");
ImGuiWindowSettings* root_window_settings = ImGui::FindWindowSettingsByID(id);
ottojo marked this conversation as resolved.
Show resolved Hide resolved

if (root_window_settings != nullptr) {
window_width = root_window_settings->Size.x;
window_height = root_window_settings->Size.y;
}
}

ImGui::GetIO().ConfigFlags |= ImGuiConfigFlags_DockingEnable;
// Create window with graphics context
GLFWwindow *window = glfwCreateWindow(window_width, window_height, "Parameter modification editor",
nullptr, nullptr);
if (window == nullptr) {
return 1;
}
glfwMakeContextCurrent(window);
glfwSwapInterval(1); // Enable vsync

const auto resourcePath = findResourcePath(argv[0]);

loadWindowIcon(window, resourcePath);

// Setup Dear ImGui style
ImGui::StyleColorsDark();
Expand Down
Loading