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.

173 lines
4.3 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "keyrepeat.h"
  8. #include "inputsystem/buttoncode.h"
  9. #include "tier0/dbg.h"
  10. // memdbgon must be the last include file in a .cpp file
  11. #include "tier0/memdbgon.h"
  12. //#define DEBUG_REPEATS
  13. #ifdef DEBUG_REPEATS
  14. #define DbgRepeat(...) ConMsg( __VA_ARGS__ )
  15. #else
  16. #define DbgRepeat(...)
  17. #endif
  18. ButtonCode_t g_iCodesForAliases[FM_NUM_KEYREPEAT_ALIASES] =
  19. {
  20. KEY_XBUTTON_UP,
  21. KEY_XBUTTON_DOWN,
  22. KEY_XBUTTON_LEFT,
  23. KEY_XBUTTON_RIGHT
  24. };
  25. //-----------------------------------------------------------------------------
  26. // Purpose: Map joystick codes to our internal ones
  27. //-----------------------------------------------------------------------------
  28. static int GetIndexForCode( ButtonCode_t code )
  29. {
  30. ButtonCode_t localCode = GetBaseButtonCode( code );
  31. switch ( localCode )
  32. {
  33. case KEY_XBUTTON_DOWN:
  34. case KEY_XSTICK1_DOWN:
  35. case KEY_XSTICK2_DOWN:
  36. return KR_ALIAS_DOWN; break;
  37. case KEY_XBUTTON_UP:
  38. case KEY_XSTICK1_UP:
  39. case KEY_XSTICK2_UP:
  40. return KR_ALIAS_UP; break;
  41. case KEY_XBUTTON_LEFT:
  42. case KEY_XSTICK1_LEFT:
  43. case KEY_XSTICK2_LEFT:
  44. return KR_ALIAS_LEFT; break;
  45. case KEY_XBUTTON_RIGHT:
  46. case KEY_XSTICK1_RIGHT:
  47. case KEY_XSTICK2_RIGHT:
  48. return KR_ALIAS_RIGHT; break;
  49. default:
  50. break;
  51. }
  52. return -1;
  53. }
  54. //-----------------------------------------------------------------------------
  55. CKeyRepeatHandler::CKeyRepeatHandler()
  56. {
  57. Reset();
  58. for ( int i = 0; i < FM_NUM_KEYREPEAT_ALIASES; i++ )
  59. {
  60. m_flRepeatTimes[i] = 0.16;
  61. }
  62. }
  63. //-----------------------------------------------------------------------------
  64. // Purpose: Clear all state
  65. //-----------------------------------------------------------------------------
  66. void CKeyRepeatHandler::Reset()
  67. {
  68. DbgRepeat( "KeyRepeat: Reset\n" );
  69. memset( m_bAliasDown, 0, sizeof( m_bAliasDown ) );
  70. m_bHaveKeyDown = false;
  71. }
  72. //-----------------------------------------------------------------------------
  73. // Purpose:
  74. //-----------------------------------------------------------------------------
  75. void CKeyRepeatHandler::KeyDown( ButtonCode_t code )
  76. {
  77. int joyStick = GetJoystickForCode( code );
  78. int iIndex = GetIndexForCode(code);
  79. if ( iIndex == -1 )
  80. return;
  81. if ( m_bAliasDown[ joyStick ][ iIndex ] )
  82. return;
  83. DbgRepeat( "KeyRepeat: KeyDown %d(%d)\n", joyStick, iIndex );
  84. Reset();
  85. m_bAliasDown[ joyStick ][ iIndex ] = true;
  86. m_flNextKeyRepeat[ joyStick ] = Plat_FloatTime() + 0.4;
  87. m_bHaveKeyDown = true;
  88. }
  89. //-----------------------------------------------------------------------------
  90. // Purpose:
  91. //-----------------------------------------------------------------------------
  92. void CKeyRepeatHandler::KeyUp( ButtonCode_t code )
  93. {
  94. int joyStick = GetJoystickForCode( code );
  95. int iIndex = GetIndexForCode(code);
  96. if ( iIndex == -1 )
  97. return;
  98. DbgRepeat( "KeyRepeat: KeyUp %d(%d)\n", joyStick, iIndex );
  99. m_bAliasDown[ joyStick ][ iIndex ] = false;
  100. m_bHaveKeyDown = false;
  101. for ( int i = 0; i < FM_NUM_KEYREPEAT_ALIASES; i++ )
  102. {
  103. for ( int j = 0; j < MAX_JOYSTICKS; j++ )
  104. {
  105. if ( m_bAliasDown[ j ][ i ] )
  106. {
  107. m_bHaveKeyDown = true;
  108. break;
  109. }
  110. }
  111. }
  112. }
  113. //-----------------------------------------------------------------------------
  114. // Purpose:
  115. //-----------------------------------------------------------------------------
  116. ButtonCode_t CKeyRepeatHandler::KeyRepeated( void )
  117. {
  118. if ( IsPC() )
  119. return BUTTON_CODE_NONE;
  120. if ( !m_bHaveKeyDown )
  121. return BUTTON_CODE_NONE;
  122. float currentTime = Plat_FloatTime();
  123. for ( int j = 0; j < MAX_JOYSTICKS; j++ )
  124. {
  125. if ( m_flNextKeyRepeat[ j ] < currentTime )
  126. {
  127. for ( int i = 0; i < FM_NUM_KEYREPEAT_ALIASES; i++ )
  128. {
  129. if ( m_bAliasDown[ j ][ i ] )
  130. {
  131. m_flNextKeyRepeat[ j ] = currentTime + m_flRepeatTimes[i];
  132. DbgRepeat( "KeyRepeat: Repeat %d(%d)\n", j, i );
  133. return ButtonCodeToJoystickButtonCode( g_iCodesForAliases[i], j );
  134. }
  135. }
  136. }
  137. }
  138. return BUTTON_CODE_NONE;
  139. }
  140. //-----------------------------------------------------------------------------
  141. // Purpose:
  142. //-----------------------------------------------------------------------------
  143. void CKeyRepeatHandler::SetKeyRepeatTime( ButtonCode_t code, float flRepeat )
  144. {
  145. int iIndex = GetIndexForCode(code);
  146. Assert( iIndex != -1 );
  147. m_flRepeatTimes[ iIndex ] = flRepeat;
  148. }