Counter Strike : Global Offensive Source Code
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.

371 lines
12 KiB

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