summaryrefslogtreecommitdiff
path: root/vgui2/dme_controls/soundrecordpanel.cpp
blob: b2feea7644e870cf15fe8c572a38e4a3fe98c717 (plain) (blame)
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
//=============================================================================

#include "dme_controls/soundrecordpanel.h"
#include "filesystem.h"
#include "tier1/KeyValues.h"
#include "vgui_controls/Button.h"
#include "vgui_controls/TextEntry.h"
#include "dme_controls/dmecontrols.h"
#include "vgui_controls/messagebox.h"
#include "soundemittersystem/isoundemittersystembase.h"
#include "vgui/IVGui.h"
#include "mathlib/mathlib.h"

// FIXME: Move sound code out of the engine + into a library!
#include "toolframework/ienginetool.h"

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"


using namespace vgui;


//-----------------------------------------------------------------------------
//
// Sound Record Dialog
//
//-----------------------------------------------------------------------------
CSoundRecordPanel::CSoundRecordPanel( vgui::Panel *pParent, const char *pTitle ) : 
	BaseClass( pParent, "SoundRecordPanel" )
{
	m_bIsRecording = false;
	m_nPlayingSound = 0;
	SetDeleteSelfOnClose( true );
	m_pOkButton = new Button( this, "OkButton", "#FileOpenDialog_Open", this, "Ok" );
	m_pCancelButton = new Button( this, "CancelButton", "#FileOpenDialog_Cancel", this, "Cancel" );
	m_pPlayButton = new Button( this, "PlayButton", "Play", this, "Play" );
	m_pRecordButton = new Button( this, "Record", "Record", this, "ToggleRecord" );
	m_pRecordTime = new TextEntry( this, "RecordTime" );
	m_pFileName = new TextEntry( this, "FileName" );

	LoadControlSettingsAndUserConfig( "resource/soundrecordpanel.res" );

	SetTitle( pTitle, false );
}

CSoundRecordPanel::~CSoundRecordPanel()
{
	StopSoundPreview();
}


//-----------------------------------------------------------------------------
// Purpose: Activate the dialog
//-----------------------------------------------------------------------------
void CSoundRecordPanel::DoModal( const char *pFileName )
{
	Assert( EngineTool() );

	char pRelativeWAVPath[MAX_PATH];
	g_pFullFileSystem->FullPathToRelativePath( pFileName, pRelativeWAVPath, sizeof(pRelativeWAVPath) );

	// Check to see if this file is not hidden owing to search paths
	bool bBadDirectory = false;
	char pRelativeDir[MAX_PATH];
	Q_strncpy( pRelativeDir, pRelativeWAVPath, sizeof( pRelativeDir ) );
	Q_StripFilename( pRelativeDir );
	char pFoundFullPath[MAX_PATH];
	g_pFullFileSystem->RelativePathToFullPath( pRelativeDir, "MOD", pFoundFullPath, sizeof( pFoundFullPath ) );
	if ( StringHasPrefix( pFileName, pFoundFullPath ) )
	{
		// Strip 'sound/' prefix
		m_FileName = pRelativeWAVPath;
		const char *pSoundName = StringAfterPrefix( pRelativeWAVPath, "sound\\" );
		if ( !pSoundName )
		{
			pSoundName = pRelativeWAVPath;
			bBadDirectory = true;
		}
		m_EngineFileName = pSoundName;
	}
	else
	{
		bBadDirectory = true;
	}

	if ( bBadDirectory )
	{
		char pBuf[1024];
		Q_snprintf( pBuf, sizeof(pBuf), "File %s is in a bad directory!\nAudio must be recorded into your mod's sound/ directory.\n", pFileName ); 
		vgui::MessageBox *pMessageBox = new vgui::MessageBox( "Bad Save Directory!\n", pBuf, GetParent() );
		pMessageBox->DoModal( );
		return;
	}

	m_pFileName->SetText( pFileName );
	m_pOkButton->SetEnabled( false );
	m_pPlayButton->SetEnabled( false );
	BaseClass::DoModal();
}


//-----------------------------------------------------------------------------
// Stop sound preview
//-----------------------------------------------------------------------------
void CSoundRecordPanel::StopSoundPreview( )
{
	if ( m_nPlayingSound != 0 )
	{
		EngineTool()->StopSoundByGuid( m_nPlayingSound );
		m_nPlayingSound = 0;
	}
}


//-----------------------------------------------------------------------------
// Plays a wav file
//-----------------------------------------------------------------------------
void CSoundRecordPanel::PlaySoundPreview( )
{
	StopSoundPreview();
	m_nPlayingSound = EngineTool()->StartSound( 0, true, -1, CHAN_STATIC, m_EngineFileName, 
		VOL_NORM, SNDLVL_NONE, vec3_origin, vec3_origin, 0, PITCH_NORM, false, 0 );
}


//-----------------------------------------------------------------------------
// Updates sound record time during recording
//-----------------------------------------------------------------------------
void CSoundRecordPanel::UpdateTimeRecorded()
{ 
	float flTime = Plat_FloatTime() - m_flRecordStartTime;
	char pTimeBuf[64];
	Q_snprintf( pTimeBuf, sizeof(pTimeBuf), "%.3f", flTime );
	m_pRecordTime->SetText( pTimeBuf );
}


//-----------------------------------------------------------------------------
// Updates sound record time during recording
//-----------------------------------------------------------------------------
void CSoundRecordPanel::OnTick()
{
	BaseClass::OnTick();

	// Update the amount of time recorded
	UpdateTimeRecorded();
}

	
//-----------------------------------------------------------------------------
// On command
//-----------------------------------------------------------------------------
void CSoundRecordPanel::OnCommand( const char *pCommand )
{
	if ( !Q_stricmp( pCommand, "ToggleRecord" ) )
	{
		if ( !m_bIsRecording )
		{
			StopSoundPreview();
			g_pFullFileSystem->RemoveFile( m_FileName, "MOD" );
			EngineTool()->StartRecordingVoiceToFile( m_FileName, "MOD" );
			m_pRecordButton->SetText( "Stop Recording" );
			m_pPlayButton->SetEnabled( false );
			m_pOkButton->SetEnabled( false );
			m_pCancelButton->SetEnabled( true );
			m_flRecordStartTime = Plat_FloatTime();
			vgui::ivgui()->AddTickSignal( GetVPanel(), 0 );	
		}
		else
		{
			EngineTool()->StopRecordingVoiceToFile();
			EngineTool()->ReloadSound( m_EngineFileName );
			ivgui()->RemoveTickSignal( GetVPanel() );
			UpdateTimeRecorded();
			m_pOkButton->SetEnabled( true );
			m_pCancelButton->SetEnabled( true );
			m_pPlayButton->SetEnabled( true );
			m_pRecordButton->SetText( "Record" );
		}
		m_bIsRecording = !m_bIsRecording;
		return;
	}

	if ( !Q_stricmp( pCommand, "Play" ) )
	{
		PlaySoundPreview();
		return;
	}

	if ( !Q_stricmp( pCommand, "Ok" ) )
	{
		PostActionSignal( new KeyValues( "SoundRecorded", "relativepath", m_EngineFileName.Get() ) );
		CloseModal();
		return;
	}

	if ( !Q_stricmp( pCommand, "Cancel" ) )
	{
		g_pFullFileSystem->RemoveFile( m_FileName, "MOD" );
		CloseModal();
		return;
	}

	BaseClass::OnCommand( pCommand );
}