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

# Mouse.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  * マウス実装
    00022  * @author Junpee
    00023  */
    00024
    00025 #include "LampBasic.h"
    00026 #include "Input/Mouse/Mouse.h"
    00027
    00028 namespace Lamp{
    00029
    00030 //------------------------------------------------------------------------------
    00031 // コンストラクタ
    00032 Mouse::Mouse(MouseDevice* device) : device_(device),
    00033     clickPositionRange_(3), doubleClickTimeRange_(45){
    00034     clickStateClear();
    00035 }
    00036 //------------------------------------------------------------------------------
    00037 // デストラクタ
    00038 Mouse::~Mouse(){
    00039 }
    00040 //------------------------------------------------------------------------------
    00041 // クリックステートクリア
    00042 void Mouse::clickStateClear(){
    00043     for(int i = 0; i < maxButtonCount; i++){
    00044         downTime_[i] = preDownTime_[i] = -1;
    00045         downOffset_[i].set(0, 0);
    00046         preDownOffset_[i].set(0, 0);
    00047         clicked_[i] = false;
    00048         doubleClicked_[i] = false;
    00049         doubleDown_[i] = false;
    00050     }
    00051 }
    00052 //------------------------------------------------------------------------------
    00053 // 次のステート設定
    00054 void Mouse::setNextState(const MouseState& state){
    00055     preState_ = state_;
    00056     state_ = state;
    00057     // クリック処理
    00058     int buttonCount = getButtonCount();
    00059     for(int i = 0; i < buttonCount; i++){
    00060         // ダウントリガに対応する
    00061         if(buttonDown(i)){
    00062             preDownTime_[i] = downTime_[i];
    00063             preDownOffset_[i] = downOffset_[i];
    00064             downTime_[i] = 0;
    00065             downOffset_[i].set(0, 0);
    00066         }
    00067         // ステート更新
    00068         if(downTime_[i] != -1){
    00069             downOffset_[i].x += getXAxis();
    00070             downOffset_[i].y += getYAxis();
    00071             downTime_[i]++;
    00072             // 移動範囲チェック
    00073             if((Math::abs(downOffset_[i].x) > clickPositionRange_) ||
    00074                 (Math::abs(downOffset_[i].y) > clickPositionRange_)){
    00075                 downTime_[i] = -1;
    00076             }
    00077         }
    00078         if(preDownTime_[i] != -1){
    00079             preDownOffset_[i].x += getXAxis();
    00080             preDownOffset_[i].y += getYAxis();
    00081             preDownTime_[i]++;
    00082             // 移動範囲チェック
    00083             if((Math::abs(preDownOffset_[i].x) > clickPositionRange_) ||
    00084                 (Math::abs(preDownOffset_[i].y) > clickPositionRange_)){
    00085                 preDownTime_[i] = -1;
    00086             }
    00087         }
    00088         // クリック判定
    00089         clicked_[i] = false;
    00090         if(buttonUp(i) && (downTime_[i] != -1)){ clicked_[i] = true; }
    00091         // ダブルクリック判定
    00092         doubleClicked_[i] = false;
    00093         if(buttonUp(i) && (downTime_[i] != -1) && (preDownTime_[i] != -1) &&
    00094             (preDownTime_[i] < doubleClickTimeRange_)){
    00095             doubleClicked_[i] = true;
    00096             // ダブルクリックが連続しないようにする
    00097             downTime_[i] = -1;
    00098         }
    00099         // ダブルダウン判定
    00100         doubleDown_[i] = false;
    00101         if(buttonDown(i) && (downTime_[i] != -1) &&
    00102             (preDownTime_[i] != -1) &&
    00103             (preDownTime_[i] < doubleClickTimeRange_)){
    00104             doubleDown_[i] = true;
    00105         }
    00106     }
    00107 }
    00108 //------------------------------------------------------------------------------
    00109 // 文字列への変換
    00110 String Mouse::toString() const{
    00111     String result, temp;
    00112     result = getName();
    00113     result += " (";
    00114     if(isAttached()){ result += " Attached"; }
    00115     if(isPolled()){ result += " NeedPolling"; }
    00116     result += " )\n";
    00117     temp.format("( X = %4d , Y = %4d , Z = %4d ) ZResolution %d\n",
    00118         getXAxis(), getYAxis(), getZAxis(), getZResolution());
    00119     result += temp;
    00120     // ボタン
    00121     temp.format("Buttons %d\n", getButtonCount());
    00122     result += temp;
    00123     result += " Pressed    ";
    00124     for(int i = 0; i < maxButtonCount; i++){
    00125         if(buttonPressed(i)){ result += " 1"; }
    00126         else{ result += " 0"; }
    00127     }
    00128     result += "\n";
    00129
    00130     result += " Down       ";
    00131     for(int i = 0; i < maxButtonCount; i++){
    00132         if(buttonDown(i)){ result += " 1"; }
    00133         else{ result += " 0"; }
    00134     }
    00135     result += "\n";
    00136
    00137     result += " Up         ";
    00138     for(int i = 0; i < maxButtonCount; i++){
    00139         if(buttonUp(i)){ result += " 1"; }
    00140         else{ result += " 0"; }
    00141     }
    00142     result += "\n";
    00143
    00144     result += " Click      ";
    00145     for(int i = 0; i < maxButtonCount; i++){
    00146         if(clicked(i)){ result += " 1"; }
    00147         else{ result += " 0"; }
    00148     }
    00149     result += "\n";
    00150
    00151     result += " DoubleClick";
    00152     for(int i = 0; i < maxButtonCount; i++){
    00153         if(doubleClicked(i)){ result += " 1"; }
    00154         else{ result += " 0"; }
    00155     }
    00156     result += "\n";
    00157
    00158     result += " DoubleDown ";
    00159     for(int i = 0; i < maxButtonCount; i++){
    00160         if(doubleDown(i)){ result += " 1"; }
    00161         else{ result += " 0"; }
    00162     }
    00163     result += "\n";
    00164     return result;
    00165 }
    00166 //------------------------------------------------------------------------------
    00167 } // End of namespace Lamp
    00168 //------------------------------------------------------------------------------