Input.cpp

00001 
00002 //
00003 // SFML - Simple and Fast Multimedia Library
00004 // Copyright (C) 2007 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 
00034 sfInput::sfInput() :
00035 myMouseX(0),
00036 myMouseY(0)
00037 {
00038     for (int i = 0; i < sfKey::Count; ++i)
00039         myKeys[i] = false;
00040 
00041     for (int i = 0; i < 3; ++i)
00042         myMouseButtons[i] = false;
00043 
00044     myJoystickX[0] = 0;
00045     myJoystickX[1] = 0;
00046     myJoystickY[0] = 0;
00047     myJoystickY[1] = 0;
00048     myJoystickZ[0] = 0;
00049     myJoystickZ[1] = 0;
00050 
00051     for (int i = 0; i < 8; ++i)
00052     {
00053         myJoystickButtons[0][i] = false;
00054         myJoystickButtons[1][i] = false;
00055     }
00056 }
00057 
00058 
00062 bool sfInput::IsKeyDown(sfKey::Code Key) const
00063 {
00064     return myKeys[Key];
00065 }
00066 
00067 
00071 bool sfInput::IsMouseButtonDown(sfMouse::Button Button) const
00072 {
00073     switch (Button)
00074     {
00075         case sfMouse::Left :   return myMouseButtons[0];
00076         case sfMouse::Right :  return myMouseButtons[1];
00077         case sfMouse::Middle : return myMouseButtons[2];
00078     }
00079 
00080     return false;
00081 }
00082 
00083 
00087 bool sfInput::IsJoystickButtonDown(unsigned int JoyId, unsigned int Button) const
00088 {
00089     if ((JoyId < 2) && (Button < 8))
00090         return myJoystickButtons[JoyId][Button];
00091     else
00092         return false;
00093 }
00094 
00095 
00099 unsigned int sfInput::GetMouseX() const
00100 {
00101     return myMouseX;
00102 }
00103 
00104 
00108 unsigned int sfInput::GetMouseY() const
00109 {
00110     return myMouseY;
00111 }
00112 
00113 
00117 int sfInput::GetJoystickX(unsigned int JoyId) const
00118 {
00119     return myJoystickX[JoyId];
00120 }
00121 
00122 
00126 int sfInput::GetJoystickY(unsigned int JoyId) const
00127 {
00128     return myJoystickY[JoyId];
00129 }
00130 
00131 
00135 int sfInput::GetJoystickZ(unsigned int JoyId) const
00136 {
00137     return myJoystickZ[JoyId];
00138 }
00139 
00140 
00144 void sfInput::OnEvent(const sfEvent& Event)
00145 {
00146     switch (Event.Type)
00147     {
00148         // Key events
00149         case sfEvent::KeyPressed :  myKeys[Event.Key.Code] = true;  break;
00150         case sfEvent::KeyReleased : myKeys[Event.Key.Code] = false; break;
00151 
00152         // Mouse pressed event
00153         case sfEvent::MouseButtonPressed :
00154             switch (Event.Mouse.Buttons)
00155             {
00156                 case sfMouse::Left :   myMouseButtons[0] = true; break;
00157                 case sfMouse::Right :  myMouseButtons[1] = true; break;
00158                 case sfMouse::Middle : myMouseButtons[2] = true; break;
00159             }
00160             break;
00161 
00162         // Mouse released event
00163         case sfEvent::MouseButtonReleased :
00164             switch (Event.Mouse.Buttons)
00165             {
00166                 case sfMouse::Left :   myMouseButtons[0] = false; break;
00167                 case sfMouse::Right :  myMouseButtons[1] = false; break;
00168                 case sfMouse::Middle : myMouseButtons[2] = false; break;
00169             }
00170             break;
00171 
00172         // Mouse move event
00173         case sfEvent::MouseMove :
00174             myMouseX = Event.Mouse.X;
00175             myMouseY = Event.Mouse.Y;
00176             break;
00177 
00178         // Joystick button events
00179         case sfEvent::JoystickButtonPressed :  myJoystickButtons[Event.Joystick.JoystickId][Event.Joystick.Button] = true;  break;
00180         case sfEvent::JoystickButtonReleased : myJoystickButtons[Event.Joystick.JoystickId][Event.Joystick.Button] = false; break;
00181 
00182         // Joystick move event
00183         case sfEvent::JoystickMove :
00184             myJoystickX[Event.Joystick.JoystickId] = Event.Joystick.X;
00185             myJoystickY[Event.Joystick.JoystickId] = Event.Joystick.Y;
00186             myJoystickZ[Event.Joystick.JoystickId] = Event.Joystick.Z;
00187             break;
00188 
00189         default :
00190             break;
00191     }
00192 }