Skip to content

Commit

Permalink
MainLoopController covers autosave on exit
Browse files Browse the repository at this point in the history
  • Loading branch information
chrxh committed Oct 22, 2024
1 parent 32671b1 commit ddd636b
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 34 deletions.
2 changes: 1 addition & 1 deletion imgui.ini
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ Collapsed=0

[Window][Exit]
Pos=810,490
Size=328,102
Size=328,107
Collapsed=0

[Window][Do you really want to terminate the program?]
Expand Down
2 changes: 1 addition & 1 deletion source/Gui/AutosaveController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void AutosaveController::shutdown()
if (!_on) {
return;
}
onSave();
//onSave();
}

bool AutosaveController::isOn() const
Expand Down
15 changes: 4 additions & 11 deletions source/Gui/ExitDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

#include "ExitDialog.h"
#include "AlienImGui.h"

void ExitDialog::initIntern(bool& onExit)
{
_onExit = &onExit;
}
#include "MainLoopController.h"

ExitDialog::ExitDialog()
: AlienDialog("Exit")
Expand All @@ -16,14 +12,11 @@ void ExitDialog::processIntern()
{
ImGui::Text("Do you really want to terminate the program?");

ImGui::Spacing();
ImGui::Spacing();
ImGui::Separator();
ImGui::Spacing();
ImGui::Spacing();
ImGui::Dummy({0, ImGui::GetContentRegionAvail().y - scale(50.0f)});
AlienImGui::Separator();

if (AlienImGui::Button("OK")) {
*_onExit = true;
MainLoopController::get().scheduleShutdown();
close();
}
ImGui::SameLine();
Expand Down
5 changes: 1 addition & 4 deletions source/Gui/ExitDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@
#include "AlienDialog.h"
#include "Definitions.h"

class ExitDialog : public AlienDialog<bool&>
class ExitDialog : public AlienDialog<>
{
MAKE_SINGLETON_NO_DEFAULT_CONSTRUCTION(ExitDialog);

private:
ExitDialog();

void initIntern(bool& onExit) override;
void processIntern() override;

bool* _onExit = nullptr;
};
60 changes: 57 additions & 3 deletions source/Gui/MainLoopController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,34 @@ void MainLoopController::process()
processFadeInUI();
} else if (_programState == ProgramState::OperatingMode) {
processOperatingMode();
} else if (_programState == ProgramState::ScheduleExit) {
processScheduleExit();
} else if (_programState == ProgramState::Exiting) {
processExiting();
}

ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(WindowController::get().getWindowData().window);
}

void MainLoopController::scheduleShutdown()
{
_programState = ProgramState::ScheduleExit;
}

bool MainLoopController::shouldClose() const
{
return _programState == ProgramState::Finished;
}

