InputManager.cpp
Go to the documentation of this file.00001 //*** InputManager.cpp *** 00002 00003 #include "InputManager.h" 00004 00005 #include "Debug.h" 00006 #include "Platform_Screen.h" 00007 #include "Platform_Input.h" 00008 #include "Platform_Input_KeyboardDevice.h" 00009 #include "Platform_Input_MouseDevice.h" 00010 #include "Bitmap_16bitAlpha.h" 00011 #include "Asset.h" 00012 #include "Filename.h" 00013 00014 00015 //*** Constructor *** 00016 00017 InputManager::InputManager(): 00018 cursorPosX_(-1), 00019 cursorPosY_(-1), 00020 keyboardDevice_(0), 00021 mouseDevice_(0), 00022 currentCursor_(-1), 00023 cursorHidden_(false) 00024 { 00025 if (Platform::GetPlatform_Input()) 00026 { 00027 keyboardDevice_=Platform::GetPlatform_Input()->GetKeyboardDevice(); 00028 mouseDevice_=Platform::GetPlatform_Input()->GetMouseDevice(); 00029 } 00030 00031 if (keyboardDevice_) 00032 { 00033 for (int i=0; i<KEYCODE_COUNT; i++) 00034 { 00035 keyStates_[i]=keyboardDevice_->IsKeyDown((Platform_Input_KeyboardDevice::KeyCode)i); 00036 previousKeyStates_[i]=keyStates_[i]; 00037 } 00038 00039 for (int i=0; i<256; i++) 00040 { 00041 charStates_[i]=keyboardDevice_->IsCharDown((unsigned char)i); 00042 previousCharStates_[i]=charStates_[i]; 00043 } 00044 } 00045 } 00046 00047 00048 //*** Destructor *** 00049 00050 InputManager::~InputManager() 00051 { 00052 } 00053 00054 00055 00056 //*** Update *** 00057 00058 void InputManager::Update() 00059 { 00060 00061 // Store previous key states 00062 for (int i=0; i<KEYCODE_COUNT; i++) 00063 { 00064 previousKeyStates_[i]=keyStates_[i]; 00065 } 00066 00067 // Store previous char states 00068 for (int i=0; i<256; i++) 00069 { 00070 previousCharStates_[i]=charStates_[i]; 00071 } 00072 00073 if (keyboardDevice_) 00074 { 00075 // Get key states 00076 for (int i=0; i<KEYCODE_COUNT; i++) 00077 { 00078 keyStates_[i]=keyboardDevice_->IsKeyDown((Platform_Input_KeyboardDevice::KeyCode)i); 00079 } 00080 00081 // Get char states 00082 for (int i=0; i<256; i++) 00083 { 00084 charStates_[i]=keyboardDevice_->IsCharDown((unsigned char)i); 00085 } 00086 00087 // Get buffered input 00088 for (int i=0; i<keyboardDevice_->GetBufferedCharacterCount(); i++) 00089 { 00090 characterBuffer_.Add(keyboardDevice_->GetBufferedCharacter(i)); 00091 } 00092 keyboardDevice_->ClearBufferedCharacters(); 00093 } 00094 00095 00096 00097 // Get cursor position 00098 if (mouseDevice_) 00099 { 00100 mouseDevice_->GetPosition(cursorPosX_,cursorPosY_); 00101 if (Platform::GetPlatform_Screen()) 00102 { 00103 Platform::GetPlatform_Screen()->TransformCursorCoordinates(cursorPosX_,cursorPosY_); 00104 } 00105 } 00106 }; 00107 00108 00109 00110 //*** GetCursorX *** 00111 00112 float InputManager::GetCursorX() 00113 { 00114 return cursorPosX_; 00115 } 00116 00117 00118 //*** GetMouseY *** 00119 00120 float InputManager::GetCursorY() 00121 { 00122 return cursorPosY_; 00123 } 00124 00125 00126 //*** IsKeyDown *** 00127 00128 bool InputManager::IsKeyDown(KeyCode key) 00129 { 00130 Assert(key>=0 && key<KEYCODE_COUNT,"Key code out of range"); 00131 if (key<0 || key>=KEYCODE_COUNT) 00132 { 00133 return false; 00134 } 00135 00136 return keyStates_[key]; 00137 } 00138 00139 00140 //*** WasKeyPressed *** 00141 00142 bool InputManager::WasKeyPressed(KeyCode key) 00143 { 00144 Assert(key>=0 && key<KEYCODE_COUNT,"Key code out of range"); 00145 if (key<0 || key>=KEYCODE_COUNT) 00146 { 00147 return false; 00148 } 00149 00150 return keyStates_[key] && !previousKeyStates_[key]; 00151 } 00152 00153 00154 //*** WasKeyReleased *** 00155 00156 bool InputManager::WasKeyReleased(KeyCode key) 00157 { 00158 Assert(key>=0 && key<KEYCODE_COUNT,"Key code out of range"); 00159 if (key<0 || key>=KEYCODE_COUNT) 00160 { 00161 return false; 00162 } 00163 00164 return !keyStates_[key] && previousKeyStates_[key]; 00165 } 00166 00167 00168 //*** IsAnyKeyDown *** 00169 00170 bool InputManager::IsAnyKeyDown() 00171 { 00172 for (int i=0; i<KEYCODE_COUNT; i++) 00173 { 00174 if (keyStates_[i]) 00175 { 00176 return true; 00177 } 00178 00179 } 00180 return false; 00181 } 00182 00183 00184 //*** WasAnyKeyPressed *** 00185 00186 bool InputManager::WasAnyKeyPressed() 00187 { 00188 for (int i=0; i<KEYCODE_COUNT; i++) 00189 { 00190 if (keyStates_[i] && !previousKeyStates_[i]) 00191 { 00192 return true; 00193 } 00194 } 00195 return false; 00196 } 00197 00198 00199 //*** WasAnyKeyReleased *** 00200 00201 bool InputManager::WasAnyKeyReleased() 00202 { 00203 for (int i=0; i<KEYCODE_COUNT; i++) 00204 { 00205 if (!keyStates_[i] && previousKeyStates_[i]) 00206 { 00207 return true; 00208 } 00209 } 00210 return false; 00211 } 00212 00213 00214 //*** IsCharDown *** 00215 00216 bool InputManager::IsCharDown(unsigned char ascii) 00217 { 00218 return charStates_[ascii]; 00219 } 00220 00221 00222 //*** WasCharPressed *** 00223 00224 bool InputManager::WasCharPressed(unsigned char ascii) 00225 { 00226 return charStates_[ascii] && !previousCharStates_[ascii]; 00227 } 00228 00229 00230 //*** WasCharReleased *** 00231 00232 bool InputManager::WasCharReleased(unsigned char ascii) 00233 { 00234 return !charStates_[ascii] && previousCharStates_[ascii]; 00235 } 00236 00237 00238 //*** ClearCharacters *** 00239 00240 void InputManager::ClearCharacters() 00241 { 00242 characterBuffer_.Clear(false); 00243 } 00244 00245 00246 //*** GetCharacterCount *** 00247 00248 int InputManager::GetCharacterCount() 00249 { 00250 return characterBuffer_.GetItemCount(); 00251 } 00252 00253 00254 //*** GetCharacter *** 00255 00256 char InputManager::GetCharacter(int index) 00257 { 00258 if (index<0 || index>=characterBuffer_.GetItemCount()) 00259 { 00260 return 0; 00261 } 00262 00263 return characterBuffer_.Get(index); 00264 } 00265 00266 00267 //*** CreateCursor *** 00268 00269 void InputManager::CreateCursor(int id, const char* image, int hotspotX, int hotspotY) 00270 { 00271 for (int i=0; i<cursors_.GetItemCount(); i++) 00272 { 00273 Assert(cursors_.Get(i).id!=id,"A cursor with that ID already exists"); 00274 if (cursors_.Get(i).id==id) 00275 { 00276 return; 00277 } 00278 } 00279 00280 Cursor cursor; 00281 cursor.id=id; 00282 cursor.handle=0; 00283 if (image && Platform::GetPlatform_Input()) 00284 { 00285 Bitmap_16bitAlpha cursorImage(image); 00286 cursor.handle=Platform::GetPlatform_Input()->CreateMouseCursor(cursorImage.GetHPitch(),cursorImage.GetVPitch(),hotspotX,hotspotY,cursorImage.GetColorData(),cursorImage.GetAlphaData()); 00287 } 00288 cursors_.Add(cursor); 00289 } 00290 00291 00293 00294 void InputManager::SetCursor(int id) 00295 { 00296 for (int i=0; i<cursors_.GetItemCount(); i++) 00297 { 00298 if (cursors_.Get(i).id==id) 00299 { 00300 currentCursor_=id; 00301 if (Platform::GetPlatform_Input()) 00302 { 00303 Platform::GetPlatform_Input()->SetMouseCursor(cursors_.Get(i).handle); 00304 } 00305 } 00306 } 00307 } 00308 00309 00310 //*** ShowCursor *** 00311 00312 void InputManager::ShowCursor() 00313 { 00314 cursorHidden_=false; 00315 if (currentCursor_==-1) 00316 { 00317 if (Platform::GetPlatform_Input()) 00318 { 00319 Platform::GetPlatform_Input()->SetDefaultMouseCursor(); 00320 } 00321 return; 00322 } 00323 00324 SetCursor(currentCursor_); 00325 } 00326 00327 00328 //*** HideCursor *** 00329 00330 void InputManager::HideCursor() 00331 { 00332 cursorHidden_=true; 00333 00334 if (Platform::GetPlatform_Screen()) 00335 { 00336 if (!Platform::GetPlatform_Screen()->GetFullscreen()) 00337 { 00338 return; 00339 } 00340 } 00341 00342 if (Platform::GetPlatform_Input()) 00343 { 00344 Platform::GetPlatform_Input()->SetMouseCursor(0); 00345 } 00346 } 00347 00348 00349 //*** RestoreCursor *** 00350 00351 void InputManager::RestoreCursor() 00352 { 00353 bool windowed=true; 00354 if (Platform::GetPlatform_Screen()) 00355 { 00356 windowed=!Platform::GetPlatform_Screen()->GetFullscreen(); 00357 } 00358 00359 if (windowed) 00360 { 00361 if (currentCursor_==-1) 00362 { 00363 if (Platform::GetPlatform_Input()) 00364 { 00365 Platform::GetPlatform_Input()->SetDefaultMouseCursor(); 00366 } 00367 return; 00368 } 00369 00370 SetCursor(currentCursor_); 00371 } 00372 else 00373 { 00374 if (cursorHidden_) 00375 { 00376 if (Platform::GetPlatform_Input()) 00377 { 00378 Platform::GetPlatform_Input()->SetMouseCursor(0); 00379 } 00380 } 00381 else 00382 { 00383 ShowCursor(); 00384 } 00385 } 00386 }
Reproduction/republishing of any material on this site without permission is strictly prohibited.
