Mouse.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 DetectParsecRunning();
  34. void Reset();
  35. void ResetMouse();
  36. enum
  37. {
  38. BUFFERSIZE = 64, ///< Number of keys cached
  39. DCLICKTIME = 300 ///< Minimum time between key release and key down to make it a double click
  40. };
  41. Renderer * mRenderer;
  42. ComPtr<IDirectInput8> mDI;
  43. ComPtr<IDirectInputDevice8> mMouse;
  44. bool mIsParsecRunning; ///< If the Parsec remote desktop solution is running, if so we can't trust the mouse movement information from DX and it will make the mouse too sensitive
  45. DIMOUSESTATE mMouseState;
  46. bool mMousePosInitialized = false;
  47. POINT mMousePos;
  48. DIDEVICEOBJECTDATA mDOD[BUFFERSIZE];
  49. DWORD mDODLength;
  50. int mTimeLeftButtonLastReleased;
  51. bool mLeftButtonDoubleClicked;
  52. };