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

SFPad.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  * SF互換パッド実装
00022  * @author Junpee
00023  */
00024 
00025 #include "LampBasic.h"
00026 #include "Input/Pad/SFPad.h"
00027 #include "Input/Joystick/Joystick.h"
00028 #include "Input/Pad/PS2Pad.h"
00029 
00030 namespace Lamp{
00031 
00032 //------------------------------------------------------------------------------
00033 // 互換性チェック
00034 bool SFPad::checkCompatibility(Joystick* joystick){
00035     // X軸、Y軸が必要
00036     if(!joystick->hasXAxis()){ return false; }
00037     if(!joystick->hasYAxis()){ return false; }
00038     // ボタンは8ボタン必要
00039     if(joystick->getButtonCount() < 8){ return false; }
00040     return true;
00041 }
00042 //------------------------------------------------------------------------------
00043 // 生成、破棄
00044 //------------------------------------------------------------------------------
00045 // コンストラクタ
00046 SFPad::SFPad(Joystick* joystick) :
00047     Pad(joystick), digitalBoundary_(0.6f){
00048     // 互換性チェック
00049     Assert(checkCompatibility(joystick_));
00050     // ボタンマップの初期化を戦略的に行う
00051     // 究極は名前ベースでのボタン割り振り?
00052     int buttonCount = joystick_->getButtonCount();
00053     if(PS2Pad::checkCompatibility(joystick_) &&
00054         (buttonCount == 12)){
00055         // PS2変換ケーブルパッド用割り振り
00056         buttonMap_[buttonA] = PS2Pad::buttonMaru;
00057         buttonMap_[buttonB] = PS2Pad::buttonBatu;
00058         buttonMap_[buttonX] = PS2Pad::buttonSankaku;
00059         buttonMap_[buttonY] = PS2Pad::buttonSikaku;
00060         buttonMap_[buttonL] = PS2Pad::buttonL1;
00061         buttonMap_[buttonR] = PS2Pad::buttonR1;
00062         buttonMap_[buttonStart] = PS2Pad::buttonStart;
00063         buttonMap_[buttonSelect] = PS2Pad::buttonSelect;
00064     }else if(PS2Pad::checkCompatibility(joystick_) &&
00065         (buttonCount == 16)){
00066         // スマートジョイパッド用割り振り
00067         buttonMap_[buttonA] = PS2Pad::buttonMaru;
00068         buttonMap_[buttonB] = PS2Pad::buttonBatu;
00069         buttonMap_[buttonX] = PS2Pad::buttonSankaku;
00070         buttonMap_[buttonY] = PS2Pad::buttonSikaku;
00071         buttonMap_[buttonL] = PS2Pad::buttonL1;
00072         buttonMap_[buttonR] = PS2Pad::buttonR1;
00073         buttonMap_[buttonStart] = PS2Pad::buttonSelect;
00074         buttonMap_[buttonSelect] = PS2Pad::buttonStart;
00075 /* // コンフィグで対応するべきっぽい
00076     }else if(buttonCount == 11){
00077         // エレコムの11ボタンパッドと仮定する
00078         buttonMap_[buttonA] = 2;
00079         buttonMap_[buttonB] = 1;
00080         buttonMap_[buttonX] = 5;
00081         buttonMap_[buttonY] = 4;
00082         buttonMap_[buttonL] = 6;
00083         buttonMap_[buttonR] = 7;
00084         buttonMap_[buttonStart] = 10;
00085         buttonMap_[buttonSelect] = 8;
00086     }else if(buttonCount == 10){
00087         // サンワの10ボタンパッドと仮定する
00088         buttonMap_[buttonA] = 2;
00089         buttonMap_[buttonB] = 0;
00090         buttonMap_[buttonX] = 3;
00091         buttonMap_[buttonY] = 1;
00092         buttonMap_[buttonL] = 4;
00093         buttonMap_[buttonR] = 6;
00094         buttonMap_[buttonStart] = 9;
00095         buttonMap_[buttonSelect] = 8;
00096     }else if(buttonCount == 9){
00097         // エレコムの9ボタンパッドと仮定する
00098         buttonMap_[buttonA] = 2;
00099         buttonMap_[buttonB] = 1;
00100         buttonMap_[buttonX] = 5;
00101         buttonMap_[buttonY] = 4;
00102         buttonMap_[buttonL] = 6;
00103         buttonMap_[buttonR] = 7;
00104         buttonMap_[buttonStart] = 0;
00105         buttonMap_[buttonSelect] = 3;
00106     }else if(buttonCount == 8){
00107         // サンワの8ボタンパッドと仮定する
00108         buttonMap_[buttonA] = 2;
00109         buttonMap_[buttonB] = 0;
00110         buttonMap_[buttonX] = 3;
00111         buttonMap_[buttonY] = 1;
00112         buttonMap_[buttonL] = 4;
00113         buttonMap_[buttonR] = 6;
00114         buttonMap_[buttonStart] = 7;
00115         buttonMap_[buttonSelect] = 5;
00116 //*/
00117     }else{
00118         // ボタンは前から、L、R、Start、Selectは後ろから割り振る
00119         for(int i = 0;i < 4; i++){ buttonMap_[i] = i; }
00120         int offset = buttonCount - 4;
00121         for(int i = 0;i < 4; i++){ buttonMap_[i + 4] = offset + i; }
00122     }
00123 }
00124 //------------------------------------------------------------------------------
00125 // デストラクタ
00126 SFPad::~SFPad(){
00127 }
00128 //------------------------------------------------------------------------------
00129 // ボタンマップ
00130 //------------------------------------------------------------------------------
00131 // ボタンマップの変更
00132 void SFPad::changeButtonMap(Button button, int id){
00133     Assert((button >= 0) && (button < maxButtonCount));
00134     if(buttonMap_[button] == id){ return; }
00135     int destinationID = buttonMap_[button];
00136     for(int i = 0; i < maxButtonCount; i++){
00137         if(buttonMap_[i] == id){ buttonMap_[i] = destinationID; }
00138     }
00139     buttonMap_[button] = id;
00140 }
00141 //------------------------------------------------------------------------------
00142 // 軸データの取得
00143 //------------------------------------------------------------------------------
00144 // X軸の取得
00145 float SFPad::getXAxis() const{
00146     return joystick_->getXAxis();
00147 }
00148 //------------------------------------------------------------------------------
00149 // Y軸の取得
00150 float SFPad::getYAxis() const{
00151     return joystick_->getYAxis();
00152 }
00153 //------------------------------------------------------------------------------
00154 // 十字キーデータの取得
00155 //------------------------------------------------------------------------------
00156 // 上キーが押されているか
00157 bool SFPad::upKeyPressed() const{
00158     return yAxisToUpKey(joystick_->getYAxis());
00159 }
00160 //------------------------------------------------------------------------------
00161 // 上キーが下がった
00162 bool SFPad::upKeyDown() const{
00163     return (yAxisToUpKey(joystick_->getYAxis()) &&
00164         (!yAxisToUpKey(joystick_->getPreYAxis())));
00165 }
00166 //------------------------------------------------------------------------------
00167 // 上キーが上がった
00168 bool SFPad::upKeyUp() const{
00169     return (!yAxisToUpKey(joystick_->getYAxis()) &&
00170         (yAxisToUpKey(joystick_->getPreYAxis())));
00171 }
00172 //------------------------------------------------------------------------------
00173 // 下キーが押されているか
00174 bool SFPad::downKeyPressed() const{
00175     return yAxisToDownKey(joystick_->getYAxis());
00176 }
00177 //------------------------------------------------------------------------------
00178 // 下キーが下がった
00179 bool SFPad::downKeyDown() const{
00180     return (yAxisToDownKey(joystick_->getYAxis()) &&
00181         (!yAxisToDownKey(joystick_->getPreYAxis())));
00182 }
00183 //------------------------------------------------------------------------------
00184 // 下キーが上がった
00185 bool SFPad::downKeyUp() const{
00186     return (!yAxisToDownKey(joystick_->getYAxis()) &&
00187         (yAxisToDownKey(joystick_->getPreYAxis())));
00188 }
00189 //------------------------------------------------------------------------------
00190 // 左キーが押されているか
00191 bool SFPad::leftKeyPressed() const{
00192     return xAxisToLeftKey(joystick_->getXAxis());
00193 }
00194 //------------------------------------------------------------------------------
00195 // 左キーが下がった
00196 bool SFPad::leftKeyDown() const{
00197     return (xAxisToLeftKey(joystick_->getXAxis()) &&
00198         (!xAxisToLeftKey(joystick_->getPreXAxis())));
00199 }
00200 //------------------------------------------------------------------------------
00201 // 左キーが上がった
00202 bool SFPad::leftKeyUp() const{
00203     return (!xAxisToLeftKey(joystick_->getXAxis()) &&
00204         (xAxisToLeftKey(joystick_->getPreXAxis())));
00205 }
00206 //------------------------------------------------------------------------------
00207 // 右キーが押されているか
00208 bool SFPad::rightKeyPressed() const{
00209     return xAxisToRightKey(joystick_->getXAxis());
00210 }
00211 //------------------------------------------------------------------------------
00212 // 右キーが下がった
00213 bool SFPad::rightKeyDown() const{
00214     return (xAxisToRightKey(joystick_->getXAxis()) &&
00215         (!xAxisToRightKey(joystick_->getPreXAxis())));
00216 }
00217 //------------------------------------------------------------------------------
00218 // 右キーが上がった
00219 bool SFPad::rightKeyUp() const{
00220     return (!xAxisToRightKey(joystick_->getXAxis()) &&
00221         (xAxisToRightKey(joystick_->getPreXAxis())));
00222 }
00223 //------------------------------------------------------------------------------
00224 // ボタンデータの取得
00225 //------------------------------------------------------------------------------
00226 // ボタンが押されているか
00227 bool SFPad::buttonPressed(Button button) const{
00228     Assert((button >= 0) && (button < maxButtonCount));
00229     return joystick_->buttonPressed(buttonMap_[button]);
00230 }
00231 //------------------------------------------------------------------------------
00232 // ボタンが下がった
00233 bool SFPad::buttonDown(Button button) const{
00234     Assert((button >= 0) && (button < maxButtonCount));
00235     return joystick_->buttonDown(buttonMap_[button]);
00236 }
00237 //------------------------------------------------------------------------------
00238 // ボタンが上がった
00239 bool SFPad::buttonUp(Button button) const{
00240     Assert((button >= 0) && (button < maxButtonCount));
00241     return joystick_->buttonUp(buttonMap_[button]);
00242 }
00243 //------------------------------------------------------------------------------
00244 // 文字列データ取得
00245 //------------------------------------------------------------------------------
00246 // ボタン文字列の取得
00247 String SFPad::getButtonString(Button button){
00248     if(button == -1){ return "Unknown"; }
00249     Assert((button >= 0) && (button < maxButtonCount));
00250     String buttonString[] = { "A", "B", "X", "Y", "L", "R", "Start", "Select" };
00251     return buttonString[button];
00252 }
00253 //------------------------------------------------------------------------------
00254 // 文字列への変換
00255 String SFPad::toString() const{
00256     String result, temp;
00257     result = Pad::toString();
00258 
00259     // 十字キー
00260     result += "          Press Down   Up\n";
00261     temp.format(" UpKey        %d    %d    %d\n",
00262         upKeyPressed(), upKeyDown(), upKeyUp());
00263     result += temp;
00264     temp.format(" DownKey      %d    %d    %d\n",
00265         downKeyPressed(), downKeyDown(), downKeyUp());
00266     result += temp;
00267     temp.format(" LeftKey      %d    %d    %d\n",
00268         leftKeyPressed(), leftKeyDown(), leftKeyUp());
00269     result += temp;
00270     temp.format(" RightKey     %d    %d    %d\n",
00271         rightKeyPressed(), rightKeyDown(), rightKeyUp());
00272     result += temp;
00273 
00274     // ボタン
00275     temp.format(" A            %d    %d    %d\n",
00276         buttonPressed(buttonA), buttonDown(buttonA), buttonUp(buttonA));
00277     result += temp;
00278     temp.format(" B            %d    %d    %d\n",
00279         buttonPressed(buttonB), buttonDown(buttonB), buttonUp(buttonB));
00280     result += temp;
00281     temp.format(" X            %d    %d    %d\n",
00282         buttonPressed(buttonX), buttonDown(buttonX), buttonUp(buttonX));
00283     result += temp;
00284     temp.format(" Y            %d    %d    %d\n",
00285         buttonPressed(buttonY), buttonDown(buttonY), buttonUp(buttonY));
00286     result += temp;
00287     temp.format(" L            %d    %d    %d\n",
00288         buttonPressed(buttonL), buttonDown(buttonL), buttonUp(buttonL));
00289     result += temp;
00290     temp.format(" R            %d    %d    %d\n",
00291         buttonPressed(buttonR), buttonDown(buttonR), buttonUp(buttonR));
00292     result += temp;
00293     temp.format(" Start        %d    %d    %d\n",
00294         buttonPressed(buttonStart), buttonDown(buttonStart),
00295         buttonUp(buttonStart));
00296     result += temp;
00297     temp.format(" Select       %d    %d    %d\n",
00298         buttonPressed(buttonSelect), buttonDown(buttonSelect),
00299         buttonUp(buttonSelect));
00300     result += temp;
00301     return result;
00302 }
00303 //------------------------------------------------------------------------------
00304 } // End of namespace Lamp
00305 //------------------------------------------------------------------------------

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