Skip to content

Commit

Permalink
add generic initialization mechanism in AlienWindow
Browse files Browse the repository at this point in the history
  • Loading branch information
chrxh committed Oct 20, 2024
1 parent 0a88f00 commit a2de5bf
Show file tree
Hide file tree
Showing 35 changed files with 138 additions and 140 deletions.
60 changes: 0 additions & 60 deletions source/Gui/AlienWindow.cpp

This file was deleted.

81 changes: 80 additions & 1 deletion source/Gui/AlienWindow.h
Original file line number Diff line number Diff line change
@@ -1,29 +1,108 @@
#pragma once

#include <imgui.h>

#include "Base/GlobalSettings.h"

#include "Definitions.h"
#include "StyleRepository.h"
#include "ShutdownInterface.h"
#include "ShutdownController.h"
#include "WindowController.h"

template<typename ...Dependencies>
class AlienWindow : public ShutdownInterface
{
public:
AlienWindow(std::string const& title, std::string const& settingsNode, bool defaultOn);

void init(Dependencies... dependencies);

void process();

bool isOn() const;
void setOn(bool value);


protected:
virtual void initIntern(Dependencies... dependencies) {}
virtual void shutdownIntern() {}
virtual void processIntern() = 0;
virtual void processBackground() {}
virtual void processActivated() {}

bool _sizeInitialized = false;
bool _on = false;
bool _defaultOn = false;
std::string _title;
std::string _settingsNode;

private:
void shutdown() override;
};
};

/************************************************************************/
/* Implementation */
/************************************************************************/

template <typename ... Dependencies>
AlienWindow<Dependencies...>::AlienWindow(std::string const& title, std::string const& settingsNode, bool defaultOn)
: _title(title)
, _settingsNode(settingsNode)
, _defaultOn(defaultOn)
{}

template <typename ... Dependencies>
void AlienWindow<Dependencies...>::init(Dependencies... dependencies)
{
_on = GlobalSettings::get().getBool(_settingsNode + ".active", _defaultOn);
ShutdownController::get().registerObject(this);
initIntern(dependencies...);
}

template <typename ... Dependencies>
void AlienWindow<Dependencies...>::process()
{
processBackground();

if (!_on) {
return;
}
ImGui::PushID(_title.c_str());

ImGui::SetNextWindowBgAlpha(Const::WindowAlpha * ImGui::GetStyle().Alpha);
if (ImGui::Begin(_title.c_str(), &_on)) {
if (!_sizeInitialized) {
auto size = ImGui::GetWindowSize();
auto factor = WindowController::get().getContentScaleFactor() / WindowController::get().getLastContentScaleFactor();
ImGui::SetWindowSize({size.x * factor, size.y * factor});
_sizeInitialized = true;
}
processIntern();
}
ImGui::End();

ImGui::PopID();
}

template <typename ... Dependencies>
bool AlienWindow<Dependencies...>::isOn() const
{
return _on;
}

template <typename ... Dependencies>
void AlienWindow<Dependencies...>::setOn(bool value)
{
_on = value;
if (value) {
processActivated();
}
}

template <typename ... Dependencies>
void AlienWindow<Dependencies...>::shutdown()
{
shutdownIntern();
GlobalSettings::get().setBool(_settingsNode + ".active", _on);
}
10 changes: 5 additions & 5 deletions source/Gui/AutosaveWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ namespace
auto constexpr AutosaveSenderId = "Autosave";
}

void AutosaveWindow::init(SimulationFacade const& simulationFacade, PersisterFacade const& persisterFacade)
AutosaveWindow::AutosaveWindow()
: AlienWindow("Autosave (work in progress)", "windows.autosave", false)
{}

void AutosaveWindow::initIntern(SimulationFacade simulationFacade, PersisterFacade persisterFacade)
{
_simulationFacade = simulationFacade;
_persisterFacade = persisterFacade;
Expand All @@ -30,10 +34,6 @@ void AutosaveWindow::init(SimulationFacade const& simulationFacade, PersisterFac
_numberOfFiles = GlobalSettings::get().getInt("windows.autosave.number of files", _origNumberOfFiles);
}

AutosaveWindow::AutosaveWindow()
: AlienWindow("Autosave (work in progress)", "windows.autosave", false)
{}

void AutosaveWindow::shutdownIntern()
{
GlobalSettings::get().setBool("windows.autosave.settings.open", _settingsOpen);
Expand Down
7 changes: 2 additions & 5 deletions source/Gui/AutosaveWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,15 @@ struct SavepointEntry
uint64_t timestep;
};

class AutosaveWindow : public AlienWindow
class AutosaveWindow : public AlienWindow<SimulationFacade, PersisterFacade>
{
MAKE_SINGLETON_NO_DEFAULT_CONSTRUCTION(AutosaveWindow);

public:
void init(SimulationFacade const& simulationFacade, PersisterFacade const& persisterFacade);

private:
AutosaveWindow();

void initIntern(SimulationFacade simulationFacade, PersisterFacade persisterFacade) override;
void shutdownIntern() override;

void processIntern() override;

void processToolbar();
Expand Down
2 changes: 1 addition & 1 deletion source/Gui/BrowserWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ namespace
{WorkspaceType_Private, std::string("private")}};
}

