Skip to content

Commit

Permalink
#5537: Refactor SkinChooser to inherit from DeclarationSelectorDialog.
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Sep 18, 2022
1 parent 9c6c7de commit 8b7c4c0
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 57 deletions.
45 changes: 9 additions & 36 deletions radiant/ui/common/SkinChooser.cpp
Expand Up @@ -3,8 +3,6 @@
#include "i18n.h"
#include "modelskin.h"

#include <wx/sizer.h>
#include <wx/splitter.h>
#include "fmt/format.h"

#include "ifavourites.h"
Expand All @@ -21,7 +19,6 @@ namespace ui
namespace
{
constexpr const char* SKIN_ICON = "skin16.png";

constexpr const char* const WINDOW_TITLE = N_("Choose Skin");

/**
Expand Down Expand Up @@ -163,6 +160,8 @@ class SkinSelector :
_materialsList->signal_visibilityChanged().connect(
sigc::mem_fun(*_preview, &wxutil::ModelPreview::queueDraw)
);

Populate();
}

void Populate() override
Expand Down Expand Up @@ -250,58 +249,32 @@ class SkinSelector :
};

SkinChooser::SkinChooser(const std::string& model) :
DialogBase(_(WINDOW_TITLE), "SkinChooser"),
_model(model),
_selector(nullptr)
DeclarationSelectorDialog(decl::Type::Skin, _(WINDOW_TITLE), "SkinChooser"),
_model(model)
{
SetSizer(new wxBoxSizer(wxVERTICAL));

wxBoxSizer* vbox = new wxBoxSizer(wxVERTICAL);
GetSizer()->Add(vbox, 1, wxEXPAND | wxALL, 12);

_selector = new SkinSelector(this, _model);

// Double-clicks on a skin closes the dialog
_selector->signal_skinActivated().connect([this]() { EndModal(wxID_OK); });

vbox->Add(_selector, 1, wxEXPAND);
vbox->Add(CreateStdDialogButtonSizer(wxOK | wxCANCEL), 0, wxALIGN_RIGHT | wxTOP, 12);

RegisterPersistableObject(_selector);
SetSelector(new SkinSelector(this, _model));
}

int SkinChooser::ShowModal()
{
_selector->Populate();

// Display the model in the window title
SetTitle(fmt::format("{0}: {1}", _(WINDOW_TITLE), _model));

return DialogBase::ShowModal();
}

std::string SkinChooser::getSelectedSkin()
{
return _selector->GetSelectedDeclName();
}

void SkinChooser::setSelectedSkin(const std::string& skin)
{
_selector->SetSelectedDeclName(skin);
return DeclarationSelectorDialog::ShowModal();
}

std::string SkinChooser::chooseSkin(const std::string& model, const std::string& prev)
std::string SkinChooser::ChooseSkin(const std::string& model, const std::string& prev)
{
auto dialog = new SkinChooser(model);

// We return the previous skin, unless the dialog returns OK
auto selectedSkin = prev;

dialog->setSelectedSkin(selectedSkin);
dialog->SetSelectedDeclName(selectedSkin);

if (dialog->ShowModal() == wxID_OK)
{
selectedSkin = dialog->getSelectedSkin();
selectedSkin = dialog->GetSelectedDeclName();
}

dialog->Destroy();
Expand Down
21 changes: 3 additions & 18 deletions radiant/ui/common/SkinChooser.h
@@ -1,39 +1,24 @@
#pragma once

#include <sigc++/connection.h>
#include "imodel.h"

#include "wxutil/dialog/DialogBase.h"
#include <string>

#include "wxutil/dataview/ResourceTreeView.h"
#include "wxutil/decl/DeclarationSelectorDialog.h"

namespace ui
{

class SkinSelector;

/** Dialog to allow selection of skins for a model. Skins are grouped
* into two toplevel categories - matching skins which are associated with the
* model, and all skins available.
*/
class SkinChooser :
public wxutil::DialogBase
public wxutil::DeclarationSelectorDialog
{
private:
// The model name to use for skin matching
std::string _model;

SkinSelector* _selector;

private:
// Constructor creates widgets
SkinChooser(const std::string& model);

// Retrieve the currently selected skin
std::string getSelectedSkin();
void setSelectedSkin(const std::string& skin);

public:
int ShowModal() override;

Expand All @@ -48,7 +33,7 @@ class SkinChooser :
* The current skin set on the model, so that the original can be returned
* if the dialog is cancelled.
*/
static std::string chooseSkin(const std::string& model,
static std::string ChooseSkin(const std::string& model,
const std::string& prevSkin);
};

Expand Down
2 changes: 1 addition & 1 deletion radiant/ui/einspector/ModelPropertyEditor.cpp
Expand Up @@ -132,7 +132,7 @@ void ModelPropertyEditor::_onSkinButton(wxCommandEvent& ev)
skinKey->setAffectedKey("skin");

std::string prevSkin = _entities.getSharedKeyValue(skinKey->getFullKey(), true);
std::string skin = SkinChooser::chooseSkin(model, prevSkin);
std::string skin = SkinChooser::ChooseSkin(model, prevSkin);

if (skin != prevSkin)
{
Expand Down
4 changes: 2 additions & 2 deletions radiant/ui/einspector/SkinPropertyEditor.cpp
Expand Up @@ -39,7 +39,7 @@ void SkinPropertyEditor::onBrowseButtonClick()

// Display the SkinChooser to get a skin from the user
std::string prevSkin = _entities.getSharedKeyValue(_key->getFullKey(), true);
std::string skin = SkinChooser::chooseSkin(model, prevSkin);
std::string skin = SkinChooser::ChooseSkin(model, prevSkin);

// Apply the key to the entity
setKeyValue(_key->getFullKey(), skin);
Expand All @@ -49,7 +49,7 @@ std::string SkinChooserDialogWrapper::runDialog(Entity* entity, const std::strin
{
std::string modelName = entity->getKeyValue("model");
std::string prevSkin = entity->getKeyValue(key);
std::string skin = SkinChooser::chooseSkin(modelName, prevSkin);
std::string skin = SkinChooser::ChooseSkin(modelName, prevSkin);

// return the new value
return skin;
Expand Down

0 comments on commit 8b7c4c0

Please sign in to comment.