Skip to content

Commit

Permalink
#5537: Migrate ParticleChooserDialog to inherit from DeclarationSelec…
Browse files Browse the repository at this point in the history
…torDialog
  • Loading branch information
codereader committed Sep 23, 2022
1 parent b682189 commit b9aa324
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 29 deletions.
1 change: 1 addition & 0 deletions install/user.xml
Expand Up @@ -341,6 +341,7 @@
this window will take if no size information has been saved so far -->
<EntityClassChooser defaultWidthFraction="0.7" defaultHeightFraction="0.8" />
<SkinChooser defaultWidthFraction="0.7" defaultHeightFraction="0.8" />
<ParticleChooser defaultWidthFraction="0.7" defaultHeightFraction="0.8" />
</windowStates>
</ui>
</user>
15 changes: 14 additions & 1 deletion libs/wxutil/decl/DeclarationSelectorDialog.cpp
Expand Up @@ -25,8 +25,11 @@ DeclarationSelectorDialog::DeclarationSelectorDialog(decl::Type declType,
GetSizer()->Add(_mainSizer, 1, wxEXPAND | wxALL, 12);

// Button row
_bottomRowSizer = new wxBoxSizer(wxHORIZONTAL);
_buttonSizer = CreateStdDialogButtonSizer(wxOK | wxCANCEL);
_mainSizer->Add(_buttonSizer, 0, wxALIGN_RIGHT, 12);
_bottomRowSizer->Add(_buttonSizer, 1, wxALIGN_CENTER_VERTICAL | wxLEFT, 12);

_mainSizer->Add(_bottomRowSizer, 0, wxEXPAND, 12);

// Save the state of this dialog on close
RegisterPersistableObject(this);
Expand All @@ -52,6 +55,16 @@ void DeclarationSelectorDialog::SetSelector(DeclarationSelector* selector)
RegisterPersistableObject(_selector);
}

void DeclarationSelectorDialog::AddItemToBottomRow(wxWindow* widget)
{
_bottomRowSizer->Prepend(widget, 0, wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL);
}

void DeclarationSelectorDialog::AddItemToBottomRow(wxSizer* sizer)
{
_bottomRowSizer->Prepend(sizer, 0, wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL);
}

