/
SoundShaderPreview.cpp
203 lines (157 loc) · 4.86 KB
/
SoundShaderPreview.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
#include "SoundShaderPreview.h"
#include "i18n.h"
#include "isound.h"
#include "itextstream.h"
#include "iuimanager.h"
#include <iostream>
#include <cstdlib>
#include <wx/artprov.h>
#include <wx/stattext.h>
#include <wx/button.h>
#include <wx/sizer.h>
namespace ui
{
SoundShaderPreview::SoundShaderPreview(wxWindow* parent) :
wxPanel(parent, wxID_ANY),
_listStore(new wxutil::TreeModel(_columns, true)),
_soundShader("")
{
SetSizer(new wxBoxSizer(wxHORIZONTAL));
_treeView = wxutil::TreeView::CreateWithModel(this, _listStore);
_treeView->SetMinClientSize(wxSize(-1, 130));
_treeView->AppendTextColumn(_("Sound Files"), _columns.shader.getColumnIndex(),
wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT, wxDATAVIEW_COL_SORTABLE);
// Connect the "changed" signal
_treeView->Connect(wxEVT_DATAVIEW_SELECTION_CHANGED,
wxDataViewEventHandler(SoundShaderPreview::onSelectionChanged), NULL, this);
_treeView->Connect(wxEVT_DATAVIEW_ITEM_ACTIVATED,
wxDataViewEventHandler(SoundShaderPreview::onItemActivated), NULL, this);
GetSizer()->Add(_treeView, 1, wxEXPAND);
GetSizer()->Add(createControlPanel(this), 0, wxALIGN_BOTTOM | wxLEFT, 12);
// Trigger the initial update of the widgets
update();
}
wxSizer* SoundShaderPreview::createControlPanel(wxWindow* parent)
{
wxBoxSizer* vbox = new wxBoxSizer(wxVERTICAL);
// Create the playback button
_playButton = new wxButton(parent, wxID_ANY, _("Play"));
_playButton->SetBitmap(wxArtProvider::GetBitmap(GlobalUIManager().ArtIdPrefix() + "media-playback-start-ltr.png"));
_stopButton = new wxButton(parent, wxID_ANY, _("Stop"));
_stopButton->SetBitmap(wxArtProvider::GetBitmap(GlobalUIManager().ArtIdPrefix() + "media-playback-stop.png"));
_playButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(SoundShaderPreview::onPlay), NULL, this);
_stopButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(SoundShaderPreview::onStop), NULL, this);
_playButton->SetMinSize(wxSize(80, -1));
_stopButton->SetMinSize(wxSize(80, -1));
_statusLabel = new wxStaticText(parent, wxID_ANY, "");
_statusLabel->Wrap(100);
vbox->Add(_statusLabel, 0, wxEXPAND | wxBOTTOM, 12);
vbox->Add(_playButton, 0, wxBOTTOM, 6);
vbox->Add(_stopButton, 0);
return vbox;
}
void SoundShaderPreview::setSoundShader(const std::string& soundShader)
{
_soundShader = soundShader;
update();
}
void SoundShaderPreview::playRandomSoundFile()
{
if (_soundShader.empty() || !_listStore) return;
// Select a random file from the list
wxDataViewItemArray children;
unsigned int numFiles = _listStore->GetChildren(_listStore->GetRoot(), children);
if (numFiles > 0)
{
int selected = rand() % numFiles;
_treeView->Select(children[selected]);
handleSelectionChange();
playSelectedFile();
}
}
void SoundShaderPreview::update()
{
// Clear the current treeview model
_listStore->Clear();
// If the soundshader string is empty, desensitise the widgets
Enable(!_soundShader.empty());
if (!_soundShader.empty())
{
// We have a sound shader, update the liststore
// Get the list of sound files associated to this shader
const ISoundShaderPtr& shader = GlobalSoundManager().getSoundShader(_soundShader);
if (!shader->getName().empty())
{
// Retrieve the list of associated filenames (VFS paths)
SoundFileList list = shader->getSoundFileList();
for (std::size_t i = 0; i < list.size(); ++i)
{
wxutil::TreeModel::Row row = _listStore->AddItem();
row[_columns.shader] = list[i];
row.SendItemAdded();
// Pre-select the first sound file, for the user's convenience
if (i == 0)
{
_treeView->Select(row.getItem());
}
}
handleSelectionChange();
}
else
{
// Not a valid soundshader, switch to inactive
Enable(false);
}
}
}
std::string SoundShaderPreview::getSelectedSoundFile()
{
wxDataViewItem item = _treeView->GetSelection();
if (item.IsOk())
{
wxutil::TreeModel::Row row(item, *_listStore);
return row[_columns.shader];
}
else
{
return "";
}
}
void SoundShaderPreview::onSelectionChanged(wxDataViewEvent& ev)
{
handleSelectionChange();
}
void SoundShaderPreview::onItemActivated(wxDataViewEvent& ev)
{
playSelectedFile();
}
void SoundShaderPreview::handleSelectionChange()
{
std::string selectedFile = getSelectedSoundFile();
// Set the sensitivity of the playbutton accordingly
_playButton->Enable(!selectedFile.empty());
}
void SoundShaderPreview::onPlay(wxCommandEvent& ev)
{
playSelectedFile();
}
void SoundShaderPreview::playSelectedFile()
{
_statusLabel->SetLabel("");
std::string selectedFile = getSelectedSoundFile();
if (!selectedFile.empty())
{
// Pass the call to the sound manager
if (!GlobalSoundManager().playSound(selectedFile))
{
_statusLabel->SetLabel(_("Error: File not found."));
}
}
}
void SoundShaderPreview::onStop(wxCommandEvent& ev)
{
// Pass the call to the sound manager
GlobalSoundManager().stopSound();
_statusLabel->SetLabel("");
}
} // namespace ui