Skip to content

Commit

Permalink
More refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
pierre-dejoue committed Aug 26, 2023
1 parent b5129b7 commit 44a6aef
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 92 deletions.
1 change: 1 addition & 0 deletions src/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
183 changes: 91 additions & 92 deletions src/gui/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "picross_file.h"
#include "settings.h"
#include "settings_window.h"
#include "style.h"

#include <picross/picross.h>
#include <stdutils/macros.h>
Expand All @@ -24,6 +25,7 @@
// See: https://github.com/ocornut/imgui/issues/4445 "OpenGL backend now embeds its own GL loader"

#include <cassert>
#include <cstdlib>
#include <iostream>
#include <iterator>
#include <memory>
Expand All @@ -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
Expand All @@ -65,49 +55,107 @@ struct AppWindows
std::vector<std::unique_ptr<PicrossFile>> 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<PicrossFile>(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<PicrossFile>(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<PicrossFile>(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
IMGUI_CHECKVERSION();
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;
Expand All @@ -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.
Expand All @@ -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<PicrossFile>(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<PicrossFile>(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<PicrossFile>(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)
Expand Down Expand Up @@ -213,25 +212,25 @@ int main(int argc, char *argv[])
if (!windows.settings) { windows.settings = std::make_unique<SettingsWindow>(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<ImVec4>(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
windows.picross.clear();
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwDestroyWindow(window);
glfwDestroyWindow(glfw_window);
glfwTerminate();

return 0;
return EXIT_SUCCESS;
}

32 changes: 32 additions & 0 deletions src/gui/src/style.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "style.h"

#include <imgui_wrap.h>


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<float, 4> get_background_color(bool dark_mode)
{
const auto c = static_cast<ImVec4>(dark_mode ? WindowMainBackgroundColor_Dark : WindowMainBackgroundColor_Classic);
return std::array<float, 4>{ c.x, c.y, c.z, c.w };
}
7 changes: 7 additions & 0 deletions src/gui/src/style.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include <array>

void imgui_set_style(bool dark_mode);

std::array<float, 4> get_background_color(bool dark_mode);

0 comments on commit 44a6aef

Please sign in to comment.