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.

462 lines
12 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //===========================================================================//
  7. #ifndef BUTTONCODE_H
  8. #define BUTTONCODE_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "inputsystem/InputEnums.h"
  13. //-----------------------------------------------------------------------------
  14. // Button enum. "Buttons" are binary-state input devices (mouse buttons, keyboard keys)
  15. //-----------------------------------------------------------------------------
  16. enum
  17. {
  18. JOYSTICK_MAX_BUTTON_COUNT = 32,
  19. JOYSTICK_POV_BUTTON_COUNT = 4,
  20. JOYSTICK_AXIS_BUTTON_COUNT = MAX_JOYSTICK_AXES * 2,
  21. };
  22. #define JOYSTICK_BUTTON_INTERNAL( _joystick, _button ) ( JOYSTICK_FIRST_BUTTON + ((_joystick) * JOYSTICK_MAX_BUTTON_COUNT) + (_button) )
  23. #define JOYSTICK_POV_BUTTON_INTERNAL( _joystick, _button ) ( JOYSTICK_FIRST_POV_BUTTON + ((_joystick) * JOYSTICK_POV_BUTTON_COUNT) + (_button) )
  24. #define JOYSTICK_AXIS_BUTTON_INTERNAL( _joystick, _button ) ( JOYSTICK_FIRST_AXIS_BUTTON + ((_joystick) * JOYSTICK_AXIS_BUTTON_COUNT) + (_button) )
  25. #define JOYSTICK_BUTTON( _joystick, _button ) ( (ButtonCode_t)JOYSTICK_BUTTON_INTERNAL( _joystick, _button ) )
  26. #define JOYSTICK_POV_BUTTON( _joystick, _button ) ( (ButtonCode_t)JOYSTICK_POV_BUTTON_INTERNAL( _joystick, _button ) )
  27. #define JOYSTICK_AXIS_BUTTON( _joystick, _button ) ( (ButtonCode_t)JOYSTICK_AXIS_BUTTON_INTERNAL( _joystick, _button ) )
  28. //-----------------------------------------------------------------------------
  29. // Button enum. "Buttons" are binary-state input devices (mouse buttons, keyboard keys)
  30. //-----------------------------------------------------------------------------
  31. enum
  32. {
  33. STEAMCONTROLLER_MAX_BUTTON_COUNT = SK_MAX_KEYS - 1,
  34. STEAMCONTROLLER_AXIS_BUTTON_COUNT = MAX_STEAMPADAXIS * 2,
  35. };
  36. #define STEAMCONTROLLER_BUTTON_INTERNAL( _joystick, _button ) ( STEAMCONTROLLER_FIRST_BUTTON + ((_joystick) * STEAMCONTROLLER_MAX_BUTTON_COUNT) + (_button) )
  37. #define STEAMCONTROLLER_AXIS_BUTTON_INTERNAL( _joystick, _button ) ( STEAMCONTROLLER_FIRST_AXIS_BUTTON + ((_joystick) * STEAMCONTROLLER_AXIS_BUTTON_COUNT) + (_button) )
  38. #define STEAMCONTROLLER_BUTTON( _joystick, _button ) ( (ButtonCode_t)STEAMCONTROLLER_BUTTON_INTERNAL( _joystick, _button ) )
  39. #define STEAMCONTROLLER_AXIS_BUTTON( _joystick, _button ) ( (ButtonCode_t)STEAMCONTROLLER_AXIS_BUTTON_INTERNAL( _joystick, _button ) )
  40. enum ButtonCode_t
  41. {
  42. BUTTON_CODE_INVALID = -1,
  43. BUTTON_CODE_NONE = 0,
  44. KEY_FIRST = 0,
  45. KEY_NONE = KEY_FIRST,
  46. KEY_0,
  47. KEY_1,
  48. KEY_2,
  49. KEY_3,
  50. KEY_4,
  51. KEY_5,
  52. KEY_6,
  53. KEY_7,
  54. KEY_8,
  55. KEY_9,
  56. KEY_A,
  57. KEY_B,
  58. KEY_C,
  59. KEY_D,
  60. KEY_E,
  61. KEY_F,
  62. KEY_G,
  63. KEY_H,
  64. KEY_I,
  65. KEY_J,
  66. KEY_K,
  67. KEY_L,
  68. KEY_M,
  69. KEY_N,
  70. KEY_O,
  71. KEY_P,
  72. KEY_Q,
  73. KEY_R,
  74. KEY_S,
  75. KEY_T,
  76. KEY_U,
  77. KEY_V,
  78. KEY_W,
  79. KEY_X,
  80. KEY_Y,
  81. KEY_Z,
  82. KEY_PAD_0,
  83. KEY_PAD_1,
  84. KEY_PAD_2,
  85. KEY_PAD_3,
  86. KEY_PAD_4,
  87. KEY_PAD_5,
  88. KEY_PAD_6,
  89. KEY_PAD_7,
  90. KEY_PAD_8,
  91. KEY_PAD_9,
  92. KEY_PAD_DIVIDE,
  93. KEY_PAD_MULTIPLY,
  94. KEY_PAD_MINUS,
  95. KEY_PAD_PLUS,
  96. KEY_PAD_ENTER,
  97. KEY_PAD_DECIMAL,
  98. KEY_LBRACKET,
  99. KEY_RBRACKET,
  100. KEY_SEMICOLON,
  101. KEY_APOSTROPHE,
  102. KEY_BACKQUOTE,
  103. KEY_COMMA,
  104. KEY_PERIOD,
  105. KEY_SLASH,
  106. KEY_BACKSLASH,
  107. KEY_MINUS,
  108. KEY_EQUAL,
  109. KEY_ENTER,
  110. KEY_SPACE,
  111. KEY_BACKSPACE,
  112. KEY_TAB,
  113. KEY_CAPSLOCK,
  114. KEY_NUMLOCK,
  115. KEY_ESCAPE,
  116. KEY_SCROLLLOCK,
  117. KEY_INSERT,
  118. KEY_DELETE,
  119. KEY_HOME,
  120. KEY_END,
  121. KEY_PAGEUP,
  122. KEY_PAGEDOWN,
  123. KEY_BREAK,
  124. KEY_LSHIFT,
  125. KEY_RSHIFT,
  126. KEY_LALT,
  127. KEY_RALT,
  128. KEY_LCONTROL,
  129. KEY_RCONTROL,
  130. KEY_LWIN,
  131. KEY_RWIN,
  132. KEY_APP,
  133. KEY_UP,
  134. KEY_LEFT,
  135. KEY_DOWN,
  136. KEY_RIGHT,
  137. KEY_F1,
  138. KEY_F2,
  139. KEY_F3,
  140. KEY_F4,
  141. KEY_F5,
  142. KEY_F6,
  143. KEY_F7,
  144. KEY_F8,
  145. KEY_F9,
  146. KEY_F10,
  147. KEY_F11,
  148. KEY_F12,
  149. KEY_CAPSLOCKTOGGLE,
  150. KEY_NUMLOCKTOGGLE,
  151. KEY_SCROLLLOCKTOGGLE,
  152. KEY_LAST = KEY_SCROLLLOCKTOGGLE,
  153. KEY_COUNT = KEY_LAST - KEY_FIRST + 1,
  154. // Mouse
  155. MOUSE_FIRST = KEY_LAST + 1,
  156. MOUSE_LEFT = MOUSE_FIRST,
  157. MOUSE_RIGHT,
  158. MOUSE_MIDDLE,
  159. MOUSE_4,
  160. MOUSE_5,
  161. MOUSE_WHEEL_UP, // A fake button which is 'pressed' and 'released' when the wheel is moved up
  162. MOUSE_WHEEL_DOWN, // A fake button which is 'pressed' and 'released' when the wheel is moved down
  163. MOUSE_LAST = MOUSE_WHEEL_DOWN,
  164. MOUSE_COUNT = MOUSE_LAST - MOUSE_FIRST + 1,
  165. // Joystick
  166. JOYSTICK_FIRST = MOUSE_LAST + 1,
  167. JOYSTICK_FIRST_BUTTON = JOYSTICK_FIRST,
  168. JOYSTICK_LAST_BUTTON = JOYSTICK_BUTTON_INTERNAL( MAX_JOYSTICKS-1, JOYSTICK_MAX_BUTTON_COUNT-1 ),
  169. JOYSTICK_FIRST_POV_BUTTON,
  170. JOYSTICK_LAST_POV_BUTTON = JOYSTICK_POV_BUTTON_INTERNAL( MAX_JOYSTICKS-1, JOYSTICK_POV_BUTTON_COUNT-1 ),
  171. JOYSTICK_FIRST_AXIS_BUTTON,
  172. JOYSTICK_LAST_AXIS_BUTTON = JOYSTICK_AXIS_BUTTON_INTERNAL( MAX_JOYSTICKS-1, JOYSTICK_AXIS_BUTTON_COUNT-1 ),
  173. JOYSTICK_LAST = JOYSTICK_LAST_AXIS_BUTTON,
  174. STEAMCONTROLLER_FIRST = JOYSTICK_LAST + 1,
  175. STEAMCONTROLLER_FIRST_BUTTON = STEAMCONTROLLER_FIRST,
  176. STEAMCONTROLLER_LAST_BUTTON = STEAMCONTROLLER_BUTTON_INTERNAL( MAX_STEAM_CONTROLLERS-1, STEAMCONTROLLER_MAX_BUTTON_COUNT-1 ),
  177. STEAMCONTROLLER_FIRST_AXIS_BUTTON,
  178. STEAMCONTROLLER_LAST_AXIS_BUTTON = STEAMCONTROLLER_AXIS_BUTTON_INTERNAL( MAX_STEAM_CONTROLLERS-1, STEAMCONTROLLER_AXIS_BUTTON_COUNT-1 ),
  179. STEAMCONTROLLER_LAST = STEAMCONTROLLER_LAST_AXIS_BUTTON,
  180. BUTTON_CODE_LAST,
  181. BUTTON_CODE_COUNT = BUTTON_CODE_LAST - KEY_FIRST + 1,
  182. // Helpers for XBox 360
  183. KEY_XBUTTON_UP = JOYSTICK_FIRST_POV_BUTTON, // POV buttons
  184. KEY_XBUTTON_RIGHT,
  185. KEY_XBUTTON_DOWN,
  186. KEY_XBUTTON_LEFT,
  187. KEY_XBUTTON_A = JOYSTICK_FIRST_BUTTON, // Buttons
  188. KEY_XBUTTON_B,
  189. KEY_XBUTTON_X,
  190. KEY_XBUTTON_Y,
  191. KEY_XBUTTON_LEFT_SHOULDER,
  192. KEY_XBUTTON_RIGHT_SHOULDER,
  193. KEY_XBUTTON_BACK,
  194. KEY_XBUTTON_START,
  195. KEY_XBUTTON_STICK1,
  196. KEY_XBUTTON_STICK2,
  197. KEY_XBUTTON_INACTIVE_START,
  198. KEY_XBUTTON_FIREMODE_SELECTOR_1,
  199. KEY_XBUTTON_FIREMODE_SELECTOR_2,
  200. KEY_XBUTTON_FIREMODE_SELECTOR_3,
  201. KEY_XBUTTON_RELOAD,
  202. KEY_XBUTTON_TRIGGER,
  203. KEY_XBUTTON_PUMP_ACTION,
  204. KEY_XBUTTON_ROLL_RIGHT,
  205. KEY_XBUTTON_ROLL_LEFT,
  206. KEY_XSTICK1_RIGHT = JOYSTICK_FIRST_AXIS_BUTTON, // XAXIS POSITIVE
  207. KEY_XSTICK1_LEFT, // XAXIS NEGATIVE
  208. KEY_XSTICK1_DOWN, // YAXIS POSITIVE
  209. KEY_XSTICK1_UP, // YAXIS NEGATIVE
  210. KEY_XBUTTON_LTRIGGER, // ZAXIS POSITIVE
  211. KEY_XBUTTON_RTRIGGER, // ZAXIS NEGATIVE
  212. KEY_XSTICK2_RIGHT, // UAXIS POSITIVE
  213. KEY_XSTICK2_LEFT, // UAXIS NEGATIVE
  214. KEY_XSTICK2_DOWN, // VAXIS POSITIVE
  215. KEY_XSTICK2_UP, // VAXIS NEGATIVE
  216. // Helpers for Steam Controller
  217. STEAMCONTROLLER_A = STEAMCONTROLLER_FIRST_BUTTON,
  218. STEAMCONTROLLER_B,
  219. STEAMCONTROLLER_X,
  220. STEAMCONTROLLER_Y,
  221. STEAMCONTROLLER_DPAD_UP,
  222. STEAMCONTROLLER_DPAD_RIGHT,
  223. STEAMCONTROLLER_DPAD_DOWN,
  224. STEAMCONTROLLER_DPAD_LEFT,
  225. STEAMCONTROLLER_LEFT_BUMPER,
  226. STEAMCONTROLLER_RIGHT_BUMPER,
  227. STEAMCONTROLLER_LEFT_TRIGGER,
  228. STEAMCONTROLLER_RIGHT_TRIGGER,
  229. STEAMCONTROLLER_LEFT_GRIP,
  230. STEAMCONTROLLER_RIGHT_GRIP,
  231. STEAMCONTROLLER_LEFT_PAD_FINGERDOWN,
  232. STEAMCONTROLLER_RIGHT_PAD_FINGERDOWN,
  233. STEAMCONTROLLER_LEFT_PAD_CLICK,
  234. STEAMCONTROLLER_RIGHT_PAD_CLICK,
  235. STEAMCONTROLLER_LEFT_PAD_UP,
  236. STEAMCONTROLLER_LEFT_PAD_RIGHT,
  237. STEAMCONTROLLER_LEFT_PAD_DOWN,
  238. STEAMCONTROLLER_LEFT_PAD_LEFT,
  239. STEAMCONTROLLER_RIGHT_PAD_UP,
  240. STEAMCONTROLLER_RIGHT_PAD_RIGHT,
  241. STEAMCONTROLLER_RIGHT_PAD_DOWN,
  242. STEAMCONTROLLER_RIGHT_PAD_LEFT,
  243. STEAMCONTROLLER_SELECT,
  244. STEAMCONTROLLER_START,
  245. STEAMCONTROLLER_STEAM,
  246. STEAMCONTROLLER_INACTIVE_START,
  247. };
  248. inline bool IsAlpha( ButtonCode_t code )
  249. {
  250. return ( code >= KEY_A ) && ( code <= KEY_Z );
  251. }
  252. inline bool IsAlphaNumeric( ButtonCode_t code )
  253. {
  254. return ( code >= KEY_0 ) && ( code <= KEY_Z );
  255. }
  256. inline bool IsSpace( ButtonCode_t code )
  257. {
  258. return ( code == KEY_ENTER ) || ( code == KEY_TAB ) || ( code == KEY_SPACE );
  259. }
  260. inline bool IsKeypad( ButtonCode_t code )
  261. {
  262. return ( code >= MOUSE_FIRST ) && ( code <= KEY_PAD_DECIMAL );
  263. }
  264. inline bool IsPunctuation( ButtonCode_t code )
  265. {
  266. return ( code >= KEY_0 ) && ( code <= KEY_SPACE ) && !IsAlphaNumeric( code ) && !IsSpace( code ) && !IsKeypad( code );
  267. }
  268. inline bool IsKeyCode( ButtonCode_t code )
  269. {
  270. return ( code >= KEY_FIRST ) && ( code <= KEY_LAST );
  271. }
  272. inline bool IsMouseCode( ButtonCode_t code )
  273. {
  274. return ( code >= MOUSE_FIRST ) && ( code <= MOUSE_LAST );
  275. }
  276. inline bool IsJoystickCode( ButtonCode_t code )
  277. {
  278. return ( ( code >= JOYSTICK_FIRST ) && ( code <= JOYSTICK_LAST ) ) ||
  279. ( ( code >= STEAMCONTROLLER_FIRST ) && ( code <= STEAMCONTROLLER_LAST ) );
  280. }
  281. inline bool IsJoystickButtonCode( ButtonCode_t code )
  282. {
  283. return ( code >= JOYSTICK_FIRST_BUTTON ) && ( code <= JOYSTICK_LAST_BUTTON );
  284. }
  285. inline bool IsJoystickPOVCode( ButtonCode_t code )
  286. {
  287. return ( code >= JOYSTICK_FIRST_POV_BUTTON ) && ( code <= JOYSTICK_LAST_POV_BUTTON );
  288. }
  289. inline bool IsJoystickAxisCode( ButtonCode_t code )
  290. {
  291. return ( code >= JOYSTICK_FIRST_AXIS_BUTTON ) && ( code <= JOYSTICK_LAST_AXIS_BUTTON );
  292. }
  293. inline bool IsSteamControllerCode( ButtonCode_t code )
  294. {
  295. return ( ( code >= STEAMCONTROLLER_FIRST ) && ( code <= STEAMCONTROLLER_LAST ) );
  296. }
  297. inline bool IsSteamControllerButtonCode( ButtonCode_t code )
  298. {
  299. return ( code >= STEAMCONTROLLER_FIRST_BUTTON ) && ( code <= STEAMCONTROLLER_LAST_BUTTON );
  300. }
  301. inline bool IsSteamControllerAxisCode( ButtonCode_t code )
  302. {
  303. return ( code >= STEAMCONTROLLER_FIRST_AXIS_BUTTON ) && ( code <= STEAMCONTROLLER_LAST_AXIS_BUTTON );
  304. }
  305. inline ButtonCode_t GetBaseButtonCode( ButtonCode_t code )
  306. {
  307. if ( IsJoystickButtonCode( code ) )
  308. {
  309. int offset = ( code - JOYSTICK_FIRST_BUTTON ) % JOYSTICK_MAX_BUTTON_COUNT;
  310. return (ButtonCode_t)( JOYSTICK_FIRST_BUTTON + offset );
  311. }
  312. if ( IsJoystickPOVCode( code ) )
  313. {
  314. int offset = ( code - JOYSTICK_FIRST_POV_BUTTON ) % JOYSTICK_POV_BUTTON_COUNT;
  315. return (ButtonCode_t)( JOYSTICK_FIRST_POV_BUTTON + offset );
  316. }
  317. if ( IsJoystickAxisCode( code ) )
  318. {
  319. int offset = ( code - JOYSTICK_FIRST_AXIS_BUTTON ) % JOYSTICK_AXIS_BUTTON_COUNT;
  320. return (ButtonCode_t)( JOYSTICK_FIRST_AXIS_BUTTON + offset );
  321. }
  322. if ( IsSteamControllerButtonCode( code ) )
  323. {
  324. int offset = ( code - STEAMCONTROLLER_FIRST_BUTTON ) % STEAMCONTROLLER_MAX_BUTTON_COUNT;
  325. return (ButtonCode_t)( STEAMCONTROLLER_FIRST_BUTTON + offset );
  326. }
  327. if ( IsSteamControllerAxisCode( code ) )
  328. {
  329. int offset = ( code - STEAMCONTROLLER_FIRST_AXIS_BUTTON ) % STEAMCONTROLLER_AXIS_BUTTON_COUNT;
  330. return (ButtonCode_t)( STEAMCONTROLLER_FIRST_AXIS_BUTTON + offset );
  331. }
  332. return code;
  333. }
  334. inline int GetJoystickForCode( ButtonCode_t code )
  335. {
  336. if ( !IsJoystickCode( code ) )
  337. return 0;
  338. if ( IsJoystickButtonCode( code ) )
  339. {
  340. int offset = ( code - JOYSTICK_FIRST_BUTTON ) / JOYSTICK_MAX_BUTTON_COUNT;
  341. return offset;
  342. }
  343. if ( IsJoystickPOVCode( code ) )
  344. {
  345. int offset = ( code - JOYSTICK_FIRST_POV_BUTTON ) / JOYSTICK_POV_BUTTON_COUNT;
  346. return offset;
  347. }
  348. if ( IsJoystickAxisCode( code ) )
  349. {
  350. int offset = ( code - JOYSTICK_FIRST_AXIS_BUTTON ) / JOYSTICK_AXIS_BUTTON_COUNT;
  351. return offset;
  352. }
  353. if ( IsSteamControllerButtonCode( code ) )
  354. {
  355. int offset = ( code - STEAMCONTROLLER_FIRST_BUTTON ) / STEAMCONTROLLER_MAX_BUTTON_COUNT;
  356. return offset;
  357. }
  358. if ( IsSteamControllerAxisCode( code ) )
  359. {
  360. int offset = ( code - STEAMCONTROLLER_FIRST_AXIS_BUTTON ) / STEAMCONTROLLER_AXIS_BUTTON_COUNT;
  361. return offset;
  362. }
  363. return 0;
  364. }
  365. inline ButtonCode_t ButtonCodeToJoystickButtonCode( ButtonCode_t code, int nDesiredJoystick )
  366. {
  367. if ( !IsJoystickCode( code ) || nDesiredJoystick == 0 )
  368. return code;
  369. nDesiredJoystick = clamp( nDesiredJoystick, 0, MAX_JOYSTICKS - 1 );
  370. code = GetBaseButtonCode( code );
  371. // Now upsample it
  372. if ( IsJoystickButtonCode( code ) )
  373. {
  374. int nOffset = code - JOYSTICK_FIRST_BUTTON;
  375. return JOYSTICK_BUTTON( nDesiredJoystick, nOffset );
  376. }
  377. if ( IsJoystickPOVCode( code ) )
  378. {
  379. int nOffset = code - JOYSTICK_FIRST_POV_BUTTON;
  380. return JOYSTICK_POV_BUTTON( nDesiredJoystick, nOffset );
  381. }
  382. if ( IsJoystickAxisCode( code ) )
  383. {
  384. int nOffset = code - JOYSTICK_FIRST_AXIS_BUTTON;
  385. return JOYSTICK_AXIS_BUTTON( nDesiredJoystick, nOffset );
  386. }
  387. if ( IsSteamControllerButtonCode( code ) )
  388. {
  389. int nOffset = code - STEAMCONTROLLER_FIRST_BUTTON;
  390. return STEAMCONTROLLER_BUTTON( nDesiredJoystick, nOffset );
  391. }
  392. if ( IsJoystickAxisCode( code ) )
  393. {
  394. int nOffset = code - STEAMCONTROLLER_FIRST_AXIS_BUTTON;
  395. return STEAMCONTROLLER_AXIS_BUTTON( nDesiredJoystick, nOffset );
  396. }
  397. return code;
  398. }
  399. #endif // BUTTONCODE_H