/
SoundChooser.cpp
158 lines (122 loc) · 4.01 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
#include "SoundChooser.h"
#include "i18n.h"
#include "isound.h"
#include "ideclmanager.h"
#include "registry/registry.h"
#include "wxutil/dataview/VFSTreePopulator.h"
#include "ui/UserInterfaceModule.h"
#include <wx/sizer.h>
#include <wx/button.h>
#include <wx/stattext.h>
#include <sigc++/functors/mem_fun.h>
namespace ui
{
namespace
{
constexpr const char* const RKEY_WINDOW_STATE = "user/ui/soundChooser/window";
constexpr const char* const RKEY_LAST_SELECTED_SHADER = "user/ui/soundChooser/lastSelectedShader";
}
// Constructor
SoundChooser::SoundChooser(wxWindow* parent) :
DialogBase(_("Choose sound"), parent),
_selector(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 );
_selector = new SoundShaderSelector(this);
_selector->Bind(wxEVT_DATAVIEW_ITEM_ACTIVATED, &SoundChooser::_onItemActivated, this);
GetSizer()->Add(_selector, 1, wxEXPAND | wxLEFT | wxRIGHT | wxTOP, 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::loadSoundShaders()
{
_selector->LoadSoundShaders();
}
std::string SoundChooser::getSelectedShader() const
{
return _selector->GetSelectedDeclName();
}
void SoundChooser::setSelectedShader(const std::string& shader)
{
_selector->SetSelectedDeclName(shader);
}
void SoundChooser::_onItemActivated(wxDataViewEvent& ev)
{
auto selectedItem = _selector->GetSelectedDeclName();;
if (selectedItem.empty())
{
ev.Skip();
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();
_windowPosition.saveToPath(RKEY_WINDOW_STATE);
_shadersReloaded.disconnect();
return returnCode;
}
void SoundChooser::_onReloadSounds(wxCommandEvent& ev)
{
_preview->ClearPreview();
// 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()
{
Destroy();
}
} // namespace