std::string DeclarationSelectorDialog::GetSelectedDeclName()
{
return _selector->GetSelectedDeclName();
Expand Down
6 changes: 6 additions & 0 deletions libs/wxutil/decl/DeclarationSelectorDialog.h
Expand Up @@ -29,6 +29,7 @@ class DeclarationSelectorDialog :

DeclarationSelector* _selector;
wxSizer* _mainSizer;
wxSizer* _bottomRowSizer;
wxStdDialogButtonSizer* _buttonSizer;

bool _restoreSelectionFromRegistry;
Expand All @@ -51,6 +52,11 @@ class DeclarationSelectorDialog :
protected:
void SetSelector(DeclarationSelector* selector);

// Adds a widget to the bottom row, to the left of the standard buttons
void AddItemToBottomRow(wxWindow* widget);
// Adds a widget to the bottom row, to the left of the standard buttons
void AddItemToBottomRow(wxSizer* sizer);

wxButton* GetAffirmativeButton();

private:
Expand Down
28 changes: 5 additions & 23 deletions radiant/ui/particles/ParticleChooserDialog.cpp
Expand Up @@ -2,9 +2,6 @@

#include "i18n.h"

#include "wxutil/dataview/ThreadedResourceTreePopulator.h"
#include "wxutil/dataview/TreeViewItemStyle.h"

#include <wx/sizer.h>
#include <wx/stattext.h>
#include <wx/radiobut.h>
Expand All @@ -13,16 +10,12 @@ namespace ui
{

ParticleChooserDialog::ParticleChooserDialog(bool showClassnameSelector) :
DialogBase(_("Choose Particle")),
DeclarationSelectorDialog(decl::Type::Particle, _("Choose Particle"), "ParticleChooser"),
_selector(new ParticleSelector(this)),
_funcEmitter(nullptr),
_funcSmoke(nullptr)
{
SetSizer(new wxBoxSizer(wxVERTICAL));

GetSizer()->Add(_selector, 1, wxEXPAND | wxALL, 12);

auto bottomHbox = new wxBoxSizer(wxHORIZONTAL);
SetSelector(_selector);

if (showClassnameSelector)
{
Expand All @@ -39,16 +32,8 @@ ParticleChooserDialog::ParticleChooserDialog(bool showClassnameSelector) :
radioHbox->Add(_funcEmitter, 0, wxALIGN_CENTER_VERTICAL | wxLEFT, 12);
radioHbox->Add(_funcSmoke, 0, wxALIGN_CENTER_VERTICAL | wxLEFT, 6);

bottomHbox->Add(radioHbox, 0, wxLEFT, 12);
AddItemToBottomRow(radioHbox);
}

bottomHbox->Add(CreateStdDialogButtonSizer(wxOK | wxCANCEL), 1, wxALIGN_CENTER_VERTICAL);

GetSizer()->Add(bottomHbox, 0, wxEXPAND | wxBOTTOM | wxLEFT | wxRIGHT, 12);

FitToScreen(0.5f, 0.6f);

_selector->Bind(wxEVT_DATAVIEW_ITEM_ACTIVATED, &ParticleChooserDialog::_onItemActivated, this);
}

std::string ParticleChooserDialog::getSelectedClassname()
Expand All @@ -58,11 +43,6 @@ std::string ParticleChooserDialog::getSelectedClassname()
return _funcEmitter->GetValue() ? "func_emitter" : "func_smoke";
}

void ParticleChooserDialog::_onItemActivated(wxDataViewEvent& ev)
{
EndModal(wxID_OK);
}

std::string ParticleChooserDialog::ChooseParticle(const std::string& currentParticle)
{
return RunDialog(false, currentParticle).selectedParticle;
Expand All @@ -77,6 +57,7 @@ ParticleChooserDialog::SelectionResult ParticleChooserDialog::RunDialog(bool sho
{
auto* dialog = new ParticleChooserDialog(showClassnameSelector);

// Prefer SetSelectedParticle to handle the .prt extension
dialog->_selector->SetSelectedParticle(currentParticle);

auto result = dialog->ShowModal();
Expand All @@ -85,6 +66,7 @@ ParticleChooserDialog::SelectionResult ParticleChooserDialog::RunDialog(bool sho

if (result == wxID_OK)
{
// GetSelectedParticle will return the name including the .prt ending
selectionResult.selectedParticle = result == wxID_OK ? dialog->_selector->GetSelectedParticle() : "";
selectionResult.selectedClassname = showClassnameSelector ? dialog->getSelectedClassname() : "";
}
Expand Down
8 changes: 3 additions & 5 deletions radiant/ui/particles/ParticleChooserDialog.h
@@ -1,6 +1,6 @@
#pragma once

#include "wxutil/dialog/DialogBase.h"
#include "wxutil/decl/DeclarationSelectorDialog.h"

#include "ParticleSelector.h"

Expand All @@ -16,8 +16,8 @@ namespace ui
* \brief
* Chooser dialog for selection and preview of particle systems.
*/
class ParticleChooserDialog :
public wxutil::DialogBase,
class ParticleChooserDialog :
public wxutil::DeclarationSelectorDialog,
public sigc::trackable
{
private:
Expand All @@ -30,8 +30,6 @@ class ParticleChooserDialog :
// Constructor creates elements
ParticleChooserDialog(bool showClassnameSelector);

void _onItemActivated( wxDataViewEvent& ev );

std::string getSelectedClassname();

public:
Expand Down
3 changes: 3 additions & 0 deletions radiant/ui/particles/ParticleSelector.h
Expand Up @@ -25,7 +25,10 @@ class ParticleSelector :
public:
ParticleSelector(wxWindow* parent);

// Returns the selected particle name, including the .prt extension
std::string GetSelectedParticle();

// Set the selected particle (particleName includes the .prt extension)
void SetSelectedParticle(const std::string& particleName);

// Populate the list of particles
Expand Down

0 comments on commit b9aa324

Please sign in to comment.