Skip to content

Commit

Permalink
#5822: EntityClassChooser supports three scenarios it's used for, pro…
Browse files Browse the repository at this point in the history
…viding distinct titles and button labels for each.
  • Loading branch information
codereader committed Mar 1, 2022
1 parent ff15533 commit 475bbef
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 30 deletions.
46 changes: 37 additions & 9 deletions libs/wxutil/EntityClassChooser.cpp
@@ -1,5 +1,6 @@
#include "EntityClassChooser.h"

#include <stdexcept>
#include "dataview/TreeModel.h"
#include "dataview/TreeViewItemStyle.h"
#include "dataview/ThreadedResourceTreePopulator.h"
Expand All @@ -26,7 +27,9 @@ namespace wxutil

namespace
{
const char* const ECLASS_CHOOSER_TITLE = N_("Create entity");
const char* const TITLE_ADD_ENTITY = N_("Create Entity");
const char* const TITLE_CONVERT_TO_ENTITY = N_("Convert to Entity");
const char* const TITLE_SELECT_ENTITY = N_("Select Entity Class");
const char* const RKEY_SPLIT_POS = "user/ui/entityClassChooser/splitPos";
const char* const RKEY_WINDOW_STATE = "user/ui/entityClassChooser/window";
const char* const RKEY_LAST_SELECTED_ECLASS = "user/ui/entityClassChooser/lastSelectedEclass";
Expand All @@ -36,6 +39,18 @@ namespace

// Registry XPath to lookup key that specifies the display folder
const char* const FOLDER_KEY_PATH = "/entityChooser/displayFolderKey";

std::string getDialogTitle(EntityClassChooser::Purpose purpose)
{
switch (purpose)
{
case EntityClassChooser::Purpose::AddEntity: return _(TITLE_ADD_ENTITY);
case EntityClassChooser::Purpose::ConvertEntity: return _(TITLE_CONVERT_TO_ENTITY);
case EntityClassChooser::Purpose::SelectClassname: return _(TITLE_SELECT_ENTITY);
default:
throw std::logic_error("Unknown entity class chooser purpose");
}
}
}

