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.

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