/
ParticlesChooser.cpp
276 lines (208 loc) · 6.54 KB
/
ParticlesChooser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include "ParticlesChooser.h"
#include "i18n.h"
#include "imainframe.h"
#include "iparticles.h"
#include "iuimanager.h"
#include "iradiant.h"
#include "debugging/ScopedDebugTimer.h"
#include "wxutil/TreeView.h"
#include <wx/sizer.h>
#include <wx/thread.h>
#include <functional>
namespace ui
{
namespace
{
// Single column list store
struct ListColumns :
public wxutil::TreeModel::ColumnRecord
{
wxutil::TreeModel::Column name;
ListColumns() :
name(add(wxutil::TreeModel::Column::String))
{}
};
ListColumns& COLUMNS() { static ListColumns _instance; return _instance; }
}
ParticlesChooser::ParticlesChooser() :
DialogBase(_("Choose particles")),
_particlesList(new wxutil::TreeModel(COLUMNS(), true)),
_selectedParticle(""),
_preview(new wxutil::ParticlePreview(this))
{
// Connect the finish callback to load the treestore
Connect(wxutil::EV_TREEMODEL_POPULATION_FINISHED,
TreeModelPopulationFinishedHandler(ParticlesChooser::onTreeStorePopulationFinished), NULL, this);
SetSizer(new wxBoxSizer(wxVERTICAL));
wxBoxSizer* hbox = new wxBoxSizer(wxHORIZONTAL);
hbox->Add(createTreeView(this), 1, wxEXPAND);
hbox->Add(_preview->getWidget(), 0, wxEXPAND | wxLEFT, 6);
GetSizer()->Add(hbox, 1, wxEXPAND | wxALL, 12);
GetSizer()->Add(CreateStdDialogButtonSizer(wxOK | wxCANCEL), 0, wxALIGN_RIGHT | wxBOTTOM | wxLEFT | wxRIGHT, 12);
FitToScreen(0.5f, 0.6f);
}
// Create the tree view
wxWindow* ParticlesChooser::createTreeView(wxWindow* parent)
{
_treeView = wxutil::TreeView::CreateWithModel(parent, _particlesList.get());
_treeView->SetSize(300, -1);
// Single text column
_treeView->AppendTextColumn(_("Particle"), COLUMNS().name.getColumnIndex(),
wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT, wxDATAVIEW_COL_SORTABLE);
// Apply full-text search to the column
_treeView->AddSearchColumn(COLUMNS().name);
// Start loading particles into the view
populateParticleList();
// Connect up the selection changed callback
_treeView->Connect(wxEVT_DATAVIEW_SELECTION_CHANGED,
wxDataViewEventHandler(ParticlesChooser::_onSelChanged), NULL, this);
return _treeView;
}
/**
* Visitor class to retrieve particle system names and add them to a
* treemodel.
*/
class ParticlesChooser::ThreadedParticlesLoader :
public wxThread
{
private:
// List store to populate
wxutil::TreeModel::Ptr _store;
wxEvtHandler* _finishedHandler;
public:
/**
* Constructor.
*/
ThreadedParticlesLoader(wxEvtHandler* finishedHandler) :
wxThread(wxTHREAD_JOINABLE),
_store(new wxutil::TreeModel(COLUMNS(), true)),
_finishedHandler(finishedHandler)
{}
~ThreadedParticlesLoader()
{
// We might have a running thread, wait for it
if (IsRunning())
{
Delete();
}
}
void visitParticle(const particles::IParticleDef& def)
{
if (TestDestroy()) return;
// Add the ".prt" extension to the name fo display in the list
std::string prtName = def.getName() + ".prt";
// Add the Def name to the list store
wxutil::TreeModel::Row row = _store->AddItem();
row[COLUMNS().name] = prtName;
}
// The worker function that will execute in the thread
ExitCode Entry()
{
ScopedDebugTimer timer("ThreadedParticlesLoader::run()");
// Create and use a ParticlesVisitor to populate the list
GlobalParticlesManager().forEachParticleDef(
std::bind(&ThreadedParticlesLoader::visitParticle, this, std::placeholders::_1));
if (!TestDestroy())
{
wxutil::TreeModel::PopulationFinishedEvent finishedEvent;
finishedEvent.SetTreeModel(_store);
_finishedHandler->AddPendingEvent(finishedEvent);
}
return static_cast<wxThread::ExitCode>(0);
}
};
// Populate the particles list
void ParticlesChooser::populateParticleList()
{
_particlesLoader.reset(new ThreadedParticlesLoader(this));
_particlesList->Clear();
wxutil::TreeModel::Row row = _particlesList->AddItem();
row[COLUMNS().name] = _("Loading...");
row.SendItemAdded();
_particlesLoader->Run();
}
void ParticlesChooser::onTreeStorePopulationFinished(wxutil::TreeModel::PopulationFinishedEvent& ev)
{
_particlesList = ev.GetTreeModel();
// Replace the existing model with the new one
_treeView->AssociateModel(_particlesList.get());
// Trigger auto-size calculation
_particlesList->ItemChanged(_particlesList->GetRoot());
// Pre-select the given class if requested
if (!_preSelectParticle.empty())
{
setSelectedParticle(_preSelectParticle);
}
_particlesLoader.reset();
}
void ParticlesChooser::onMainFrameShuttingDown()
{
rMessage() << "ParticlesChooser shutting down." << std::endl;
_preview.reset();
// Destroy the window
SendDestroyEvent();
getInstancePtr().reset();
}
void ParticlesChooser::reloadParticles()
{
// Try to select the previously selected particle again
_preSelectParticle = _selectedParticle;
populateParticleList();
}
ParticlesChooserPtr& ParticlesChooser::getInstancePtr()
{
static ParticlesChooserPtr _instancePtr;
return _instancePtr;
}
// Static instance owner
ParticlesChooser& ParticlesChooser::getInstance()
{
ParticlesChooserPtr& 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)
{
wxDataViewItem item = _particlesList->FindString(particleName, COLUMNS().name);
if (item.IsOk())
{
_treeView->Select(item);
_treeView->EnsureVisible(item);
_preSelectParticle.clear();
return;
}
}
// Choose a particle system
std::string ParticlesChooser::ChooseParticle(const std::string& current)
{
ParticlesChooser& instance = getInstance();
instance._selectedParticle.clear();
// Try to select the particle right away (will clear _preSelectParticle if successful)
instance._preSelectParticle = current;
instance.setSelectedParticle(current);
int returnCode = instance.ShowModal();
// Don't destroy the instance, just hide it
instance.Hide();
return returnCode == wxID_OK ? instance._selectedParticle : "";
}
void ParticlesChooser::_onSelChanged(wxDataViewEvent& ev)
{
// Get the selection and store it
wxDataViewItem item = _treeView->GetSelection();
if (item.IsOk())
{
wxutil::TreeModel::Row row(item, *_particlesList);
_selectedParticle = row[COLUMNS().name];
_preview->setParticle(_selectedParticle);
}
}
} // namespace ui