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.

415 lines
13 KiB

  1. //===== Copyright 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //===========================================================================//
  8. #if !defined( INPUT_H )
  9. #define INPUT_H
  10. #ifdef _WIN32
  11. #pragma once
  12. #endif
  13. #include "iinput.h"
  14. #include "mathlib/vector.h"
  15. #include "kbutton.h"
  16. #include "ehandle.h"
  17. #include "inputsystem/AnalogCode.h"
  18. #include "shareddefs.h"
  19. typedef uint32 CRC32_t;
  20. // TrackIR
  21. extern QAngle g_angleCenter;
  22. // TrackIR
  23. //-----------------------------------------------------------------------------
  24. // Purpose:
  25. //-----------------------------------------------------------------------------
  26. class CKeyboardKey
  27. {
  28. public:
  29. // Name for key
  30. char name[ 32 ];
  31. // Pointer to the underlying structure
  32. kbutton_t *pkey;
  33. // Next key in key list.
  34. CKeyboardKey *next;
  35. };
  36. class ConVar;
  37. class CVerifiedUserCmd
  38. {
  39. public:
  40. CUserCmd m_cmd;
  41. CRC32_t m_crc;
  42. };
  43. class CInput : public ::IInput
  44. {
  45. // Interface
  46. public:
  47. CInput( void );
  48. ~CInput( void );
  49. void InitMouse( void );
  50. virtual void Init_All( void );
  51. virtual void Shutdown_All( void );
  52. virtual int GetButtonBits( bool bResetState );
  53. virtual void CreateMove ( int sequence_number, float input_sample_frametime, bool active );
  54. virtual void ExtraMouseSample( float frametime, bool active );
  55. virtual bool WriteUsercmdDeltaToBuffer( int nSlot, bf_write *buf, int from, int to, bool isnewcommand );
  56. virtual void EncodeUserCmdToBuffer( int nSlot, bf_write& buf, int slot );
  57. virtual void DecodeUserCmdFromBuffer( int nSlot, bf_read& buf, int slot );
  58. virtual CUserCmd *GetUserCmd( int nSlot, int sequence_number );
  59. virtual void MakeWeaponSelection( C_BaseCombatWeapon *weapon );
  60. virtual float KeyState( kbutton_t *key );
  61. virtual int KeyEvent( int down, ButtonCode_t keynum, const char *pszCurrentBinding );
  62. virtual kbutton_t *FindKey( const char *name );
  63. virtual void ControllerCommands( void );
  64. virtual void Joystick_Advanced( bool bSilent );
  65. virtual void Joystick_SetSampleTime(float frametime);
  66. virtual float Joystick_GetPitch( void );
  67. virtual float Joystick_GetYaw( void );
  68. virtual void Joystick_Querry( float &forward, float &side, float &pitch, float &yaw );
  69. virtual void Joystick_ForceRecentering( int nStick, bool bSet = true );
  70. virtual void IN_SetSampleTime( float frametime );
  71. virtual void AccumulateMouse( int nSlot );
  72. virtual void ActivateMouse( void );
  73. virtual void DeactivateMouse( void );
  74. virtual void ClearStates( void );
  75. virtual float GetLookSpring( void );
  76. virtual void GetFullscreenMousePos( int *mx, int *my, int *unclampedx = NULL, int *unclampedy = NULL );
  77. virtual void SetFullscreenMousePos( int mx, int my );
  78. virtual void ResetMouse( void );
  79. // virtual bool IsNoClipping( void );
  80. virtual float GetLastForwardMove( void );
  81. virtual void ClearInputButton( int bits );
  82. virtual void CAM_Think( void );
  83. virtual int CAM_IsThirdPerson( int nSlot = -1 );
  84. virtual bool CAM_IsThirdPersonOverview( int nSlot = -1 );
  85. virtual void CAM_GetCameraOffset( Vector& ofs );
  86. virtual void CAM_ToThirdPerson(void);
  87. virtual void CAM_ToFirstPerson(void);
  88. virtual void CAM_ToThirdPersonShoulder(void);
  89. virtual void CAM_ToThirdPersonOverview(void);
  90. virtual void CAM_StartMouseMove(void);
  91. virtual void CAM_EndMouseMove(void);
  92. virtual void CAM_StartDistance(void);
  93. virtual void CAM_EndDistance(void);
  94. virtual int CAM_InterceptingMouse( void );
  95. virtual void CAM_Command( int command );
  96. // orthographic camera info
  97. virtual void CAM_ToOrthographic();
  98. virtual bool CAM_IsOrthographic() const;
  99. virtual void CAM_OrthographicSize( float& w, float& h ) const;
  100. #if defined( HL2_CLIENT_DLL )
  101. // IK back channel info
  102. virtual void AddIKGroundContactInfo( int entindex, float minheight, float maxheight );
  103. #endif
  104. virtual void LevelInit( void );
  105. virtual void CAM_SetCameraThirdData( CameraThirdData_t *pCameraData, const QAngle &vecCameraOffset );
  106. virtual void CAM_CameraThirdThink( void );
  107. virtual void CheckPaused( CUserCmd *cmd );
  108. virtual void CheckSplitScreenMimic( int nSlot, CUserCmd *cmd, CUserCmd *pPlayer0Command );
  109. // Private Implementation
  110. protected:
  111. // Implementation specific initialization
  112. virtual void Init_Camera( void );
  113. void Init_Keyboard( void );
  114. void Init_Mouse( void );
  115. void Shutdown_Keyboard( void );
  116. // Add a named key to the list queryable by the engine
  117. void AddKeyButton( const char *name, kbutton_t *pkb );
  118. // Mouse/keyboard movement input helpers
  119. void ScaleMovements( CUserCmd *cmd );
  120. void ComputeForwardMove( int nSlot, CUserCmd *cmd );
  121. void ComputeUpwardMove( int nSlot, CUserCmd *cmd );
  122. void ComputeSideMove( int nSlot, CUserCmd *cmd );
  123. void AdjustAngles ( int nSlot, float frametime );
  124. void ClampAngles( QAngle& viewangles );
  125. void AdjustPitch( int nSlot, float speed, QAngle& viewangles );
  126. void AdjustYaw( int nSlot, float speed, QAngle& viewangles );
  127. float DetermineKeySpeed( int nSlot, float frametime );
  128. void GetAccumulatedMouseDeltasAndResetAccumulators( int nSlot, float *mx, float *my );
  129. void GetMouseDelta( int nSlot, float inmousex, float inmousey, float *pOutMouseX, float *pOutMouseY );
  130. void ScaleMouse( int nSlot, float *x, float *y );
  131. virtual void ApplyMouse( int nSlot, QAngle& viewangles, CUserCmd *cmd, float mouse_x, float mouse_y );
  132. void MouseMove ( int nSlot, CUserCmd *cmd );
  133. void MotionControllerMove( float frametime, CUserCmd *cmd );
  134. float HandleMotionControllerInputSmoothing( float flDeadZonePct, float val );
  135. // Joystick movement input helpers
  136. void ControllerMove( int nSlot, float frametime, CUserCmd *cmd );
  137. float ScaleAxisValue( const float axisValue, const float axisThreshold );
  138. virtual void JoyStickMove( float frametime, CUserCmd *cmd );
  139. virtual void SteamControllerMove( float frametime, CUserCmd *cmd );
  140. virtual bool ControllerModeActive( void );
  141. virtual bool JoyStickActive();
  142. virtual void JoyStickSampleAxes( float &forward, float &side, float &pitch, float &yaw, bool &bAbsoluteYaw, bool &bAbsolutePitch );
  143. virtual void JoyStickThirdPersonPlatformer( CUserCmd *cmd, float &forward, float &side, float &pitch, float &yaw );
  144. virtual void JoyStickTurn( CUserCmd *cmd, float &yaw, float &pitch, float frametime, bool bAbsoluteYaw, bool bAbsolutePitch );
  145. virtual void JoyStickForwardSideControl( float forward, float side, float &joyForwardMove, float &joySideMove );
  146. virtual void JoyStickApplyMovement( CUserCmd *cmd, float joyForwardMove, float joySideMove );
  147. float ResponseCurveLookDefault( int nSlot, float x, int axis, float otherAxis, float dist, float frametime );
  148. float ResponseCurveLook( int nSlot, int curve, float x, int axis, float otherAxis, float dist, float frametime );
  149. float ResponseCurveLookAccelerated( int nSlot, float x, int axis, float otherAxis, float dist, float frametime );
  150. float ResponseCurveLookPolynomial( int nSlot, float x, int axis, float otherAxis, float dist, float frametime );
  151. float AutoAimDampening( float x, int axis, float dist );
  152. float UpdateAndGetQuickTurnYaw( int nSlot, float lookFrametime, float angle );
  153. // Call this to get the cursor position. The call will be logged in the VCR file if there is one.
  154. void GetMousePos(int &x, int &y);
  155. void SetMousePos(int x, int y);
  156. virtual void GetWindowCenter( int&x, int& y );
  157. // Called once per frame to allow convar overrides to acceleration settings when mouse is active
  158. void CheckMouseAcclerationVars();
  159. void ValidateUserCmd( CUserCmd *usercmd, int sequence_number );
  160. // TrackIR
  161. void TrackIRMove ( float frametime, CUserCmd *cmd );
  162. void Init_TrackIR( void );
  163. void Shutdown_TrackIR( void );
  164. bool m_fTrackIRAvailable;
  165. // TrackIR
  166. // SteamController
  167. void ApplySteamControllerCameraMove( QAngle& viewangles, CUserCmd *cmd, Vector2D vecPosition );
  168. protected:
  169. typedef struct
  170. {
  171. unsigned int AxisFlags;
  172. unsigned int AxisMap;
  173. unsigned int ControlMap;
  174. } joy_axis_t;
  175. void DescribeJoystickAxis( int nJoystick, char const *axis, joy_axis_t *mapping );
  176. char const *DescribeAxis( int index );
  177. enum
  178. {
  179. GAME_AXIS_NONE = 0,
  180. GAME_AXIS_FORWARD,
  181. GAME_AXIS_PITCH,
  182. GAME_AXIS_SIDE,
  183. GAME_AXIS_YAW,
  184. MAX_GAME_AXES
  185. };
  186. enum
  187. {
  188. CAM_COMMAND_NONE = 0,
  189. CAM_COMMAND_TOTHIRDPERSON = 1,
  190. CAM_COMMAND_TOFIRSTPERSON = 2
  191. };
  192. enum
  193. {
  194. MOUSE_ACCEL_THRESHHOLD1 = 0, // if mouse moves > this many mickey's double it
  195. MOUSE_ACCEL_THRESHHOLD2, // if mouse moves > this many mickey's double it a second time
  196. MOUSE_SPEED_FACTOR, // 0 = disabled, 1 = threshold 1 enabled, 2 = threshold 2 enabled
  197. NUM_MOUSE_PARAMS,
  198. };
  199. // Has the mouse been initialized?
  200. bool m_fMouseInitialized;
  201. // Is the mosue active?
  202. bool m_fMouseActive;
  203. // Has the joystick advanced initialization been run?
  204. bool m_fJoystickAdvancedInit;
  205. // Between controller and mouse, what's the primary input
  206. bool m_bControllerMode;
  207. float m_fAccumulatedMouseMove;
  208. float m_lastAutoAimValue;
  209. // Accumulated mouse deltas
  210. struct PerUserInput_t
  211. {
  212. PerUserInput_t()
  213. {
  214. m_flAccumulatedMouseXMovement = 0;
  215. m_flAccumulatedMouseYMovement = 0;
  216. m_flPreviousMouseXPosition = 0;
  217. m_flPreviousMouseYPosition = 0;
  218. m_flRemainingJoystickSampleTime = 0;
  219. m_flKeyboardSampleTime = 0;
  220. m_flSpinFrameTime = 0;
  221. m_flSpinRate = 0;
  222. m_flLastYawAngle = 0;
  223. // Is the 3rd person camera using the mouse?
  224. m_fCameraInterceptingMouse = 0;
  225. // Are we in 3rd person view?
  226. m_fCameraInThirdPerson = 0;
  227. // Should we move view along with mouse?
  228. m_fCameraMovingWithMouse = 0;
  229. // What is the current camera offset from the view origin?
  230. m_vecCameraOffset.Init();
  231. // Is the camera in distance moving mode?
  232. m_fCameraDistanceMove = 0;
  233. // Old and current mouse position readings.
  234. m_nCameraOldX = 0;
  235. m_nCameraOldY = 0;
  236. m_nCameraX = 0;
  237. m_nCameraY = 0;
  238. // orthographic camera settings
  239. m_CameraIsOrthographic = 0;
  240. m_CameraIsThirdPersonOverview = 0;
  241. m_angPreviousViewAngles.Init();
  242. m_angPreviousViewAnglesTilt.Init();
  243. m_flLastForwardMove = 0;
  244. m_pCommands = 0;
  245. m_pVerifiedCommands = NULL;
  246. m_nClearInputState = 0;
  247. m_pCameraThirdData = NULL;
  248. m_nCamCommand = 0;
  249. m_flPreviousJoystickForwardMove = 0;
  250. m_flPreviousJoystickSideMove = 0;
  251. m_flPreviousJoystickYaw = 0;
  252. m_flPreviousJoystickPitch = 0;
  253. m_bPreviousJoystickUseAbsoluteYaw = 0;
  254. m_bPreviousJoystickUseAbsolutePitch = 0;
  255. m_bForceJoystickRecentering[ 0 ] = false;
  256. m_bForceJoystickRecentering[ 1 ] = false;
  257. }
  258. float m_flAccumulatedMouseXMovement;
  259. float m_flAccumulatedMouseYMovement;
  260. float m_flPreviousMouseXPosition;
  261. float m_flPreviousMouseYPosition;
  262. float m_flRemainingJoystickSampleTime;
  263. float m_flKeyboardSampleTime;
  264. float m_flSpinFrameTime;
  265. float m_flSpinRate;
  266. float m_flLastYawAngle;
  267. // Joystick Axis data
  268. joy_axis_t m_rgAxes[ MAX_JOYSTICK_AXES ];
  269. // Is the 3rd person camera using the mouse?
  270. bool m_fCameraInterceptingMouse;
  271. // Are we in 3rd person view?
  272. bool m_fCameraInThirdPerson;
  273. // Should we move view along with mouse?
  274. bool m_fCameraMovingWithMouse;
  275. // What is the current camera offset from the view origin?
  276. Vector m_vecCameraOffset;
  277. // Is the camera in distance moving mode?
  278. bool m_fCameraDistanceMove;
  279. // Old and current mouse position readings.
  280. int m_nCameraOldX;
  281. int m_nCameraOldY;
  282. int m_nCameraX;
  283. int m_nCameraY;
  284. // orthographic camera settings
  285. bool m_CameraIsOrthographic;
  286. // orthographic camera settings
  287. bool m_CameraIsThirdPersonOverview;
  288. QAngle m_angPreviousViewAngles;
  289. QAngle m_angPreviousViewAnglesTilt;
  290. float m_flLastForwardMove;
  291. int m_nClearInputState;
  292. CUserCmd *m_pCommands;
  293. CVerifiedUserCmd *m_pVerifiedCommands;
  294. // Set until polled by CreateMove and cleared
  295. CHandle< C_BaseCombatWeapon > m_hSelectedWeapon;
  296. #if defined( HL2_CLIENT_DLL )
  297. CUtlVector< CEntityGroundContact > m_EntityGroundContact;
  298. #endif
  299. CameraThirdData_t *m_pCameraThirdData;
  300. int m_nCamCommand;
  301. // Cached movement from the previous sample
  302. // Note: To be used when we run out of sample time,
  303. // instead of leaving zeros in CUserCmd's.
  304. float m_flPreviousJoystickForwardMove;
  305. float m_flPreviousJoystickSideMove;
  306. float m_flPreviousJoystickYaw;
  307. float m_flPreviousJoystickPitch;
  308. bool m_bPreviousJoystickUseAbsoluteYaw;
  309. bool m_bPreviousJoystickUseAbsolutePitch;
  310. bool m_bForceJoystickRecentering[ 2 ];
  311. };
  312. PerUserInput_t &GetPerUser( int nSlot = -1 );
  313. const PerUserInput_t &GetPerUser( int nSlot = -1 ) const;
  314. // Flag to restore systemparameters when exiting
  315. bool m_fRestoreSPI;
  316. // Original mouse parameters
  317. int m_rgOrigMouseParms[ NUM_MOUSE_PARAMS ];
  318. // Current mouse parameters.
  319. int m_rgNewMouseParms[ NUM_MOUSE_PARAMS ];
  320. bool m_rgCheckMouseParam[ NUM_MOUSE_PARAMS ];
  321. // Are the parameters valid
  322. bool m_fMouseParmsValid;
  323. // List of queryable keys
  324. CKeyboardKey *m_pKeys;
  325. PerUserInput_t m_PerUser[ MAX_SPLITSCREEN_PLAYERS ];
  326. InputContextHandle_t m_hInputContext;
  327. CThreadFastMutex m_IKContactPointMutex;
  328. };
  329. extern kbutton_t in_strafe;
  330. extern kbutton_t in_speed;
  331. extern kbutton_t in_zoom;
  332. extern kbutton_t in_jlook;
  333. extern kbutton_t in_graph;
  334. extern kbutton_t in_moveleft;
  335. extern kbutton_t in_moveright;
  336. extern kbutton_t in_forward;
  337. extern kbutton_t in_back;
  338. extern kbutton_t in_joyspeed;
  339. extern kbutton_t in_lookspin;
  340. extern class ConVar in_joystick;
  341. extern class ConVar joy_autosprint;
  342. extern void KeyDown( kbutton_t *b, const char *c );
  343. extern void KeyUp( kbutton_t *b, const char *c );
  344. #endif // INPUT_H