00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "CATControllerMapper.h"
00014 #include "CATDirectInput.h"
00015 #include "CATDIJoystick.h"
00016
00017 CATControllerMapper::CATControllerMapper()
00018 {
00019 fDirectInput = 0;
00020 fController = 0;
00021 fMap = 0;
00022 fPollFreq = 50;
00023 fValidAxis = 0;
00024 fInitialized = false;
00025 }
00026
00027 CATControllerMapper::~CATControllerMapper()
00028 {
00029 if (fInitialized)
00030 (void)Uninitialize();
00031 }
00032
00033 CATResult CATControllerMapper::Initialize()
00034 {
00035 if (fInitialized)
00036 return CAT_SUCCESS;
00037
00038 CATResult result;
00039
00040 fDirectInput = new CATDirectInput();
00041 result = fDirectInput->Init();
00042 if (CATFAILED(result))
00043 Uninitialize();
00044 else
00045 fInitialized = true;
00046
00047 return result;
00048 }
00049
00050 CATResult CATControllerMapper::Uninitialize()
00051 {
00052 StopMapping();
00053
00054 if (fDirectInput)
00055 {
00056 delete fDirectInput;
00057 fDirectInput = 0;
00058 }
00059 fInitialized = false;
00060 return CAT_SUCCESS;
00061 }
00062
00063
00064 void JoystickListCB(const CATWChar* name, void* userParam)
00065 {
00066 std::vector<CATString>* joystickList = (std::vector<CATString>*)userParam;
00067 joystickList->push_back(name);
00068 }
00069
00070
00071 CATResult CATControllerMapper::GetControllerList( std::vector<CATString>& controllerList )
00072 {
00073 if (!fInitialized)
00074 return CATRESULT(CAT_ERR_NOT_INITIALIZED);
00075
00076 fDirectInput->EnumJoysticks(JoystickListCB,&controllerList);
00077
00078 return CAT_SUCCESS;
00079 }
00080
00081 CATResult CATControllerMapper::GetMappingList( const CATString& controllerName,
00082 std::vector<CATString>& mappingList)
00083 {
00084 return CAT_SUCCESS;
00085 }
00086
00087 CATResult CATControllerMapper::StartMapping( const CATString& controllerName,
00088 const CATString& mappingName)
00089 {
00090 fController = fDirectInput->CreateJoystick(controllerName);
00091
00092 if (!fController)
00093 return CAT_ERROR;
00094
00095 fValidAxis = fController->GetValidAxis();
00096
00097 fController->GetStatus(fLastStatus);
00098 fMap = new CATControlMap();
00099
00100 this->Start(fPollFreq);
00101
00102 return CAT_SUCCESS;
00103 }
00104
00105 CATResult CATControllerMapper::StopMapping()
00106 {
00107 this->Stop();
00108
00109 if (fController)
00110 {
00111 delete fController;
00112 fController = 0;
00113 }
00114
00115 if (fMap)
00116 {
00117 delete fMap;
00118 fMap = 0;
00119 }
00120 return CAT_SUCCESS;
00121 }
00122
00123
00124 void CATControllerMapper::OnThreadIdle()
00125 {
00126 if (fController)
00127 {
00128 CATJOYSTICKSTRUCT status;
00129
00130 if (fController->GetStatus(status))
00131 {
00132 for (CATUInt32 i = 0; i < 12; i++)
00133 {
00134 if ((status.buttonMap & (1 << i)) != (fLastStatus.buttonMap & (1 << i)))
00135 {
00136 OnButtonChange( 0 != (status.buttonMap & (1 << i)), i);
00137 }
00138 }
00139
00140
00141
00142 OnAxis(status.xAxis, CATJoystick::X_AXIS);
00143 OnAxis(status.xHigh, CATJoystick::X_AXIS_HIGH);
00144 OnAxis(status.xLow, CATJoystick::X_AXIS_LOW);
00145 OnAxis(status.xRot, CATJoystick::X_ROT);
00146 OnAxis(status.xRotLow, CATJoystick::X_ROT_LOW);
00147 OnAxis(status.xRotHigh, CATJoystick::X_ROT_HIGH);
00148 OnAxis(status.yAxis, CATJoystick::Y_AXIS);
00149 OnAxis(status.yHigh, CATJoystick::Y_AXIS_HIGH);
00150 OnAxis(status.yLow, CATJoystick::Y_AXIS_LOW);
00151 OnAxis(status.yRot, CATJoystick::Y_ROT);
00152 OnAxis(status.yRotLow, CATJoystick::Y_ROT_LOW);
00153 OnAxis(status.yRotHigh, CATJoystick::Y_ROT_HIGH);
00154 OnAxis(status.zAxis, CATJoystick::Z_AXIS);
00155 OnAxis(status.zHigh, CATJoystick::Z_AXIS_HIGH);
00156 OnAxis(status.zLow, CATJoystick::Z_AXIS_LOW);
00157 OnAxis(status.zRot, CATJoystick::Z_ROT);
00158 OnAxis(status.zRotLow, CATJoystick::Z_ROT_LOW);
00159 OnAxis(status.zRotHigh, CATJoystick::Z_ROT_HIGH);
00160
00161 fLastStatus = status;
00162 }
00163
00164 }
00165 }
00166
00167 CATUInt32 CATControllerMapper::OnThreadMessage(CATUInt32 msg, CATUInt32 wParam, CATUInt32 lParam)
00168 {
00169 return 0;
00170 }
00171
00172 void CATControllerMapper::OnAxis(CATInt32 val, CATJoystick::AXIS_TYPE axisType)
00173 {
00174 }
00175
00176
00177 void CATControllerMapper::OnButtonChange(bool state, CATUInt32 button)
00178 {
00179
00180 }