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/Pad/PS2Pad.h"
00027 #include "Input/Joystick/Joystick.h"
00028
00029 namespace Lamp{
00030
00031
00032
00033 bool PS2Pad::checkCompatibility(Joystick* joystick){
00034
00035
00036 if(!joystick->hasXAxis()){ return false; }
00037 if(!joystick->hasYAxis()){ return false; }
00038 if(!joystick->hasZAxis()){ return false; }
00039 if(!joystick->hasZRotation()){ return false; }
00040
00041 if(joystick->getButtonCount() < 12){ return false; }
00042
00043 if(joystick->getPOVCount() < 1){
00044
00045 if(joystick->getButtonCount() < 16){ return false; }
00046 }
00047 return true;
00048 }
00049
00050
00051
00052
00053 PS2Pad::PS2Pad(Joystick* joystick) : Pad(joystick){
00054
00055 Assert(checkCompatibility(joystick_));
00056
00057 for(int i = 0; i < maxButtonCount; i++){ buttonMap_[i] = i; }
00058
00059 isSmartJoypad_ = false;
00060 if((joystick_->getPOVCount() < 1) && (joystick->getButtonCount() >= 16)){
00061 isSmartJoypad_ = true;
00062
00063 changeButtonMap(buttonStart, buttonSelect);
00064 }
00065 }
00066
00067
00068 PS2Pad::~PS2Pad(){
00069 }
00070
00071
00072
00073
00074 void PS2Pad::changeButtonMap(Button button, int id){
00075 Assert((button >= 0) && (button < maxButtonCount));
00076 if(buttonMap_[button] == id){ return; }
00077 int destinationID = buttonMap_[button];
00078 for(int i = 0; i < maxButtonCount; i++){
00079 if(buttonMap_[i] == id){ buttonMap_[i] = destinationID; }
00080 }
00081 buttonMap_[button] = id;
00082 }
00083
00084
00085
00086
00087 float PS2Pad::getLeftXAxis() const{
00088 return joystick_->getXAxis();
00089 }
00090
00091
00092 float PS2Pad::getLeftYAxis() const{
00093 return joystick_->getYAxis();
00094 }
00095
00096
00097 float PS2Pad::getRightXAxis() const{
00098 if(isSmartJoypad_){ return joystick_->getZRotation(); }
00099 return joystick_->getZAxis();
00100 }
00101
00102
00103 float PS2Pad::getRightYAxis() const{
00104 if(isSmartJoypad_){ return joystick_->getZAxis(); }
00105 return joystick_->getZRotation();
00106 }
00107
00108
00109
00110
00111 bool PS2Pad::upKeyPressed() const{
00112 if(isSmartJoypad_){ return buttonPressed(buttonPOVUp); }
00113 return povToUpKey(joystick_->getPOV(0));
00114 }
00115
00116
00117 bool PS2Pad::upKeyDown() const{
00118 if(isSmartJoypad_){ return buttonDown(buttonPOVUp); }
00119 return (povToUpKey(joystick_->getPOV(0)) &&
00120 (!povToUpKey(joystick_->getPrePOV(0))));
00121 }
00122
00123
00124 bool PS2Pad::upKeyUp() const{
00125 if(isSmartJoypad_){ return buttonUp(buttonPOVUp); }
00126 return (!povToUpKey(joystick_->getPOV(0)) &&
00127 (povToUpKey(joystick_->getPrePOV(0))));
00128 }
00129
00130
00131 bool PS2Pad::downKeyPressed() const{
00132 if(isSmartJoypad_){ return buttonPressed(buttonPOVDown); }
00133 return povToDownKey(joystick_->getPOV(0));
00134 }
00135
00136
00137 bool PS2Pad::downKeyDown() const{
00138 if(isSmartJoypad_){ return buttonDown(buttonPOVDown); }
00139 return (povToDownKey(joystick_->getPOV(0)) &&
00140 (!povToDownKey(joystick_->getPrePOV(0))));
00141 }
00142
00143
00144 bool PS2Pad::downKeyUp() const{
00145 if(isSmartJoypad_){ return buttonUp(buttonPOVDown); }
00146 return (!povToDownKey(joystick_->getPOV(0)) &&
00147 (povToDownKey(joystick_->getPrePOV(0))));
00148 }
00149
00150
00151 bool PS2Pad::leftKeyPressed() const{
00152 if(isSmartJoypad_){ return buttonPressed(buttonPOVLeft); }
00153 return povToLeftKey(joystick_->getPOV(0));
00154 }
00155
00156
00157 bool PS2Pad::leftKeyDown() const{
00158 if(isSmartJoypad_){ return buttonDown(buttonPOVLeft); }
00159 return (povToLeftKey(joystick_->getPOV(0)) &&
00160 (!povToLeftKey(joystick_->getPrePOV(0))));
00161 }
00162
00163
00164 bool PS2Pad::leftKeyUp() const{
00165 if(isSmartJoypad_){ return buttonUp(buttonPOVLeft); }
00166 return (!povToLeftKey(joystick_->getPOV(0)) &&
00167 (povToLeftKey(joystick_->getPrePOV(0))));
00168 }
00169
00170
00171 bool PS2Pad::rightKeyPressed() const{
00172 if(isSmartJoypad_){ return buttonPressed(buttonPOVRight); }
00173 return povToRightKey(joystick_->getPOV(0));
00174 }
00175
00176
00177 bool PS2Pad::rightKeyDown() const{
00178 if(isSmartJoypad_){ return buttonDown(buttonPOVRight); }
00179 return (povToRightKey(joystick_->getPOV(0)) &&
00180 (!povToRightKey(joystick_->getPrePOV(0))));
00181 }
00182
00183
00184 bool PS2Pad::rightKeyUp() const{
00185 if(isSmartJoypad_){ return buttonUp(buttonPOVRight); }
00186 return (!povToRightKey(joystick_->getPOV(0)) &&
00187 (povToRightKey(joystick_->getPrePOV(0))));
00188 }
00189
00190
00191
00192
00193 bool PS2Pad::buttonPressed(Button button) const{
00194 Assert((button >= 0) && (button < maxButtonCount));
00195 return joystick_->buttonPressed(buttonMap_[button]);
00196 }
00197
00198
00199 bool PS2Pad::buttonDown(Button button) const{
00200 Assert((button >= 0) && (button < maxButtonCount));
00201 return joystick_->buttonDown(buttonMap_[button]);
00202 }
00203
00204
00205 bool PS2Pad::buttonUp(Button button) const{
00206 Assert((button >= 0) && (button < maxButtonCount));
00207 return joystick_->buttonUp(buttonMap_[button]);
00208 }
00209
00210
00211
00212
00213 String PS2Pad::getButtonString(Button button){
00214 if(button == -1){ return "Unknown"; }
00215 Assert((button >= 0) && (button < maxButtonCount));
00216 String buttonString[] = {
00217 "Sankaku", "Maru", "Batu", "Sikaku",
00218 "L2", "R2", "L1", "R1", "Start", "Select", "L3", "R3",
00219 };
00220 return buttonString[button];
00221 }
00222
00223
00224 String PS2Pad::toString() const{
00225 String result, temp;
00226 result = Pad::toString();
00227
00228 temp.format(" Axis ( %5.2f , %5.2f ) ( %5.2f , %5.2f )\n",
00229 getLeftXAxis(), getLeftYAxis(), getRightXAxis(), getRightYAxis());
00230 result += temp;
00231
00232 result += " Press Down Up\n";
00233 temp.format(" UpKey %d %d %d\n",
00234 upKeyPressed(), upKeyDown(), upKeyUp());
00235 result += temp;
00236 temp.format(" DownKey %d %d %d\n",
00237 downKeyPressed(), downKeyDown(), downKeyUp());
00238 result += temp;
00239 temp.format(" LeftKey %d %d %d\n",
00240 leftKeyPressed(), leftKeyDown(), leftKeyUp());
00241 result += temp;
00242 temp.format(" RightKey %d %d %d\n",
00243 rightKeyPressed(), rightKeyDown(), rightKeyUp());
00244 result += temp;
00245
00246 temp.format(" Sankaku %d %d %d\n",
00247 buttonPressed(buttonSankaku), buttonDown(buttonSankaku),
00248 buttonUp(buttonSankaku));
00249 result += temp;
00250 temp.format(" Maru %d %d %d\n",
00251 buttonPressed(buttonMaru), buttonDown(buttonMaru),
00252 buttonUp(buttonMaru));
00253 result += temp;
00254 temp.format(" Batu %d %d %d\n",
00255 buttonPressed(buttonBatu), buttonDown(buttonBatu),
00256 buttonUp(buttonBatu));
00257 result += temp;
00258 temp.format(" Sikaku %d %d %d\n",
00259 buttonPressed(buttonSikaku), buttonDown(buttonSikaku),
00260 buttonUp(buttonSikaku));
00261 result += temp;
00262 temp.format(" L2 %d %d %d\n",
00263 buttonPressed(buttonL2), buttonDown(buttonL2),
00264 buttonUp(buttonL2));
00265 result += temp;
00266 temp.format(" R2 %d %d %d\n",
00267 buttonPressed(buttonR2), buttonDown(buttonR2),
00268 buttonUp(buttonR2));
00269 result += temp;
00270 temp.format(" L1 %d %d %d\n",
00271 buttonPressed(buttonL1), buttonDown(buttonL1),
00272 buttonUp(buttonL1));
00273 result += temp;
00274 temp.format(" R1 %d %d %d\n",
00275 buttonPressed(buttonR1), buttonDown(buttonR1),
00276 buttonUp(buttonR1));
00277 result += temp;
00278 temp.format(" Start %d %d %d\n",
00279 buttonPressed(buttonStart), buttonDown(buttonStart),
00280 buttonUp(buttonStart));
00281 result += temp;
00282 temp.format(" Select %d %d %d\n",
00283 buttonPressed(buttonSelect), buttonDown(buttonSelect),
00284 buttonUp(buttonSelect));
00285 result += temp;
00286 temp.format(" L3 %d %d %d\n",
00287 buttonPressed(buttonL3), buttonDown(buttonL3),
00288 buttonUp(buttonL3));
00289 result += temp;
00290 temp.format(" R3 %d %d %d\n",
00291 buttonPressed(buttonR3), buttonDown(buttonR3),
00292 buttonUp(buttonR3));
00293 result += temp;
00294
00295 return result;
00296 }
00297
00298 }
00299