Skip to content

Commit

Permalink
Add loading message when launching game from editor
Browse files Browse the repository at this point in the history
  • Loading branch information
nerudaj committed Feb 17, 2024
1 parent ce39cc8 commit fdfc6ef
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/lib-app/include/app/AppStateEditor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "Commands/CommandHistory.hpp"
#include "Commands/CommandQueue.hpp"
#include "Dialogs/LoadLevelDialog.hpp"
#include "Dialogs/LoadingDialog.hpp"
#include "Dialogs/NewLevelDialog.hpp"
#include "Dialogs/SaveLevelDialog.hpp"
#include "Editor/Editor.hpp"
Expand All @@ -18,6 +19,7 @@
#include <TGUI/Backend/SFML-Graphics.hpp>
#include <TGUI/TGUI.hpp>
#include <optional>
#include <stack>

import Memory;
import Options;
Expand Down Expand Up @@ -55,6 +57,7 @@ class AppStateEditor : public dgm::AppState
virtual void restoreFocus()
{
jukebox->stop();
dialogLoading.close();
}

protected:
Expand Down Expand Up @@ -102,6 +105,7 @@ class AppStateEditor : public dgm::AppState
std::optional<std::string> pathToConfigOverride = {});
void handleSaveLevel(bool forceNewPath = false) noexcept;
void saveLevel();
void handlePlayLevelWrapper(bool useBot);
void handlePlayLevel(bool useBot);
void handleUndo();
void handleRedo();
Expand Down Expand Up @@ -148,8 +152,11 @@ class AppStateEditor : public dgm::AppState
ModernNewLevelDialog dialogNewLevel;
LoadLevelDialog dialogLoadLevel;
NewSaveLevelDialog dialogSaveLevel;
LoadingDialog dialogLoading;
ClickPreventer clickPreventer;
mem::Rc<LevelMetadata> levelMetadata;
std::stack<std::function<void(void)>> delayedActions;
int delayActionsForNumFrames = 0;

std::function<void(void)> onStateChanged = [this]
{
Expand Down
23 changes: 21 additions & 2 deletions src/lib-app/src/app/AppStateEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ AppStateEditor::AppStateEditor(
, dialogLoadLevel(gui, settings->cmdSettings.resourcesDir)
, dialogSaveLevel(
gui, Filesystem::getLevelsDir(settings->cmdSettings.resourcesDir))
, dialogLoading(gui)
{
jukebox->stop();

Expand Down Expand Up @@ -145,6 +146,14 @@ void AppStateEditor::update()
{
clickPreventer.update();

while (!delayedActions.empty() && delayActionsForNumFrames == 0)
{
delayedActions.top()();
delayedActions.pop();
}

if (delayActionsForNumFrames > 0) delayActionsForNumFrames--;

if (commandQueue->isEmpty()) return;

unsavedChanges = true;
Expand Down Expand Up @@ -235,8 +244,10 @@ tgui::MenuBar::Ptr AppStateEditor::buildMenuBarLayout(
SAVE, [this] { handleSaveLevel(); }, sf::Keyboard::S);
addFileMenuItem(SAVE_AS, [this] { handleSaveLevel(true); });
addFileMenuItem(
PLAY, [this] { handlePlayLevel("useBot"_false); }, sf::Keyboard::F5);
addFileMenuItem(PLAY2P, [this] { handlePlayLevel("useBot"_true); });
PLAY,
[this] { handlePlayLevelWrapper("useBot"_false); },
sf::Keyboard::F5);
addFileMenuItem(PLAY2P, [this] { handlePlayLevelWrapper("useBot"_true); });
addFileMenuItem(
UNDO, [this] { handleUndo(); }, sf::Keyboard::Z);
addFileMenuItem(
Expand Down Expand Up @@ -363,6 +374,13 @@ void AppStateEditor::saveLevel()
}
}

void AppStateEditor::handlePlayLevelWrapper(bool useBot)
{
dialogLoading.open(Strings::Dialog::Message::COMPUTING_DISTANCE_INDEX);
delayedActions.push([this, useBot]() { handlePlayLevel(useBot); });
delayActionsForNumFrames = 1;
}

void AppStateEditor::handlePlayLevel(bool useBot)
{
if (savePath.empty())
Expand All @@ -385,6 +403,7 @@ void AppStateEditor::handlePlayLevel(bool useBot)

auto lvd = LevelD {};
lvd.loadFromFile(savePath);

app.pushState<AppStateIngame>(
resmgr,
nativeGui,
Expand Down
4 changes: 4 additions & 0 deletions src/lib-defines/include/Configs/Strings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ namespace Strings
RAWSTRING SAVE_LEVEL = "Save level";
RAWSTRING EDIT_METADATA = "Edit level metadata";
RAWSTRING NEW_LEVEL = "New level";
RAWSTRING ERR = "Error";
RAWSTRING LOADING = "Loading";
} // namespace Title

namespace Message
Expand All @@ -86,6 +88,8 @@ namespace Strings
"Cannot play level because no level is opened.";
RAWSTRING LEVEL_NAME_EMPTY = "Level name cannot be empty.";
RAWSTRING LEVEL_NAME_TAKEN = "Level name is already taken";
RAWSTRING BAKING_LIGHTS = "Baking in lights...";
RAWSTRING COMPUTING_DISTANCE_INDEX = "Computing distance index...";
} // namespace Message

namespace Body
Expand Down
20 changes: 20 additions & 0 deletions src/lib-editor/include/Dialogs/LoadingDialog.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include "Gui.hpp"
#include "Interfaces/DialogInterfaces.hpp"

import Memory;

class [[nodiscard]] LoadingDialog final
{
public:
LoadingDialog(mem::Rc<Gui> gui) noexcept : gui(gui) {}

public:
void open(const std::string& text);
void close();

private:
mem::Rc<Gui> gui;
constexpr static inline const char* DIALOG_ID = "LoadingDialog";
};
3 changes: 2 additions & 1 deletion src/lib-editor/src/Dialogs/ErrorInfoDialog.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#include "Dialogs/ErrorInfoDialog.hpp"
#include "Globals.hpp"
#include <Configs/Strings.hpp>

void ErrorInfoDialog::open(const std::string& text)
{
auto modal = gui->createNewChildWindow("Error");
auto modal = gui->createNewChildWindow(Strings::Dialog::Title::ERR);
modal->setSize("30%", "20%");
modal->setPosition("35%", "40%");
modal->setPositionLocked(true);
Expand Down
24 changes: 24 additions & 0 deletions src/lib-editor/src/Dialogs/LoadingDialog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <Configs/Strings.hpp>
#include <Dialogs/LoadingDialog.hpp>

void LoadingDialog::open(const std::string& text)
{
auto modal = gui->createNewChildWindow(Strings::Dialog::Title::LOADING);
modal->setSize("50%", "20%");
modal->setPosition("25%", "40%");
modal->setPositionLocked(true);
gui->addModal(modal, DIALOG_ID);

auto label = tgui::Label::create(text);
label->setSize({ "98%", "98%" });
label->setPosition({ "1%", "1%" });
label->setTextSize(Sizers::GetMenuBarTextHeight());
modal->add(label);
}

void LoadingDialog::close()
{
gui->closeModal(DIALOG_ID);
}

0 comments on commit fdfc6ef

Please sign in to comment.