File: AIVocalSetPreview.cpp

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (125 lines) | stat: -rw-r--r-- 2,891 bytes parent folder | download | duplicates (3)
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
#include "AIVocalSetPreview.h"

#include "i18n.h"
#include "isound.h"
#include "eclass.h"

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include <wx/sizer.h>
#include "wxutil/Bitmap.h"
#include <wx/button.h>
#include <wx/stattext.h>

namespace ui
{

AIVocalSetPreview::AIVocalSetPreview(wxWindow* parent) :
	wxPanel(parent, wxID_ANY)
{
	createControlPanel();

	// Trigger the initial update of the widgets
	update();

	srand(static_cast<unsigned int>(time(NULL)));
}

void AIVocalSetPreview::createControlPanel()
{
	SetMinClientSize(wxSize(200, -1));
	SetSizer(new wxBoxSizer(wxVERTICAL));

	// Create the playback button
	_playButton = new wxButton(this, wxID_ANY);
	_playButton->SetBitmap(wxutil::GetLocalBitmap("media-playback-start-ltr.png"));

	_stopButton = new wxButton(this, wxID_ANY);
	_stopButton->SetBitmap(wxutil::GetLocalBitmap("media-playback-stop.png"));

	_playButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(AIVocalSetPreview::onPlay), NULL, this);
	_stopButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(AIVocalSetPreview::onStop), NULL, this);

	wxBoxSizer* btnHBox = new wxBoxSizer(wxHORIZONTAL);

	btnHBox->Add(_playButton, 1, wxRIGHT, 6);
	btnHBox->Add(_stopButton, 1);

	_statusLabel = new wxStaticText(this, wxID_ANY, "");

	GetSizer()->Add(_statusLabel);
	GetSizer()->Add(btnHBox);
}

void AIVocalSetPreview::setVocalSetEclass(const IEntityClassPtr& vocalSetDef)
{
	_vocalSetDef = vocalSetDef;

	update();
}

void AIVocalSetPreview::update()
{
	_setShaders.clear();

	if (_vocalSetDef != NULL)
	{
        eclass::AttributeList sndAttrs = eclass::getSpawnargsWithPrefix(
            _vocalSetDef, "snd_"
        );

		for (eclass::AttributeList::const_iterator i = sndAttrs.begin();
			i != sndAttrs.end(); ++i)
		{
			_setShaders.push_back(i->getValue());
		}
	}

	// If the soundshader string is empty, desensitise the widgets
	Enable(_vocalSetDef != NULL && !_setShaders.empty());
}

std::string AIVocalSetPreview::getRandomSoundFile()
{
	// get a random sound shader
	std::size_t idx = static_cast<std::size_t>(rand()) % _setShaders.size();

	auto soundShader = GlobalSoundManager().getSoundShader(_setShaders[idx]);

	if (!soundShader) return "";

	SoundFileList files = soundShader->getSoundFileList();

	if (files.empty()) return "";

	std::size_t fileIdx = static_cast<std::size_t>(rand()) % files.size();

	return files[fileIdx];
}

void AIVocalSetPreview::onPlay(wxCommandEvent& ev)
{
	_statusLabel->SetLabelMarkup("");

	std::string file = getRandomSoundFile();

	if (!file.empty())
	{
		// Pass the call to the sound manager
		if (!GlobalSoundManager().playSound(file))
		{
			_statusLabel->SetLabelMarkup(_("<b>Error:</b> File not found."));
		}
	}
}

void AIVocalSetPreview::onStop(wxCommandEvent& ev)
{
	// Pass the call to the sound manager
	GlobalSoundManager().stopSound();
	_statusLabel->SetLabelMarkup("");
}

} // namespace ui