00001 #include <SDL/SDL.h>
00002 #include "MercuryInputSDL.h"
00003
00004 using namespace std;
00005
00006 CursorDevice * DefaultCursorDevice = new SDLMouse;
00007 bool SDLKBResponce = AutoInputDeviceTracker.AddDevice( "KBSDL", new DeviceKeyboardSDL );
00008
00009 void DeviceKeyboardSDL::Init( MString Name )
00010 {
00011 int length = 0;
00012 SDL_GetKeyState(&length);
00013 m_iNumkeys = length;
00014 m_vKeys = new unsigned char[length];
00015 memset( m_bAllKeys, 0, 512 );
00016 memcpy( m_vKeys, SDL_GetKeyState(NULL), sizeof(unsigned char) * length );
00017 }
00018
00019 InputEvent RemapSDLKB( InputEvent & ie )
00020 {
00021 switch ( ie.ButtonNumber )
00022 {
00023 case 301: ie.ButtonNumber = 15; break;
00024 case 271: ie.ButtonNumber = 13; break;
00025 case 311: ie.ButtonNumber = 91; break;
00026 case 319: ie.ButtonNumber = 93; break;
00027 case 304: ie.ButtonNumber = 160; break;
00028 case 303: ie.ButtonNumber = 161; break;
00029 case 307: ie.ButtonNumber = 164; break;
00030 case 308: ie.ButtonNumber = 165; break;
00031 case 305: ie.ButtonNumber = 162; break;
00032 case 306: ie.ButtonNumber = 163; break;
00033 default:
00034 break;
00035
00036 }
00037
00038 return ie;
00039 }
00040
00041 bool DeviceKeyboardSDL::IsButtonDown( int ButtonNumber )
00042 {
00043 if ( ( ButtonNumber >= 0 ) && ( ButtonNumber < 512 ) )
00044 return m_bAllKeys[ButtonNumber];
00045 else
00046 return false;
00047 }
00048
00049 int DeviceKeyboardSDL::NumButtons()
00050 {
00051 return 512;
00052 }
00053
00054 void DeviceKeyboardSDL::Update( )
00055 {
00056 int length = 0;
00057 unsigned char* curKeys = SDL_GetKeyState(&length);
00058
00059 curKeys[16] = curKeys[304] || curKeys[303];
00060 curKeys[17] = curKeys[305] || curKeys[306];
00061 curKeys[18] = curKeys[307] || curKeys[308];
00062
00063 {
00064 int buttons = SDL_GetMouseState(NULL, NULL);
00065 if (buttons & '\01')
00066 curKeys[1] = true;
00067 else
00068 curKeys[1] = false;
00069
00070 if (buttons & '\04')
00071 curKeys[2] = true;
00072 else
00073 curKeys[2] = false;
00074
00075
00076
00077 }
00078
00079 for ( int i = 0; i < length; i++ )
00080 {
00081 if ( curKeys[i] && !m_vKeys[i] )
00082 {
00083 InputEvent ie = InputEvent( 0,i,IET_DOWN);
00084 RemapSDLKB( ie );
00085 m_sEvents.push_back( ie );
00086 m_bAllKeys[ie.ButtonNumber] = true;
00087 }
00088 if ( !curKeys[i] && m_vKeys[i] )
00089 {
00090 InputEvent ie = InputEvent( 0,i,IET_RELEASE);
00091 RemapSDLKB( ie );
00092 m_sEvents.push_back( ie );
00093 m_bAllKeys[ie.ButtonNumber] = false;
00094 }
00095 }
00096
00097 memcpy( m_vKeys, curKeys, sizeof(unsigned char) * length );
00098 }
00099
00100 InputEvent DeviceKeyboardSDL::PopLastEvent()
00101 {
00102 if ( m_sEvents.empty() )
00103 return InputEvent( 0,0,IET_NONE);
00104 InputEvent ret = m_sEvents.front();
00105 m_sEvents.pop_front();
00106 return ret;
00107 }
00108
00109 SDLMouse::SDLMouse( )
00110 :CursorDevice()
00111 {
00112 SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
00113 SDL_WarpMouse(m_Xoff, m_Yoff);
00114 }
00115
00116 SDLMouse::~SDLMouse( )
00117 {
00118 if ( !m_bSubscribed )
00119 MESSAGEMAN->Unsubscribe("changefocus", MercuryCallback(UpdateFocus,this) );
00120 }
00121
00122 void SDLMouse::GetPosition( int &x, int &y )
00123 {
00124 x = m_x;
00125 y = m_y;
00126 }
00127
00128 void SDLMouse::Update( )
00129 {
00130 int x,y;
00131 x = y = 0;
00132 if ( !m_bSubscribed )
00133 {
00134 MESSAGEMAN->Subscribe( "changefocus", MercuryCallback(UpdateFocus,this) );
00135 m_bSubscribed = true;
00136 }
00137
00138 if ( !m_bHasFocus )
00139 return;
00140
00141 SDL_PumpEvents();
00142 SDL_GetMouseState(&x, &y);
00143
00144 m_x += (x - m_Xoff);
00145 m_y += (y - m_Yoff);
00146
00147 SDL_WarpMouse(m_Xoff, m_Yoff);
00148 }
00149
00150 void SDLMouse::UpdateFocus( const MString &message, void* data, PStack& info )
00151 {
00152 float bHasFocus = info.PopItem().GetValueB();
00153 if ( !bHasFocus )
00154 {
00155
00156 SDL_ShowCursor(true);
00157 m_bHasFocus = false;
00158 }
00159 else
00160 {
00161 SDL_ShowCursor(false);
00162 m_bHasFocus = true;
00163 }
00164 }
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192