Skip to content

Commit

Permalink
#6013: Refactor ExportSelectedAsModel command signature, adjust dialo…
Browse files Browse the repository at this point in the history
…g to pass the correct set of arguments
  • Loading branch information
codereader committed Aug 19, 2022
1 parent f8a1de2 commit c080c53
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 108 deletions.
52 changes: 52 additions & 0 deletions libs/ModelExportOptions.h
@@ -0,0 +1,52 @@
#pragma once

#include <stdexcept>
#include <string>
#include "math/Vector3.h"

namespace model
{

enum class ModelExportOrigin
{
MapOrigin,
SelectionCenter,
EntityOrigin,
CustomOrigin,
};

struct ModelExportOptions
{
std::string outputFilename; // full export path
std::string outputFormat; // model exporter extension
bool skipCaulk; // whether to skip caulk
ModelExportOrigin exportOrigin; // where to center objects
bool replaceSelectionWithModel; // delete the selection and put the exported model in its place
std::string entityName; // when EntityOrigin is chosen, this defines the entity to pick
bool exportLightsAsObjects; // will export lights as small octahedrons
Vector3 customExportOrigin; // used when exportOrigin == ModelExportOrigin::CustomOrigin
};

inline std::string getExportOriginString(ModelExportOrigin option)
{
switch (option)
{
case ModelExportOrigin::MapOrigin: return "MapOrigin";
case ModelExportOrigin::SelectionCenter: return "SelectionCenter";
case ModelExportOrigin::EntityOrigin: return "EntityOrigin";
case ModelExportOrigin::CustomOrigin: return "CustomOrigin";
default: throw std::invalid_argument("Unknown model export option");
}
}

inline ModelExportOrigin getExportOriginFromString(const std::string& optionString)
{
if (optionString == "MapOrigin") return ModelExportOrigin::MapOrigin;
if (optionString == "SelectionCenter") return ModelExportOrigin::SelectionCenter;
if (optionString == "EntityOrigin") return ModelExportOrigin::EntityOrigin;
if (optionString == "CustomOrigin") return ModelExportOrigin::CustomOrigin;

return ModelExportOrigin::MapOrigin;
}

}
59 changes: 40 additions & 19 deletions radiant/ui/modelexport/ExportAsModelDialog.cpp
Expand Up @@ -15,6 +15,7 @@
#include <wx/choice.h>
#include <wx/radiobut.h>
#include <wx/sizer.h>
#include <wx/textctrl.h>
#include "string/case_conv.h"
#include "string/split.h"

Expand Down Expand Up @@ -157,7 +158,7 @@ void ExportAsModelDialog::populateWindow()
entitySelector->Select(0);
}

auto exportOrigin = static_cast<ExportOrigin>(registry::getValue<int>(RKEY_MODEL_EXPORT_EXPORT_ORIGIN));
auto exportOrigin = static_cast<model::ModelExportOrigin>(registry::getValue<int>(RKEY_MODEL_EXPORT_EXPORT_ORIGIN));
setSelectedExportOrigin(exportOrigin);

if (entitySelector->GetCount() > 0)
Expand All @@ -171,9 +172,9 @@ void ExportAsModelDialog::populateWindow()
findNamedObject<wxChoice>(this, "ExportDialogEntitySelector")->Enable(false);

// Switch to another option
if (getSelectedExportOrigin() == ExportOrigin::EntityOrigin)
if (getSelectedExportOrigin() == model::ModelExportOrigin::EntityOrigin)
{
setSelectedExportOrigin(ExportOrigin::SelectionCenter);
setSelectedExportOrigin(model::ModelExportOrigin::SelectionCenter);
}
}

Expand All @@ -193,6 +194,25 @@ void ExportAsModelDialog::onExport(wxCommandEvent& ev)
std::string outputFormat = wxutil::ChoiceHelper::GetSelectedStoredString(findNamedObject<wxChoice>(this, "ExportDialogFormatChoice"));
bool replaceSelectionWithModel = findNamedObject<wxCheckBox>(this, "ExportDialogReplaceWithModel")->GetValue();
bool exportLightsAsObjects = findNamedObject<wxCheckBox>(this, "ExportDialogExportLightsAsObjects")->GetValue();
auto customOrigin = findNamedObject<wxTextCtrl>(this, "ExportDialogCustomOrigin")->GetValue().ToStdString();
auto entityName = std::string();

