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.

499 lines
13 KiB

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