Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

PS2Pad.cpp

Go to the documentation of this file.
00001 //------------------------------------------------------------------------------
00002 // Lamp : Open source game middleware
00003 // Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
00004 //
00005 // This library is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 //
00010 // This library is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with this library; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 //------------------------------------------------------------------------------
00019 
00020 /** @file
00021  * PS2互換パッド実装
00022  * @author Junpee
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     // X軸、Y軸、Z軸、Z回転が必要
00035     // 互換パッドでこれ以外の割り振りが存在すればAxisMapに対応する
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     // ボタンは12ボタン必要
00041     if(joystick->getButtonCount() < 12){ return false; }
00042     // 視点コントローラが一つ必要
00043     if(joystick->getPOVCount() < 1){
00044         // 16ボタンあればSmartJoypadと判定する
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     // POVが無くボタンが16以上あればスマートジョイパッド判定
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 // 左X軸の取得
00087 float PS2Pad::getLeftXAxis() const{
00088     return joystick_->getXAxis();
00089 }
00090 //------------------------------------------------------------------------------
00091 // 左Y軸の取得
00092 float PS2Pad::getLeftYAxis() const{
00093     return joystick_->getYAxis();
00094 }
00095 //------------------------------------------------------------------------------
00096 // 右X軸の取得
00097 float PS2Pad::getRightXAxis() const{
00098     if(isSmartJoypad_){ return joystick_->getZRotation(); }
00099     return joystick_->getZAxis();
00100 }
00101 //------------------------------------------------------------------------------
00102 // 右Y軸の取得
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 } // End of namespace Lamp
00299 //------------------------------------------------------------------------------

Generated on Wed Mar 16 10:29:34 2005 for Lamp by doxygen 1.3.2