00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "LampBasic.h"
00026 #include "Input/System/LampInput.h"
00027 #include "Input/System/BufferedInput.h"
00028 #include "Input/Keyboard/KeyboardKey.h"
00029 #include "Input/Keyboard/Keyboard.h"
00030 #include "Input/Keyboard/KeyboardDevice.h"
00031 #include "Input/Mouse/Mouse.h"
00032 #include "Input/Mouse/MouseDevice.h"
00033 #include "Input/Joystick/Joystick.h"
00034 #include "Input/Joystick/JoystickDevice.h"
00035 #include "Core/InputOutput/BinaryFileWriter.h"
00036 #include "Core/InputOutput/BinaryFileReader.h"
00037
00038 namespace Lamp{
00039
00040
00041 HWND LampInput::windowHandle_ = NULL;
00042
00043 DirectInput* LampInput::directInput_ = NULL;
00044
00045 LampInput::InputMode LampInput::inputMode_ = LampInput::modePolling;
00046
00047 BufferedInput* LampInput::bufferedInput_ = NULL;
00048
00049
00050 BinaryWriter* LampInput::logWriter_ = NULL;
00051
00052 BinaryWriter* LampInput::binaryFileWriter_ = NULL;
00053
00054 BinaryReader* LampInput::logReader_ = NULL;
00055
00056 BinaryReader* LampInput::binaryFileReader_ = NULL;
00057
00058
00059 Keyboard* LampInput::keyboard_ = NULL;
00060
00061 KeyboardDevice* LampInput::keyboardDevice_ = NULL;
00062
00063 Mouse* LampInput::mouse_ = NULL;
00064
00065 MouseDevice* LampInput::mouseDevice_ = NULL;
00066
00067 ArrayList<Joystick*> LampInput::joysticks_;
00068
00069 ArrayList<JoystickDevice*> LampInput::joystickDevices_;
00070
00071
00072 bool LampInput::isInitialized_ = false;
00073
00074 bool LampInput::isLogging_ = false;
00075
00076 bool LampInput::isLogPlaying_ = false;
00077
00078
00079
00080
00081
00082 bool LampInput::initialize(
00083 HINSTANCE instanceHandle, HWND windowHandle, InputMode inputMode){
00084 if(isInitialized_){ return true; }
00085 Assert(inputMode_ == modePolling);
00086 windowHandle_ = windowHandle;
00087 LampCore::initialize();
00088 KeyboardKey::initializeKeyString();
00089
00090 if(DirectXFailed(DirectInputCreate(instanceHandle, DIRECTINPUT_VERSION,
00091 DirectInputInterfaceID, (void**)&directInput_, NULL))){
00092 ErrorOut("LampInput::initialize() "
00093 "DirectInputの作成に失敗しました");
00094 return false;
00095 }
00096
00097
00098 DirectInputDevice* device;
00099 if(DirectXFailed(directInput_->CreateDevice(
00100 GUID_SysKeyboard, &device, NULL))){
00101 ErrorOut("LampInput::initialize() "
00102 "キーボードデバイスの初期化に失敗しました");
00103 return false;
00104 }
00105 keyboardDevice_ = new KeyboardDevice();
00106 if(!keyboardDevice_->initialize(device, windowHandle_)){ return false; }
00107 keyboard_ = new Keyboard(keyboardDevice_);
00108
00109
00110 if(DirectXFailed(directInput_->CreateDevice(
00111 GUID_SysMouse, &device, NULL))){
00112 ErrorOut("LampInput::initialize() "
00113 "マウスデバイスの初期化に失敗しました");
00114 return false;
00115 }
00116 mouseDevice_ = new MouseDevice();
00117 if(!mouseDevice_->initialize(device, windowHandle_)){ return false; }
00118 mouse_ = new Mouse(mouseDevice_);
00119
00120
00121 if(DirectXFailed(directInput_->EnumDevices(
00122 DirectInputDeviceClass_GameController, joystickEnumeration,
00123 NULL, DIEDFL_ATTACHEDONLY))){
00124 ErrorOut("LampInput::initialize() "
00125 "ジョイスティックデバイスの列挙に失敗しました");
00126 return false;
00127 }
00128
00129 isInitialized_ = true;
00130 setInputMode(inputMode);
00131 return true;
00132 }
00133
00134
00135 int __stdcall LampInput::joystickEnumeration(
00136 const DIDEVICEINSTANCE* instance, void* userData){
00137
00138 DirectInputDevice* device;
00139 if(DirectXFailed(directInput_->CreateDevice(
00140 instance->guidInstance, &device, NULL))){
00141 ErrorOut("LampInput::joystickEnumeration() "
00142 "ジョイスティックデバイスの作成に失敗しました");
00143 return DIENUM_STOP;
00144 }
00145
00146 JoystickDevice* joystickDevice = new JoystickDevice();
00147 if(!joystickDevice->initialize(device, windowHandle_)){
00148 ErrorOut("LampInput::joystickEnumeration() "
00149 "ジョイスティックデバイスの初期化に失敗しました");
00150 return DIENUM_STOP;
00151 }
00152 joystickDevices_.add(joystickDevice);
00153 joysticks_.add(new Joystick(joystickDevice));
00154 return DIENUM_CONTINUE;
00155 }
00156
00157
00158 void LampInput::finalize(){
00159
00160 if(isLogPlaying()){ stopLog(); }
00161
00162 if(isLogging()){ endLogging(); }
00163
00164 if(inputMode_ == modeBuffering){ finalizeBufferInput(); }
00165
00166 for(int i = joysticks_.getCount() - 1; i >= 0; i--){ delete joysticks_[i]; }
00167 joysticks_.clear();
00168 for(int i = joystickDevices_.getCount() - 1; i >= 0; i--){
00169 delete joystickDevices_[i];
00170 }
00171 joystickDevices_.clear();
00172 SafeDelete(mouse_);
00173 SafeDelete(mouseDevice_);
00174 SafeDelete(keyboard_);
00175 SafeDelete(keyboardDevice_);
00176
00177 SafeRelease(directInput_);
00178
00179 isInitialized_ = false;
00180 }
00181
00182
00183 void LampInput::clear(){
00184 Assert(isInitialized_);
00185 keyboard_->clear();
00186 mouse_->clear();
00187 int joystickCount = joysticks_.getCount();
00188 for(int i = 0; i < joystickCount; i++){ joysticks_[i]->clear(); }
00189 }
00190
00191
00192 void LampInput::bufferClear(){
00193 Assert(isInitialized_);
00194 Assert(inputMode_ == modeBuffering);
00195 while(bufferedInput_->hasMoreInput()){ bufferedInput_->nextInput(); }
00196
00197 clear();
00198 }
00199
00200
00201
00202
00203 void LampInput::setInputMode(InputMode inputMode){
00204 Assert(isInitialized_);
00205 Assert(!isLogPlaying());
00206 Assert(!isLogging());
00207 if(inputMode_ == inputMode){ return; }
00208
00209 if(inputMode_ == modeBuffering){ finalizeBufferInput(); }
00210 inputMode_ = inputMode;
00211
00212 if(inputMode_ == modeBuffering){ initializeBufferInput(); }
00213 }
00214
00215
00216
00217
00218 bool LampInput::polling(){
00219 Assert(isInitialized_);
00220 Assert(inputMode_ == modePolling);
00221 bool result = true;
00222 if(!isLogPlaying_){
00223
00224 if(!devicePolling()){ result = false; }
00225 }else{
00226
00227 logPolling();
00228 }
00229
00230 writeLog();
00231 return result;
00232 }
00233
00234
00235 bool LampInput::devicePolling(){
00236 bool result = true;
00237
00238 if(!keyboardDevice_->polling()){ result = false; }
00239 keyboard_->setNextState(keyboardDevice_->getKeyboardState());
00240
00241
00242 if(!mouseDevice_->polling()){ result = false; }
00243 mouse_->setNextState(mouseDevice_->getMouseState());
00244
00245
00246 int joystickDeviceCount = getJoystickDeviceCount();
00247 for(int i = 0; i < joystickDeviceCount; i++){
00248 JoystickDevice* joystickDevice = getJoystickDevice(i);
00249 if(!joystickDevice->polling()){ result = false; }
00250 getJoystick(i)->setNextState(joystickDevice->getJoystickState());
00251 }
00252 return result;
00253 }
00254
00255
00256 void LampInput::logPolling(){
00257
00258 KeyboardState keyboardState;
00259 keyboardState.readBinary(logReader_);
00260 keyboard_->setNextState(keyboardState);
00261
00262
00263 MouseState mouseState;
00264 mouseState.readBinary(logReader_);
00265 mouse_->setNextState(mouseState);
00266
00267
00268 int joystickCount = getJoystickCount();
00269 for(int i = 0; i < joystickCount; i++){
00270 JoystickState joystickState;
00271 joystickState.readBinary(logReader_);
00272 getJoystick(i)->setNextState(joystickState);
00273 }
00274
00275
00276 if(logReader_->isEnd()){ stopLog(); }
00277 }
00278
00279
00280 void LampInput::writeLog(){
00281 if(!isLogging()){ return; }
00282 keyboard_->getState().writeBinary(logWriter_);
00283 mouse_->getState().writeBinary(logWriter_);
00284 int joystickCount = getJoystickCount();
00285 for(int i = 0; i < joystickCount; i++){
00286 getJoystick(i)->getState().writeBinary(logWriter_);
00287 }
00288 }
00289
00290
00291
00292
00293 void LampInput::initializeBufferInput(){
00294 Assert(bufferedInput_ == NULL);
00295
00296 bufferedInput_ = new BufferedInput();
00297 bufferedInput_->setKeyboard(keyboard_, keyboardDevice_);
00298 bufferedInput_->setMouse(mouse_, mouseDevice_);
00299 int joystickCount = joysticks_.getCount();
00300 for(int i = 0; i < joystickCount; i++){
00301 bufferedInput_->addJoystick(joysticks_[i], joystickDevices_[i]);
00302 }
00303
00304 bufferedInput_->start();
00305 }
00306
00307
00308 void LampInput::finalizeBufferInput(){
00309 Assert(bufferedInput_ != NULL);
00310
00311 bufferedInput_->stop();
00312
00313 SafeDelete(bufferedInput_);
00314 }
00315
00316
00317 bool LampInput::hasMoreInput(){
00318 Assert(isInitialized_);
00319 Assert(inputMode_ == modeBuffering);
00320 return bufferedInput_->hasMoreInput();
00321 }
00322
00323
00324 void LampInput::waitForInput(){
00325 while(true){
00326 if(hasMoreInput()){ return; }
00327 Thread::sleep(1);
00328 }
00329 }
00330
00331
00332 void LampInput::nextInput(){
00333 Assert(isInitialized_);
00334 Assert(inputMode_ == modeBuffering);
00335 bufferedInput_->nextInput();
00336 }
00337
00338
00339 int LampInput::getInputCount(){
00340 Assert(isInitialized_);
00341 Assert(inputMode_ == modeBuffering);
00342 return bufferedInput_->getInputCount();
00343 }
00344
00345
00346
00347
00348 void LampInput::startLogging(const String& filePath){
00349 Assert(binaryFileWriter_ == NULL);
00350 binaryFileWriter_ = new BinaryFileWriter(filePath);
00351 startLogging(binaryFileWriter_);
00352 }
00353
00354
00355 void LampInput::startLogging(BinaryWriter* binaryWriter){
00356 Assert(!isLogging_);
00357 Assert(logWriter_ == NULL);
00358 logWriter_ = binaryWriter;
00359
00360 logWriter_->writeString("LampInputLog");
00361 logWriter_->writeInt(getJoystickCount());
00362 InputMode inputMode = getInputMode();
00363 logWriter_->writeInt(inputMode);
00364 if(inputMode == modeBuffering){ bufferedInput_->startLogging(logWriter_); }
00365 isLogging_ = true;
00366 }
00367
00368
00369 void LampInput::endLogging(){
00370 Assert(isLogging_);
00371 Assert(logWriter_ != NULL);
00372 isLogging_ = false;
00373 if(getInputMode() == modeBuffering){ bufferedInput_->endLogging(); }
00374 logWriter_->flush();
00375 logWriter_ = NULL;
00376 SafeDelete(binaryFileWriter_);
00377 }
00378
00379
00380
00381
00382 void LampInput::playLog(const String& filePath){
00383 Assert(binaryFileReader_ == NULL);
00384 binaryFileReader_ = new BinaryFileReader(filePath);
00385 playLog(binaryFileReader_);
00386 }
00387
00388
00389 void LampInput::playLog(BinaryReader* binaryReader){
00390 Assert(!isLogPlaying_);
00391 Assert(logReader_ == NULL);
00392 logReader_ = binaryReader;
00393
00394 String headerString = logReader_->readString();
00395 if(!headerString.equals("LampInputLog")){
00396 ErrorOut("LampInput::playLog LampInputLog形式のファイルではありません");
00397 }
00398 int joystickCount = logReader_->readInt();
00399 if(joystickCount != getJoystickCount()){
00400 ErrorOut("LampInput::playLog ジョイスティックの数が違います");
00401 }
00402
00403
00404 InputMode inputMode = (InputMode)logReader_->readInt();
00405 setInputMode(inputMode);
00406 if(inputMode == modeBuffering){ bufferedInput_->playLog(binaryReader); }
00407 isLogPlaying_ = true;
00408 }
00409
00410
00411 void LampInput::stopLog(){
00412 Assert(isLogPlaying_);
00413 Assert(logReader_ != NULL);
00414 isLogPlaying_ = false;
00415 if(getInputMode() == modeBuffering){ bufferedInput_->stopLog(); }
00416 logReader_ = NULL;
00417 SafeDelete(binaryFileReader_);
00418 }
00419
00420
00421 LRESULT LampInput::windowProcedure(
00422 HWND windowHandle, u_int message, WPARAM wParam, LPARAM lParam){
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440 return 0;
00441 }
00442
00443 }
00444