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 * ジョイスティックヘッダ 00022 * @author Junpee 00023 */ 00024 00025 #ifndef JOYSTICK_H_ 00026 #define JOYSTICK_H_ 00027 00028 #include <Input/Joystick/JoystickAxis.h> 00029 #include <Input/Joystick/JoystickState.h> 00030 #include <Input/Joystick/JoystickDevice.h> 00031 00032 namespace Lamp{ 00033 00034 //------------------------------------------------------------------------------ 00035 /** 00036 * ジョイスティック 00037 */ 00038 class Joystick : public JoystickAxis{ 00039 friend class LampInput; 00040 friend class BufferedInput; 00041 public: 00042 //-------------------------------------------------------------------------- 00043 // 定数 00044 //-------------------------------------------------------------------------- 00045 /// 最大視点コントローラ数 00046 static const int maxPOVCount = JoystickState::maxPOVCount; 00047 00048 /// 最大スライダ数 00049 static const int maxSliderCount = JoystickState::maxSliderCount; 00050 00051 /// 最大ボタン数 00052 static const int maxButtonCount = JoystickState::maxButtonCount; 00053 00054 /// 軸データ最大値 00055 static const int maxAxisValue = JoystickState::maxAxisValue; 00056 00057 /// 軸データ最小値 00058 static const int minAxisValue = JoystickState::minAxisValue; 00059 00060 /// 視点コントローラ最大値 00061 static const int maxPOVValue = JoystickState::maxPOVValue; 00062 00063 //-------------------------------------------------------------------------- 00064 // 軸 00065 //-------------------------------------------------------------------------- 00066 /** 00067 * 軸数の取得 00068 * @return 軸数 00069 */ 00070 virtual int getAxisCount() const{ return device_->getAxisCount(); } 00071 00072 /** 00073 * スライダ以外の軸数取得 00074 * @return スライダ以外の軸数 00075 */ 00076 virtual int getAxisCountWithoutSlider() const{ 00077 return (device_->getAxisCount() - device_->getSliderCount()); 00078 } 00079 00080 //-------------------------------------------------------------------------- 00081 /** 00082 * 軸を持つか 00083 * @param axis 軸 00084 * @return 軸を持っていればture 00085 */ 00086 virtual bool hasAxis(Axis axis) const{ 00087 if(axis == axisX){ return hasXAxis(); } 00088 if(axis == axisY){ return hasYAxis(); } 00089 if(axis == axisZ){ return hasZAxis(); } 00090 if(axis == axisRotationX){ return hasXRotation(); } 00091 if(axis == axisRotationY){ return hasYRotation(); } 00092 if(axis == axisRotationZ){ return hasZRotation(); } 00093 if(axis == axisSlider0){ return (getSliderCount() >= 1); } 00094 if(axis == axisSlider1){ return (getSliderCount() >= 2); } 00095 Assert(false); 00096 return false; 00097 } 00098 00099 /** 00100 * 軸の値取得 00101 * @return 軸の値 00102 */ 00103 virtual float getAxis(Axis axis) const{ 00104 if(axis == axisX){ return getXAxis(); } 00105 if(axis == axisY){ return getYAxis(); } 00106 if(axis == axisZ){ return getZAxis(); } 00107 if(axis == axisRotationX){ return getXRotation(); } 00108 if(axis == axisRotationY){ return getYRotation(); } 00109 if(axis == axisRotationZ){ return getZRotation(); } 00110 if(axis == axisSlider0){ return getSlider(0); } 00111 if(axis == axisSlider1){ return getSlider(1); } 00112 Assert(false); 00113 return 0.f; 00114 } 00115 00116 //-------------------------------------------------------------------------- 00117 /** 00118 * X軸を持つか 00119 * @return X軸を持てばtrue 00120 */ 00121 virtual bool hasXAxis() const{ return device_->hasXAxis(); } 00122 00123 /** 00124 * X軸の取得 00125 * @return 1から-1の値をとるX軸 00126 */ 00127 virtual float getXAxis() const{ 00128 return correctAxisValue(state_.getXAxis()); 00129 } 00130 00131 /** 00132 * 前回のX軸の取得 00133 * @return 1から-1の値をとるX軸 00134 */ 00135 virtual float getPreXAxis() const{ 00136 return correctAxisValue(preState_.getXAxis()); 00137 } 00138 00139 //-------------------------------------------------------------------------- 00140 /** 00141 * Y軸を持つか 00142 * @return Y軸を持てばtrue 00143 */ 00144 virtual bool hasYAxis() const{ return device_->hasYAxis(); } 00145 00146 /** 00147 * Y軸の取得 00148 * @return 1から-1の値をとるY軸 00149 */ 00150 virtual float getYAxis() const{ 00151 return correctAxisValue(state_.getYAxis()); 00152 } 00153 00154 /** 00155 * 前回のY軸の取得 00156 * @return 1から-1の値をとるY軸 00157 */ 00158 virtual float getPreYAxis() const{ 00159 return correctAxisValue(preState_.getYAxis()); 00160 } 00161 00162 //-------------------------------------------------------------------------- 00163 /** 00164 * Z軸を持つか 00165 * @return Z軸を持てばtrue 00166 */ 00167 virtual bool hasZAxis() const{ return device_->hasZAxis(); } 00168 00169 /** 00170 * Z軸の取得 00171 * @return 1から-1の値をとるZ軸 00172 */ 00173 virtual float getZAxis() const{ 00174 return correctAxisValue(state_.getZAxis()); 00175 } 00176 00177 /** 00178 * 前回のZ軸の取得 00179 * @return 1から-1の値をとるZ軸 00180 */ 00181 virtual float getPreZAxis() const{ 00182 return correctAxisValue(preState_.getZAxis()); 00183 } 00184 00185 //-------------------------------------------------------------------------- 00186 /** 00187 * X回転を持つか 00188 * @return X回転を持てばtrue 00189 */ 00190 virtual bool hasXRotation() const{ return device_->hasXRotation(); } 00191 00192 /** 00193 * X回転の取得 00194 * @return 1から-1の値をとるX回転 00195 */ 00196 virtual float getXRotation() const{ 00197 return correctAxisValue(state_.getXRotation()); 00198 } 00199 00200 /** 00201 * 前回のX回転の取得 00202 * @return 1から-1の値をとるX回転 00203 */ 00204 virtual float getPreXRotation() const{ 00205 return correctAxisValue(preState_.getXRotation()); 00206 } 00207 00208 //-------------------------------------------------------------------------- 00209 /** 00210 * Y回転を持つか 00211 * @return Y回転を持てばtrue 00212 */ 00213 virtual bool hasYRotation() const{ return device_->hasYRotation(); } 00214 00215 /** 00216 * Y回転の取得 00217 * @return 1から-1の値をとるY回転 00218 */ 00219 virtual float getYRotation() const{ 00220 return correctAxisValue(state_.getYRotation()); 00221 } 00222 00223 /** 00224 * 前回のY回転の取得 00225 * @return 1から-1の値をとるY回転 00226 */ 00227 virtual float getPreYRotation() const{ 00228 return correctAxisValue(preState_.getYRotation()); 00229 } 00230 00231 //-------------------------------------------------------------------------- 00232 /** 00233 * Z回転を持つか 00234 * @return Z回転を持てばtrue 00235 */ 00236 virtual bool hasZRotation() const{ return device_->hasZRotation(); } 00237 00238 /** 00239 * Z回転の取得 00240 * @return 1から-1の値をとるZ回転 00241 */ 00242 virtual float getZRotation() const{ 00243 return correctAxisValue(state_.getZRotation()); 00244 } 00245 00246 /** 00247 * 前回のZ回転の取得 00248 * @return 1から-1の値をとるZ回転 00249 */ 00250 virtual float getPreZRotation() const{ 00251 return correctAxisValue(preState_.getZRotation()); 00252 } 00253 00254 //-------------------------------------------------------------------------- 00255 // 視点コントローラ 00256 //-------------------------------------------------------------------------- 00257 /** 00258 * 視点コントローラ数の取得 00259 * @return 視点コントローラ数 00260 */ 00261 virtual int getPOVCount() const{ return device_->getPOVCount(); } 00262 00263 /** 00264 * 視点コントローラ値の取得 00265 * @param id 視点コントローラID 00266 * @return 視点コントローラ値 00267 */ 00268 virtual int getPOV(int id) const{ return state_.getPOV(id); } 00269 00270 /** 00271 * 前回の視点コントローラ値の取得 00272 * @param id 視点コントローラID 00273 * @return 視点コントローラ値 00274 */ 00275 virtual int getPrePOV(int id) const{ return preState_.getPOV(id); } 00276 00277 /** 00278 * 視点コントローラが中心か 00279 * @param id 視点コントローラID 00280 * @return 視点コントローラが中心ならtrue 00281 */ 00282 virtual bool isPOVCenter(int id) const{ 00283 return ((state_.getPOV(id) & 0xffff) == 0xffff); 00284 } 00285 00286 //-------------------------------------------------------------------------- 00287 // スライダー 00288 //-------------------------------------------------------------------------- 00289 /** 00290 * スライダ数の取得 00291 * @return スライダ数 00292 */ 00293 virtual int getSliderCount() const{ return device_->getSliderCount(); } 00294 00295 /** 00296 * スライダ値の取得 00297 * @param id スライダID 00298 * @return 1から-1の値をとるスライダ値 00299 */ 00300 virtual float getSlider(int id) const{ 00301 return correctSliderValue(state_.getSlider(id)); 00302 } 00303 00304 //-------------------------------------------------------------------------- 00305 // ボタン 00306 //-------------------------------------------------------------------------- 00307 /** 00308 * ボタン数の取得 00309 * @return ボタン数 00310 */ 00311 virtual int getButtonCount() const{ return device_->getButtonCount(); } 00312 00313 /** 00314 * ボタンが押されているか 00315 * @param id 対象ボタンID 00316 * @return ボタンが押されていればtrue 00317 */ 00318 virtual bool buttonPressed(int id) const{ 00319 return state_.buttonPressed(id); 00320 } 00321 00322 /** 00323 * ボタンが下がった 00324 * @param id ボタンID 00325 * @return ボタンが下がったならばtrue 00326 */ 00327 virtual bool buttonDown(int id) const{ 00328 return (state_.buttonPressed(id) && (!preState_.buttonPressed(id))); 00329 } 00330 00331 /** 00332 * ボタンが上がった 00333 * @param id ボタンID 00334 * @return ボタンが上がったならばtrue 00335 */ 00336 virtual bool buttonUp(int id) const{ 00337 return ((!state_.buttonPressed(id)) && preState_.buttonPressed(id)); 00338 } 00339 00340 //-------------------------------------------------------------------------- 00341 /** 00342 * 名前の取得 00343 * @return 名前 00344 */ 00345 virtual String getName() const{ return device_->getProductName(); } 00346 00347 /** 00348 * アタッチされているか 00349 * @return アタッチされていればtrue 00350 */ 00351 virtual bool isAttached() const{ return device_->isAttached(); } 00352 00353 /** 00354 * ポーリングが必要か 00355 * @return ポーリングが必要ならtrue 00356 */ 00357 virtual bool isPolled() const{ return device_->isPolled(); } 00358 00359 /** 00360 * 文字列への変換 00361 * @return 文字列 00362 */ 00363 virtual String toString() const; 00364 00365 /** 00366 * クリア 00367 */ 00368 virtual void clear(){ 00369 state_.clear(); 00370 preState_.clear(); 00371 } 00372 00373 /** 00374 * 協調レベルの設定 00375 * @param exclusive 排他モードならtrue 00376 * @param foreground フォアグラウンドモードならtrue 00377 * @return 成功すればtrue 00378 */ 00379 virtual bool setCooperativeLevel(bool exclusive, bool foreground){ 00380 return device_->setCooperativeLevel(exclusive, foreground); 00381 } 00382 00383 /** 00384 * 排他モードか 00385 * @return 排他モードならtrue 00386 */ 00387 virtual bool isExclusive() const{ return device_->isExclusive(); } 00388 00389 /** 00390 * フォアグラウンドモードか 00391 * @return フォアグラウンドモードならtrue、バックグラウンドモードならfalse 00392 */ 00393 virtual bool isForeground() const{ return device_->isForeground(); } 00394 00395 protected: 00396 //-------------------------------------------------------------------------- 00397 /** 00398 * コンストラクタ 00399 * @param device デバイス 00400 */ 00401 Joystick(JoystickDevice* device); 00402 00403 /** 00404 * デストラクタ 00405 */ 00406 virtual ~Joystick(); 00407 00408 /** 00409 * 次のステート設定 00410 * @param state 次のステート 00411 */ 00412 virtual void setNextState(const JoystickState& state); 00413 00414 /** 00415 * ステートの取得 00416 * @return ステート 00417 */ 00418 virtual const JoystickState& getState(){ return state_; } 00419 00420 /** 00421 * 軸補正 00422 * @param axisValue 補正する値 00423 * @return 補正された値 00424 */ 00425 virtual float correctAxisValue(int axisValue) const{ 00426 return (float)axisValue / (float)maxAxisValue; 00427 } 00428 00429 /** 00430 * スライダ補正 00431 * @param sliderValue 補正する値 00432 * @return 補正された値 00433 */ 00434 virtual float correctSliderValue(int sliderValue) const{ 00435 return (float)sliderValue / (float)maxAxisValue; 00436 } 00437 00438 private: 00439 //-------------------------------------------------------------------------- 00440 // デバイス 00441 JoystickDevice* device_; 00442 // ステート 00443 JoystickState state_; 00444 // 前回のステート 00445 JoystickState preState_; 00446 00447 }; 00448 00449 //------------------------------------------------------------------------------ 00450 } // End of namespace Lamp 00451 #endif // End of JOYSTICK_H_ 00452 //------------------------------------------------------------------------------