Navigation Menu

Skip to content

Commit

Permalink
GameSetupDialog can now actively apply the configuration to the GameM…
Browse files Browse the repository at this point in the history
…anager.
  • Loading branch information
codereader committed Dec 5, 2017
1 parent 1b9e157 commit 0578da0
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 73 deletions.
50 changes: 30 additions & 20 deletions radiant/settings/GameManager.cpp
Expand Up @@ -79,19 +79,28 @@ void Manager::initialiseModule(const ApplicationContext& ctx)
}
}

GlobalCommandSystem().addCommand("ProjectSettings",
std::bind(&Manager::showGameSetupDialog, this, std::placeholders::_1));

// Scan the <applicationpath>/games folder for .game files
loadGameFiles(ctx.getRuntimeDataPath());

// Check if there's a saved game type in the registry,
// try to setup a default if nothing is found.
initialiseGameType();

// Try to retrieve a persisted game setup from the registry
GameConfiguration config;
config.loadFromRegistry();

// Check validity of the saved game configuration
// and invoke the UI if it's not a valid one.
initialiseConfig();
if (config.pathsValid())
{
applyConfig(config);
}
else
{
// The UI will call applyConfig on its own
showGameSetupDialog();
}
}

const std::string& Manager::getFSGame() const
Expand Down Expand Up @@ -201,16 +210,18 @@ std::string Manager::getUserEnginePath()
return _config.enginePath;
}

void Manager::initialiseConfig()
void Manager::applyConfig(const GameConfiguration& config)
{
// Try to retrieve a saved value for the game setup
_config.loadFromRegistry();

if (!_config.pathsValid())
if (!config.pathsValid())
{
showGameSetupDialog(cmd::ArgumentList());
rError() << "GameManager: Cannot apply invalid configuration, paths not valid" << std::endl;
return;
}

// Store the configuration, and persist it to the registry
_config = config;
_config.saveToRegistry();

// Extract the fs_game / fs_game_base settings from the mod path
std::string fsGame = os::getRelativePath(_config.modPath, _config.enginePath);
string::trim_right(fsGame, "/");
Expand All @@ -225,16 +236,10 @@ void Manager::initialiseConfig()
initialiseVfs();
}

void Manager::showGameSetupDialog(const cmd::ArgumentList& args)
void Manager::showGameSetupDialog()
{
// Paths not valid, ask the user to select something
GameConfiguration result = ui::GameSetupDialog::Show(cmd::ArgumentList());

if (!result.enginePath.empty())
{
_config = result;
_config.saveToRegistry();
}
ui::GameSetupDialog::Show(cmd::ArgumentList());
}

void Manager::setMapAndPrefabPaths(const std::string& baseGamePath)
Expand Down Expand Up @@ -393,7 +398,12 @@ void Manager::loadGameFiles(const std::string& appPath)
}
}

} // namespace game

// The static module definition (self-registers)
module::StaticModule<game::Manager> gameManagerModule;

Manager& Manager::Instance()
{
return *gameManagerModule.getModule();
}

} // namespace game
72 changes: 34 additions & 38 deletions radiant/settings/GameManager.h
Expand Up @@ -24,34 +24,22 @@ class Manager :
typedef std::map<std::string, GamePtr> GameMap;

private:

// Map of named games
// Map of named games
GameMap _games;

// Map of indexed games
GameList _sortedGames;

// The currently active game configuration
GameConfiguration _config;

private:
// Set the map and prefab file paths from the current game information
void setMapAndPrefabPaths(const std::string& baseGamePath);

// greebo: Loads the paths from the registry and constructs a few secondary paths.
void initialiseConfig();

public:
Manager();

// greebo: Sets up the VFS paths and calls initialise()
void initialiseVfs();

/** greebo: Gets the engine path (e.g. /usr/local/doom3/).
*/
// greebo: Gets the engine path (e.g. /usr/local/doom3/).
const std::string& getEnginePath() const;

/** greebo: Get the user engine path (is OS-specific)
*/
// greebo: Get the user engine path (is OS-specific)
std::string getUserEnginePath() override;

/**
Expand All @@ -66,42 +54,50 @@ class Manager :
*/
const std::string& getModBasePath() const override;

/** greebo: Accessor method for the fs_game parameter
*/
// greebo: Accessor method for the fs_game parameter
const std::string& getFSGame() const override;

/** greebo: Accessor method for the fs_game_base parameter
*/
// greebo: Accessor method for the fs_game_base parameter
const std::string& getFSGameBase() const override;

void showGameSetupDialog(const cmd::ArgumentList& args);

/** greebo: Returns the current Game (shared_ptr).
*/
virtual IGamePtr currentGame() override;
// greebo: Returns the current Game (shared_ptr).
IGamePtr currentGame() override;

// Get the list of available games, sorted by their index
const GameList& getSortedGameList() override;

/**
* greebo: Loads the game type from the saved settings.
* Tries to fall back to a reasonable default. Afterwards, the
* _config.gameType member is properly filled in.
*/
// Returns the sorted game path list
const PathList& getVFSSearchPaths() const override;

// RegisterableModule implementation
const std::string& getName() const override;
const StringSet& getDependencies() const override;
void initialiseModule(const ApplicationContext& ctx) override;

// greebo: Stores the given config, initialises VFS and constructs a few secondary paths.
void applyConfig(const GameConfiguration& config);

// Module-internal accessor to the GameManager instance
static Manager& Instance();

private:
/**
* greebo: Loads the game type from the saved settings.
* Tries to fall back to a reasonable default. Afterwards, the
* _config.gameType member is properly filled in.
*/
void initialiseGameType();

