OSDN Git Service

DTXManiaソリューション、DTXManiaプロジェクト、DTXCreatorプロジェクト、FDKプロジェクトについて英語化。
[dtxmania/dtxmania.git] / FDK / コード / 02.入力 / CInputMouse.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Diagnostics;
5 using SharpDX;
6 using SharpDX.DirectInput;
7
8 namespace FDK
9 {
10         public class CInputMouse : IInputDevice, IDisposable
11         {
12                 // 定数
13
14                 public const int nマウスの最大ボタン数 = 8;
15
16
17                 // コンストラクタ
18
19                 public CInputMouse( IntPtr hWnd, DirectInput directInput )
20                 {
21                         this.e入力デバイス種別 = E入力デバイス種別.Mouse;
22                         this.GUID = "";
23                         this.ID = 0;
24                         try
25                         {
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 + " を生成しました。" );
30                         }
31                         catch
32                         {
33                                 if( this.devMouse != null )
34                                 {
35                                         this.devMouse.Dispose();
36                                         this.devMouse = null;
37                                 }
38                                 Trace.TraceWarning( "Mouse デバイスの生成に失敗しました。" );
39                                 throw;
40                         }
41                         try
42                         {
43                                 this.devMouse.Acquire();
44                         }
45                         catch
46                         {
47                         }
48
49                         for( int i = 0; i < this.bMouseState.Length; i++ )
50                                 this.bMouseState[ i ] = false;
51
52                         //this.timer = new CTimer( CTimer.E種別.MultiMedia );
53                         this.list入力イベント = new List<STInputEvent>( 32 );
54                 }
55
56
57                 // メソッド
58
59                 #region [ IInputDevice 実装 ]
60                 //-----------------
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; }
65
66                 public void tポーリング( bool bWindowがアクティブ中, bool bバッファ入力を使用する )
67                 {
68                         for( int i = 0; i < 8; i++ )
69                         {
70                                 this.bMousePushDown[ i ] = false;
71                                 this.bMousePullUp[ i ] = false;
72                         }
73
74                         if( bWindowがアクティブ中 && ( this.devMouse != null ) )
75                         {
76                                 this.devMouse.Acquire();
77                                 this.devMouse.Poll();
78
79                                 // this.list入力イベント = new List<STInputEvent>( 32 );
80                                 this.list入力イベント.Clear();                    // #xxxxx 2012.6.11 yyagi; To optimize, I removed new();
81
82                                 if( bバッファ入力を使用する )
83                                 {
84                                         #region [ a.バッファ入力 ]
85                                         //-----------------------------
86                                         var bufferedData = this.devMouse.GetBufferedData();
87                                         //if( Result.Last.IsSuccess && bufferedData != null )
88                                         {
89                                                 foreach( MouseUpdate data in bufferedData )
90                                                 {
91                                                         var mouseButton = new[] {
92                                                                  MouseOffset.Buttons0,
93                                                                  MouseOffset.Buttons1,
94                                                                  MouseOffset.Buttons2,
95                                                                  MouseOffset.Buttons3,
96                                                                  MouseOffset.Buttons4,
97                                                                  MouseOffset.Buttons5,
98                                                                  MouseOffset.Buttons6,
99                                                                  MouseOffset.Buttons7,
100                                                         };
101
102                                                         for( int k = 0; k < 8; k++ )
103                                                         {
104                                                                 //if( data.IsPressed( k ) )
105                                                                 if( data.Offset == mouseButton[ k ] && ( ( data.Value & 0x80 ) != 0 ) )
106                                                                 {
107                                                                         STInputEvent item = new STInputEvent()
108                                                                         {
109                                                                                 nKey = k,
110                                                                                 b押された = true,
111                                                                                 b離された = false,
112                                                                                 nTimeStamp = CSound管理.rc演奏用タイマ.nサウンドタイマーのシステム時刻msへの変換( data.Timestamp ),
113                                                                                 nVelocity = CInput管理.n通常音量
114                                                                         };
115                                                                         this.list入力イベント.Add( item );
116
117                                                                         this.bMouseState[ k ] = true;
118                                                                         this.bMousePushDown[ k ] = true;
119                                                                 }
120                                                                 else if( data.Offset == mouseButton[ k ] && this.bMouseState[ k ] == true && ( ( data.Value & 0x80 ) == 0 ) )
121                                                                 //else if( data.IsReleased( k ) )
122                                                                 {
123                                                                         STInputEvent item = new STInputEvent()
124                                                                         {
125                                                                                 nKey = k,
126                                                                                 b押された = false,
127                                                                                 b離された = true,
128                                                                                 nTimeStamp = CSound管理.rc演奏用タイマ.nサウンドタイマーのシステム時刻msへの変換( data.Timestamp ),
129                                                                                 nVelocity = CInput管理.n通常音量
130                                                                         };
131                                                                         this.list入力イベント.Add( item );
132
133                                                                         this.bMouseState[ k ] = false;
134                                                                         this.bMousePullUp[ k ] = true;
135                                                                 }
136                                                         }
137                                                 }
138                                         }
139                                         //-----------------------------
140                                         #endregion
141                                 }
142                                 else
143                                 {
144                                         #region [ b.状態入力 ]
145                                         //-----------------------------
146                                         MouseState currentState = this.devMouse.GetCurrentState();
147                                         //if( Result.Last.IsSuccess && currentState != null )
148                                         {
149                                                 bool[] buttons = currentState.Buttons;
150
151                                                 for( int j = 0; ( j < buttons.Length ) && ( j < 8 ); j++ )
152                                                 {
153                                                         if( this.bMouseState[ j ] == false && buttons[ j ] == true )
154                                                         {
155                                                                 var ev = new STInputEvent() {
156                                                                         nKey = j,
157                                                                         b押された = true,
158                                                                         b離された = false,
159                                                                         nTimeStamp = CSound管理.rc演奏用タイマ.nシステム時刻,     // 演奏用タイマと同じタイマを使うことで、BGMと譜面、入力ずれを防ぐ。
160                                                                         nVelocity = CInput管理.n通常音量,
161                                                                 };
162                                                                 this.list入力イベント.Add( ev );
163
164                                                                 this.bMouseState[ j ] = true;
165                                                                 this.bMousePushDown[ j ] = true;
166                                                         }
167                                                         else if( this.bMouseState[ j ] == true && buttons[ j ] == false )
168                                                         {
169                                                                 var ev = new STInputEvent() {
170                                                                         nKey = j,
171                                                                         b押された = false,
172                                                                         b離された = true,
173                                                                         nTimeStamp = CSound管理.rc演奏用タイマ.nシステム時刻,     // 演奏用タイマと同じタイマを使うことで、BGMと譜面、入力ずれを防ぐ。
174                                                                         nVelocity = CInput管理.n通常音量,
175                                                                 };
176                                                                 this.list入力イベント.Add( ev );
177
178                                                                 this.bMouseState[ j ] = false;
179                                                                 this.bMousePullUp[ j ] = true;
180                                                         }
181                                                 }
182                                         }
183                                         //-----------------------------
184                                         #endregion
185                                 }
186                         }
187                 }
188                 public bool bキーが押された( int nButton )
189                 {
190                         return ( ( ( 0 <= nButton ) && ( nButton < 8 ) ) && this.bMousePushDown[ nButton ] );
191                 }
192                 public bool bキーが押されている( int nButton )
193                 {
194                         return ( ( ( 0 <= nButton ) && ( nButton < 8 ) ) && this.bMouseState[ nButton ] );
195                 }
196                 public bool bキーが離された( int nButton )
197                 {
198                         return ( ( ( 0 <= nButton ) && ( nButton < 8 ) ) && this.bMousePullUp[ nButton ] );
199                 }
200                 public bool bキーが離されている( int nButton )
201                 {
202                         return ( ( ( 0 <= nButton ) && ( nButton < 8 ) ) && !this.bMouseState[ nButton ] );
203                 }
204                 //-----------------
205                 #endregion
206
207                 #region [ IDisposable 実装 ]
208                 //-----------------
209                 public void Dispose()
210                 {
211                         if( !this.bDispose完了済み )
212                         {
213                                 if( this.devMouse != null )
214                                 {
215                                         this.devMouse.Dispose();
216                                         this.devMouse = null;
217                                 }
218                                 //if( this.timer != null )
219                                 //{
220                                 //    this.timer.Dispose();
221                                 //    this.timer = null;
222                                 //}
223                                 if ( this.list入力イベント != null )
224                                 {
225                                         this.list入力イベント = null;
226                                 }
227                                 this.bDispose完了済み = true;
228                         }
229                 }
230                 //-----------------
231                 #endregion
232
233
234                 // その他
235
236                 #region [ private ]
237                 //-----------------
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;
244                 //-----------------
245                 #endregion
246         }
247 }