Sleeping Wombat GUI  0.100
swGUI
MouseState.h
Go to the documentation of this file.
1 #pragma once
2 
9 #include "KeyState.h"
10 #include "InputDeviceEvent.h"
11 
12 
13 namespace sw {
14 namespace input
15 {
16 
21 
22 
23 
28 {
29 private:
30 
31  float m_axes[ 4 ];
34 
35 public:
36  MouseState();
37  ~MouseState();
38 
39  const float* GetAxesState () const { return m_axes; }
40  const KeyState* GetButtonsState () const { return m_buttons; }
41 
42  uint16 GetPositionX () const { return m_position[ 0 ]; }
43  uint16 GetPositionY () const { return m_position[ 1 ]; }
44 
45  void SetPosition ( short X, short Y );
46 
47  float WheelDelta () const { m_axes[ Mouse::PhysicalAxes::WHEEL ]; }
48 
49  const KeyState* LeftButton () const;
50  const KeyState* RightButton () const;
51  const KeyState* MiddleButton () const;
52  const KeyState* XButton1 () const;
53  const KeyState* XButton2 () const;
54 
55  const KeyState& operator[] ( Mouse::PhysicalButtons button ) const { return m_buttons[ button ]; }
56 
59  void RemoveEvents ();
61 
62 
64  void ApplyEvent ( const ButtonEvent& event );
65 
67  void ApplyEvent ( const AxisEvent& event );
68 
70  void ApplyEvent ( const CursorEvent& event );
71 
73  void ApplyEvent ( const DeviceEvent& event );
74 };
75 
78 {
79  for( auto& val : m_axes )
80  val = 0.0f;
81  for( auto& val : m_buttons )
82  val = 0;
83 }
84 
87 {}
88 
92 inline void MouseState::SetPosition ( short X, short Y )
93 {
94  m_position[ 0 ] = X;
95  m_position[ 1 ] = Y;
96 }
97 
100 {
101  for( int i = 0; i < MOUSE_STATE_MAX_NUM_BUTTONS; ++i )
102  m_buttons[ i ].HoldState();
103 }
104 
105 // ================================ //
106 //
107 inline void MouseState::ApplyEvent ( const ButtonEvent& event )
108 {
109  KeyState& state = m_buttons[ event.Button ];
110  state = event.State.IsPressed();
111 }
112 
113 // ================================ //
114 //
115 inline void MouseState::ApplyEvent ( const AxisEvent& event )
116 {
117  float& axis = m_axes[ event.Axis ];
118  axis += event.Delta;
119 }
120 
121 // ================================ //
122 //
123 inline void MouseState::ApplyEvent ( const CursorEvent& event )
124 {
125  m_position[ 0 ] += event.OffsetX;
126  m_position[ 1 ] += event.OffsetY;
127 }
128 
129 // ================================ //
130 //
131 inline void MouseState::ApplyEvent ( const DeviceEvent& event )
132 {
133  switch( event.Type )
134  {
136  ApplyEvent( event.Button );
137  break;
139  ApplyEvent( event.Axis );
140  break;
142  ApplyEvent( event.Cursor );
143  break;
144  default:
145  break;
146  }
147 }
148 
149 
150 //====================================================================================//
151 // Helper accessor
152 //====================================================================================//
153 
154 // ================================ //
155 //
156 inline const KeyState* MouseState::LeftButton() const
157 {
158  return &m_buttons[ Mouse::LEFT_BUTTON ];
159 }
160 
161 // ================================ //
162 //
163 inline const KeyState* MouseState::RightButton() const
164 {
165  return &m_buttons[ Mouse::RIGHT_BUTTON ];
166 }
167 
168 // ================================ //
169 //
170 inline const KeyState* MouseState::MiddleButton() const
171 {
172  return &m_buttons[ Mouse::MIDDLE_BUTTON ];
173 }
174 
175 // ================================ //
176 //
177 inline const KeyState* MouseState::XButton1() const
178 {
179  return &m_buttons[ Mouse::PhysicalButtons::XBUTTON1 ];
180 }
181 
182 // ================================ //
183 //
184 inline const KeyState* MouseState::XButton2() const
185 {
186  return &m_buttons[ Mouse::PhysicalButtons::XBUTTON2 ];
187 }
188 
189 
190 } // input
191 } // sw
PhysicalButtons
Physical buttons.
Definition: InputDeviceEvent.h:215
uint16_t uint16
Definition: TypesDefinitions.h:29
KeyState m_buttons[MOUSE_STATE_MAX_NUM_BUTTONS]
Definition: MouseState.h:33
Definition: InputDeviceEvent.h:226
State of mouse.
Definition: MouseState.h:27
Definition: InputDeviceEvent.h:228
uint16 m_position[2]
Współrzędne X i Y.
Definition: MouseState.h:32
float WheelDelta() const
Definition: MouseState.h:47
int8_t int8
Definition: TypesDefinitions.h:26
Definition: DirectInputModule.cpp:11
MouseState()
Definition: MouseState.h:77
const KeyState * LeftButton() const
Definition: MouseState.h:156
void SetPosition(short X, short Y)
Ustawia pozycję myszy.
Definition: MouseState.h:92
KeyStates changed events.
Definition: InputDeviceEvent.h:313
void ApplyEvent(const ButtonEvent &event)
Updates state depending on event.
Definition: MouseState.h:107
const KeyState * XButton1() const
Definition: MouseState.h:177
float m_axes[4]
Definition: MouseState.h:31
void RemoveEvents()
Czyści tablicę z eventów o wciśnięciu klawiszy, ale podtrzymuje stan przycisków.
Definition: MouseState.h:99
Mouse button Change event.
Definition: InputDeviceEvent.h:289
~MouseState()
Definition: MouseState.h:86
const KeyState & operator[](Mouse::PhysicalButtons button) const
Definition: MouseState.h:55
const int8 MOUSE_STATE_MAX_NUM_AXES
Max number of mouse axes.
Definition: MouseState.h:20
Cursor position changed event.
Definition: InputDeviceEvent.h:305
AxisEvent Axis
Definition: InputDeviceEvent.h:320
Button state structure..
Definition: KeyState.h:26
const KeyState * MiddleButton() const
Definition: MouseState.h:170
bool IsPressed() const
Definition: KeyState.h:45
const KeyState * XButton2() const
Definition: MouseState.h:184
const float * GetAxesState() const
Definition: MouseState.h:39
uint16 GetPositionX() const
Definition: MouseState.h:42
Mouse or joystick axis value changed event.
Definition: InputDeviceEvent.h:297
ButtonEvent Button
Definition: InputDeviceEvent.h:319
const KeyState * RightButton() const
Definition: MouseState.h:163
CursorEvent Cursor
Definition: InputDeviceEvent.h:321
const int8 MOUSE_STATE_MAX_NUM_BUTTONS
Max number of mouse buttons.
Definition: MouseState.h:18
DeviceEventType Type
Definition: InputDeviceEvent.h:324
const KeyState * GetButtonsState() const
Definition: MouseState.h:40
uint16 GetPositionY() const
Definition: MouseState.h:43
Definition: InputDeviceEvent.h:227