Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

334 lines
10 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "OptionsSubVoice.h"
  9. #include "cvarslider.h"
  10. #include <vgui/IVGui.h>
  11. #include <vgui_controls/ImagePanel.h>
  12. #include <vgui_controls/CheckButton.h>
  13. #include <vgui_controls/Slider.h>
  14. #include "EngineInterface.h"
  15. #include "ivoicetweak.h"
  16. #include "CvarToggleCheckButton.h"
  17. #include "tier1/KeyValues.h"
  18. #include "tier1/convar.h"
  19. #include <steam/steam_api.h>
  20. // memdbgon must be the last include file in a .cpp file!!!
  21. #include "tier0/memdbgon.h"
  22. using namespace vgui;
  23. //-----------------------------------------------------------------------------
  24. // Purpose:
  25. //-----------------------------------------------------------------------------
  26. COptionsSubVoice::COptionsSubVoice(vgui::Panel *parent) : PropertyPage(parent, NULL)
  27. {
  28. #if !defined( NO_VOICE ) //#ifndef _XBOX
  29. m_pVoiceTweak = engine->GetVoiceTweakAPI();
  30. #endif
  31. m_pMicMeter = new ImagePanel(this, "MicMeter");
  32. m_pMicMeter2 = new ImagePanel(this, "MicMeter2");
  33. m_pReceiveSliderLabel = new Label(this, "ReceiveLabel", "#GameUI_VoiceReceiveVolume");
  34. m_pReceiveVolume = new CCvarSlider( this, "VoiceReceive", "#GameUI_ReceiveVolume",
  35. 0.0f, 1.0f, "voice_scale" );
  36. m_pMicrophoneSliderLabel = new Label(this, "MicrophoneLabel", "#GameUI_VoiceTransmitVolume");
  37. m_pMicrophoneVolume = new Slider( this, "#GameUI_MicrophoneVolume" );
  38. m_pMicrophoneVolume->SetRange( 0, 100 );
  39. m_pMicrophoneVolume->AddActionSignalTarget( this );
  40. m_pVoiceEnableCheckButton = new CCvarToggleCheckButton( this, "voice_modenable", "#GameUI_EnableVoice", "voice_modenable" );
  41. m_pMicBoost = new CheckButton(this, "MicBoost", "#GameUI_BoostMicrophone" );
  42. m_pMicBoost->AddActionSignalTarget( this );
  43. m_pTestMicrophoneButton = new Button(this, "TestMicrophone", "#GameUI_TestMicrophone");
  44. LoadControlSettings("Resource\\OptionsSubVoice.res");
  45. m_bVoiceOn = false;
  46. m_pMicMeter2->SetVisible(false);
  47. // no voice tweak - then disable all buttons
  48. if (!m_pVoiceTweak)
  49. {
  50. m_pReceiveVolume->SetEnabled(false);
  51. m_pMicrophoneVolume->SetEnabled(false);
  52. m_pVoiceEnableCheckButton->SetEnabled(false);
  53. m_pMicBoost->SetEnabled(false);
  54. m_pTestMicrophoneButton->SetEnabled(false);
  55. }
  56. else
  57. {
  58. OnResetData();
  59. }
  60. }
  61. //-----------------------------------------------------------------------------
  62. // Purpose:
  63. //-----------------------------------------------------------------------------
  64. COptionsSubVoice::~COptionsSubVoice()
  65. {
  66. // turn off voice if it was on, since we're leaving this page
  67. if (m_bVoiceOn)
  68. {
  69. EndTestMicrophone();
  70. }
  71. }
  72. //-----------------------------------------------------------------------------
  73. // Purpose:
  74. //-----------------------------------------------------------------------------
  75. void COptionsSubVoice::OnResetData()
  76. {
  77. if (!m_pVoiceTweak)
  78. return;
  79. float micVolume = m_pVoiceTweak->GetControlFloat( MicrophoneVolume );
  80. m_pMicrophoneVolume->SetValue( (int)( 100.0f * micVolume ) );
  81. m_nMicVolumeValue = m_pMicrophoneVolume->GetValue();
  82. float fMicBoost = m_pVoiceTweak->GetControlFloat( MicBoost );
  83. m_pMicBoost->SetSelected( fMicBoost != 0.0f );
  84. m_bMicBoostSelected = m_pMicBoost->IsSelected();
  85. m_pReceiveVolume->Reset();
  86. m_fReceiveVolume = m_pReceiveVolume->GetSliderValue();
  87. m_pVoiceEnableCheckButton->Reset();
  88. }
  89. //-----------------------------------------------------------------------------
  90. // Purpose:
  91. //-----------------------------------------------------------------------------
  92. void COptionsSubVoice::OnSliderMoved( int position )
  93. {
  94. if (m_pVoiceTweak)
  95. {
  96. if (m_pMicrophoneVolume->GetValue() != m_nMicVolumeValue)
  97. {
  98. PostActionSignal(new KeyValues("ApplyButtonEnable"));
  99. }
  100. }
  101. }
  102. //-----------------------------------------------------------------------------
  103. // Purpose:
  104. //-----------------------------------------------------------------------------
  105. void COptionsSubVoice::OnCheckButtonChecked( int state )
  106. {
  107. if (m_pVoiceTweak)
  108. {
  109. // if our state is different
  110. if ( m_pMicBoost->IsSelected() != m_bMicBoostSelected)
  111. {
  112. PostActionSignal(new KeyValues("ApplyButtonEnable"));
  113. }
  114. }
  115. }
  116. //-----------------------------------------------------------------------------
  117. // Purpose:
  118. //-----------------------------------------------------------------------------
  119. void COptionsSubVoice::OnApplyChanges()
  120. {
  121. if (!m_pVoiceTweak)
  122. return;
  123. m_nMicVolumeValue = m_pMicrophoneVolume->GetValue();
  124. float fMicVolume = (float) m_nMicVolumeValue / 100.0f;
  125. m_pVoiceTweak->SetControlFloat( MicrophoneVolume, fMicVolume );
  126. m_bMicBoostSelected = m_pMicBoost->IsSelected();
  127. m_pVoiceTweak->SetControlFloat( MicBoost, m_bMicBoostSelected ? 1.0f : 0.0f );
  128. m_pReceiveVolume->ApplyChanges();
  129. m_fReceiveVolume = m_pReceiveVolume->GetSliderValue();
  130. m_pVoiceEnableCheckButton->ApplyChanges();
  131. }
  132. //-----------------------------------------------------------------------------
  133. // Purpose:
  134. //-----------------------------------------------------------------------------
  135. void COptionsSubVoice::StartTestMicrophone()
  136. {
  137. if (!m_pVoiceTweak || m_bVoiceOn)
  138. return;
  139. m_bVoiceOn = true;
  140. UseCurrentVoiceParameters();
  141. if (m_pVoiceTweak->StartVoiceTweakMode())
  142. {
  143. m_pTestMicrophoneButton->SetText("#GameUI_StopTestMicrophone");
  144. m_pReceiveVolume->SetEnabled(false);
  145. m_pMicrophoneVolume->SetEnabled(false);
  146. m_pVoiceEnableCheckButton->SetEnabled(false);
  147. m_pMicBoost->SetEnabled(false);
  148. m_pMicrophoneSliderLabel->SetEnabled(false);
  149. m_pReceiveSliderLabel->SetEnabled(false);
  150. m_pMicMeter2->SetVisible(true);
  151. }
  152. else
  153. {
  154. ResetVoiceParameters();
  155. // we couldn't start it
  156. m_bVoiceOn = false;
  157. return;
  158. }
  159. }
  160. //-----------------------------------------------------------------------------
  161. // Purpose:
  162. //-----------------------------------------------------------------------------
  163. void COptionsSubVoice::UseCurrentVoiceParameters()
  164. {
  165. int nVal = m_pMicrophoneVolume->GetValue();
  166. float val = (float) nVal / 100.0f;
  167. m_pVoiceTweak->SetControlFloat( MicrophoneVolume, val );
  168. bool bSelected = m_pMicBoost->IsSelected();
  169. val = bSelected ? 1.0f : 0.0f;
  170. m_pVoiceTweak->SetControlFloat( MicBoost, val );
  171. // get where the current slider is
  172. m_nReceiveSliderValue = m_pReceiveVolume->GetValue();
  173. m_pReceiveVolume->ApplyChanges();
  174. }
  175. //-----------------------------------------------------------------------------
  176. // Purpose:
  177. //-----------------------------------------------------------------------------
  178. void COptionsSubVoice::ResetVoiceParameters()
  179. {
  180. float fMicVolume = (float) m_nMicVolumeValue / 100.0f;
  181. m_pVoiceTweak->SetControlFloat( MicrophoneVolume, fMicVolume );
  182. m_pVoiceTweak->SetControlFloat( MicBoost, m_bMicBoostSelected ? 1.0f : 0.0f );
  183. // restore the old value
  184. ConVarRef voice_scale( "voice_scale" );
  185. voice_scale.SetValue( m_fReceiveVolume );
  186. m_pReceiveVolume->Reset();
  187. // set the slider to 'new' value, but we've reset the 'start' value where it was
  188. m_pReceiveVolume->SetValue(m_nReceiveSliderValue);
  189. }
  190. //-----------------------------------------------------------------------------
  191. // Purpose:
  192. //-----------------------------------------------------------------------------
  193. void COptionsSubVoice::EndTestMicrophone()
  194. {
  195. if (!m_pVoiceTweak || !m_bVoiceOn)
  196. return;
  197. if ( m_pVoiceTweak->IsStillTweaking() )
  198. {
  199. m_pVoiceTweak->EndVoiceTweakMode();
  200. }
  201. ResetVoiceParameters();
  202. m_pTestMicrophoneButton->SetText("#GameUI_TestMicrophone");
  203. m_bVoiceOn = false;
  204. m_pReceiveVolume->SetEnabled(true);
  205. m_pMicrophoneVolume->SetEnabled(true);
  206. m_pVoiceEnableCheckButton->SetEnabled(true);
  207. m_pMicBoost->SetEnabled(true);
  208. m_pMicrophoneSliderLabel->SetEnabled(true);
  209. m_pReceiveSliderLabel->SetEnabled(true);
  210. m_pMicMeter2->SetVisible(false);
  211. }
  212. //-----------------------------------------------------------------------------
  213. // Purpose:
  214. //-----------------------------------------------------------------------------
  215. void COptionsSubVoice::OnCommand( const char *command)
  216. {
  217. if (!stricmp(command, "TestMicrophone"))
  218. {
  219. if (!m_bVoiceOn)
  220. {
  221. StartTestMicrophone();
  222. }
  223. else
  224. {
  225. EndTestMicrophone();
  226. }
  227. }
  228. else if (!stricmp(command, "SteamVoiceSettings"))
  229. {
  230. if ( steamapicontext && steamapicontext->SteamFriends() )
  231. {
  232. steamapicontext->SteamFriends()->ActivateGameOverlay( "voicesettings" );
  233. }
  234. }
  235. else
  236. {
  237. BaseClass::OnCommand(command);
  238. }
  239. }
  240. //-----------------------------------------------------------------------------
  241. // Purpose:
  242. //-----------------------------------------------------------------------------
  243. void COptionsSubVoice::OnPageHide()
  244. {
  245. // turn off voice if it was on, since we're leaving this page
  246. if (m_bVoiceOn)
  247. {
  248. EndTestMicrophone();
  249. }
  250. BaseClass::OnPageHide();
  251. }
  252. //-----------------------------------------------------------------------------
  253. // Purpose:
  254. //-----------------------------------------------------------------------------
  255. void COptionsSubVoice::OnControlModified()
  256. {
  257. PostActionSignal(new KeyValues("ApplyButtonEnable"));
  258. }
  259. #define BAR_WIDTH 160
  260. #define BAR_INCREMENT 8
  261. //-----------------------------------------------------------------------------
  262. // Purpose:
  263. //-----------------------------------------------------------------------------
  264. void COptionsSubVoice::OnThink()
  265. {
  266. BaseClass::OnThink();
  267. if (m_bVoiceOn)
  268. {
  269. if ( !m_pVoiceTweak->IsStillTweaking() )
  270. {
  271. DevMsg( 1, "Lost Voice Tweak channels, resetting\n" );
  272. EndTestMicrophone();
  273. }
  274. else
  275. {
  276. float val = m_pVoiceTweak->GetControlFloat( SpeakingVolume );
  277. int nValue = static_cast<int>( val*32768.0f + 0.5f );
  278. int width = (BAR_WIDTH * nValue) / 32768;
  279. width = ((width + (BAR_INCREMENT-1)) / BAR_INCREMENT) * BAR_INCREMENT; // round to nearest BAR_INCREMENT
  280. int wide, tall;
  281. m_pMicMeter2->GetSize(wide, tall);
  282. m_pMicMeter2->SetSize(width, tall);
  283. m_pMicMeter2->Repaint();
  284. }
  285. }
  286. }