/** greebo: Scans the "games/" subfolder for .game description foles.
*/
// greebo: Scans the "games/" subfolder for .game description foles.
void loadGameFiles(const std::string& appPath);

// Returns the sorted game path list
virtual const PathList& getVFSSearchPaths() const override;
// Set the map and prefab file paths from the current game information
void setMapAndPrefabPaths(const std::string& baseGamePath);

// RegisterableModule implementation
virtual const std::string& getName() const override;
virtual const StringSet& getDependencies() const override;
virtual void initialiseModule(const ApplicationContext& ctx) override;
// greebo: Sets up the VFS paths and calls GlobalFileSystem().initialise(), using the internal _config member
void initialiseVfs();

void showGameSetupDialog();
};

} // namespace game
3 changes: 2 additions & 1 deletion radiant/ui/UserInterfaceModule.cpp
Expand Up @@ -10,7 +10,7 @@

#include "modulesystem/StaticModule.h"

#include "settings/GameManager.h"
#include "ui/prefdialog/GameSetupDialog.h"
#include "ui/layers/LayerOrthoContextMenuItem.h"
#include "ui/layers/LayerControlDialog.h"
#include "ui/overlay/OverlayDialog.h"
Expand Down Expand Up @@ -112,6 +112,7 @@ void UserInterfaceModule::registerUICommands()
{
TexTool::registerCommands();

GlobalCommandSystem().addCommand("ProjectSettings", GameSetupDialog::Show);
GlobalCommandSystem().addCommand("Preferences", PrefDialog::ShowPrefDialog);

GlobalCommandSystem().addCommand("ToggleConsole", Console::toggle);
Expand Down
35 changes: 22 additions & 13 deletions radiant/ui/prefdialog/GameSetupDialog.cpp
Expand Up @@ -12,6 +12,7 @@
#include <wx/sizer.h>
#include <wx/choicebk.h>
#include <wx/stattext.h>
#include "settings/GameManager.h"

namespace ui
{
Expand Down Expand Up @@ -125,13 +126,13 @@ std::string GameSetupDialog::getSelectedGameType()
return data->GetData().ToStdString();
}

void GameSetupDialog::onSave(wxCommandEvent& ev)
void GameSetupDialog::tryEndModal(wxStandardID result)
{
if (getSelectedGameType().empty())
{
// Ask the user to select a game type
wxutil::Messagebox::Show(_("Invalid Settings"),
_("Please select a game type"), ui::IDialog::MESSAGE_CONFIRM, nullptr);
wxutil::Messagebox::Show(_("Invalid Settings"),
_("Please select a game type"), IDialog::MESSAGE_CONFIRM, nullptr);
return;
}

Expand All @@ -146,20 +147,27 @@ void GameSetupDialog::onSave(wxCommandEvent& ev)
{
std::string msg = fmt::format(_("Warning:\n{0}\nDo you want to correct these settings?"), ex.what());

if (wxutil::Messagebox::Show(_("Invalid Settings"),
msg, ui::IDialog::MESSAGE_ASK, nullptr) == wxutil::Messagebox::RESULT_YES)
if (wxutil::Messagebox::Show(_("Invalid Settings"),
msg, IDialog::MESSAGE_ASK, nullptr) == wxutil::Messagebox::RESULT_YES)
{
// User wants to correct the settings, don't exit
return;
}
}

EndModal(wxID_OK);
EndModal(result);
}

void GameSetupDialog::onSave(wxCommandEvent& ev)
{
// Confirm valid or invalid settings and end the dialog
tryEndModal(wxID_OK);
}

void GameSetupDialog::onCancel(wxCommandEvent& ev)
{
EndModal(wxID_CANCEL);
// Confirm valid or invalid settings and end the dialog
tryEndModal(wxID_CANCEL);
}

void GameSetupDialog::onPageChanged(wxBookCtrlEvent& ev)
Expand All @@ -175,10 +183,8 @@ void GameSetupDialog::onPageChanged(wxBookCtrlEvent& ev)
}
}

game::GameConfiguration GameSetupDialog::Show(const cmd::ArgumentList& args)
void GameSetupDialog::Show(const cmd::ArgumentList& args)
{
game::GameConfiguration result;

// greebo: Check if the mainframe module is already "existing". It might be
// uninitialised if this dialog is shown during DarkRadiant startup
wxWindow* parent = module::GlobalModuleRegistry().moduleExists(MODULE_MAINFRAME) ?
Expand All @@ -190,13 +196,16 @@ game::GameConfiguration GameSetupDialog::Show(const cmd::ArgumentList& args)
{
GameSetupPage* page = dialog->getSelectedPage();

assert(page != nullptr);

// Copy the values from the page instance to our result
result = page->getConfiguration();
const game::GameConfiguration& config = page->getConfiguration();

// Apply the configuration (don't use the GlobalGameManager accessor yet)
game::Manager::Instance().applyConfig(config);
}

dialog->Destroy();

return result;
}

}
4 changes: 3 additions & 1 deletion radiant/ui/prefdialog/GameSetupDialog.h
Expand Up @@ -34,7 +34,7 @@ class GameSetupDialog :
std::string getSelectedGameType();

// greebo: The command target to show the Game settings preferences.
static game::GameConfiguration Show(const cmd::ArgumentList& args);
static void Show(const cmd::ArgumentList& args);

private:
GameSetupPage* getSelectedPage();
Expand All @@ -46,6 +46,8 @@ class GameSetupDialog :
void onSave(wxCommandEvent& ev);
void onCancel(wxCommandEvent& ev);
void onPageChanged(wxBookCtrlEvent& ev);

void tryEndModal(wxStandardID result);
};

}

0 comments on commit 0578da0

Please sign in to comment.