Skip to content

Commit

Permalink
WIP commit, trying to move all the path setup logic to the idTech set…
Browse files Browse the repository at this point in the history
…up page.
  • Loading branch information
codereader committed Nov 8, 2017
1 parent 6cb6787 commit b23f9c1
Show file tree
Hide file tree
Showing 6 changed files with 267 additions and 62 deletions.
58 changes: 27 additions & 31 deletions radiant/settings/GameManager.cpp
Expand Up @@ -59,20 +59,38 @@ const StringSet& Manager::getDependencies() const {

void Manager::initialiseModule(const ApplicationContext& ctx)
{
// Read command line parameters, these override any existing preference setting
const ApplicationContext::ArgumentList& args = ctx.getCmdLineArgs();

for (const std::string& arg : args)
{
if (string::istarts_with(arg, "fs_game="))
{
GlobalRegistry().set(RKEY_FS_GAME, arg.substr(8));
}
else if (string::istarts_with(arg, "fs_game_base="))
{
GlobalRegistry().set(RKEY_FS_GAME_BASE, arg.substr(13));
}
}

initialise(ctx.getRuntimeDataPath());

initEnginePath();
}

const std::string& Manager::getFSGame() const {
return _fsGame;
const std::string& Manager::getFSGame() const
{
return GlobalRegistry().get(RKEY_FS_GAME);
}

const std::string& Manager::getFSGameBase() const {
return _fsGameBase;
const std::string& Manager::getFSGameBase() const
{
return GlobalRegistry().get(RKEY_FS_GAME_BASE);
}

const std::string& Manager::getModPath() const {
const std::string& Manager::getModPath() const
{
// Return the fs_game path if available
return (!_modPath.empty()) ? _modPath : _modBasePath;
}
Expand Down Expand Up @@ -125,7 +143,7 @@ void Manager::initialise(const std::string& appPath)
// No game types available, bail out, the program would crash anyway on
// module load
wxutil::Messagebox::ShowFatalError(
_("GameManager: No valid game files found, can't continue."), NULL
_("GameManager: No valid game files found, can't continue."), nullptr
);
}

Expand Down Expand Up @@ -159,10 +177,10 @@ void Manager::initialise(const std::string& appPath)
// The game type should be selected now
if (!_currentGameName.empty())
{
rMessage() << "GameManager: Selected game type: "
<< _currentGameName << std::endl;
rMessage() << "GameManager: Selected game type: " << _currentGameName << std::endl;
}
else {
else
{
// No game type selected, bail out, the program would crash anyway on module load
wxutil::Messagebox::ShowFatalError(_("No game type selected."));
}
Expand Down Expand Up @@ -199,28 +217,6 @@ void Manager::constructPaths()
// Make sure it's a well formatted path
_enginePath = os::standardPathWithSlash(_enginePath);

// Read command line parameters, these override any existing preference setting
const ApplicationContext::ArgumentList& args(
module::ModuleRegistry::Instance().getApplicationContext().getCmdLineArgs()
);

for (ApplicationContext::ArgumentList::const_iterator i = args.begin();
i != args.end();
++i)
{
// get the argument and investigate it
std::string arg = *i;

if (string::istarts_with(arg, "fs_game="))
{
GlobalRegistry().set(RKEY_FS_GAME, arg.substr(8));
}
else if (string::istarts_with(arg, "fs_game_base="))
{
GlobalRegistry().set(RKEY_FS_GAME_BASE, arg.substr(13));
}
}

// Load the fsGame and fsGameBase from the registry
_fsGame = GlobalRegistry().get(RKEY_FS_GAME);
_fsGameBase = GlobalRegistry().get(RKEY_FS_GAME_BASE);
Expand Down
97 changes: 80 additions & 17 deletions radiant/ui/prefdialog/GameSetupDialog.cpp
Expand Up @@ -5,6 +5,7 @@
#include "igame.h"
#include "modulesystem/ModuleRegistry.h"

#include "wxutil/dialog/MessageBox.h"
#include "registry/registry.h"
#include "GameSetupPageIdTech.h"
#include <wx/panel.h>
Expand All @@ -31,7 +32,20 @@ GameSetupDialog::GameSetupDialog(wxWindow* parent) :
mainVbox->Add(label);
mainVbox->Add(_book, 1, wxEXPAND);

mainVbox->Add(CreateStdDialogButtonSizer(wxOK | wxCANCEL), 0, wxALIGN_RIGHT);
wxBoxSizer* buttonHBox = new wxBoxSizer(wxHORIZONTAL);

// Create the Save button
wxButton* saveButton = new wxButton(this, wxID_SAVE);
saveButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(GameSetupDialog::onSave), nullptr, this);

// Create the assign shortcut button
wxButton* cancelButton = new wxButton(this, wxID_CANCEL);
cancelButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(GameSetupDialog::onCancel), NULL, this);

buttonHBox->Add(saveButton, 0, wxRIGHT, 6);
buttonHBox->Add(cancelButton, 0, wxRIGHT, 6);

mainVbox->Add(buttonHBox, 0, wxALIGN_RIGHT | wxTOP | wxBOTTOM, 12);

initialiseControls();

Expand Down Expand Up @@ -76,44 +90,93 @@ void GameSetupDialog::initialiseControls()
}
}

