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.

143 lines
3.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //===========================================================================//
  8. #include "tier2/keybindings.h"
  9. #include "tier2/tier2.h"
  10. #include "inputsystem/iinputsystem.h"
  11. #include "tier1/utlbuffer.h"
  12. // memdbgon must be the last include file in a .cpp file!!!
  13. #include "tier0/memdbgon.h"
  14. //-----------------------------------------------------------------------------
  15. // Set a key binding
  16. //-----------------------------------------------------------------------------
  17. void CKeyBindings::SetBinding( ButtonCode_t code, const char *pBinding )
  18. {
  19. if ( code == BUTTON_CODE_INVALID || code == KEY_NONE )
  20. return;
  21. // free old bindings
  22. if ( !m_KeyInfo[code].IsEmpty() )
  23. {
  24. // Exactly the same, don't re-bind and fragment memory
  25. if ( !Q_stricmp( m_KeyInfo[code], pBinding ) )
  26. return;
  27. }
  28. // allocate memory for new binding
  29. m_KeyInfo[code] = pBinding;
  30. }
  31. void CKeyBindings::SetBinding( const char *pButtonName, const char *pBinding )
  32. {
  33. ButtonCode_t code = g_pInputSystem->StringToButtonCode( pButtonName );
  34. SetBinding( code, pBinding );
  35. }
  36. void CKeyBindings::Unbind( ButtonCode_t code )
  37. {
  38. if ( code != KEY_NONE && code != BUTTON_CODE_INVALID )
  39. {
  40. m_KeyInfo[code] = "";
  41. }
  42. }
  43. void CKeyBindings::Unbind( const char *pButtonName )
  44. {
  45. ButtonCode_t code = g_pInputSystem->StringToButtonCode( pButtonName );
  46. Unbind( code );
  47. }
  48. void CKeyBindings::UnbindAll()
  49. {
  50. for ( int i = 0; i < BUTTON_CODE_LAST; i++ )
  51. {
  52. m_KeyInfo[i] = "";
  53. }
  54. }
  55. //-----------------------------------------------------------------------------
  56. // Count number of lines of bindings we'll be writing
  57. //-----------------------------------------------------------------------------
  58. int CKeyBindings::GetBindingCount( ) const
  59. {
  60. int nCount = 0;
  61. for ( int i = 0; i < BUTTON_CODE_LAST; i++ )
  62. {
  63. if ( !m_KeyInfo[i].IsEmpty() )
  64. {
  65. nCount++;
  66. }
  67. }
  68. return nCount;
  69. }
  70. //-----------------------------------------------------------------------------
  71. // Writes lines containing "bind key value"
  72. //-----------------------------------------------------------------------------
  73. void CKeyBindings::WriteBindings( CUtlBuffer &buf )
  74. {
  75. for ( int i = 0; i < BUTTON_CODE_LAST; i++ )
  76. {
  77. if ( !m_KeyInfo[i].IsEmpty() )
  78. {
  79. const char *pButtonCode = g_pInputSystem->ButtonCodeToString( (ButtonCode_t)i );
  80. buf.Printf( "bind \"%s\" \"%s\"\n", pButtonCode, m_KeyInfo[i].Get() );
  81. }
  82. }
  83. }
  84. //-----------------------------------------------------------------------------
  85. // Returns the keyname to which a binding string is bound. E.g., if
  86. // TAB is bound to +use then searching for +use will return "TAB"
  87. //-----------------------------------------------------------------------------
  88. const char *CKeyBindings::ButtonNameForBinding( const char *pBinding )
  89. {
  90. const char *pBind = pBinding;
  91. if ( pBinding[0] == '+' )
  92. {
  93. ++pBind;
  94. }
  95. for ( int i = 0; i < BUTTON_CODE_LAST; i++ )
  96. {
  97. if ( m_KeyInfo[i].IsEmpty() )
  98. continue;
  99. if ( m_KeyInfo[i][0] == '+' )
  100. {
  101. if ( !Q_stricmp( &m_KeyInfo[i].Get()[1], pBind ) )
  102. return g_pInputSystem->ButtonCodeToString( (ButtonCode_t)i );
  103. }
  104. else
  105. {
  106. if ( !Q_stricmp( m_KeyInfo[i], pBind ) )
  107. return g_pInputSystem->ButtonCodeToString( (ButtonCode_t)i );
  108. }
  109. }
  110. return NULL;
  111. }
  112. const char *CKeyBindings::GetBindingForButton( ButtonCode_t code )
  113. {
  114. if ( m_KeyInfo[code].IsEmpty() )
  115. return NULL;
  116. return m_KeyInfo[ code ];
  117. }