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.

112 lines
2.2 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "CvarTextEntry.h"
  8. #include "EngineInterface.h"
  9. #include <vgui/IVGui.h>
  10. #include "IGameUIFuncs.h"
  11. #include "tier1/KeyValues.h"
  12. #include "tier1/convar.h"
  13. // memdbgon must be the last include file in a .cpp file!!!
  14. #include "tier0/memdbgon.h"
  15. using namespace vgui;
  16. static const int MAX_CVAR_TEXT = 64;
  17. CCvarTextEntry::CCvarTextEntry( Panel *parent, const char *panelName, char const *cvarname )
  18. : TextEntry( parent, panelName)
  19. {
  20. m_pszCvarName = cvarname ? strdup( cvarname ) : NULL;
  21. m_pszStartValue[0] = 0;
  22. if ( m_pszCvarName )
  23. {
  24. Reset();
  25. }
  26. AddActionSignalTarget( this );
  27. }
  28. CCvarTextEntry::~CCvarTextEntry()
  29. {
  30. if ( m_pszCvarName )
  31. {
  32. free( m_pszCvarName );
  33. }
  34. }
  35. void CCvarTextEntry::ApplySchemeSettings(IScheme *pScheme)
  36. {
  37. BaseClass::ApplySchemeSettings(pScheme);
  38. if (GetMaximumCharCount() < 0 || GetMaximumCharCount() > MAX_CVAR_TEXT)
  39. {
  40. SetMaximumCharCount(MAX_CVAR_TEXT - 1);
  41. }
  42. }
  43. void CCvarTextEntry::ApplyChanges( bool immediate )
  44. {
  45. if ( !m_pszCvarName )
  46. return;
  47. char szText[ MAX_CVAR_TEXT ];
  48. GetText( szText, MAX_CVAR_TEXT );
  49. if ( !szText[ 0 ] )
  50. return;
  51. if ( immediate )
  52. {
  53. // set immediately - don't wait for the next frame
  54. ConVarRef newCvar( m_pszCvarName );
  55. newCvar.SetValue( szText );
  56. }
  57. else
  58. {
  59. char szCommand[ 256 ];
  60. sprintf( szCommand, "%s \"%s\"\n", m_pszCvarName, szText );
  61. engine->ClientCmd_Unrestricted( szCommand );
  62. }
  63. Q_strncpy( m_pszStartValue, szText, sizeof( m_pszStartValue ) );
  64. }
  65. void CCvarTextEntry::Reset()
  66. {
  67. // char *value = engine->pfnGetCvarString( m_pszCvarName );
  68. ConVarRef var( m_pszCvarName );
  69. if ( !var.IsValid() )
  70. return;
  71. const char *value = var.GetString();
  72. if ( value && value[ 0 ] )
  73. {
  74. SetText( value );
  75. Q_strncpy( m_pszStartValue, value, sizeof( m_pszStartValue ) );
  76. }
  77. }
  78. bool CCvarTextEntry::HasBeenModified()
  79. {
  80. char szText[ MAX_CVAR_TEXT ];
  81. GetText( szText, MAX_CVAR_TEXT );
  82. return stricmp( szText, m_pszStartValue );
  83. }
  84. void CCvarTextEntry::OnTextChanged()
  85. {
  86. if ( !m_pszCvarName )
  87. return;
  88. if (HasBeenModified())
  89. {
  90. PostActionSignal(new KeyValues("ControlModified"));
  91. }
  92. }