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.

178 lines
5.7 KiB

  1. //====== Copyright � 1996-2005, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #ifndef GAMEUI_UTIL_H
  7. #define GAMEUI_UTIL_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. // Set by the player who "owns" the gameui/settings/etc.
  12. void SetGameUIActiveSplitScreenPlayerSlot( int nSlot );
  13. int GetGameUIActiveSplitScreenPlayerSlot();
  14. #include "tier1/convar.h"
  15. void GameUI_MakeSafeName( const char *oldName, char *newName, int newNameBufSize );
  16. //-----------------------------------------------------------------------------
  17. // Useful for game ui since game ui has a single active "splitscreen" owner and since
  18. // it can gracefully handle non-FCVAR_SS vars without code changes required.
  19. //-----------------------------------------------------------------------------
  20. class CGameUIConVarRef
  21. {
  22. public:
  23. explicit CGameUIConVarRef( const char *pName );
  24. CGameUIConVarRef( const char *pName, bool bIgnoreMissing );
  25. explicit CGameUIConVarRef( IConVar *pConVar );
  26. void Init( const char *pName, bool bIgnoreMissing );
  27. bool IsValid() const;
  28. bool IsFlagSet( int nFlags ) const;
  29. // Get/Set value
  30. float GetFloat( int iSlot = -1 ) const;
  31. int GetInt( int iSlot = -1 ) const;
  32. float GetMin( int iSlot = -1 ) const;
  33. float GetMax( int iSlot = -1 ) const;
  34. bool GetBool( int iSlot = -1 ) const { return !!GetInt(); }
  35. const char *GetString( int iSlot = -1 ) const;
  36. void SetValue( const char *pValue, int iSlot = -1 );
  37. void SetValue( float flValue, int iSlot = -1 );
  38. void SetValue( int nValue, int iSlot = -1 );
  39. void SetValue( bool bValue, int iSlot = -1 );
  40. const char *GetName( int iSlot = -1 ) const;
  41. const char *GetDefault() const;
  42. const char *GetBaseName() const;
  43. protected:
  44. int GetActiveSplitScreenPlayerSlot() const;
  45. private:
  46. struct cv_t
  47. {
  48. IConVar *m_pConVar;
  49. ConVar *m_pConVarState;
  50. };
  51. cv_t m_Info[ MAX_SPLITSCREEN_CLIENTS ];
  52. };
  53. // In GAMUI we should never use the regular ConVarRef
  54. #define ConVarRef CGameUIConVarRef
  55. FORCEINLINE int CGameUIConVarRef::GetActiveSplitScreenPlayerSlot() const
  56. {
  57. return GetGameUIActiveSplitScreenPlayerSlot();
  58. }
  59. //-----------------------------------------------------------------------------
  60. // Did we find an existing convar of that name?
  61. //-----------------------------------------------------------------------------
  62. FORCEINLINE bool CGameUIConVarRef::IsFlagSet( int nFlags ) const
  63. {
  64. return ( m_Info[ 0 ].m_pConVar->IsFlagSet( nFlags ) != 0 );
  65. }
  66. FORCEINLINE const char *CGameUIConVarRef::GetName( int iSlot ) const
  67. {
  68. int nSlot = iSlot == -1 ? GetActiveSplitScreenPlayerSlot() : iSlot;
  69. return m_Info[ nSlot ].m_pConVar->GetName();
  70. }
  71. FORCEINLINE const char *CGameUIConVarRef::GetBaseName() const
  72. {
  73. return m_Info[ 0 ].m_pConVar->GetBaseName();
  74. }
  75. //-----------------------------------------------------------------------------
  76. // Purpose: Return ConVar value as a float
  77. //-----------------------------------------------------------------------------
  78. FORCEINLINE float CGameUIConVarRef::GetFloat( int iSlot ) const
  79. {
  80. int nSlot = iSlot == -1 ? GetActiveSplitScreenPlayerSlot() : iSlot;
  81. return m_Info[ nSlot ].m_pConVarState->GetFloat();
  82. }
  83. //-----------------------------------------------------------------------------
  84. // Purpose: Return ConVar value as an int
  85. //-----------------------------------------------------------------------------
  86. FORCEINLINE int CGameUIConVarRef::GetInt( int iSlot ) const
  87. {
  88. int nSlot = iSlot == -1 ? GetActiveSplitScreenPlayerSlot() : iSlot;
  89. return m_Info[ nSlot ].m_pConVarState->GetInt();
  90. }
  91. //-----------------------------------------------------------------------------
  92. // Purpose: Return ConVar value as a string, return "" for bogus string pointer, etc.
  93. //-----------------------------------------------------------------------------
  94. FORCEINLINE const char *CGameUIConVarRef::GetString( int iSlot ) const
  95. {
  96. Assert( !IsFlagSet( FCVAR_NEVER_AS_STRING ) );
  97. int nSlot = iSlot == -1 ? GetActiveSplitScreenPlayerSlot() : iSlot;
  98. return m_Info[ nSlot ].m_pConVarState->GetString();
  99. }
  100. FORCEINLINE_CVAR float CGameUIConVarRef::GetMax( int iSlot ) const
  101. {
  102. int nSlot = iSlot == -1 ? GetActiveSplitScreenPlayerSlot() : iSlot;
  103. return m_Info[ nSlot ].m_pConVarState->GetMaxValue();
  104. }
  105. // [jbright] - Convenience function for retrieving the min value of the convar
  106. FORCEINLINE_CVAR float CGameUIConVarRef::GetMin( int iSlot ) const
  107. {
  108. int nSlot = iSlot == -1 ? GetActiveSplitScreenPlayerSlot() : iSlot;
  109. return m_Info[ nSlot ].m_pConVarState->GetMinValue();
  110. }
  111. FORCEINLINE void CGameUIConVarRef::SetValue( const char *pValue, int iSlot )
  112. {
  113. int nSlot = iSlot == -1 ? GetActiveSplitScreenPlayerSlot() : iSlot;
  114. m_Info[ nSlot ].m_pConVar->SetValue( pValue );
  115. }
  116. FORCEINLINE void CGameUIConVarRef::SetValue( float flValue, int iSlot )
  117. {
  118. int nSlot = iSlot == -1 ? GetActiveSplitScreenPlayerSlot() : iSlot;
  119. m_Info[ nSlot ].m_pConVar->SetValue( flValue );
  120. }
  121. FORCEINLINE void CGameUIConVarRef::SetValue( int nValue, int iSlot )
  122. {
  123. int nSlot = iSlot == -1 ? GetActiveSplitScreenPlayerSlot() : iSlot;
  124. m_Info[ nSlot ].m_pConVar->SetValue( nValue );
  125. }
  126. FORCEINLINE void CGameUIConVarRef::SetValue( bool bValue, int iSlot )
  127. {
  128. int nSlot = iSlot == -1 ? GetActiveSplitScreenPlayerSlot() : iSlot;
  129. m_Info[ nSlot ].m_pConVar->SetValue( bValue ? 1 : 0 );
  130. }
  131. FORCEINLINE const char *CGameUIConVarRef::GetDefault() const
  132. {
  133. return m_Info[ 0 ].m_pConVarState->GetDefault();
  134. }
  135. //-----------------------------------------------------------------------------
  136. class CGameUiSetActiveSplitScreenPlayerGuard
  137. {
  138. public:
  139. explicit CGameUiSetActiveSplitScreenPlayerGuard( int slot );
  140. ~CGameUiSetActiveSplitScreenPlayerGuard();
  141. private:
  142. int m_nSaveSlot;
  143. };
  144. #define GAMEUI_ACTIVE_SPLITSCREEN_PLAYER_GUARD( slot ) CGameUiSetActiveSplitScreenPlayerGuard g_UISSGuard( slot );
  145. #endif // GAMEUI_UTIL_H