Advertisement
huutho_96

Input.h

Sep 16th, 2016
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma once
  2. #ifndef INPUT_H
  3. #define INPUT_H
  4. #define WIN32_LEAN_AND_MEAN
  5. #include <d3d9.h>
  6. #include <Windows.h>
  7. #include <windowsx.h>
  8. #include <string>
  9. #include "ERROR.h"
  10. #include <Xinput.h>
  11.  
  12.  
  13. #ifndef HID_USAGE_PAGE_GENERIC
  14. #define HID_USAGE_PAGE_GENERIC         ((USHORT) 0x01)
  15. #endif
  16. #ifndef HID_USAGE_GENERIC_MOUSE
  17. #define HID_USAGE_GENERIC_MOUSE        ((USHORT) 0x02)
  18. #endif
  19.  
  20.  
  21. namespace InputNS
  22. {
  23.     const int KEYS_ARRAY_LENGTH = 256;
  24.  
  25.     const UCHAR KEYS_DOWN = 1;
  26.     const UCHAR KEYS_PRESSED = 2;
  27.     const UCHAR MOUSE = 4;
  28.     const UCHAR TEXT_IN = 8;
  29.     const UCHAR KEYS_MOUSE_TEXT = KEYS_DOWN + KEYS_PRESSED + MOUSE + TEXT_IN;
  30. }
  31.  
  32.  
  33.  
  34. const short GAMEPAD_THUMBSTICK_DEADZONE = (short)(0.20f * 0X7FFF);
  35. const short GAMEPAD_TRIGGER_DEADZONE = 20;
  36. const DWORD MAX_CONTROLLERS = 4;
  37.  
  38.  
  39. const DWORD GAMEPAD_DPAD_UP = 0x0001;
  40. const DWORD GAMEPAD_DPAD_DOWN = 0x0002;
  41. const DWORD GAMEPAD_DPAD_LEFT = 0x0004;
  42. const DWORD GAMEPAD_DPAD_RIGHT = 0x0008;
  43. const DWORD GAMEPAD_START_BUTTON = 0x0010;
  44. const DWORD GAMEPAD_BACK_BUTTON = 0x0020;
  45. const DWORD GAMEPAD_LEFT_THUMB = 0x0040;
  46. const DWORD GAMEPAD_RIGHT_THUMB = 0x0080;
  47. const DWORD GAMEPAD_LEFT_SHOULDER = 0x0100;
  48. const DWORD GAMEPAD_RIGHT_SHOULDER = 0x0200;
  49. const DWORD GAMEPAD_A = 0x1000;
  50. const DWORD GAMEPAD_B = 0x2000;
  51. const DWORD GAMEPAD_X = 0x4000;
  52. const DWORD GAMEPAD_Y = 0x8000;
  53.  
  54.  
  55. struct ControllerState
  56. {
  57.     XINPUT_STATE state;
  58.     XINPUT_VIBRATION vibration;
  59.     float vibrateTimeLeft;
  60.     float vibrateTimeRight;
  61.     bool connected;
  62. };
  63. class Input
  64. {
  65.     bool _keyDown[InputNS::KEYS_ARRAY_LENGTH];
  66.     bool _keyPressed[InputNS::KEYS_ARRAY_LENGTH];
  67.     std::string _textIn;
  68.     char _charIn;
  69.     bool _newLine;
  70.     int _mouseX;
  71.     int _mouseY;
  72.     int _mouseRawX;
  73.     int _mouseRawY;
  74.     RAWINPUTDEVICE _rid[1];
  75.     bool _mouseCaptured;
  76.     bool _mouseLeftButton;
  77.     bool _mouseRightButton;
  78.     bool _mouseMiddleButton;
  79.     bool _mouseX1Button;
  80.     bool _mouseX2Button;
  81.     ControllerState _controllerState[MAX_CONTROLLERS];
  82.  
  83.     short _thumbstickDeadzone;
  84.     short _triggerDeadzone;
  85. public:
  86.     Input();
  87.     ~Input();
  88.     void checkControllers();
  89.     void initialize(HWND hwnd, bool capture);
  90.  
  91.     //key input
  92.     void keyDown(WPARAM w);
  93.     void keyUp(WPARAM w);
  94.     void keyIn(WPARAM w);
  95.     bool isKeyDown(UCHAR w) const;
  96.     bool wasKeyPressed(UCHAR w) const;
  97.     bool anyKeyPressed() const;
  98.     std::string getTextIn() { return _textIn; }
  99.     char getCharIn() { return _charIn; }
  100.     void clearKeyPress(UCHAR w);
  101.     void clearTextIn() { _textIn.clear(); }
  102.     void clearAll();
  103.     void clear(UCHAR what);
  104.  
  105.     //mouse input
  106.     void mouseIn(LPARAM l);
  107.     void mouseRawIn(LPARAM l);
  108.     void setMouseLeftButton(bool b) { _mouseLeftButton = b; }
  109.     void setMouseRightButton(bool b) { _mouseRightButton = b; }
  110.     void setMouseMiddleButton(bool b) { _mouseMiddleButton = b; }
  111.     void setMouseXButton(WPARAM w) { _mouseX1Button = (w & MK_XBUTTON1) ? true : false; _mouseX2Button = (w & MK_XBUTTON2) ? true : false; }
  112.     int getMouseX() const { return _mouseX; }
  113.     int getMouseY() const { return _mouseY; }
  114.     int getMouseRawX() const { return _mouseRawX; }
  115.     int getMouseRawY() const { return _mouseRawY; }
  116.     bool getMouseLeftButton() const { return _mouseLeftButton; }
  117.     bool getMouseRightButton() const { return _mouseRightButton; }
  118.     bool getMouseMiddleButton() const { return _mouseMiddleButton; }
  119.     bool getMouseX1() const { return _mouseX1Button; }
  120.     bool getMouseX2() const { return _mouseX2Button; }
  121.  
  122.     //game pad controller
  123.     void readController();
  124.     const ControllerState* getControllerState(UINT n);
  125.     const WORD getGamepadButtons(UINT n);
  126.  
  127.     bool getGamepadDPadUp(UINT n);
  128.     bool getGamepadDPadDown(UINT n);
  129.     bool getGamepadDPadLeft(UINT n);
  130.     bool getGamepadDPadRight(UINT n);
  131.     bool getGamepadStart(UINT n);
  132.     bool getGamepadBack(UINT n);
  133.     bool getGamepadLeftThumb(UINT n);
  134.     bool getGamepadRightThumb(UINT n);
  135.     bool getGamepadLeftShoulder(UINT n);
  136.     bool getGamepadRightShoulder(UINT n);
  137.     bool getGamepadA(UINT n);
  138.     bool getGamepadB(UINT n);
  139.     bool getGamepadX(UINT n);
  140.     bool getGamepadY(UINT n);
  141.  
  142.     //Gamepad Left
  143.     BYTE getGamepadLeftTrigger(UINT n);
  144.     BYTE getGamepadLeftTriggerUndead(UINT n);
  145.     SHORT getGamepadThumbLX(UINT n);
  146.     SHORT getGamepadThumbLXUndead(UINT n);
  147.     SHORT getGamepadThumbLY(UINT n);
  148.     SHORT getGamepadThumbLYUndead(UINT n);
  149.     void gamePadVibrateLeft(UINT n, WORD speed, float sec);
  150.  
  151.     //Gamepad right
  152.     BYTE getGamepadRightTrigger(UINT n);
  153.     BYTE getGamepadRightTriggerUndead(UINT n);
  154.     SHORT getGamepadThumbRX(UINT n);
  155.     SHORT getGamepadThumbRXUndead(UINT n);
  156.     SHORT getGamepadThumbRY(UINT n);
  157.     SHORT getGamepadThumbRYUndead(UINT n);
  158.     void gamePadVibrateRight(UINT n, WORD speed, float sec);
  159.  
  160.     //
  161.     void vibrateControllers(float frameTime);
  162. };
  163.  
  164.  
  165.  
  166. #endif // !INPUT_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement