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.

160 lines
5.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "vgui_controls/ControllerMap.h"
  8. #include "vgui/ISurface.h"
  9. #include "vgui/KeyCode.h"
  10. #include "KeyValues.h"
  11. // memdbgon must be the last include file in a .cpp file!!!
  12. #include "tier0/memdbgon.h"
  13. using namespace vgui;
  14. struct keystring_t
  15. {
  16. int code;
  17. const char *name;
  18. };
  19. static const keystring_t s_ControllerButtons[] = { { KEY_XBUTTON_UP, "KEY_XBUTTON_UP" },
  20. { KEY_XBUTTON_DOWN, "KEY_XBUTTON_DOWN" },
  21. { KEY_XBUTTON_LEFT, "KEY_XBUTTON_LEFT" },
  22. { KEY_XBUTTON_RIGHT, "KEY_XBUTTON_RIGHT" },
  23. { KEY_XBUTTON_START, "KEY_XBUTTON_START" },
  24. { KEY_XBUTTON_BACK, "KEY_XBUTTON_BACK" },
  25. { KEY_XBUTTON_STICK1, "KEY_XBUTTON_STICK1" },
  26. { KEY_XBUTTON_STICK2, "KEY_XBUTTON_STICK2" },
  27. { KEY_XBUTTON_A, "KEY_XBUTTON_A" },
  28. { KEY_XBUTTON_B, "KEY_XBUTTON_B" },
  29. { KEY_XBUTTON_X, "KEY_XBUTTON_X" },
  30. { KEY_XBUTTON_Y, "KEY_XBUTTON_Y" },
  31. { KEY_XBUTTON_LEFT_SHOULDER, "KEY_XBUTTON_LEFT_SHOULDER" },
  32. { KEY_XBUTTON_RIGHT_SHOULDER, "KEY_XBUTTON_RIGHT_SHOULDER" },
  33. { KEY_XBUTTON_LTRIGGER, "KEY_XBUTTON_LTRIGGER" },
  34. { KEY_XBUTTON_RTRIGGER, "KEY_XBUTTON_RTRIGGER" },
  35. { KEY_XSTICK1_UP, "KEY_XSTICK1_UP" },
  36. { KEY_XSTICK1_DOWN, "KEY_XSTICK1_DOWN" },
  37. { KEY_XSTICK1_LEFT, "KEY_XSTICK1_LEFT" },
  38. { KEY_XSTICK1_RIGHT, "KEY_XSTICK1_RIGHT" },
  39. { KEY_XSTICK2_UP, "KEY_XSTICK2_UP" },
  40. { KEY_XSTICK2_DOWN, "KEY_XSTICK2_DOWN" },
  41. { KEY_XSTICK2_LEFT, "KEY_XSTICK2_LEFT" },
  42. { KEY_XSTICK2_RIGHT, "KEY_XSTICK2_RIGHT" } };
  43. //-----------------------------------------------------------------------------
  44. // Purpose: for the UtlMap
  45. //-----------------------------------------------------------------------------
  46. bool lessFunc( const int &lhs, const int &rhs )
  47. {
  48. return lhs < rhs;
  49. }
  50. //-----------------------------------------------------------------------------
  51. // Purpose: converts a button name string to the equivalent keycode
  52. //-----------------------------------------------------------------------------
  53. int StringToButtonCode( const char *name )
  54. {
  55. for ( int i = 0; i < ARRAYSIZE( s_ControllerButtons ); ++i )
  56. {
  57. if ( !Q_stricmp( s_ControllerButtons[i].name, name ) )
  58. return s_ControllerButtons[i].code;
  59. }
  60. return -1;
  61. }
  62. //-----------------------------------------------------------------------------
  63. // Purpose: intercepts the keycode from its parent, and handles it according to
  64. // the button map. If the keycode isn't handled, it gets passed on to the parent.
  65. //-----------------------------------------------------------------------------
  66. void CControllerMap::OnKeyCodeTyped( vgui::KeyCode code )
  67. {
  68. int idx = m_buttonMap.Find( code );
  69. if ( idx != m_buttonMap.InvalidIndex() )
  70. {
  71. GetParent()->OnCommand( m_buttonMap[idx].cmd.String() );
  72. }
  73. else
  74. {
  75. // Disable input before forwarding the message
  76. // so it doesn't feed back here again.
  77. SetKeyBoardInputEnabled( false );
  78. GetParent()->OnKeyCodeTyped( code );
  79. SetKeyBoardInputEnabled( true );
  80. }
  81. }
  82. //-----------------------------------------------------------------------------
  83. // Purpose: constructor
  84. //-----------------------------------------------------------------------------
  85. CControllerMap::CControllerMap( vgui::Panel *parent, const char *name ) : BaseClass( parent, name )
  86. {
  87. m_buttonMap.SetLessFunc( lessFunc );
  88. }
  89. //-----------------------------------------------------------------------------
  90. // Purpose: sets up the button/command bindings
  91. //-----------------------------------------------------------------------------
  92. void CControllerMap::ApplySettings( KeyValues *inResourceData )
  93. {
  94. BaseClass::ApplySettings( inResourceData );
  95. // loop through all the data adding items to the menu
  96. for (KeyValues *dat = inResourceData->GetFirstSubKey(); dat != NULL; dat = dat->GetNextKey())
  97. {
  98. if ( !Q_stricmp( dat->GetName(), "button" ) )
  99. {
  100. const char *buttonName = dat->GetString( "name", "" );
  101. int keycode = StringToButtonCode( buttonName );
  102. if ( keycode != -1 )
  103. {
  104. button_t b;
  105. b.cmd = CUtlSymbol( dat->GetString( "command", "" ) );
  106. // text and icon are optional - their existence means this button
  107. // should be displayed in the footer panel.
  108. const char *helpText = dat->GetString( "text", NULL );
  109. if ( helpText )
  110. {
  111. b.text = CUtlSymbol( helpText );
  112. b.icon = CUtlSymbol( dat->GetString( "icon", NULL ) );
  113. }
  114. m_buttonMap.Insert( keycode, b );
  115. }
  116. }
  117. }
  118. }
  119. //-----------------------------------------------------------------------------
  120. // Purpose: gets the help text for a binding, if it exists
  121. //-----------------------------------------------------------------------------
  122. const char *CControllerMap::GetBindingText( int idx )
  123. {
  124. CUtlSymbol s = m_buttonMap[idx].text;
  125. if ( s.IsValid() )
  126. {
  127. return s.String();
  128. }
  129. return NULL;
  130. }
  131. //-----------------------------------------------------------------------------
  132. // Purpose: gets the icon for a binding, if it exists
  133. //-----------------------------------------------------------------------------
  134. const char *CControllerMap::GetBindingIcon( int idx )
  135. {
  136. CUtlSymbol s = m_buttonMap[idx].icon;
  137. if ( s.IsValid() )
  138. {
  139. return s.String();
  140. }
  141. return NULL;
  142. }
  143. DECLARE_BUILD_FACTORY( CControllerMap );