/* マウスジェスチャーサンプルソース */ /* 2006/12/13 watam */ //====================================================================== //定数 //====================================================================== #define MG_BUFFER_SIZE 16 /* マウスジェスチャー用のバッファサイズ */ #define MG_CMD_MAX 32 /* マウスジェスチャーコマンドの総数 */ #define MG_RANGE 32 /* ジェスチャー一回ごとの判定距離 */ #define MG_RIGHT 1 /* →に動いた */ #define MG_DOWN 2 /* ↓に動いた */ #define MG_LEFT 3 /* ←に動いた */ #define MG_UP 4 /* ↑に動いた */ //====================================================================== //構造体 //====================================================================== /* マウスジェスチャーコマンド設定用構造体 */ typedef struct{ int FuncCode; /* 発行するコマンド */ int BufferLength; /* ジェスチャー情報の長さ */ short Buffer[MG_BUFFER_SIZE]; /* ジェスチャー情報 */ }MG_CMD; //====================================================================== //変数 //====================================================================== POINT MG_pos; /* 開始座標 */ int MG_IsGesture; /* ジェスチャー記録中かどうか */ int MG_BufferLength; /* 記録中のジェスチャーの長さ */ short MG_Buffer[MG_BUFFER_SIZE]; /* 記録用バッファ */ MG_CMD MG_CMD_LIST[MG_CMD_MAX]; /* マウスジェスチャーコマンド割り当てリスト(共通設定辺りで設定) */ //====================================================================== //プロトタイプ宣言 //====================================================================== void MouseGesture_Start( int xPos, int yPos ); void MouseGesture_Execute( int xPos, int yPos ); bool MouseGesture_Finish(); int MouseGesture_Check( MG_CMD *MG_Command ); bool MouseGesture_IsGesture(); //---------------------------------------------------------------------- //マウスジェスチャー開始 //---------------------------------------------------------------------- void MouseGesture_Start( int xPos, int yPos ) { MG_pos.x = xPos; MG_pos.y = yPos; MG_IsGesture = true; MG_BufferLength = 0; memset( MG_Buffer, 0, sizeof(short)*MG_BUFFER_SIZE ); } //---------------------------------------------------------------------- //マウスジェスチャー記録中 //---------------------------------------------------------------------- void MouseGesture_Execute( int xPos, int yPos ) { int xlen = xPos - MG_pos.x; int ylen = yPos - MG_pos.y; int arrow = 0; if( xlen > MG_RANGE ){ arrow = MG_RIGHT; }else if( ylen > MG_RANGE ){ arrow = MG_DOWN; }else if( xlen < -MG_RANGE ){ arrow = MG_LEFT; }else if( ylen < -MG_RANGE ){ arrow = MG_UP; } if( arrow ){ /* 座標を更新 */ MG_pos.x = xPos; MG_pos.y = yPos; /* 直前の記録と同じ方向の場合は受け付けない */ if( !(MG_BufferLength && MG_Buffer[ MG_BufferLength-1 ] == arrow) ){ if( MG_BufferLength < MG_BUFFER_SIZE ){ MG_Buffer[ MG_BufferLength++ ] = arrow; /* 現在の状態と該当するコマンドがあるかどうか */ MG_CMD *MG_Command = NULL; MouseGesture_Check( MG_Command ); /* MG_Bufferの中身を文字列でステータスバーに表示 */ if( MG_Command ){ /* ジェスチャーに割り当てられているコマンドをステータスバーに表示 */ /* 「↓→:(閉じる)」、など */ } } } } } //---------------------------------------------------------------------- //マウスジェスチャー終了 //---------------------------------------------------------------------- bool MouseGesture_Finish() { /* ジェスチャー中 */ if( MG_IsGesture ){ MG_IsGesture = false; /* ジェスチャーの結果に該当するものがあれば、コマンドを発行 */ MG_CMD *MG_Command = NULL; if( MouseGesture_Check( MG_Command ) ){ /* コマンドを発行した場合は、trueを返して右クリックメニューなどを抑制する */ return true; } } return false; } //---------------------------------------------------------------------- //マウスジェスチャー記録中かどうか //---------------------------------------------------------------------- bool MouseGesture_IsGesture() { return MG_IsGesture; } //---------------------------------------------------------------------- //コマンド判定 //---------------------------------------------------------------------- int MouseGesture_Check( MG_CMD *MG_Command ) { int i; int ret = false; for( i = 0; i < MG_CMD_MAX; i++ ){ if( !MG_CMD_LIST[i].BufferLength ){ break; } /* 長さの同じものだけを判定する */ if( MG_BufferLength == MG_CMD_LIST[i].BufferLength ){ if( !memcmp( MG_Buffer, MG_CMD_LIST[i].Buffer, sizeof(short)*MG_BufferLength ) ){ if( MG_Command ){ /* 該当するコマンドを返す */ MG_Command = &MG_CMD_LIST[i]; } ret = true; break; } } } return ret; }