void BrowserWindow::init(SimulationFacade const& simulationFacade, PersisterFacade const& persisterFacade)
void BrowserWindow::initIntern(SimulationFacade simulationFacade, PersisterFacade persisterFacade)
{
_simulationFacade = simulationFacade;
_persisterFacade = persisterFacade;
Expand Down
4 changes: 2 additions & 2 deletions source/Gui/BrowserWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@
#include "Definitions.h"
#include "LastSessionBrowserData.h"

class BrowserWindow : public AlienWindow
class BrowserWindow : public AlienWindow<SimulationFacade, PersisterFacade>
{
MAKE_SINGLETON_NO_DEFAULT_CONSTRUCTION(BrowserWindow);

public:
void init(SimulationFacade const& simulationFacade, PersisterFacade const& persisterFacade);

void onRefresh();
WorkspaceType getCurrentWorkspaceType() const;
Expand All @@ -32,6 +31,7 @@ class BrowserWindow : public AlienWindow
private:
BrowserWindow();

void initIntern(SimulationFacade simulationFacade, PersisterFacade persisterFacade) override;
void shutdownIntern() override;

struct WorkspaceId
Expand Down
1 change: 0 additions & 1 deletion source/Gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ PUBLIC
AlienDialog.h
AlienImGui.cpp
AlienImGui.h
AlienWindow.cpp
AlienWindow.h
AutosaveController.cpp
AutosaveController.h
Expand Down
2 changes: 1 addition & 1 deletion source/Gui/CreatorWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace
auto const RightColumnWidth = 160.0f;
}

void CreatorWindow::init(SimulationFacade const& simulationFacade)
void CreatorWindow::initIntern(SimulationFacade simulationFacade)
{
_simulationFacade = simulationFacade;
}
Expand Down
7 changes: 3 additions & 4 deletions source/Gui/CreatorWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,19 @@ enum CreationMode_
CreationMode_Drawing
};

class CreatorWindow : public AlienWindow
class CreatorWindow : public AlienWindow<SimulationFacade>
{
MAKE_SINGLETON_NO_DEFAULT_CONSTRUCTION(CreatorWindow);

public:
void init(SimulationFacade const& simulationFacade);

void onDrawing();
void finishDrawing();

private:
CreatorWindow();

void processIntern();
void initIntern(SimulationFacade simulationFacade) override;
void processIntern() override;

void createCell();
void createParticle();
Expand Down
2 changes: 1 addition & 1 deletion source/Gui/Definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class _MainWindow;
using MainWindow = std::shared_ptr<_MainWindow>;

class AlienWindow;
//class AlienWindow;

class SimulationView;

Expand Down
2 changes: 2 additions & 0 deletions source/Gui/EditorController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ namespace
void EditorController::init(SimulationFacade const& simulationFacade)
{
_simulationFacade = simulationFacade;

SelectionWindow::get().init();
EditorModel::get().init(_simulationFacade);
GenomeEditorWindow::get().init(_simulationFacade);
PatternEditorWindow::get().init(_simulationFacade);
Expand Down
2 changes: 1 addition & 1 deletion source/Gui/GenomeEditorWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace
auto const SubWindowRightMargin = 0.0f;
}

void GenomeEditorWindow::init(SimulationFacade const& simulationFacade)
void GenomeEditorWindow::initIntern(SimulationFacade simulationFacade)
{
_simulationFacade = simulationFacade;

Expand Down
5 changes: 2 additions & 3 deletions source/Gui/GenomeEditorWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,18 @@
#include "AlienWindow.h"
#include "Definitions.h"

class GenomeEditorWindow : public AlienWindow
class GenomeEditorWindow : public AlienWindow<SimulationFacade>
{
MAKE_SINGLETON_NO_DEFAULT_CONSTRUCTION(GenomeEditorWindow);

public:
void init(SimulationFacade const& simulationFacade);

void openTab(GenomeDescription const& genome, bool openGenomeEditorIfClosed = true);
GenomeDescription const& getCurrentGenome() const;

private:
GenomeEditorWindow();

void initIntern(SimulationFacade simulationFacade) override;
void shutdownIntern() override;

void processIntern() override;
Expand Down
2 changes: 1 addition & 1 deletion source/Gui/GettingStartedWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <windows.h>
#endif

void GettingStartedWindow::init()
void GettingStartedWindow::initIntern()
{
_showAfterStartup = _on;
}
Expand Down
6 changes: 2 additions & 4 deletions source/Gui/GettingStartedWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@
#include "Definitions.h"
#include "AlienWindow.h"

class GettingStartedWindow : public AlienWindow
class GettingStartedWindow : public AlienWindow<>
{
MAKE_SINGLETON_NO_DEFAULT_CONSTRUCTION(GettingStartedWindow);

public:
void init();

private:
GettingStartedWindow();

void initIntern() override;
void shutdownIntern() override;
void processIntern() override;

Expand Down
2 changes: 1 addition & 1 deletion source/Gui/LogWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "GuiLogger.h"
#include "AlienImGui.h"

void LogWindow::init(GuiLogger const& logger)
void LogWindow::initIntern(GuiLogger logger)
{
_logger = logger;
_verbose = GlobalSettings::get().getBool("windows.log.verbose", false);
Expand Down
6 changes: 2 additions & 4 deletions source/Gui/LogWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@
#include "Definitions.h"
#include "AlienWindow.h"

class LogWindow : public AlienWindow
class LogWindow : public AlienWindow<GuiLogger>
{
MAKE_SINGLETON_NO_DEFAULT_CONSTRUCTION(LogWindow);

public:
void init(GuiLogger const& logger);

private:
LogWindow();

void initIntern(GuiLogger logger) override;
void shutdownIntern() override;
void processIntern() override;

Expand Down
Loading

0 comments on commit a2de5bf

Please sign in to comment.