/*
Expand Down Expand Up @@ -146,18 +161,31 @@ class ThreadedEntityClassLoader final :
};

// Main constructor
EntityClassChooser::EntityClassChooser() :
DialogBase(_(ECLASS_CHOOSER_TITLE)),
EntityClassChooser::EntityClassChooser(Purpose purpose) :
DialogBase(getDialogTitle(purpose)),
_treeView(nullptr),
_selectedName("")
{
loadNamedPanel(this, "EntityClassChooserMainPanel");

// Connect button signals
findNamedObject<wxButton>(this, "EntityClassChooserAddButton")->Bind(
wxEVT_BUTTON, &EntityClassChooser::onOK, this);
findNamedObject<wxButton>(this, "EntityClassChooserAddButton")->SetBitmap(
wxArtProvider::GetBitmap(wxART_PLUS));
auto confirmButton = findNamedObject<wxButton>(this, "EntityClassChooserAddButton");
confirmButton->Bind(wxEVT_BUTTON, &EntityClassChooser::onOK, this);

switch (purpose)
{
case EntityClassChooser::Purpose::AddEntity:
confirmButton->SetBitmap(wxArtProvider::GetBitmap(wxART_PLUS));
break;
case EntityClassChooser::Purpose::ConvertEntity:
confirmButton->SetLabelText(_("Convert"));
break;
case EntityClassChooser::Purpose::SelectClassname:
confirmButton->SetLabelText(_("Select"));
break;
default:
throw std::logic_error("Unknown entity class chooser purpose");
}

findNamedObject<wxButton>(this, "EntityClassChooserCancelButton")->Bind(
wxEVT_BUTTON, &EntityClassChooser::onCancel, this);
Expand Down Expand Up @@ -208,9 +236,9 @@ EntityClassChooser::~EntityClassChooser()
}

// Display the singleton instance
std::string EntityClassChooser::chooseEntityClass(const std::string& eclassToSelect)
std::string EntityClassChooser::ChooseEntityClass(Purpose purpose, const std::string& eclassToSelect)
{
EntityClassChooser instance;
EntityClassChooser instance{ purpose };

// Fall back to the value we saved in the registry if we didn't get any other instructions
auto preselectEclass = !eclassToSelect.empty() ? eclassToSelect :
Expand Down
17 changes: 15 additions & 2 deletions libs/wxutil/EntityClassChooser.h
Expand Up @@ -28,6 +28,16 @@ class EntityClassChooser final :
public DialogBase,
private XmlResourceBasedWidget
{
public:
// Enumeration of possible modes of this dialog
// Influences the dialog title and button labels
enum class Purpose
{
AddEntity,
ConvertEntity,
SelectClassname,
};

private:
ResourceTreeView::Columns _columns;
ResourceTreeView* _treeView;
Expand All @@ -45,7 +55,7 @@ class EntityClassChooser final :
sigc::connection _defsReloaded;

private:
EntityClassChooser();
EntityClassChooser(Purpose purpose);
~EntityClassChooser();

void loadEntityClasses();
Expand Down Expand Up @@ -81,12 +91,15 @@ class EntityClassChooser final :
/**
* \brief Construct and show the dialog to choose an entity class,
* returning the result.
*
* \param purpose
* The scenario this dialog has been requested for
*
* \param preselectEclass
* Optional initial class to locate and highlight in the tree after the
* dialog is shown.
*/
static std::string chooseEntityClass(const std::string& preselectEclass = {});
static std::string ChooseEntityClass(Purpose purpose, const std::string& preselectEclass = {});
};

} // namespace
7 changes: 5 additions & 2 deletions plugins/dm.difficulty/DifficultyEditor.cpp
Expand Up @@ -98,11 +98,14 @@ void DifficultyEditor::populateWindow()
void DifficultyEditor::chooseEntityClass()
{
// Show dialog and set the entry box text with the chosen entity class
std::string chosenEntity = wxutil::EntityClassChooser::chooseEntityClass(
_classEntry->GetValue().ToStdString()
auto chosenEntity = wxutil::EntityClassChooser::ChooseEntityClass(
wxutil::EntityClassChooser::Purpose::SelectClassname, _classEntry->GetValue().ToStdString()
);

if (!chosenEntity.empty())
{
_classEntry->SetValue(chosenEntity);
}
}

int DifficultyEditor::getSelectedSettingId()
Expand Down
5 changes: 3 additions & 2 deletions radiant/ui/einspector/ClassnamePropertyEditor.cpp
Expand Up @@ -23,7 +23,7 @@ ClassnamePropertyEditor::ClassnamePropertyEditor(wxWindow* parent, IEntitySelect
: PropertyEditor(entities),
_key(name)
{
constructBrowseButtonPanel(parent, _("Choose entity class..."),
constructBrowseButtonPanel(parent, _("Choose Entity Class..."),
PropertyEditorFactory::getBitmapFor("classname"));
}

Expand All @@ -32,7 +32,8 @@ void ClassnamePropertyEditor::onBrowseButtonClick()
std::string currentEclass = _entities.getSharedKeyValue(_key, false);

// Use the EntityClassChooser dialog to get a selection from the user
std::string selection = wxutil::EntityClassChooser::chooseEntityClass(currentEclass);
std::string selection = wxutil::EntityClassChooser::ChooseEntityClass(
wxutil::EntityClassChooser::Purpose::SelectClassname, currentEclass);

// Only apply if the classname has actually changed
if (!selection.empty() && selection != currentEclass)
Expand Down
3 changes: 2 additions & 1 deletion radiant/ui/materials/editor/MaterialEditor.cpp
Expand Up @@ -309,7 +309,8 @@ void MaterialEditor::setupPreviewLightProperties(wxWindow* previewPanel)
getControl<wxButton>("MaterialPreviewLightChooseClassnameButton")->Bind(wxEVT_BUTTON, [this](wxCommandEvent& ev)
{
auto textCtrl = getControl<wxTextCtrl>("MaterialPreviewLightClassname");
auto newClassName = wxutil::EntityClassChooser::chooseEntityClass(textCtrl->GetValue().ToStdString());
auto newClassName = wxutil::EntityClassChooser::ChooseEntityClass(
wxutil::EntityClassChooser::Purpose::SelectClassname, textCtrl->GetValue().ToStdString());
textCtrl->SetValue(newClassName);
});

Expand Down
31 changes: 17 additions & 14 deletions radiant/ui/ortho/OrthoContextMenu.cpp
Expand Up @@ -249,22 +249,25 @@ std::string OrthoContextMenu::getRegistryKeyWithDefault(
void OrthoContextMenu::addEntity()
{
// Display the chooser to select an entity classname
std::string cName = wxutil::EntityClassChooser::chooseEntityClass();
auto purpose = _selectionInfo.anythingSelected ?
wxutil::EntityClassChooser::Purpose::ConvertEntity :
wxutil::EntityClassChooser::Purpose::AddEntity;

if (!cName.empty())
{
UndoableCommand command("createEntity");
auto className = wxutil::EntityClassChooser::ChooseEntityClass(purpose);

// Create the entity. We might get an EntityCreationException if the
// wrong number of brushes is selected.
try
{
GlobalEntityModule().createEntityFromSelection(cName, _lastPoint);
}
catch (cmd::ExecutionFailure& e)
{
wxutil::Messagebox::ShowError(e.what());
}
if (className.empty()) return;

UndoableCommand command(_selectionInfo.anythingSelected ? "convertToEntity" : "createEntity");

// Create the entity. We might get an EntityCreationException if the
// wrong number of brushes is selected.
try
{
GlobalEntityModule().createEntityFromSelection(className, _lastPoint);
}
catch (cmd::ExecutionFailure& e)
{
wxutil::Messagebox::ShowError(e.what());
}
}

Expand Down

0 comments on commit 475bbef

Please sign in to comment.