2
0

Mouse.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. // We're using DX8's DirectInput API
  6. #define DIRECTINPUT_VERSION 0x0800
  7. #include <dinput.h>
  8. class Renderer;
  9. /// Mouse interface class, keeps track of the mouse button state and of the absolute and relative movements of the mouse.
  10. class Mouse
  11. {
  12. public:
  13. /// Constructor
  14. Mouse();
  15. ~Mouse();
  16. /// Initialization / shutdown
  17. bool Initialize(Renderer *inWindow);
  18. void Shutdown();
  19. /// Update the mouse state
  20. void Poll();
  21. int GetX() const { return mMousePos.x; }
  22. int GetY() const { return mMousePos.y; }
  23. int GetDX() const { return mMouseState.lX; }
  24. int GetDY() const { return mMouseState.lY; }
  25. bool IsLeftPressed() const { return (mMouseState.rgbButtons[0] & 0x80) != 0; }
  26. bool IsRightPressed() const { return (mMouseState.rgbButtons[1] & 0x80) != 0; }
  27. bool IsMiddlePressed() const { return (mMouseState.rgbButtons[2] & 0x80) != 0; }
  28. bool IsLeftDoubleClicked() const { return mLeftButtonDoubleClicked; }
  29. void HideCursor();
  30. void ShowCursor();
  31. void SetExclusive(bool inExclusive = true);
  32. private:
  33. void Reset();
  34. void ResetMouse();
  35. enum
  36. {
  37. BUFFERSIZE = 64, ///< Number of keys cached
  38. DCLICKTIME = 300 ///< Minimum time between key release and key down to make it a double click
  39. };
  40. Renderer * mRenderer;
  41. ComPtr<IDirectInput8> mDI;
  42. ComPtr<IDirectInputDevice8> mMouse;
  43. DIMOUSESTATE mMouseState;
  44. bool mMousePosInitialized = false;
  45. POINT mMousePos;
  46. DIDEVICEOBJECTDATA mDOD[BUFFERSIZE];
  47. DWORD mDODLength;
  48. int mTimeLeftButtonLastReleased;
  49. bool mLeftButtonDoubleClicked;
  50. };