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/Joystick/JoystickDevice.h"
00027
00028 namespace Lamp{
00029
00030
00031
00032 JoystickDevice::JoystickDevice() : InputDevice(true), povCount_(0),
00033 sliderCount_(0), hasXAxis_(false), hasYAxis_(false), hasZAxis_(false),
00034 hasXRotation_(false), hasYRotation_(false), hasZRotation_(false){
00035 }
00036
00037
00038 JoystickDevice::~JoystickDevice(){
00039 }
00040
00041
00042 bool JoystickDevice::initialize(DirectInputDevice* inputDevice, HWND windowHandle){
00043 if(!InputDevice::initialize(inputDevice, windowHandle)){ return false; }
00044
00045 if(DirectXFailed(inputDevice->SetDataFormat(&c_dfDIJoystick2))){
00046 ErrorOut("JoystickDevice::initialize() "
00047 "ジョイスティックデバイスのデータフォーマット指定に失敗しました。");
00048 return false;
00049 }
00050
00051 if(DirectXFailed(inputDevice->EnumObjects(
00052 joystickObjectEnumeration, this, DIDFT_ALL))){
00053 ErrorOut("JoystickDevice::initialize() "
00054 "ジョイスティックデバイスオブジェクトの列挙に失敗しました。");
00055 return false;
00056 }
00057
00058 if(!setCooperativeLevel(isExclusive(), isForeground())){ return false; }
00059 return true;
00060 }
00061
00062
00063 int __stdcall JoystickDevice::joystickObjectEnumeration(
00064 const DIDEVICEOBJECTINSTANCE* instance, void* userData){
00065 JoystickDevice* joystickDevice = (JoystickDevice* )userData;
00066 if(!joystickDevice->checkJoystickObject(instance)){ return DIENUM_STOP; }
00067 return DIENUM_CONTINUE;
00068 }
00069
00070
00071 bool JoystickDevice::checkJoystickObject(
00072 const DIDEVICEOBJECTINSTANCE* instance){
00073
00074 if((instance->dwType & DIDFT_AXIS) != 0){
00075 DIPROPRANGE range;
00076 range.diph.dwSize = sizeof(DIPROPRANGE);
00077 range.diph.dwHeaderSize = sizeof(DIPROPHEADER);
00078 range.diph.dwHow = DIPH_BYID;
00079 range.diph.dwObj = instance->dwType;
00080 range.lMin = minAxisValue;
00081 range.lMax = maxAxisValue;
00082 if(DirectXFailed(inputDevice_->SetProperty(DIPROP_RANGE, &range.diph))){
00083 ErrorOut("JoystickDevice::initialize() "
00084 "ジョイスティックの軸範囲設定に失敗しました。");
00085 return false;
00086 }
00087 }
00088
00089 if(((instance->dwType & DIDFT_AXIS) != 0) &&
00090 (instance->guidType != GUID_Slider)){
00091 DIPROPDWORD property;
00092 property.diph.dwSize = sizeof(DIPROPDWORD);
00093 property.diph.dwHeaderSize = sizeof(DIPROPHEADER);
00094 property.diph.dwHow = DIPH_BYID;
00095 property.diph.dwObj = instance->dwType;
00096 property.dwData = deadZone_;
00097 if(DirectXFailed(inputDevice_->SetProperty(
00098 DIPROP_DEADZONE, &property.diph))){
00099 ErrorOut("JoystickDevice::initialize() "
00100 "ジョイスティックのデッドゾーン設定に失敗しました。");
00101 return false;
00102 }
00103 property.dwData = saturationZone_;
00104 if(DirectXFailed(inputDevice_->SetProperty(
00105 DIPROP_SATURATION, &property.diph))){
00106 ErrorOut("JoystickDevice::initialize() "
00107 "ジョイスティックの飽和ゾーン設定に失敗しました。");
00108 return false;
00109 }
00110 }
00111
00112 if(instance->guidType == GUID_XAxis){ hasXAxis_ = true; }
00113
00114 if(instance->guidType == GUID_YAxis){ hasYAxis_ = true; }
00115
00116 if(instance->guidType == GUID_ZAxis){ hasZAxis_ = true; }
00117
00118 if(instance->guidType == GUID_RxAxis){ hasXRotation_ = true; }
00119
00120 if(instance->guidType == GUID_RyAxis){ hasYRotation_ = true; }
00121
00122 if(instance->guidType == GUID_RzAxis){ hasZRotation_ = true; }
00123
00124 if(instance->guidType == GUID_POV){ povCount_++; }
00125
00126 if(instance->guidType == GUID_Slider){ sliderCount_++; }
00127 return true;
00128 }
00129
00130
00131 bool JoystickDevice::polling(){
00132 InputDevice::polling();
00133
00134 DIJOYSTATE2 state;
00135 HRESULT result = inputDevice_->GetDeviceState(
00136 sizeof(DIJOYSTATE2), &state);
00137 if(DirectXSucceeded(result)){
00138
00139 joystickState_.setXAxis(state.lX);
00140 joystickState_.setYAxis(state.lY);
00141 joystickState_.setZAxis(state.lZ);
00142 joystickState_.setXRotation(state.lRx);
00143 joystickState_.setYRotation(state.lRy);
00144 joystickState_.setZRotation(state.lRz);
00145 for(int i = 0; i < maxPOVCount; i++){
00146 joystickState_.setPOV(i, state.rgdwPOV[i]);
00147 }
00148 for(int i = 0; i < maxSliderCount; i++){
00149 joystickState_.setSlider(i, state.rglSlider[i]);
00150 }
00151 for(int i = 0; i < maxButtonCount; i++){
00152 joystickState_.setButtonPressed(i,
00153 ((state.rgbButtons[i] & 0x80) != 0));
00154 }
00155 return true;
00156 }else{
00157
00158 joystickState_.clear();
00159
00160 if((result != DIERR_INPUTLOST) && (result != DIERR_NOTACQUIRED)){
00161 ErrorOut("GetDeviceState()に失敗しました。");
00162 }else{
00163
00164 acquire();
00165 }
00166 }
00167 return false;
00168 }
00169
00170
00171 String JoystickDevice::toString() const{
00172 String result = getInputDeviceString();
00173 String joystickString;
00174 joystickString.format("Axis ( %d %d %d ) Rotation ( %d %d %d ) "
00175 "POV %d Slider %d\n",
00176 hasXAxis(), hasYAxis(), hasZAxis(),
00177 hasXRotation(), hasYRotation(), hasZRotation(),
00178 getPOVCount(), getSliderCount());
00179 result += joystickString;
00180 result += joystickState_.toString();
00181 return result;
00182 }
00183
00184 }
00185