void GameSetupDialog::save()
GameSetupPage* GameSetupDialog::getSelectedPage()
{
// Extract the game type value from the current page and save it to the registry
wxWindow* container = _book->GetPage(_book->GetSelection());
return dynamic_cast<GameSetupPage*>(wxWindow::FindWindowByName("GameSetupPage", container));
}

std::string GameSetupDialog::getSelectedGameType()
{
if (_book->GetSelection() == wxNOT_FOUND)
// Extract the game type value from the current page and save it to the registry
GameSetupPage* page = getSelectedPage();

if (page == nullptr) return std::string();

wxStringClientData* data = static_cast<wxStringClientData*>(page->GetClientData());

return data->GetData().ToStdString();
}

void GameSetupDialog::onSave(wxCommandEvent& ev)
{
if (getSelectedGameType().empty())
{
rError() << "Cannot save game type, nothing selected" << std::endl;
// Ask the user to select a game type
wxutil::Messagebox::Show(_("Invalid Settings"),
_("Please select a game type"), ui::IDialog::MESSAGE_CONFIRM, nullptr);
return;
}

// Extract the game type value from the current page and save it to the registry
wxWindow* container = _book->GetPage(_book->GetSelection());
GameSetupPage* page = dynamic_cast<GameSetupPage*>(wxWindow::FindWindowByName("GameSetupPage", container));
GameSetupPage* page = GameSetupDialog::getSelectedPage();
assert(page != nullptr);

wxStringClientData* data = static_cast<wxStringClientData*>(page->GetClientData());
try
{
page->validateSettings();
}
catch (GameSettingsInvalidException& ex)
{
std::string msg = fmt::format(_("Warning:\n{0}\nDo you want to correct these settings?"), ex.what());

std::string selectedGame = data->GetData().ToStdString();
registry::setValue(RKEY_GAME_TYPE, selectedGame);
if (wxutil::Messagebox::Show(_("Invalid Settings"),
msg, ui::IDialog::MESSAGE_ASK, nullptr) == wxutil::Messagebox::RESULT_YES)
{
// User wants to correct the settings, don't exit
return;
}
}

EndModal(wxID_OK);
}

// Ask the current page to set the paths to the registry
page->saveSettings();
void GameSetupDialog::onCancel(wxCommandEvent& ev)
{
EndModal(wxID_CANCEL);
}

void GameSetupDialog::Show(const cmd::ArgumentList& args)
GameSetupDialog::Result GameSetupDialog::Show(const cmd::ArgumentList& args)
{
GameSetupDialog::Result 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) ?
GlobalMainFrame().getWxTopLevelWindow() : nullptr;

GameSetupDialog* dialog = new GameSetupDialog(parent);

int result = dialog->ShowModal();

if (result == wxID_OK)
if (dialog->ShowModal() == wxID_OK)
{
dialog->save();
result.gameType = dialog->getSelectedGameType();

if (result.gameType.empty())
{
rError() << "Cannot save game paths, nothing selected" << std::endl;
return result;
}

GameSetupPage* page = dialog->getSelectedPage();

result.enginePath = page->getEnginePath();
result.modPath = page->getModPath();
result.modBasePath = page->getModBasePath();
}

dialog->Destroy();

return result;
}

}
18 changes: 16 additions & 2 deletions radiant/ui/prefdialog/GameSetupDialog.h
Expand Up @@ -30,14 +30,28 @@ class GameSetupDialog :
GameSetupDialog(wxWindow* parent);

public:
std::string getSelectedGameType();

// The result after the user is done with the dialog
struct Result
{
std::string gameType; // Display name of the selected game
std::string enginePath; // selected engine path
std::string modPath; // selected mod path
std::string modBasePath; // selected mod base path
};

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

private:
GameSetupPage* getSelectedPage();

void initialiseControls();

void save();
void onSave(wxCommandEvent& ev);
void onCancel(wxCommandEvent& ev);
};

}
13 changes: 11 additions & 2 deletions radiant/ui/prefdialog/GameSetupPage.h
Expand Up @@ -39,8 +39,17 @@ class GameSetupPage :
// GameSettingsInvalidException in case something is not correct.
virtual void validateSettings() = 0;

// Saves the settings to the registry
virtual void saveSettings() = 0;
// The following three path accessors are needed by the owning GameManager
// to continue setting up the VFS search order, map paths, etc.

// Returns the engine path as derived from the user's input
virtual std::string getEnginePath() = 0;

// Returns the mod base path as derived from the user's input
virtual std::string getModBasePath() = 0;

// Returns the mod path as derived from the user's input
virtual std::string getModPath() = 0;

public:
typedef std::function<GameSetupPage*(wxWindow*)> CreateInstanceFunc;
Expand Down

0 comments on commit b23f9c1

Please sign in to comment.