// Validate the user-entered origin
if (exportOrigin == model::ModelExportOrigin::CustomOrigin)
{
auto invalidOrigin = Vector3(65536, 65536, 65536);
if (string::convert<Vector3>(customOrigin, invalidOrigin) == invalidOrigin)
{
wxutil::Messagebox::Show(_("Invalid Origin"),
_("The origin you entered could not be parsed.\nUse the format \"x y z\" (without quotes, separated with spaces)"),
IDialog::MessageType::MESSAGE_ERROR);
return;
}
}
else if (exportOrigin == model::ModelExportOrigin::EntityOrigin)
{
entityName = wxutil::ChoiceHelper::GetSelectedStoredString(findNamedObject<wxChoice>(this, "ExportDialogEntitySelector"));
}

if (outputFilename.empty())
{
Expand Down Expand Up @@ -221,15 +241,16 @@ void ExportAsModelDialog::onExport(wxCommandEvent& ev)

try
{
// ExportSelectedAsModel <Path> <ExportFormat> [<CenterObjects>] [<SkipCaulk>] [<ReplaceSelectionWithModel>] [<UseEntityOrigin>] [<ExportLightsAsObjects>]
// ExportSelectedAsModel <Path> <ExportFormat> [<ExportOrigin>] [<OriginEntityName>] [<CustomOrigin>] [<SkipCaulk>] [<ReplaceSelectionWithModel>] [<ExportLightsAsObjects>]
cmd::ArgumentList argList;

argList.push_back(outputFilename);
argList.push_back(outputFormat);
argList.push_back(exportOrigin != ExportOrigin::MapOrigin);
argList.push_back(model::getExportOriginString(exportOrigin));
argList.push_back(entityName);
argList.push_back(customOrigin);
argList.push_back(skipCaulk);
argList.push_back(replaceSelectionWithModel);
argList.push_back(exportOrigin == ExportOrigin::EntityOrigin);
argList.push_back(exportLightsAsObjects);

GlobalCommandSystem().executeCommand("ExportSelectedAsModel", argList);
Expand Down Expand Up @@ -320,45 +341,45 @@ void ExportAsModelDialog::saveOptionsToRegistry()
findNamedObject<wxCheckBox>(this, "ExportDialogExportLightsAsObjects")->GetValue());
}

ExportAsModelDialog::ExportOrigin ExportAsModelDialog::getSelectedExportOrigin()
model::ModelExportOrigin ExportAsModelDialog::getSelectedExportOrigin()
{
if (findNamedObject<wxRadioButton>(this, "ExportOriginUseMapOrigin")->GetValue())
{
return ExportOrigin::MapOrigin;
return model::ModelExportOrigin::MapOrigin;
}
else if (findNamedObject<wxRadioButton>(this, "ExportOriginUseSelectionCenter")->GetValue())
{
return ExportOrigin::SelectionCenter;
return model::ModelExportOrigin::SelectionCenter;
}
else if (findNamedObject<wxRadioButton>(this, "ExportOriginUseEntityOrigin")->GetValue())
{
return ExportOrigin::EntityOrigin;
return model::ModelExportOrigin::EntityOrigin;
}
else if (findNamedObject<wxRadioButton>(this, "ExportOriginUseCustomOrigin")->GetValue())
{
return ExportOrigin::CustomOrigin;
return model::ModelExportOrigin::CustomOrigin;
}

return ExportOrigin::MapOrigin;
return model::ModelExportOrigin::MapOrigin;
}

void ExportAsModelDialog::setSelectedExportOrigin(ExportOrigin exportOrigin)
void ExportAsModelDialog::setSelectedExportOrigin(model::ModelExportOrigin exportOrigin)
{
findNamedObject<wxRadioButton>(this, "ExportOriginUseMapOrigin")->SetValue(exportOrigin == ExportOrigin::MapOrigin);
findNamedObject<wxRadioButton>(this, "ExportOriginUseSelectionCenter")->SetValue(exportOrigin == ExportOrigin::SelectionCenter);
findNamedObject<wxRadioButton>(this, "ExportOriginUseEntityOrigin")->SetValue(exportOrigin == ExportOrigin::EntityOrigin);
findNamedObject<wxRadioButton>(this, "ExportOriginUseCustomOrigin")->SetValue(exportOrigin == ExportOrigin::CustomOrigin);
findNamedObject<wxRadioButton>(this, "ExportOriginUseMapOrigin")->SetValue(exportOrigin == model::ModelExportOrigin::MapOrigin);
findNamedObject<wxRadioButton>(this, "ExportOriginUseSelectionCenter")->SetValue(exportOrigin == model::ModelExportOrigin::SelectionCenter);
findNamedObject<wxRadioButton>(this, "ExportOriginUseEntityOrigin")->SetValue(exportOrigin == model::ModelExportOrigin::EntityOrigin);
findNamedObject<wxRadioButton>(this, "ExportOriginUseCustomOrigin")->SetValue(exportOrigin == model::ModelExportOrigin::CustomOrigin);
}

