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.

316 lines
8.8 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "cvarslider.h"
  9. #include <stdio.h>
  10. #include "engineinterface.h"
  11. #include "tier1/keyvalues.h"
  12. #include "tier1/convar.h"
  13. #include <vgui/IVGui.h>
  14. #include <vgui_controls/PropertyPage.h>
  15. #define CVARSLIDER_SCALE_FACTOR 100.0f
  16. // memdbgon must be the last include file in a .cpp file!!!
  17. #include <tier0/memdbgon.h>
  18. using namespace vgui;
  19. DECLARE_BUILD_FACTORY( CCvarSlider );
  20. //-----------------------------------------------------------------------------
  21. // Purpose:
  22. //-----------------------------------------------------------------------------
  23. CCvarSlider::CCvarSlider( Panel *parent, const char *name ) : Slider( parent, name )
  24. {
  25. SetupSlider( 0, 1, "", false );
  26. m_bCreatedInCode = false;
  27. AddActionSignalTarget( this );
  28. }
  29. //-----------------------------------------------------------------------------
  30. // Purpose:
  31. //-----------------------------------------------------------------------------
  32. CCvarSlider::CCvarSlider( Panel *parent, const char *panelName, char const *caption,
  33. float minValue, float maxValue, char const *cvarname, bool bAllowOutOfRange )
  34. : Slider( parent, panelName )
  35. {
  36. AddActionSignalTarget( this );
  37. SetupSlider( minValue, maxValue, cvarname, bAllowOutOfRange );
  38. // For backwards compatability. Ignore .res file settings for forced setup sliders.
  39. m_bCreatedInCode = true;
  40. }
  41. //-----------------------------------------------------------------------------
  42. // Purpose:
  43. //-----------------------------------------------------------------------------
  44. void CCvarSlider::SetupSlider( float minValue, float maxValue, const char *cvarname, bool bAllowOutOfRange )
  45. {
  46. m_flMinValue = minValue;
  47. m_flMaxValue = maxValue;
  48. // scale by CVARSLIDER_SCALE_FACTOR
  49. SetRange( (int)( CVARSLIDER_SCALE_FACTOR * minValue ), (int)( CVARSLIDER_SCALE_FACTOR * maxValue ) );
  50. char szMin[ 32 ];
  51. char szMax[ 32 ];
  52. Q_snprintf( szMin, sizeof( szMin ), "%.2f", minValue );
  53. Q_snprintf( szMax, sizeof( szMax ), "%.2f", maxValue );
  54. SetTickCaptions( szMin, szMax );
  55. Q_strncpy( m_szCvarName, cvarname, sizeof( m_szCvarName ) );
  56. m_bModifiedOnce = false;
  57. m_bAllowOutOfRange = bAllowOutOfRange;
  58. // Set slider to current value
  59. Reset();
  60. }
  61. //-----------------------------------------------------------------------------
  62. // Purpose:
  63. //-----------------------------------------------------------------------------
  64. CCvarSlider::~CCvarSlider()
  65. {
  66. }
  67. //-----------------------------------------------------------------------------
  68. // Purpose:
  69. //-----------------------------------------------------------------------------
  70. void CCvarSlider::ApplySettings( KeyValues *inResourceData )
  71. {
  72. BaseClass::ApplySettings( inResourceData );
  73. if ( !m_bCreatedInCode )
  74. {
  75. float minValue = inResourceData->GetFloat( "minvalue", 0 );
  76. float maxValue = inResourceData->GetFloat( "maxvalue", 1 );
  77. const char *cvarname = inResourceData->GetString( "cvar_name", "" );
  78. bool bAllowOutOfRange = inResourceData->GetBool( "allowoutofrange", false );
  79. SetupSlider( minValue, maxValue, cvarname, bAllowOutOfRange );
  80. if ( GetParent() )
  81. {
  82. // HACK: If our parent is a property page, we want the dialog containing it
  83. if ( dynamic_cast<vgui::PropertyPage*>(GetParent()) && GetParent()->GetParent() )
  84. {
  85. GetParent()->GetParent()->AddActionSignalTarget( this );
  86. }
  87. else
  88. {
  89. GetParent()->AddActionSignalTarget( this );
  90. }
  91. }
  92. }
  93. }
  94. //-----------------------------------------------------------------------------
  95. // Purpose: Get control settings for editing
  96. //-----------------------------------------------------------------------------
  97. void CCvarSlider::GetSettings( KeyValues *outResourceData )
  98. {
  99. BaseClass::GetSettings(outResourceData);
  100. if ( !m_bCreatedInCode )
  101. {
  102. outResourceData->SetFloat( "minvalue", m_flMinValue );
  103. outResourceData->SetFloat( "maxvalue", m_flMaxValue );
  104. outResourceData->SetString( "cvar_name", m_szCvarName );
  105. outResourceData->SetBool( "allowoutofrange", m_bAllowOutOfRange );
  106. }
  107. }
  108. void CCvarSlider::SetCVarName( char const *cvarname )
  109. {
  110. Q_strncpy( m_szCvarName, cvarname, sizeof( m_szCvarName ) );
  111. m_bModifiedOnce = false;
  112. // Set slider to current value
  113. Reset();
  114. }
  115. void CCvarSlider::SetMinMaxValues( float minValue, float maxValue, bool bSetTickDisplay )
  116. {
  117. SetRange( (int)( CVARSLIDER_SCALE_FACTOR * minValue ), (int)( CVARSLIDER_SCALE_FACTOR * maxValue ) );
  118. if ( bSetTickDisplay )
  119. {
  120. char szMin[ 32 ];
  121. char szMax[ 32 ];
  122. Q_snprintf( szMin, sizeof( szMin ), "%.2f", minValue );
  123. Q_snprintf( szMax, sizeof( szMax ), "%.2f", maxValue );
  124. SetTickCaptions( szMin, szMax );
  125. }
  126. // Set slider to current value
  127. Reset();
  128. }
  129. void CCvarSlider::SetTickColor( Color color )
  130. {
  131. m_TickColor = color;
  132. }
  133. //-----------------------------------------------------------------------------
  134. // Purpose:
  135. //-----------------------------------------------------------------------------
  136. void CCvarSlider::Paint()
  137. {
  138. // Get engine's current value
  139. // float curvalue = engine->pfnGetCvarFloat( m_szCvarName );
  140. ConVarRef var( m_szCvarName );
  141. if ( !var.IsValid() )
  142. return;
  143. float curvalue = var.GetFloat();
  144. // did it get changed from under us?
  145. if (curvalue != m_fStartValue)
  146. {
  147. int val = (int)( CVARSLIDER_SCALE_FACTOR * curvalue );
  148. m_fStartValue = curvalue;
  149. m_fCurrentValue = curvalue;
  150. SetValue( val );
  151. m_iStartValue = GetValue();
  152. }
  153. BaseClass::Paint();
  154. }
  155. //-----------------------------------------------------------------------------
  156. // Purpose:
  157. //-----------------------------------------------------------------------------
  158. void CCvarSlider::ApplyChanges()
  159. {
  160. if (m_bModifiedOnce)
  161. {
  162. m_iStartValue = GetValue();
  163. if (m_bAllowOutOfRange)
  164. {
  165. m_fStartValue = m_fCurrentValue;
  166. }
  167. else
  168. {
  169. m_fStartValue = (float) m_iStartValue / CVARSLIDER_SCALE_FACTOR;
  170. }
  171. //engine->Cvar_SetValue( m_szCvarName, m_fStartValue );
  172. ConVarRef var( m_szCvarName );
  173. var.SetValue( (float)m_fStartValue );
  174. }
  175. }
  176. //-----------------------------------------------------------------------------
  177. // Purpose:
  178. //-----------------------------------------------------------------------------
  179. float CCvarSlider::GetSliderValue()
  180. {
  181. if (m_bAllowOutOfRange)
  182. {
  183. return m_fCurrentValue;
  184. }
  185. else
  186. {
  187. return ((float)GetValue())/ CVARSLIDER_SCALE_FACTOR;
  188. }
  189. }
  190. //-----------------------------------------------------------------------------
  191. // Purpose:
  192. //-----------------------------------------------------------------------------
  193. void CCvarSlider::SetSliderValue(float fValue)
  194. {
  195. int nVal = (int)( CVARSLIDER_SCALE_FACTOR * fValue );
  196. SetValue( nVal, false);
  197. // remember this slider value
  198. m_iLastSliderValue = GetValue();
  199. if (m_fCurrentValue != fValue)
  200. {
  201. m_fCurrentValue = fValue;
  202. m_bModifiedOnce = true;
  203. }
  204. }
  205. //-----------------------------------------------------------------------------
  206. // Purpose:
  207. //-----------------------------------------------------------------------------
  208. void CCvarSlider::Reset()
  209. {
  210. // Set slider to current value
  211. // m_fStartValue = engine->pfnGetCvarFloat( m_szCvarName );
  212. ConVarRef var( m_szCvarName );
  213. if ( !var.IsValid() )
  214. {
  215. m_fCurrentValue = m_fStartValue = 0.0f;
  216. SetValue( 0 );
  217. m_iStartValue = GetValue();
  218. m_iLastSliderValue = m_iStartValue;
  219. return;
  220. }
  221. float m_fStartValue = var.GetFloat();
  222. m_fCurrentValue = m_fStartValue;
  223. int value = (int)( CVARSLIDER_SCALE_FACTOR * m_fStartValue );
  224. SetValue( value );
  225. m_iStartValue = GetValue();
  226. m_iLastSliderValue = m_iStartValue;
  227. }
  228. //-----------------------------------------------------------------------------
  229. // Purpose:
  230. //-----------------------------------------------------------------------------
  231. bool CCvarSlider::HasBeenModified()
  232. {
  233. if (GetValue() != m_iStartValue)
  234. {
  235. m_bModifiedOnce = true;
  236. }
  237. return m_bModifiedOnce;
  238. }
  239. //-----------------------------------------------------------------------------
  240. // Purpose:
  241. // Input : position -
  242. //-----------------------------------------------------------------------------
  243. void CCvarSlider::OnSliderMoved()
  244. {
  245. if (HasBeenModified())
  246. {
  247. if (m_iLastSliderValue != GetValue())
  248. {
  249. m_iLastSliderValue = GetValue();
  250. m_fCurrentValue = ((float) m_iLastSliderValue)/CVARSLIDER_SCALE_FACTOR;
  251. }
  252. // tell parent that we've been modified
  253. PostActionSignal(new KeyValues("ControlModified"));
  254. }
  255. }
  256. //-----------------------------------------------------------------------------
  257. // Purpose:
  258. //-----------------------------------------------------------------------------
  259. void CCvarSlider::OnApplyChanges( void )
  260. {
  261. if ( !m_bCreatedInCode )
  262. {
  263. ApplyChanges();
  264. }
  265. }