/
SoundChooser.cpp
322 lines (256 loc) · 9.66 KB
/
SoundChooser.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include "SoundChooser.h"
#include "i18n.h"
#include "isound.h"
#include "ideclmanager.h"
#include "ifavourites.h"
#include "registry/registry.h"
#include "wxutil/dataview/ThreadedDeclarationTreePopulator.h"
#include "wxutil/dataview/VFSTreePopulator.h"
#include "wxutil/dataview/TreeViewItemStyle.h"
#include "wxutil/dataview/ResourceTreeViewToolbar.h"
#include "debugging/ScopedDebugTimer.h"
#include "ui/UserInterfaceModule.h"
#include <wx/sizer.h>
#include <wx/button.h>
#include <wx/stattext.h>
#include "wxutil/Bitmap.h"
#include <sigc++/functors/mem_fun.h>
namespace ui
{
namespace
{
const char* const SHADER_ICON = "icon_sound.png";
const char* const FOLDER_ICON = "folder16.png";
const char* const RKEY_WINDOW_STATE = "user/ui/soundChooser/window";
const char* const RKEY_LAST_SELECTED_SHADER = "user/ui/soundChooser/lastSelectedShader";
}
/**
* Visitor class to enumerate sound shaders and add them to the tree store.
*/
class SoundShaderPopulator :
public wxutil::VFSTreePopulator
{
private:
const wxutil::DeclarationTreeView::Columns& _columns;
wxIcon _shaderIcon;
wxIcon _folderIcon;
std::set<std::string> _favourites;
public:
// Constructor
SoundShaderPopulator(const wxutil::TreeModel::Ptr& treeStore,
const wxutil::DeclarationTreeView::Columns& columns) :
VFSTreePopulator(treeStore),
_columns(columns)
{
_shaderIcon.CopyFromBitmap(wxutil::GetLocalBitmap(SHADER_ICON));
_folderIcon.CopyFromBitmap(wxutil::GetLocalBitmap(FOLDER_ICON));
// Get the list of favourites
_favourites = GlobalFavouritesManager().getFavourites(decl::getTypeName(decl::Type::SoundShader));
}
// Invoked for each sound shader
void addShader(const ISoundShader::Ptr& shader)
{
// Construct a "path" into the sound shader tree,
// using the mod name as first folder level
// angua: if there is a displayFolder present, put it between the mod name and the shader name
std::string displayFolder = shader->getDisplayFolder();
// Some shaders contain backslashes, sort them in the tree by replacing the backslashes
std::string shaderNameForwardSlashes = shader->getDeclName();
std::replace(shaderNameForwardSlashes.begin(), shaderNameForwardSlashes.end(), '\\', '/');
std::string fullPath = !displayFolder.empty() ?
shader->getModName() + "/" + displayFolder + "/" + shaderNameForwardSlashes :
shader->getModName() + "/" + shaderNameForwardSlashes;
// Sort the shader into the tree and set the values
addPath(fullPath, [&](wxutil::TreeModel::Row& row, const std::string& path,
const std::string& leafName, bool isFolder)
{
bool isFavourite = !isFolder && _favourites.count(path) > 0;
row[_columns.iconAndName] = wxVariant(
wxDataViewIconText(leafName, isFolder ? _folderIcon : _shaderIcon));
row[_columns.leafName] = shader->getDeclName();
row[_columns.fullName] = path;
row[_columns.isFolder] = isFolder;
row[_columns.declName] = shader->getDeclName();
row[_columns.isFavourite] = isFavourite;
row[_columns.iconAndName] = wxutil::TreeViewItemStyle::Declaration(isFavourite); // assign attributes
row.SendItemAdded();
});
}
};
// Local class for loading sound shader definitions in a separate thread
class ThreadedSoundShaderLoader :
public wxutil::ThreadedDeclarationTreePopulator
{
// Column specification struct
const wxutil::DeclarationTreeView::Columns& _columns;
public:
// Construct and initialise variables
ThreadedSoundShaderLoader(const wxutil::DeclarationTreeView::Columns& columns) :
ThreadedDeclarationTreePopulator(columns),
_columns(columns)
{}
~ThreadedSoundShaderLoader()
{
EnsureStopped();
}
void PopulateModel(const wxutil::TreeModel::Ptr& model) override
{
ScopedDebugTimer timer("ThreadedSoundShaderLoader::run()");
// Populate it with the list of sound shaders by using a visitor class.
SoundShaderPopulator visitor(model, _columns);
// Visit all sound shaders and collect them for later insertion
GlobalSoundManager().forEachShader([&](const ISoundShader::Ptr& shader)
{
ThrowIfCancellationRequested();
visitor.addShader(shader);
});
}
void SortModel(const wxutil::TreeModel::Ptr& model) override
{
// angua: Ensure sound shaders are sorted before giving them to the tree view
model->SortModelFoldersFirst(_columns.iconAndName, _columns.isFolder);
}
};
// Constructor
SoundChooser::SoundChooser(wxWindow* parent) :
DialogBase(_("Choose sound"), parent),
_treeView(nullptr),
_preview(new SoundShaderPreview(this))
{
SetSizer(new wxBoxSizer(wxVERTICAL));
auto* buttonSizer = CreateStdDialogButtonSizer(wxOK | wxCANCEL);
auto* reloadButton = new wxButton(this, wxID_ANY, _("Reload Sounds"));
reloadButton->Bind(wxEVT_BUTTON, &SoundChooser::_onReloadSounds, this);
buttonSizer->Prepend(reloadButton, 0, wxRIGHT, 32);
auto* dblClickHint = new wxStaticText( this, wxID_ANY, _( "Ctrl + Double Click on treeview items for quick play" ), wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT );
auto* grid = new wxFlexGridSizer( 2 );
grid->AddGrowableCol( 1 );
grid->Add( dblClickHint, 0, wxALIGN_CENTER_VERTICAL );
grid->Add( buttonSizer, 0, wxALIGN_RIGHT );
createTreeView(this);
auto* toolbar = new wxutil::ResourceTreeViewToolbar(this, _treeView);
GetSizer()->Add(toolbar, 0, wxEXPAND | wxALIGN_LEFT | wxALL, 12);
GetSizer()->Add(_treeView, 1, wxEXPAND | wxLEFT | wxRIGHT, 12);
GetSizer()->Add(_preview, 0, wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM, 12);
GetSizer()->Add(grid, 0, wxEXPAND | wxBOTTOM | wxLEFT | wxRIGHT, 12);
_windowPosition.initialise(this, RKEY_WINDOW_STATE, 0.5f, 0.7f);
// Load the shaders
loadSoundShaders();
}
void SoundChooser::createTreeView(wxWindow* parent)
{
// Tree view with single text icon column
_treeView = new wxutil::DeclarationTreeView(parent, decl::Type::SoundShader, _columns);
_treeView->AppendIconTextColumn(_("Soundshader"), _columns.iconAndName.getColumnIndex(),
wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT, wxDATAVIEW_COL_SORTABLE);
// Use the TreeModel's full string search function
_treeView->AddSearchColumn(_columns.iconAndName);
_treeView->SetExpandTopLevelItemsAfterPopulation(true);
// Get selection and connect the changed callback
_treeView->Bind(wxEVT_DATAVIEW_SELECTION_CHANGED, &SoundChooser::_onSelectionChange, this);
_treeView->Bind(wxEVT_DATAVIEW_ITEM_ACTIVATED, &SoundChooser::_onItemActivated, this);
}
void SoundChooser::loadSoundShaders()
{
_treeView->Populate(std::make_shared<ThreadedSoundShaderLoader>(_columns));
}
const std::string& SoundChooser::getSelectedShader() const
{
return _selectedShader;
}
void SoundChooser::setSelectedShader(const std::string& shader)
{
_treeView->SetSelectedDeclName(shader);
}
void SoundChooser::handleSelectionChange()
{
_selectedShader = !_treeView->IsDirectorySelected() ?
_treeView->GetSelectedDeclName() : std::string();
// Notify the preview widget about the change
_preview->setSoundShader(_selectedShader);
}
void SoundChooser::_onSelectionChange(wxDataViewEvent& ev)
{
handleSelectionChange();
}
void SoundChooser::_onItemActivated(wxDataViewEvent& ev)
{
wxDataViewItem item = ev.GetItem();
if (item.IsOk())
{
wxutil::TreeModel::Row row(item, *_treeView->GetTreeModel());
bool isFolder = row[_columns.isFolder].getBool();
if (isFolder)
{
// In case we double-click a folder, toggle its expanded state
if (_treeView->IsExpanded(item))
{
_treeView->Collapse(item);
}
else
{
_treeView->Expand(item);
}
return;
}
if ( !wxGetKeyState( WXK_CONTROL ) ) { // simple double click closes modal, ctrl+dblclk plays sound
EndModal( wxID_OK );
return;
}
// It's a regular item, try to play it back
_preview->playRandomSoundFile();
}
}
int SoundChooser::ShowModal()
{
_windowPosition.applyPosition();
_shadersReloaded = GlobalDeclarationManager().signal_DeclsReloaded(decl::Type::SoundShader)
.connect(sigc::mem_fun(this, &SoundChooser::onShadersReloaded));
int returnCode = DialogBase::ShowModal();
if (returnCode != wxID_OK)
{
_selectedShader.clear();
}
_windowPosition.saveToPath(RKEY_WINDOW_STATE);
return returnCode;
}
void SoundChooser::_onReloadSounds(wxCommandEvent& ev)
{
_preview->setSoundShader(std::string());
// Send the command to the SoundManager
// After parsing it will fire the sounds reloaded signal => onShadersReloaded()
GlobalCommandSystem().executeCommand("ReloadSounds");
}
void SoundChooser::onShadersReloaded()
{
// This signal is fired by the SoundManager, possibly from a non-UI thread
GetUserInterfaceModule().dispatch([this]()
{
loadSoundShaders();
});
}
std::string SoundChooser::chooseResource(const std::string& shaderToPreselect)
{
auto preselected = !shaderToPreselect.empty() ? shaderToPreselect :
registry::getValue<std::string>(RKEY_LAST_SELECTED_SHADER);
if (!preselected.empty())
{
setSelectedShader(preselected);
}
std::string selectedShader;
if (ShowModal() == wxID_OK)
{
selectedShader = getSelectedShader();
if (!selectedShader.empty())
{
registry::setValue(RKEY_LAST_SELECTED_SHADER, selectedShader);
}
}
return selectedShader;
}
void SoundChooser::destroyDialog()
{
_shadersReloaded.disconnect();
Destroy();
}
} // namespace