2 using System.Collections.Generic;
4 using System.Diagnostics;
6 using SharpDX.DirectInput;
10 public class CInputMouse : IInputDevice, IDisposable
14 public const int nマウスの最大ボタン数 = 8;
19 public CInputMouse( IntPtr hWnd, DirectInput directInput )
21 this.e入力デバイス種別 = E入力デバイス種別.Mouse;
26 this.devMouse = new Mouse( directInput );
27 this.devMouse.SetCooperativeLevel( hWnd, CooperativeLevel.Foreground | CooperativeLevel.NonExclusive );
28 this.devMouse.Properties.BufferSize = 0x20;
29 Trace.TraceInformation( this.devMouse.Information.ProductName + " を生成しました。" );
33 if( this.devMouse != null )
35 this.devMouse.Dispose();
38 Trace.TraceWarning( "Mouse デバイスの生成に失敗しました。" );
43 this.devMouse.Acquire();
49 for( int i = 0; i < this.bMouseState.Length; i++ )
50 this.bMouseState[ i ] = false;
52 //this.timer = new CTimer( CTimer.E種別.MultiMedia );
53 this.list入力イベント = new List<STInputEvent>( 32 );
59 #region [ IInputDevice 実装 ]
61 public E入力デバイス種別 e入力デバイス種別 { get; private set; }
62 public string GUID { get; private set; }
63 public int ID { get; private set; }
64 public List<STInputEvent> list入力イベント { get; private set; }
66 public void tポーリング( bool bWindowがアクティブ中, bool bバッファ入力を使用する )
68 for( int i = 0; i < 8; i++ )
70 this.bMousePushDown[ i ] = false;
71 this.bMousePullUp[ i ] = false;
74 if( bWindowがアクティブ中 && ( this.devMouse != null ) )
76 this.devMouse.Acquire();
79 // this.list入力イベント = new List<STInputEvent>( 32 );
80 this.list入力イベント.Clear(); // #xxxxx 2012.6.11 yyagi; To optimize, I removed new();
85 //-----------------------------
86 var bufferedData = this.devMouse.GetBufferedData();
87 //if( Result.Last.IsSuccess && bufferedData != null )
89 foreach( MouseUpdate data in bufferedData )
91 var mouseButton = new[] {
102 for( int k = 0; k < 8; k++ )
104 //if( data.IsPressed( k ) )
105 if( data.Offset == mouseButton[ k ] && ( ( data.Value & 0x80 ) != 0 ) )
107 STInputEvent item = new STInputEvent()
112 nTimeStamp = CSound管理.rc演奏用タイマ.nサウンドタイマーのシステム時刻msへの変換( data.Timestamp ),
113 nVelocity = CInput管理.n通常音量
115 this.list入力イベント.Add( item );
117 this.bMouseState[ k ] = true;
118 this.bMousePushDown[ k ] = true;
120 else if( data.Offset == mouseButton[ k ] && this.bMouseState[ k ] == true && ( ( data.Value & 0x80 ) == 0 ) )
121 //else if( data.IsReleased( k ) )
123 STInputEvent item = new STInputEvent()
128 nTimeStamp = CSound管理.rc演奏用タイマ.nサウンドタイマーのシステム時刻msへの変換( data.Timestamp ),
129 nVelocity = CInput管理.n通常音量
131 this.list入力イベント.Add( item );
133 this.bMouseState[ k ] = false;
134 this.bMousePullUp[ k ] = true;
139 //-----------------------------
145 //-----------------------------
146 MouseState currentState = this.devMouse.GetCurrentState();
147 //if( Result.Last.IsSuccess && currentState != null )
149 bool[] buttons = currentState.Buttons;
151 for( int j = 0; ( j < buttons.Length ) && ( j < 8 ); j++ )
153 if( this.bMouseState[ j ] == false && buttons[ j ] == true )
155 var ev = new STInputEvent() {
159 nTimeStamp = CSound管理.rc演奏用タイマ.nシステム時刻, // 演奏用タイマと同じタイマを使うことで、BGMと譜面、入力ずれを防ぐ。
160 nVelocity = CInput管理.n通常音量,
162 this.list入力イベント.Add( ev );
164 this.bMouseState[ j ] = true;
165 this.bMousePushDown[ j ] = true;
167 else if( this.bMouseState[ j ] == true && buttons[ j ] == false )
169 var ev = new STInputEvent() {
173 nTimeStamp = CSound管理.rc演奏用タイマ.nシステム時刻, // 演奏用タイマと同じタイマを使うことで、BGMと譜面、入力ずれを防ぐ。
174 nVelocity = CInput管理.n通常音量,
176 this.list入力イベント.Add( ev );
178 this.bMouseState[ j ] = false;
179 this.bMousePullUp[ j ] = true;
183 //-----------------------------
188 public bool bキーが押された( int nButton )
190 return ( ( ( 0 <= nButton ) && ( nButton < 8 ) ) && this.bMousePushDown[ nButton ] );
192 public bool bキーが押されている( int nButton )
194 return ( ( ( 0 <= nButton ) && ( nButton < 8 ) ) && this.bMouseState[ nButton ] );
196 public bool bキーが離された( int nButton )
198 return ( ( ( 0 <= nButton ) && ( nButton < 8 ) ) && this.bMousePullUp[ nButton ] );
200 public bool bキーが離されている( int nButton )
202 return ( ( ( 0 <= nButton ) && ( nButton < 8 ) ) && !this.bMouseState[ nButton ] );
207 #region [ IDisposable 実装 ]
209 public void Dispose()
211 if( !this.bDispose完了済み )
213 if( this.devMouse != null )
215 this.devMouse.Dispose();
216 this.devMouse = null;
218 //if( this.timer != null )
220 // this.timer.Dispose();
221 // this.timer = null;
223 if ( this.list入力イベント != null )
225 this.list入力イベント = null;
227 this.bDispose完了済み = true;
238 private bool bDispose完了済み;
239 private bool[] bMousePullUp = new bool[ 8 ];
240 private bool[] bMousePushDown = new bool[ 8 ];
241 private bool[] bMouseState = new bool[ 8 ];
242 private Mouse devMouse;
243 //private CTimer timer;