void MainLoopController::processFirstTick()
{
drawLoadingScreen();

auto senderInfo = SenderInfo{.senderId = SenderId{StartupSenderId}, .wishResultData = true, .wishErrorInfo = true};
auto readData = ReadSimulationRequestData{Const::AutosaveFile};
_startupSimRequestId = _persisterFacade->scheduleReadSimulationFromFile(senderInfo, readData);
_loadSimRequestId = _persisterFacade->scheduleReadSimulationFromFile(senderInfo, readData);
_programState = ProgramState::LoadingScreen;

OverlayController::get().process();
Expand All @@ -123,9 +137,9 @@ void MainLoopController::processLoadingScreen()
{
drawLoadingScreen();

auto requestedSimState = _persisterFacade->getRequestState(_startupSimRequestId);
auto requestedSimState = _persisterFacade->getRequestState(_loadSimRequestId);
if (requestedSimState == PersisterRequestState::Finished) {
auto const& data = _persisterFacade->fetchReadSimulationData(_startupSimRequestId);
auto const& data = _persisterFacade->fetchReadSimulationData(_loadSimRequestId);
auto const& deserializedSim = data.deserializedSimulation;
_simulationFacade->newSimulation(
data.simulationName,
Expand Down Expand Up @@ -217,6 +231,46 @@ void MainLoopController::processOperatingMode()
popGlobalStyle();

FpsController::get().processForceFps(WindowController::get().getFps());

if (glfwWindowShouldClose(WindowController::get().getWindowData().window)) {
scheduleShutdown();
}
}

void MainLoopController::processScheduleExit()
{
if (AutosaveController::get().isOn()) {
printOverlayMessage("Saving ...");

auto senderInfo = SenderInfo{.senderId = SenderId{StartupSenderId}, .wishResultData = true, .wishErrorInfo = false};
auto saveData = SaveSimulationRequestData{Const::AutosaveFile, Viewport::get().getZoomFactor(), Viewport::get().getCenterInWorldPos()};
_saveSimRequestId = _persisterFacade->scheduleSaveSimulationToFile(senderInfo, saveData);
_programState = ProgramState::Exiting;
} else {
_programState = ProgramState::Finished;
}
}

void MainLoopController::processExiting()
{
SimulationView::get().draw();

pushGlobalStyle();
processMenubar();
MainLoopEntityController::get().process();
OverlayController::get().process();
SimulationView::get().processSimulationScrollbars();
popGlobalStyle();

FpsController::get().processForceFps(WindowController::get().getFps());

auto requestedSimState = _persisterFacade->getRequestState(_saveSimRequestId);
if (requestedSimState == PersisterRequestState::Finished) {
_persisterFacade->fetchSaveSimulationData(_saveSimRequestId);
_programState = ProgramState::Finished;
} else if (requestedSimState == PersisterRequestState::Error) {
_programState = ProgramState::Finished;
}
}

void MainLoopController::drawLoadingScreen()
Expand Down
13 changes: 11 additions & 2 deletions source/Gui/MainLoopController.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@ class MainLoopController
public:
void setup(SimulationFacade const& simulationFacade, PersisterFacade const& persisterFacade);
void process();
void scheduleShutdown();

bool shouldClose() const;

private:
void processFirstTick();
void processLoadingScreen();
void processFadeOutLoadingScreen();
void processFadeInUI();
void processOperatingMode();
void processScheduleExit();
void processExiting();

void drawLoadingScreen();
void decreaseAlphaForFadeOutLoadingScreen();
Expand All @@ -41,11 +46,15 @@ class MainLoopController
LoadingScreen,
FadeOutLoadingScreen,
FadeInUI,
OperatingMode
OperatingMode,
ScheduleExit,
Exiting,
Finished
};
ProgramState _programState = ProgramState::FirstTick;

PersisterRequestId _startupSimRequestId;
PersisterRequestId _loadSimRequestId;
PersisterRequestId _saveSimRequestId;

TextureData _logo;
std::optional<std::chrono::steady_clock::time_point> _simulationLoadedTimepoint;
Expand Down
5 changes: 2 additions & 3 deletions source/Gui/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ _MainWindow::_MainWindow(SimulationFacade const& simulationFacade, PersisterFaca
SimulationParametersWindow::get().setup(_simulationFacade);
GpuSettingsDialog::get().setup(_simulationFacade);
MainLoopController::get().setup(_simulationFacade, _persisterFacade);
ExitDialog::get().setup(_onExit);
ExitDialog::get().setup();
MassOperationsDialog::get().setup(_simulationFacade);
LogWindow::get().setup(_logger);
GettingStartedWindow::get().setup();
Expand Down Expand Up @@ -167,8 +167,7 @@ _MainWindow::_MainWindow(SimulationFacade const& simulationFacade, PersisterFaca

void _MainWindow::mainLoop()
{
auto window = WindowController::get().getWindowData().window;
while (!glfwWindowShouldClose(window) && !_onExit)
while (!MainLoopController::get().shouldClose())
{
MainLoopController::get().process();
}
Expand Down
2 changes: 0 additions & 2 deletions source/Gui/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,4 @@ class _MainWindow

PersisterFacade _persisterFacade;
SimulationFacade _simulationFacade;

bool _onExit = false;
};
15 changes: 8 additions & 7 deletions source/Gui/OverlayController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,20 @@ void OverlayController::processLoadingBar()

auto viewSize = toRealVector2D(Viewport::get().getViewSize());
auto width = viewSize.x / 6 + 1.0f;
auto height = scale(20.0f);
auto height = scale(15.0f);
auto center = ImVec2{viewSize.x / 2, viewSize.y - scale(60.0f)};

auto constexpr N = 20;
auto constexpr N = 12;
for (int i = 0; i < N; ++i) {
auto amplitude1 = sinf(toFloat(i) * 10.0f / toFloat(N) - duration / 240.0f);
auto amplitude2 = sinf(toFloat(i) * 14.0f / toFloat(N) - duration / 135.0f);
auto amplitude1 = sinf(toFloat(i) * 10.0f / toFloat(N) - powf(duration / 340.0f, 1.3f) /** (1.0f + 0.3f * sin(duration / 2000))*/);
auto amplitude2 = sinf(toFloat(i) * 14.0f / toFloat(N) - powf(duration / 235.0f, 1.3f) /** (1.0f + 0.3f * sin(duration / 2000))*/);
//auto hue = toFloat((i * 1000 / N + toInt(duration)) % 3000) / 4500.0f;
//hue = hue < 0.33f ? 0.66f + hue : 0.66f + 0.66f - hue;

auto y1 = center.y + height / 2 - amplitude1 * height;
auto y2 = center.y + height / 2 - amplitude2 * height;
drawList->AddRectFilled(
ImVec2{center.x - width / 2 + toFloat(i) / N * width, center.y + height / 2 - amplitude1 * height},
ImVec2{center.x - width / 2 + toFloat(i + 1) / N * width - scale(3), center.y + height / 2 - amplitude2 * height / 2},
ImVec2{center.x - width / 2 + toFloat(i) / N * width, std::min(y1, y2)},
ImVec2{center.x - width / 2 + toFloat(i + 1) / N * width - scale(3), std::max(y1, y2)},
ImColor::HSV(0, 0.1f, 0.35f, 0.6f));
}
drawList->AddText(
Expand Down

0 comments on commit ddd636b

Please sign in to comment.