Skip to content

Commit

Permalink
Allow a new FM folder to be created on the fly if it doesn't exist at…
Browse files Browse the repository at this point in the history
… game setup time.
  • Loading branch information
codereader committed Dec 6, 2017
1 parent c85f690 commit c88424d
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
10 changes: 9 additions & 1 deletion radiant/ui/prefdialog/GameSetupDialog.cpp
Expand Up @@ -136,7 +136,7 @@ void GameSetupDialog::tryEndModal(wxStandardID result)
return;
}

GameSetupPage* page = GameSetupDialog::getSelectedPage();
GameSetupPage* page = getSelectedPage();
assert(page != nullptr);

try
Expand All @@ -160,6 +160,14 @@ void GameSetupDialog::tryEndModal(wxStandardID result)

void GameSetupDialog::onSave(wxCommandEvent& ev)
{
GameSetupPage* page = getSelectedPage();
assert(page != nullptr);

if (!page->onPreSave())
{
return; // pre-save action returned false, prevent dialog close
}

// Confirm valid or invalid settings and end the dialog
tryEndModal(wxID_OK);
}
Expand Down
7 changes: 7 additions & 0 deletions radiant/ui/prefdialog/GameSetupPage.h
Expand Up @@ -53,6 +53,13 @@ class GameSetupPage :
// Called by the owning GameSetupDialog when this page is selected
virtual void onPageShown() = 0;

// Fired before the dialog is about to run the save routine
// Override and return false here to veto the event
virtual bool onPreSave()
{
return true; // return true by default, don't veto
}

// Accessor to the result config structure
virtual const game::GameConfiguration& getConfiguration();

Expand Down
51 changes: 50 additions & 1 deletion radiant/ui/prefdialog/GameSetupPageTdm.cpp
Expand Up @@ -4,6 +4,7 @@
#include "imodule.h"
#include "igame.h"

#include "wxutil/dialog/MessageBox.h"
#include "wxutil/PathEntry.h"

#include <fmt/format.h>
Expand Down Expand Up @@ -73,12 +74,60 @@ const char* GameSetupPageTdm::getType()
return TYPE();
}

bool GameSetupPageTdm::onPreSave()
{
constructPaths();

// Validate the engine path first, otherwise we can't do anything
if (!os::fileOrDirExists(_config.enginePath))
{
return true; // proceed to normal validation routine, which will error out anyway
}

// Check the mod path (=mission path), if not empty
if (!_config.modPath.empty() && !os::fileOrDirExists(_config.modPath))
{
// Mod name is not empty, but mod folder doesnt' exist, this might indicate that
// the user wants to start a new mission, ask him whether we should create the folder
std::string missionName = _missionEntry->GetValue().ToStdString();
std::string msg = fmt::format(_("The mission path {0} doesn't exist.\nDo you intend to start a "
"new mission and create that folder?"), _config.modPath);

if (wxutil::Messagebox::Show(fmt::format(_("Start a new Mission named {0}?"), missionName),
msg, IDialog::MESSAGE_ASK, wxGetTopLevelParent(this)) == wxutil::Messagebox::RESULT_YES)
{
// User wants to create the path
rMessage() << "Creating mission directory " << _config.modPath << std::endl;

// Create the directory
if (!os::makeDirectory(_config.modPath))
{
wxutil::Messagebox::Show(_("Could not create directory"), fmt::format(_("Failed to create the folder {0}"), _config.modPath),
IDialog::MessageType::MESSAGE_ERROR, wxGetTopLevelParent(this));

// Veto the event
return false;
}

// Everything went smooth, proceed to the normal save routine
return true;
}

// User doesn't want to create a new mission, so veto the save event
return false;
}

// Engine path is OK, mod path is empty or exists already
return true;
}

void GameSetupPageTdm::validateSettings()
{
constructPaths();

std::string errorMsg;

// Validate the engine path first, otherwise we can't do anything
if (!os::fileOrDirExists(_config.enginePath))
{
// Engine path doesn't exist
Expand All @@ -88,7 +137,7 @@ void GameSetupPageTdm::validateSettings()
// Check the mod path (=mission path), if not empty
if (!_config.modPath.empty() && !os::fileOrDirExists(_config.modPath))
{
// Mod name is not empty, but mod folder doesnt' exist
// Path not existent => error
errorMsg += fmt::format(_("The mission path \"{0}\" does not exist.\n"), _config.modPath);
}

Expand Down
1 change: 1 addition & 0 deletions radiant/ui/prefdialog/GameSetupPageTdm.h
Expand Up @@ -28,6 +28,7 @@ class GameSetupPageTdm :

const char* getType() override;
void validateSettings() override;
bool onPreSave() override;
void onPageShown() override;

private:
Expand Down

0 comments on commit c88424d

Please sign in to comment.