Skip to content

Commit

Permalink
GUI: Refactor settings window
Browse files Browse the repository at this point in the history
  • Loading branch information
pierre-dejoue committed Aug 26, 2023
1 parent 9b619a0 commit b5129b7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 60 deletions.
39 changes: 26 additions & 13 deletions src/gui/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "picross_file.h"
#include "settings.h"
#include "settings_window.h"

#include <picross/picross.h>
#include <stdutils/macros.h>
Expand Down Expand Up @@ -57,6 +58,13 @@ void imgui_set_style(bool dark_mode)
}
}

// Application windows
struct AppWindows
{
std::unique_ptr<SettingsWindow> settings;
std::vector<std::unique_ptr<PicrossFile>> picross;
};

} // Anonymous namespace


Expand Down Expand Up @@ -86,8 +94,6 @@ int main(int argc, char *argv[])
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
UNUSED(io);

// Setup Platform/Renderer backends
ImGui_ImplGlfw_InitForOpenGL(window, true);
Expand All @@ -103,12 +109,13 @@ int main(int argc, char *argv[])
bool imgui_dark_mode = false;
imgui_set_style(imgui_dark_mode);

// Open settings window
// Application Settings
Settings settings;
settings.open_window();

// Application Windows
AppWindows windows;

// Main loop
std::vector<std::unique_ptr<PicrossFile>> picross_files;
while (!glfwWindowShouldClose(window))
{
// Poll and handle events (inputs, window resize, etc.)
Expand Down Expand Up @@ -136,7 +143,7 @@ int main(int argc, char *argv[])
{
const auto format = picross::io::picross_file_format_from_filepath(path);
std::cout << "User selected file " << path << " (format: " << format << ")" << std::endl;
picross_files.emplace_back(std::make_unique<PicrossFile>(path, format));
windows.picross.emplace_back(std::make_unique<PicrossFile>(path, format));
}
}
if (ImGui::MenuItem("Import bitmap", "Ctrl+I"))
Expand All @@ -147,7 +154,7 @@ int main(int argc, char *argv[])
{
const auto format = picross::io::PicrossFileFormat::PBM;
std::cout << "User selected bitmap " << path << " (format: " << format << ")" << std::endl;
picross_files.emplace_back(std::make_unique<PicrossFile>(path, format));
windows.picross.emplace_back(std::make_unique<PicrossFile>(path, format));
}
}
if (ImGui::MenuItem("Import solution"))
Expand All @@ -158,7 +165,7 @@ int main(int argc, char *argv[])
{
const auto format = picross::io::PicrossFileFormat::OutputGrid;
std::cout << "User selected solution file " << path << std::endl;
picross_files.emplace_back(std::make_unique<PicrossFile>(path, format));
windows.picross.emplace_back(std::make_unique<PicrossFile>(path, format));
}
}
ImGui::Separator();
Expand All @@ -181,24 +188,30 @@ int main(int argc, char *argv[])
}

// Picross files windows (one window per grid, so possibly multiple windows per file)
for (auto it = std::begin(picross_files); it != std::end(picross_files);)
for (auto it = std::begin(windows.picross); it != std::end(windows.picross);)
{
bool can_be_erased = false;
(*it)->visit_windows(can_be_erased, settings);
it = can_be_erased ? picross_files.erase(it) : std::next(it);
it = can_be_erased ? windows.picross.erase(it) : std::next(it);
}

// Settings window (always ON)
// Settings window
if (windows.settings)
{
bool can_be_erased = false;
settings.visit_window(can_be_erased);
windows.settings->visit(can_be_erased);
assert(can_be_erased == false); // Always ON
}

#if PICROSS_GUI_IMGUI_DEMO_FLAG
// Dear Imgui Demo
ImGui::ShowDemoWindow();
#endif

// We delay the Settings window opening until after the first ImGui rendering pass so that the actual work space
// is known on first visit. This may be important for ImGui::Set* functions with ImGuiCond_Once.
if (!windows.settings) { windows.settings = std::make_unique<SettingsWindow>(settings); }

// Rendering
ImGui::Render();
int display_w, display_h;
Expand All @@ -212,7 +225,7 @@ int main(int argc, char *argv[])
} // while (!glfwWindowShouldClose(window))

// Cleanup
picross_files.clear();
windows.picross.clear();
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
Expand Down
38 changes: 3 additions & 35 deletions src/gui/src/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ namespace
} // Anonymous namespace

Settings::Settings()
: tile_settings()
, solver_settings()
, animation_settings()
{
read_tile_settings();
read_solver_settings();
read_animation_settings();
}

Settings::Tile* Settings::get_tile_settings()
Expand Down Expand Up @@ -146,35 +146,3 @@ const Settings::AnimationLimits& Settings::read_animation_settings_limits()
static Settings::AnimationLimits result = animation_settings_limits();
return result;
}

void Settings::open_window()
{
get_settings_window();

read_tile_settings();
read_solver_settings();
read_animation_settings();
}

void Settings::visit_window(bool& can_be_erased)
{
can_be_erased = false;
if (tile_settings || solver_settings || animation_settings || settings_window)
{
auto& settings_window_ref = get_settings_window();
bool window_can_be_erased = false;
settings_window_ref.visit(window_can_be_erased);
assert(!window_can_be_erased); // Do not erase settings window once open
can_be_erased &= window_can_be_erased;
}
}

SettingsWindow& Settings::get_settings_window()
{
if (!settings_window)
{
settings_window = std::make_unique<SettingsWindow>(*this);
}
assert(settings_window);
return *settings_window;
}
10 changes: 1 addition & 9 deletions src/gui/src/settings.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#pragma once

#include "parameter.h"
#include "settings_window.h"

#include <algorithm>
#include <memory>


class Settings
{
public:
Expand Down Expand Up @@ -68,15 +67,8 @@ class Settings
const Animation& read_animation_settings();
static const AnimationLimits& read_animation_settings_limits();

void open_window();
void visit_window(bool& can_be_erased);

private:
SettingsWindow& get_settings_window();

private:
std::unique_ptr<Tile> tile_settings;
std::unique_ptr<Solver> solver_settings;
std::unique_ptr<Animation> animation_settings;
std::unique_ptr<SettingsWindow> settings_window;
};
7 changes: 4 additions & 3 deletions src/gui/src/settings_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ void SettingsWindow::visit(bool& can_be_erased)

ImGui::SetNextWindowSizeConstraints(ImVec2(0, 300), ImVec2(FLT_MAX, 600));

constexpr ImGuiWindowFlags win_flags = ImGuiWindowFlags_AlwaysAutoResize;
bool is_window_open = true;
if (!ImGui::Begin(title.c_str(), &is_window_open, win_flags))
constexpr ImGuiWindowFlags win_flags = ImGuiWindowFlags_AlwaysAutoResize
| ImGuiWindowFlags_NoSavedSettings;

if (!ImGui::Begin(title.c_str(), nullptr, win_flags))
{
// Collapsed
ImGui::End();
Expand Down

0 comments on commit b5129b7

Please sign in to comment.