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 
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 < 3; ++i)
00044         myMouseButtons[i] = false;
00045 
00046     myJoystickX[0] = 0;
00047     myJoystickX[1] = 0;
00048     myJoystickY[0] = 0;
00049     myJoystickY[1] = 0;
00050     myJoystickZ[0] = 0;
00051     myJoystickZ[1] = 0;
00052 
00053     for (int i = 0; i < 8; ++i)
00054     {
00055         myJoystickButtons[0][i] = false;
00056         myJoystickButtons[1][i] = false;
00057     }
00058 }
00059 
00060 
00064 bool Input::IsKeyDown(Key::Code KeyCode) const
00065 {
00066     return myKeys[KeyCode];
00067 }
00068 
00069 
00073 bool Input::IsMouseButtonDown(Mouse::Button Button) const
00074 {
00075     switch (Button)
00076     {
00077         case Mouse::Left :   return myMouseButtons[0];
00078         case Mouse::Right :  return myMouseButtons[1];
00079         case Mouse::Middle : return myMouseButtons[2];
00080     }
00081 
00082     return false;
00083 }
00084 
00085 
00089 bool Input::IsJoystickButtonDown(unsigned int JoyId, unsigned int Button) const
00090 {
00091     if ((JoyId < 2) && (Button < 8))
00092         return myJoystickButtons[JoyId][Button];
00093     else
00094         return false;
00095 }
00096 
00097 
00101 unsigned int Input::GetMouseX() const
00102 {
00103     return myMouseX;
00104 }
00105 
00106 
00110 unsigned int Input::GetMouseY() const
00111 {
00112     return myMouseY;
00113 }
00114 
00115 
00119 int Input::GetJoystickX(unsigned int JoyId) const
00120 {
00121     return myJoystickX[JoyId];
00122 }
00123 
00124 
00128 int Input::GetJoystickY(unsigned int JoyId) const
00129 {
00130     return myJoystickY[JoyId];
00131 }
00132 
00133 
00137 int Input::GetJoystickZ(unsigned int JoyId) const
00138 {
00139     return myJoystickZ[JoyId];
00140 }
00141 
00142 
00146 void Input::OnEvent(const Event& EventReceived)
00147 {
00148     switch (EventReceived.Type)
00149     {
00150         // Key events
00151         case Event::KeyPressed :  myKeys[EventReceived.Key.Code] = true;  break;
00152         case Event::KeyReleased : myKeys[EventReceived.Key.Code] = false; break;
00153 
00154         // Mouse pressed event
00155         case Event::MouseButtonPressed :
00156             switch (EventReceived.Mouse.Buttons)
00157             {
00158                 case Mouse::Left :   myMouseButtons[0] = true; break;
00159                 case Mouse::Right :  myMouseButtons[1] = true; break;
00160                 case Mouse::Middle : myMouseButtons[2] = true; break;
00161             }
00162             break;
00163 
00164         // Mouse released event
00165         case Event::MouseButtonReleased :
00166             switch (EventReceived.Mouse.Buttons)
00167             {
00168                 case Mouse::Left :   myMouseButtons[0] = false; break;
00169                 case Mouse::Right :  myMouseButtons[1] = false; break;
00170                 case Mouse::Middle : myMouseButtons[2] = false; break;
00171             }
00172             break;
00173 
00174         // Mouse move event
00175         case Event::MouseMove :
00176             myMouseX = EventReceived.Mouse.X;
00177             myMouseY = EventReceived.Mouse.Y;
00178             break;
00179 
00180         // Joystick button events
00181         case Event::JoystickButtonPressed :  myJoystickButtons[EventReceived.Joystick.JoystickId][EventReceived.Joystick.Button] = true;  break;
00182         case Event::JoystickButtonReleased : myJoystickButtons[EventReceived.Joystick.JoystickId][EventReceived.Joystick.Button] = false; break;
00183 
00184         // Joystick move event
00185         case Event::JoystickMove :
00186             myJoystickX[EventReceived.Joystick.JoystickId] = EventReceived.Joystick.X;
00187             myJoystickY[EventReceived.Joystick.JoystickId] = EventReceived.Joystick.Y;
00188             myJoystickZ[EventReceived.Joystick.JoystickId] = EventReceived.Joystick.Z;
00189             break;
00190 
00191         default :
00192             break;
00193     }
00194 }
00195 
00196 } // namespace sf