From 44a6aefec2aa01963c8a81c3b9d9046cbd223e70 Mon Sep 17 00:00:00 2001 From: Pierre Dejoue Date: Sat, 26 Aug 2023 20:44:02 +0200 Subject: [PATCH] More refactoring --- src/gui/CMakeLists.txt | 1 + src/gui/src/main.cpp | 183 ++++++++++++++++++++--------------------- src/gui/src/style.cpp | 32 +++++++ src/gui/src/style.h | 7 ++ 4 files changed, 131 insertions(+), 92 deletions(-) create mode 100644 src/gui/src/style.cpp create mode 100644 src/gui/src/style.h diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index 16099a8..b273130 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -17,6 +17,7 @@ set(GUI_SOURCES src/picross_file.cpp src/settings.cpp src/settings_window.cpp + src/style.cpp ) file(GLOB GUI_HEADERS src/*.h) diff --git a/src/gui/src/main.cpp b/src/gui/src/main.cpp index d8ef54d..e2df075 100644 --- a/src/gui/src/main.cpp +++ b/src/gui/src/main.cpp @@ -9,6 +9,7 @@ #include "picross_file.h" #include "settings.h" #include "settings_window.h" +#include "style.h" #include #include @@ -24,6 +25,7 @@ // See: https://github.com/ocornut/imgui/issues/4445 "OpenGL backend now embeds its own GL loader" #include +#include #include #include #include @@ -39,23 +41,11 @@ void glfw_error_callback(int error, const char* description) std::cerr << "Glfw Error " << error << ": " << description << std::endl; } -const ImColor WindowBackgroundColor_Classic(29, 75, 99, 255); -const ImColor WindowBackgroundColor_Dark(4, 8, 25, 255); -const ImColor WindowMainBackgroundColor_Classic(35, 92, 121, 255); -const ImColor WindowMainBackgroundColor_Dark(10, 25, 50, 255); - -void imgui_set_style(bool dark_mode) +std::string project_title() { - if (dark_mode) - { - ImGui::StyleColorsDark(); - ImGui::GetStyle().Colors[ImGuiCol_WindowBg] = WindowBackgroundColor_Dark; - } - else - { - ImGui::StyleColorsClassic(); - ImGui::GetStyle().Colors[ImGuiCol_WindowBg] = WindowBackgroundColor_Classic; - } + std::stringstream title; + title << "Picross Solver " << picross::get_version_string(); + return title.str(); } // Application windows @@ -65,30 +55,87 @@ struct AppWindows std::vector> picross; }; +void main_menu_bar(AppWindows& windows, bool& application_should_close, bool& gui_dark_mode) +{ + application_should_close = false; + if (ImGui::BeginMainMenuBar()) + { + if (ImGui::BeginMenu("File")) + { + if (ImGui::MenuItem("Open", "Ctrl+O")) + { + const auto paths = pfd::open_file("Select a Picross file", "", + { "Picross file", "*.txt *.nin *.non", "All files", "*" }).result(); + for (const auto& path : paths) + { + const auto format = picross::io::picross_file_format_from_filepath(path); + std::cout << "User selected file " << path << " (format: " << format << ")" << std::endl; + windows.picross.emplace_back(std::make_unique(path, format)); + } + } + if (ImGui::MenuItem("Import bitmap", "Ctrl+I")) + { + const auto paths = pfd::open_file("Select a bitmap file", "", + { "PBM file", "*.pbm", "All files", "*" }).result(); + for (const auto& path : paths) + { + const auto format = picross::io::PicrossFileFormat::PBM; + std::cout << "User selected bitmap " << path << " (format: " << format << ")" << std::endl; + windows.picross.emplace_back(std::make_unique(path, format)); + } + } + if (ImGui::MenuItem("Import solution")) + { + const auto paths = pfd::open_file("Select a solution file", "", + { "All files", "*" }).result(); + for (const auto& path : paths) + { + const auto format = picross::io::PicrossFileFormat::OutputGrid; + std::cout << "User selected solution file " << path << std::endl; + windows.picross.emplace_back(std::make_unique(path, format)); + } + } + ImGui::Separator(); + if (ImGui::BeginMenu("Options")) + { + if (ImGui::MenuItem("Dark Mode", "", &gui_dark_mode)) + { + imgui_set_style(gui_dark_mode); + } + ImGui::EndMenu(); + } + ImGui::Separator(); + if (ImGui::MenuItem("Quit", "Alt+F4")) + { + application_should_close = true; + } + ImGui::EndMenu(); + } + ImGui::EndMainMenuBar(); + } +} + } // Anonymous namespace int main(int argc, char *argv[]) { - UNUSED(argc); - UNUSED(argv); - - // Versions - std::stringstream picross_title; - picross_title << "Picross Solver " << picross::get_version_string(); - std::cout << picross_title.str() << std::endl; + UNUSED(argc); UNUSED(argv); // Setup main window + constexpr int WINDOW_WIDTH = 1280; + constexpr int WINDOW_HEIGHT = 720; glfwSetErrorCallback(glfw_error_callback); if (!glfwInit()) - return 1; + return EXIT_FAILURE; const char* glsl_version = "#version 130"; glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); - GLFWwindow* window = glfwCreateWindow(1280, 720, picross_title.str().c_str(), nullptr, nullptr); - if (window == nullptr) - return 1; - glfwMakeContextCurrent(window); + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Important on MacOS for OpenGL 3.0+ + GLFWwindow* glfw_window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, project_title().data(), nullptr, nullptr); + if (glfw_window == nullptr) + return EXIT_FAILURE; + glfwMakeContextCurrent(glfw_window); glfwSwapInterval(1); // Enable vsync // Setup Dear ImGui context @@ -96,18 +143,19 @@ int main(int argc, char *argv[]) ImGui::CreateContext(); // Setup Platform/Renderer backends - ImGui_ImplGlfw_InitForOpenGL(window, true); + ImGui_ImplGlfw_InitForOpenGL(glfw_window, true); ImGui_ImplOpenGL3_Init(glsl_version); // Print out version information + std::cout << project_title() << std::endl; std::cout << "Dear ImGui " << IMGUI_VERSION << std::endl; std::cout << "GLFW " << GLFW_VERSION_MAJOR << "." << GLFW_VERSION_MINOR << "." << GLFW_VERSION_REVISION << std::endl; std::cout << "OpenGL Version " << glGetString(GL_VERSION) << std::endl; std::cout << "OpenGL Renderer " << glGetString(GL_RENDERER) << std::endl; // Style - bool imgui_dark_mode = false; - imgui_set_style(imgui_dark_mode); + bool gui_dark_mode = false; + imgui_set_style(gui_dark_mode); // Application Settings Settings settings; @@ -116,7 +164,7 @@ int main(int argc, char *argv[]) AppWindows windows; // Main loop - while (!glfwWindowShouldClose(window)) + while (!glfwWindowShouldClose(glfw_window)) { // Poll and handle events (inputs, window resize, etc.) // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs. @@ -131,60 +179,11 @@ int main(int argc, char *argv[]) ImGui::NewFrame(); // Main menu - if (ImGui::BeginMainMenuBar()) { - if (ImGui::BeginMenu("File")) - { - if (ImGui::MenuItem("Open", "Ctrl+O")) - { - const auto paths = pfd::open_file("Select a Picross file", "", - { "Picross file", "*.txt *.nin *.non", "All files", "*" }).result(); - for (const auto& path : paths) - { - const auto format = picross::io::picross_file_format_from_filepath(path); - std::cout << "User selected file " << path << " (format: " << format << ")" << std::endl; - windows.picross.emplace_back(std::make_unique(path, format)); - } - } - if (ImGui::MenuItem("Import bitmap", "Ctrl+I")) - { - const auto paths = pfd::open_file("Select a bitmap file", "", - { "PBM file", "*.pbm", "All files", "*" }).result(); - for (const auto& path : paths) - { - const auto format = picross::io::PicrossFileFormat::PBM; - std::cout << "User selected bitmap " << path << " (format: " << format << ")" << std::endl; - windows.picross.emplace_back(std::make_unique(path, format)); - } - } - if (ImGui::MenuItem("Import solution")) - { - const auto paths = pfd::open_file("Select a solution file", "", - { "All files", "*" }).result(); - for (const auto& path : paths) - { - const auto format = picross::io::PicrossFileFormat::OutputGrid; - std::cout << "User selected solution file " << path << std::endl; - windows.picross.emplace_back(std::make_unique(path, format)); - } - } - ImGui::Separator(); - if (ImGui::BeginMenu("Options")) - { - if (ImGui::MenuItem("Dark Mode", "", &imgui_dark_mode)) - { - imgui_set_style(imgui_dark_mode); - } - ImGui::EndMenu(); - } - ImGui::Separator(); - if (ImGui::MenuItem("Quit", "Alt+F4")) - { - glfwSetWindowShouldClose(window, 1); - } - ImGui::EndMenu(); - } - ImGui::EndMainMenuBar(); + bool app_should_close = false; + main_menu_bar(windows, app_should_close, gui_dark_mode); + if (app_should_close) + glfwSetWindowShouldClose(glfw_window, 1); } // Picross files windows (one window per grid, so possibly multiple windows per file) @@ -213,15 +212,15 @@ int main(int argc, char *argv[]) if (!windows.settings) { windows.settings = std::make_unique(settings); } // Rendering - ImGui::Render(); int display_w, display_h; - glfwGetFramebufferSize(window, &display_w, &display_h); + glfwGetFramebufferSize(glfw_window, &display_w, &display_h); glViewport(0, 0, display_w, display_h); - const auto clear_color = static_cast(imgui_dark_mode ? WindowMainBackgroundColor_Dark : WindowMainBackgroundColor_Classic); - glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w); + const auto clear_color = get_background_color(gui_dark_mode); + glClearColor(clear_color[0], clear_color[1], clear_color[2], clear_color[3]); glClear(GL_COLOR_BUFFER_BIT); + ImGui::Render(); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); - glfwSwapBuffers(window); + glfwSwapBuffers(glfw_window); } // while (!glfwWindowShouldClose(window)) // Cleanup @@ -229,9 +228,9 @@ int main(int argc, char *argv[]) ImGui_ImplOpenGL3_Shutdown(); ImGui_ImplGlfw_Shutdown(); ImGui::DestroyContext(); - glfwDestroyWindow(window); + glfwDestroyWindow(glfw_window); glfwTerminate(); - return 0; + return EXIT_SUCCESS; } diff --git a/src/gui/src/style.cpp b/src/gui/src/style.cpp new file mode 100644 index 0000000..ca29afd --- /dev/null +++ b/src/gui/src/style.cpp @@ -0,0 +1,32 @@ +#include "style.h" + +#include + + +namespace +{ + constexpr ImColor WindowBackgroundColor_Classic(29, 75, 99, 217); + constexpr ImColor WindowBackgroundColor_Dark(4, 8, 25, 240); + constexpr ImColor WindowMainBackgroundColor_Classic(35, 92, 121, 255); + constexpr ImColor WindowMainBackgroundColor_Dark(10, 25, 50, 255); +} + +void imgui_set_style(bool dark_mode) +{ + if (dark_mode) + { + ImGui::StyleColorsDark(); + ImGui::GetStyle().Colors[ImGuiCol_WindowBg] = WindowBackgroundColor_Dark; + } + else + { + ImGui::StyleColorsClassic(); + ImGui::GetStyle().Colors[ImGuiCol_WindowBg] = WindowBackgroundColor_Classic; + } +} + +std::array get_background_color(bool dark_mode) +{ + const auto c = static_cast(dark_mode ? WindowMainBackgroundColor_Dark : WindowMainBackgroundColor_Classic); + return std::array{ c.x, c.y, c.z, c.w }; +} diff --git a/src/gui/src/style.h b/src/gui/src/style.h new file mode 100644 index 0000000..91bfae6 --- /dev/null +++ b/src/gui/src/style.h @@ -0,0 +1,7 @@ +#pragma once + +#include + +void imgui_set_style(bool dark_mode); + +std::array get_background_color(bool dark_mode);