BRE12
Mouse.h
1 #pragma once
2 
3 #define DIRECTINPUT_VERSION 0x0800
4 #include <cstdint>
5 #include <dinput.h>
6 
7 namespace BRE {
8 class Mouse {
9 public:
10  // Preconditions:
11  // - Create() must be called once
12  static Mouse& Create(IDirectInput8& directInput,
13  const HWND windowHandle) noexcept;
14 
15  // Preconditions:
16  // - Create() method must be called before
17  static Mouse& Get() noexcept;
18 
19  enum MouseButton {
20  MouseButtonsLeft = 0,
21  MouseButtonsRight,
22  MouseButtonsMiddle,
23  MouseButtonsX1
24  };
25 
26  ~Mouse();
27  Mouse(const Mouse&) = delete;
28  const Mouse& operator=(const Mouse&) = delete;
29  Mouse(Mouse&&) = delete;
30  Mouse& operator=(Mouse&&) = delete;
31 
32  void Update();
33 
34  __forceinline const DIMOUSESTATE& GetCurrentState() const
35  {
36  return mCurrentState;
37  }
38  __forceinline const DIMOUSESTATE& GetLastState() const
39  {
40  return mLastState;
41  }
42  __forceinline std::int32_t GetX() const
43  {
44  return mX;
45  }
46  __forceinline std::int32_t GetY() const
47  {
48  return mY;
49  }
50  __forceinline std::int32_t GetWheel() const
51  {
52  return mWheel;
53  }
54  __forceinline bool IsButtonUp(const MouseButton button) const
55  {
56  return (mCurrentState.rgbButtons[button] & 0x80) == 0;
57  }
58  __forceinline bool IsButtonDown(const MouseButton button) const
59  {
60  return (mCurrentState.rgbButtons[button] & 0x80) != 0;
61  }
62  __forceinline bool WasButtonUp(const MouseButton button) const
63  {
64  return (mLastState.rgbButtons[button] & 0x80) == 0;
65  }
66  __forceinline bool WasButtonDown(const MouseButton button) const
67  {
68  return (mLastState.rgbButtons[button] & 0x80) != 0;
69  }
70  __forceinline bool WasButtonPressedThisFrame(const MouseButton button) const
71  {
72  return IsButtonDown(button) && WasButtonUp(button);
73  }
74  __forceinline bool WasButtonReleasedThisFrame(const MouseButton button) const
75  {
76  return IsButtonUp(button) && WasButtonDown(button);
77  }
78  __forceinline bool IsButtonHeldDown(const MouseButton button) const
79  {
80  return IsButtonDown(button) && WasButtonDown(button);
81  }
82 
83 private:
84  explicit Mouse(IDirectInput8& directInput, const HWND windowHandle);
85 
86  IDirectInput8& mDirectInput;
87  LPDIRECTINPUTDEVICE8 mDevice{ nullptr };
88  DIMOUSESTATE mCurrentState{};
89  DIMOUSESTATE mLastState{};
90  std::int32_t mX{ 0 };
91  std::int32_t mY{ 0 };
92  std::int32_t mWheel{ 0 };
93 };
94 }
95 
Definition: Camera.cpp:8
Definition: Mouse.h:8