From 7333fdc4011dc6055f028a398289ca7e11df7c64 Mon Sep 17 00:00:00 2001 From: illyum <90023277+itzilly@users.noreply.github.com> Date: Thu, 8 Aug 2024 23:56:59 -0600 Subject: [PATCH] Add exporting/importing --- main.cpp | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/main.cpp b/main.cpp index 4b28200..619f436 100644 --- a/main.cpp +++ b/main.cpp @@ -8,6 +8,8 @@ #include "imgui_impl_glfw.h" #include "imgui_impl_opengl3.h" #include +#include +#include void generateRandomColors(unsigned char& r, unsigned char& g, unsigned char& b); @@ -144,6 +146,53 @@ void sendDMXData(HANDLE hSerial, unsigned char* data, int length) { WriteFile(hSerial, data, length, &bytesWritten, NULL); } +// Function to export markers to a JSON file +void exportMarkersToFile(const std::string& filename) { + nlohmann::json j; + + for (const auto& marker : markers) { + j.push_back({ + {"time", std::get<0>(marker)}, + {"r", std::get<1>(marker)}, + {"g", std::get<2>(marker)}, + {"b", std::get<3>(marker)} + }); + } + + std::ofstream file(filename); + if (file.is_open()) { + file << j.dump(4); // Pretty print with indentation of 4 spaces + file.close(); + } + else { + std::cerr << "Could not open file for writing: " << filename << std::endl; + } +} + +// Function to load markers from a JSON file +void loadMarkersFromFile(const std::string& filename) { + nlohmann::json j; + std::ifstream file(filename); + + if (file.is_open()) { + file >> j; + file.close(); + + markers.clear(); + for (const auto& element : j) { + markers.emplace_back( + element["time"].get(), + element["r"].get(), + element["g"].get(), + element["b"].get() + ); + } + } + else { + std::cerr << "Could not open file for reading: " << filename << std::endl; + } +} + GLFWwindow* initOpenGL() { if (!glfwInit()) { std::cerr << "Failed to initialize GLFW" << std::endl; @@ -193,6 +242,16 @@ void renderImGui() { // Create a window ImGui::Begin("DMX Controller"); + if (ImGui::Button("Export Lightshow")) { + exportMarkersToFile("lightshow.json"); + } + + ImGui::SameLine(); + + if (ImGui::Button("Load Lightshow")) { + loadMarkersFromFile("lightshow.json"); + } + // Create sliders for the first three DMX channels ImGui::SliderInt("Channel 1", &channel1, 0, 255); ImGui::SliderInt("Channel 2", &channel2, 0, 255);