void ExportAsModelDialog::ShowDialog(const cmd::ArgumentList& args)
void ExportAsModelDialog::ShowDialog(const cmd::ArgumentList&)
{
if (GlobalSelectionSystem().countSelected() == 0)
{
wxutil::Messagebox::Show(_("Empty Selection"), _("Nothing selected, cannot run exporter"), IDialog::MessageType::MESSAGE_ERROR);
return;
}

ExportAsModelDialog* instance = new ExportAsModelDialog;
auto instance = new ExportAsModelDialog;

instance->ShowModal();
instance->Destroy();
Expand Down
17 changes: 5 additions & 12 deletions radiant/ui/modelexport/ExportAsModelDialog.h
Expand Up @@ -2,6 +2,7 @@

#include "icommandsystem.h"

#include "ModelExportOptions.h"
#include "wxutil/dialog/DialogBase.h"
#include "wxutil/XmlResourceBasedWidget.h"

Expand All @@ -12,17 +13,7 @@ class ExportAsModelDialog :
public wxutil::DialogBase,
private wxutil::XmlResourceBasedWidget
{
private:
enum class ExportOrigin
{
MapOrigin,
SelectionCenter,
EntityOrigin,
CustomOrigin,
};

public:
// Constructor
ExportAsModelDialog(wxWindow* parent = nullptr);

static void ShowDialog(const cmd::ArgumentList& args);
Expand All @@ -39,8 +30,10 @@ class ExportAsModelDialog :

void saveOptionsToRegistry();
void handleFormatSelectionChange();
ExportOrigin getSelectedExportOrigin();
void setSelectedExportOrigin(ExportOrigin exportOrigin);

Vector3 getExportOrigin();
model::ModelExportOrigin getSelectedExportOrigin();
void setSelectedExportOrigin(model::ModelExportOrigin exportOrigin);
};

}
18 changes: 10 additions & 8 deletions radiantcore/map/Map.cpp
Expand Up @@ -1000,14 +1000,16 @@ void Map::registerCommands()
GlobalCommandSystem().addCommand("SaveSelected", Map::exportSelection);
GlobalCommandSystem().addCommand("FocusViews", std::bind(&Map::focusViewCmd, this, std::placeholders::_1), { cmd::ARGTYPE_VECTOR3, cmd::ARGTYPE_VECTOR3 });
GlobalCommandSystem().addCommand("FocusCameraOnSelection", std::bind(&Map::focusCameraOnSelectionCmd, this, std::placeholders::_1));
GlobalCommandSystem().addCommand("ExportSelectedAsModel", map::algorithm::exportSelectedAsModelCmd,
{ cmd::ARGTYPE_STRING,
cmd::ARGTYPE_STRING,
cmd::ARGTYPE_INT | cmd::ARGTYPE_OPTIONAL,
cmd::ARGTYPE_INT | cmd::ARGTYPE_OPTIONAL,
cmd::ARGTYPE_INT | cmd::ARGTYPE_OPTIONAL,
cmd::ARGTYPE_INT | cmd::ARGTYPE_OPTIONAL,
cmd::ARGTYPE_INT | cmd::ARGTYPE_OPTIONAL });
// ExportSelectedAsModel <Path> <ExportFormat> [<ExportOrigin>] [<OriginEntityName>] [<CustomOrigin>] [<SkipCaulk>] [<ReplaceSelectionWithModel>] [<ExportLightsAsObjects>]
GlobalCommandSystem().addCommand("ExportSelectedAsModel", algorithm::exportSelectedAsModelCmd,
{ cmd::ARGTYPE_STRING, // path
cmd::ARGTYPE_STRING, // export format
cmd::ARGTYPE_STRING | cmd::ARGTYPE_OPTIONAL, // export origin type
cmd::ARGTYPE_STRING | cmd::ARGTYPE_OPTIONAL, // origin entity name
cmd::ARGTYPE_VECTOR3 | cmd::ARGTYPE_OPTIONAL, // custom origin
cmd::ARGTYPE_INT | cmd::ARGTYPE_OPTIONAL, // skip caulk
cmd::ARGTYPE_INT | cmd::ARGTYPE_OPTIONAL, // replace selection with model
cmd::ARGTYPE_INT | cmd::ARGTYPE_OPTIONAL }); // export lights as objects

// Add undo commands
GlobalCommandSystem().addCommand("Undo", std::bind(&Map::undoCmd, this, std::placeholders::_1));
Expand Down

0 comments on commit c080c53

Please sign in to comment.