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 {
00036 Input::Input() :
00037 myMouseX(0),
00038 myMouseY(0)
00039 {
00040     for (int i = 0; i < Key::Count; ++i)
00041         myKeys[i] = false;
00042 
00043     for (int i = 0; i < Mouse::Count; ++i)
00044         myMouseButtons[i] = false;
00045 
00046     for (int i = 0; i < 16; ++i)
00047     {
00048         myJoystickButtons[0][i] = false;
00049         myJoystickButtons[1][i] = false;
00050     }
00051 
00052     for (int i = 0; i < Joy::Count; ++i)
00053     {
00054         myJoystickAxis[0][i] = 0.f;
00055         myJoystickAxis[1][i] = 0.f;
00056     }
00057 }
00058 
00059 
00063 bool Input::IsKeyDown(Key::Code KeyCode) const
00064 {
00065     return myKeys[KeyCode];
00066 }
00067 
00068 
00072 bool Input::IsMouseButtonDown(Mouse::Button Button) const
00073 {
00074     return myMouseButtons[Button];
00075 }
00076 
00077 
00081 bool Input::IsJoystickButtonDown(unsigned int JoyId, unsigned int Button) const
00082 {
00083     if ((JoyId < 2) && (Button < 16))
00084         return myJoystickButtons[JoyId][Button];
00085     else
00086         return false;
00087 }
00088 
00089 
00093 int Input::GetMouseX() const
00094 {
00095     return myMouseX;
00096 }
00097 
00098 
00102 int Input::GetMouseY() const
00103 {
00104     return myMouseY;
00105 }
00106 
00107 
00111 float Input::GetJoystickAxis(unsigned int JoyId, Joy::Axis Axis) const
00112 {
00113     return myJoystickAxis[JoyId][Axis];
00114 }
00115 
00116 
00120 void Input::OnEvent(const Event& EventReceived)
00121 {
00122     switch (EventReceived.Type)
00123     {
00124         // Key events
00125         case Event::KeyPressed :  myKeys[EventReceived.Key.Code] = true;  break;
00126         case Event::KeyReleased : myKeys[EventReceived.Key.Code] = false; break;
00127 
00128         // Mouse event
00129         case Event::MouseButtonPressed :  myMouseButtons[EventReceived.MouseButton.Button] = true;  break;
00130         case Event::MouseButtonReleased : myMouseButtons[EventReceived.MouseButton.Button] = false; break;
00131 
00132         // Mouse move event
00133         case Event::MouseMoved :
00134             myMouseX = EventReceived.MouseMove.X;
00135             myMouseY = EventReceived.MouseMove.Y;
00136             break;
00137 
00138         // Joystick button events
00139         case Event::JoyButtonPressed :  myJoystickButtons[EventReceived.JoyButton.JoystickId][EventReceived.JoyButton.Button] = true;  break;
00140         case Event::JoyButtonReleased : myJoystickButtons[EventReceived.JoyButton.JoystickId][EventReceived.JoyButton.Button] = false; break;
00141 
00142         // Joystick move event
00143         case Event::JoyMoved :
00144             myJoystickAxis[EventReceived.JoyMove.JoystickId][EventReceived.JoyMove.Axis] = EventReceived.JoyMove.Position;
00145             break;
00146 
00147         // Lost focus event : we must reset all persistent states
00148         case Event::LostFocus :
00149         {
00150             for (int i = 0; i < Key::Count; ++i)
00151                 myKeys[i] = false;
00152 
00153             for (int i = 0; i < Mouse::Count; ++i)
00154                 myMouseButtons[i] = false;
00155 
00156             for (int i = 0; i < 16; ++i)
00157             {
00158                 myJoystickButtons[0][i] = false;
00159                 myJoystickButtons[1][i] = false;
00160             }
00161             break;
00162         }
00163 
00164         default :
00165             break;
00166     }
00167 }
00168 
00169 } // namespace sf

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