Skip to content

Commit

Permalink
#5127: There's no real benefit in the ParticlesChooser being a static…
Browse files Browse the repository at this point in the history
… singleton, convert it to a regular instantiable dialog.
  • Loading branch information
codereader committed Jan 4, 2021
1 parent 1236c92 commit d631f9d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 52 deletions.
54 changes: 9 additions & 45 deletions radiant/ui/particles/ParticlesChooser.cpp
@@ -1,7 +1,6 @@
#include "ParticlesChooser.h"

#include "i18n.h"
#include "imainframe.h"
#include "iparticles.h"
#include "iuimanager.h"
#include "iradiant.h"
Expand Down Expand Up @@ -43,6 +42,10 @@ ParticlesChooser::ParticlesChooser() :
GetSizer()->Add(CreateStdDialogButtonSizer(wxOK | wxCANCEL), 0, wxALIGN_RIGHT | wxBOTTOM | wxLEFT | wxRIGHT, 12);

FitToScreen(0.5f, 0.6f);

GlobalParticlesManager().signal_particlesReloaded().connect(
sigc::mem_fun(this, &ParticlesChooser::reloadParticles)
);
}

// Create the tree view
Expand Down Expand Up @@ -129,48 +132,11 @@ void ParticlesChooser::populateParticleList()
_treeView->Populate(std::make_shared<ThreadedParticlesLoader>(_columns));
}

void ParticlesChooser::onMainFrameShuttingDown()
{
rMessage() << "ParticlesChooser shutting down." << std::endl;

_preview.reset();

// Destroy the window
SendDestroyEvent();
getInstancePtr().reset();
}

void ParticlesChooser::reloadParticles()
{
populateParticleList();
}

ParticlesChooser::Ptr& ParticlesChooser::getInstancePtr()
{
static Ptr _instancePtr;
return _instancePtr;
}

// Static instance owner
ParticlesChooser& ParticlesChooser::getInstance()
{
auto& instancePtr = getInstancePtr();

if (!instancePtr)
{
instancePtr.reset(new ParticlesChooser);

GlobalMainFrame().signal_MainFrameShuttingDown().connect(
sigc::mem_fun(*instancePtr, &ParticlesChooser::onMainFrameShuttingDown)
);
GlobalParticlesManager().signal_particlesReloaded().connect(
sigc::mem_fun(*instancePtr, &ParticlesChooser::reloadParticles)
);
}

return *instancePtr;
}

void ParticlesChooser::setSelectedParticle(const std::string& particleName)
{
_treeView->SetSelectedFullname(particleName);
Expand All @@ -179,17 +145,15 @@ void ParticlesChooser::setSelectedParticle(const std::string& particleName)
// Choose a particle system
std::string ParticlesChooser::ChooseParticle(const std::string& current)
{
ParticlesChooser& instance = getInstance();
auto* dialog = new ParticlesChooser();

instance._selectedParticle.clear();
instance.setSelectedParticle(current);
dialog->setSelectedParticle(current);

int returnCode = instance.ShowModal();
auto returnValue = dialog->ShowModal() == wxID_OK ? dialog->_selectedParticle : "";

// Don't destroy the instance, just hide it
instance.Hide();
dialog->Destroy();

return returnCode == wxID_OK ? instance._selectedParticle : "";
return returnValue;
}

void ParticlesChooser::_onSelChanged(wxDataViewEvent& ev)
Expand Down
10 changes: 3 additions & 7 deletions radiant/ui/particles/ParticlesChooser.h
Expand Up @@ -9,6 +9,7 @@
#include <string>
#include <map>
#include <memory>
#include <sigc++/trackable.h>

namespace ui
{
Expand All @@ -18,7 +19,8 @@ namespace ui
* Chooser dialog for selection and preview of particle systems.
*/
class ParticlesChooser :
public wxutil::DialogBase
public wxutil::DialogBase,
public sigc::trackable
{
private:
wxutil::ResourceTreeView::Columns _columns;
Expand All @@ -42,18 +44,12 @@ class ParticlesChooser :
// WIDGET CONSTRUCTION
wxutil::ResourceTreeView* createTreeView(wxWindow* parent);

// Static instance owner
using Ptr = std::shared_ptr<ParticlesChooser>;
static ParticlesChooser& getInstance();
static Ptr& getInstancePtr();

// Populate the list of particles
void populateParticleList();

void setSelectedParticle(const std::string& particleName);

private:
void onMainFrameShuttingDown();
void reloadParticles();

public:
Expand Down

0 comments on commit d631f9d

Please sign in to comment.