Input.cpp
1 //
3 // SFML - Simple and Fast Multimedia Library
4 // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
5 //
6 // This software is provided 'as-is', without any express or implied warranty.
7 // In no event will the authors be held liable for any damages arising from the use of this software.
8 //
9 // Permission is granted to anyone to use this software for any purpose,
10 // including commercial applications, and to alter it and redistribute it freely,
11 // subject to the following restrictions:
12 //
13 // 1. The origin of this software must not be misrepresented;
14 // you must not claim that you wrote the original software.
15 // If you use this software in a product, an acknowledgment
16 // in the product documentation would be appreciated but is not required.
17 //
18 // 2. Altered source versions must be plainly marked as such,
19 // and must not be misrepresented as being the original software.
20 //
21 // 3. This notice may not be removed or altered from any source distribution.
22 //
24 
26 // Headers
28 #include <SFML/Window/Input.hpp>
29 
30 
31 namespace sf
32 {
37 myMouseX(0),
38 myMouseY(0)
39 {
40  ResetStates();
41 }
42 
43 
47 bool Input::IsKeyDown(Key::Code KeyCode) const
48 {
49  return myKeys[KeyCode];
50 }
51 
52 
56 bool Input::IsMouseButtonDown(Mouse::Button Button) const
57 {
58  return myMouseButtons[Button];
59 }
60 
61 
65 bool Input::IsJoystickButtonDown(unsigned int JoyId, unsigned int Button) const
66 {
67  if ((JoyId < Joy::Count) && (Button < Joy::ButtonCount))
68  return myJoystickButtons[JoyId][Button];
69  else
70  return false;
71 }
72 
73 
77 int Input::GetMouseX() const
78 {
79  return myMouseX;
80 }
81 
82 
86 int Input::GetMouseY() const
87 {
88  return myMouseY;
89 }
90 
91 
95 float Input::GetJoystickAxis(unsigned int JoyId, Joy::Axis Axis) const
96 {
97  if (JoyId < Joy::Count)
98  return myJoystickAxis[JoyId][Axis];
99  else
100  return 0.f;
101 }
102 
103 
107 void Input::OnEvent(const Event& EventReceived)
108 {
109  switch (EventReceived.Type)
110  {
111  // Key events
112  case Event::KeyPressed : myKeys[EventReceived.Key.Code] = true; break;
113  case Event::KeyReleased : myKeys[EventReceived.Key.Code] = false; break;
114 
115  // Mouse event
116  case Event::MouseButtonPressed : myMouseButtons[EventReceived.MouseButton.Button] = true; break;
117  case Event::MouseButtonReleased : myMouseButtons[EventReceived.MouseButton.Button] = false; break;
118 
119  // Mouse move event
120  case Event::MouseMoved :
121  myMouseX = EventReceived.MouseMove.X;
122  myMouseY = EventReceived.MouseMove.Y;
123  break;
124 
125  // Joystick button events
126  case Event::JoyButtonPressed : myJoystickButtons[EventReceived.JoyButton.JoystickId][EventReceived.JoyButton.Button] = true; break;
127  case Event::JoyButtonReleased : myJoystickButtons[EventReceived.JoyButton.JoystickId][EventReceived.JoyButton.Button] = false; break;
128 
129  // Joystick move event
130  case Event::JoyMoved :
131  myJoystickAxis[EventReceived.JoyMove.JoystickId][EventReceived.JoyMove.Axis] = EventReceived.JoyMove.Position;
132  break;
133 
134  // Lost focus event : we must reset all persistent states
135  case Event::LostFocus :
136  {
137  ResetStates();
138  break;
139  }
140 
141  default :
142  break;
143  }
144 }
145 
146 
150 void Input::ResetStates()
151 {
152  for (int i = 0; i < Key::Count; ++i)
153  myKeys[i] = false;
154 
155  for (int i = 0; i < Mouse::ButtonCount; ++i)
156  myMouseButtons[i] = false;
157 
158  for (int i = 0; i < Joy::Count; ++i)
159  {
160  for (int j = 0; j < Joy::ButtonCount; ++j)
161  myJoystickButtons[i][j] = false;
162 
163  for (int j = 0; j < Joy::AxisCount; ++j)
164  myJoystickAxis[i][j] = 0.f;
165  myJoystickAxis[i][Joy::AxisPOV] = -1.f;
166  }
167 }
168 
169 } // namespace sf
EventType Type
Type of the event.
Definition: Event.hpp:303
Input()
Default constructor.
Definition: Input.cpp:36
float GetJoystickAxis(unsigned int JoyId, Joy::Axis Axis) const
Get a joystick axis position.
Definition: Input.cpp:95
Total number of supported joystick buttons.
Definition: Event.hpp:189
Total number of supported joysticks.
Definition: Event.hpp:188
bool IsJoystickButtonDown(unsigned int JoyId, unsigned int Button) const
Get the state of a joystick button.
Definition: Input.cpp:65
int GetMouseY() const
Get the mouse Y position.
Definition: Input.cpp:86
bool IsKeyDown(Key::Code KeyCode) const
Get the state of a key.
Definition: Input.cpp:47
bool IsMouseButtonDown(Mouse::Button Button) const
Get the state of a mouse button.
Definition: Input.cpp:56
int GetMouseX() const
Get the mouse X position.
Definition: Input.cpp:77
Event defines a system event and its parameters.
Definition: Event.hpp:197