Mouse.h 1.8 KB

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