SFML logo
  • Main Page
  • Namespaces
  • Classes
  • Files
  • File List

Input.cpp

00001 
00002 //
00003 // SFML - Simple and Fast Multimedia Library
00004 // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
00005 //
00006 // This software is provided 'as-is', without any express or implied warranty.
00007 // In no event will the authors be held liable for any damages arising from the use of this software.
00008 //
00009 // Permission is granted to anyone to use this software for any purpose,
00010 // including commercial applications, and to alter it and redistribute it freely,
00011 // subject to the following restrictions:
00012 //
00013 // 1. The origin of this software must not be misrepresented;
00014 //    you must not claim that you wrote the original software.
00015 //    If you use this software in a product, an acknowledgment
00016 //    in the product documentation would be appreciated but is not required.
00017 //
00018 // 2. Altered source versions must be plainly marked as such,
00019 //    and must not be misrepresented as being the original software.
00020 //
00021 // 3. This notice may not be removed or altered from any source distribution.
00022 //
00024 
00026 // Headers
00028 #include <SFML/Window/Input.hpp>
00029 
00030 
00031 namespace sf
00032 {
00034 Input::Input() :
00035 myMouseX(0),
00036 myMouseY(0)
00037 {
00038     ResetStates();
00039 }
00040 
00041 
00043 bool Input::IsKeyDown(Key::Code key) const
00044 {
00045     return myKeys[key];
00046 }
00047 
00048 
00050 bool Input::IsMouseButtonDown(Mouse::Button button) const
00051 {
00052     return myMouseButtons[button];
00053 }
00054 
00055 
00057 bool Input::IsJoystickButtonDown(unsigned int joystick, unsigned int button) const
00058 {
00059     if ((joystick < Joy::Count) && (button < Joy::ButtonCount))
00060         return myJoystickButtons[joystick][button];
00061     else
00062         return false;
00063 }
00064 
00065 
00067 int Input::GetMouseX() const
00068 {
00069     return myMouseX;
00070 }
00071 
00072 
00074 int Input::GetMouseY() const
00075 {
00076     return myMouseY;
00077 }
00078 
00079 
00081 float Input::GetJoystickAxis(unsigned int joystick, Joy::Axis axis) const
00082 {
00083     if (joystick < Joy::Count)
00084         return myJoystickAxis[joystick][axis];
00085     else
00086         return 0.f;
00087 }
00088 
00089 
00091 void Input::OnEvent(const Event& event)
00092 {
00093     switch (event.Type)
00094     {
00095         // Key events
00096         case Event::KeyPressed :  myKeys[event.Key.Code] = true;  break;
00097         case Event::KeyReleased : myKeys[event.Key.Code] = false; break;
00098 
00099         // Mouse event
00100         case Event::MouseButtonPressed :  myMouseButtons[event.MouseButton.Button] = true;  break;
00101         case Event::MouseButtonReleased : myMouseButtons[event.MouseButton.Button] = false; break;
00102 
00103         // Mouse move event
00104         case Event::MouseMoved :
00105             myMouseX = event.MouseMove.X;
00106             myMouseY = event.MouseMove.Y;
00107             break;
00108 
00109         // Joystick button events
00110         case Event::JoyButtonPressed :  myJoystickButtons[event.JoyButton.JoystickId][event.JoyButton.Button] = true;  break;
00111         case Event::JoyButtonReleased : myJoystickButtons[event.JoyButton.JoystickId][event.JoyButton.Button] = false; break;
00112 
00113         // Joystick move event
00114         case Event::JoyMoved :
00115             myJoystickAxis[event.JoyMove.JoystickId][event.JoyMove.Axis] = event.JoyMove.Position;
00116             break;
00117 
00118         // Lost focus event : we must reset all persistent states
00119         case Event::LostFocus :
00120             ResetStates();
00121             break;
00122 
00123         default :
00124             break;
00125     }
00126 }
00127 
00128 
00130 void Input::ResetStates()
00131 {
00132     for (int i = 0; i < Key::Count; ++i)
00133         myKeys[i] = false;
00134 
00135     for (int i = 0; i < Mouse::ButtonCount; ++i)
00136         myMouseButtons[i] = false;
00137 
00138     for (int i = 0; i < Joy::Count; ++i)
00139     {
00140         for (int j = 0; j < Joy::ButtonCount; ++j)
00141             myJoystickButtons[i][j] = false;
00142 
00143         for (int j = 0; j < Joy::AxisCount; ++j)
00144             myJoystickAxis[i][j] = 0.f;
00145         myJoystickAxis[i][Joy::AxisPOV] = -1.f;
00146     }
00147 }
00148 
00149 } // namespace sf

 ::  Copyright © 2007-2008 Laurent Gomila, all rights reserved  ::  Documentation generated by doxygen 1.5.2  ::