Debian Codegraph @

plugins/dm.editing/AIVocalSetPreview.cpp

raw
1 #include "AIVocalSetPreview.h"
2
3 #include "i18n.h"
4 #include "isound.h"
5 #include "eclass.h"
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <time.h>
10
11 #include <wx/sizer.h>
12 #include "wxutil/Bitmap.h"
13 #include <wx/button.h>
14 #include <wx/stattext.h>
15
16 namespace ui
17 {
18
19 AIVocalSetPreview::AIVocalSetPreview(wxWindow* parent) :
20 wxPanel(parent, wxID_ANY)
21 {
22 createControlPanel();
23
24 // Trigger the initial update of the widgets
25 update();
26
27 srand(static_cast<unsigned int>(time(NULL)));
28 }
29
30 void AIVocalSetPreview::createControlPanel()
31 {
32 SetMinClientSize(wxSize(200, -1));
33 SetSizer(new wxBoxSizer(wxVERTICAL));
34
35 // Create the playback button
36 _playButton = new wxButton(this, wxID_ANY);
37 _playButton->SetBitmap(wxutil::GetLocalBitmap("media-playback-start-ltr.png"));
38
39 _stopButton = new wxButton(this, wxID_ANY);
40 _stopButton->SetBitmap(wxutil::GetLocalBitmap("media-playback-stop.png"));
41
42 _playButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(AIVocalSetPreview::onPlay), NULL, this);
43 _stopButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(AIVocalSetPreview::onStop), NULL, this);
44
45 wxBoxSizer* btnHBox = new wxBoxSizer(wxHORIZONTAL);
46
47 btnHBox->Add(_playButton, 1, wxRIGHT, 6);
48 btnHBox->Add(_stopButton, 1);
49
50 _statusLabel = new wxStaticText(this, wxID_ANY, "");
51
52 GetSizer()->Add(_statusLabel);
53 GetSizer()->Add(btnHBox);
54 }
55
56 void AIVocalSetPreview::setVocalSetEclass(const IEntityClassPtr& vocalSetDef)
57 {
58 _vocalSetDef = vocalSetDef;
59
60 update();
61 }
62
63 void AIVocalSetPreview::update()
64 {
65 _setShaders.clear();
66
67 if (_vocalSetDef != NULL)
68 {
69 eclass::AttributeList sndAttrs = eclass::getSpawnargsWithPrefix(
70 _vocalSetDef, "snd_"
71 );
72
73 for (eclass::AttributeList::const_iterator i = sndAttrs.begin();
74 i != sndAttrs.end(); ++i)
75 {
76 _setShaders.push_back(i->getValue());
77 }
78 }
79
80 // If the soundshader string is empty, desensitise the widgets
81 Enable(_vocalSetDef != NULL && !_setShaders.empty());
82 }
83
84 std::string AIVocalSetPreview::getRandomSoundFile()
85 {
86 // get a random sound shader
87 std::size_t idx = static_cast<std::size_t>(rand()) % _setShaders.size();
88
89 auto soundShader = GlobalSoundManager().getSoundShader(_setShaders[idx]);
90
91 if (!soundShader) return "";
92
93 SoundFileList files = soundShader->getSoundFileList();
94
95 if (files.empty()) return "";
96
97 std::size_t fileIdx = static_cast<std::size_t>(rand()) % files.size();
98
99 return files[fileIdx];
100 }
101
102 void AIVocalSetPreview::onPlay(wxCommandEvent& ev)
103 {
104 _statusLabel->SetLabelMarkup("");
105
106 std::string file = getRandomSoundFile();
107
108 if (!file.empty())
109 {
110 // Pass the call to the sound manager
111 if (!GlobalSoundManager().playSound(file))
112 {
113 _statusLabel->SetLabelMarkup(_("<b>Error:</b> File not found."));
114 }
115 }
116 }
117
118 void AIVocalSetPreview::onStop(wxCommandEvent& ev)
119 {
120 // Pass the call to the sound manager
121 GlobalSoundManager().stopSound();
122 _statusLabel->SetLabelMarkup("");
123 }
124
125 } // namespace ui