OSDN Git Service

Dispose 漏れを修正。
[strokestylet/CsWin10Desktop3.git] / StrokeStyleT / StrokeStyleT.cs
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Diagnostics;
5 using System.Linq;
6 using System.Windows.Forms;
7
8 namespace SST
9 {
10         class StrokeStyleT : FDK.ApplicationFormBase
11         {
12                 // グローバルリソース (static) 
13                 public static SST.フォルダ フォルダ => StrokeStyleT.bs_フォルダ;
14                 public static FDK.入力.Keyboard キーボード入力 => StrokeStyleT.bs_キーボード入力;
15                 public static FDK.入力.MidiIn MIDI入力 => StrokeStyleT.bs_MIDI入力;
16                 public static FDK.メディア.サウンド.WASAPI排他.ExclusiveDevice Wasapiデバイス => StrokeStyleT.bs_Wasapiデバイス;
17                 public static Random 乱数 => StrokeStyleT.bs_乱数;
18                 public static SST.ユーザ.ユーザ管理 ユーザ管理 => StrokeStyleT.bs_ユーザ管理;
19                 public static SST.曲.曲ツリー管理 曲ツリー管理 => StrokeStyleT.bs_曲ツリー管理;
20                 public static SSTFormat.スコア 演奏スコア { get; set; } = null;
21
22                 public static void すべての入力デバイスをポーリングする()
23                 {
24                         // hack: 追加の入力デバイスができたら、ここにポーリングコードを追加すること。
25                         StrokeStyleT.キーボード入力?.ポーリングする();
26                         StrokeStyleT.MIDI入力?.ポーリングする();
27                 }
28
29                 // get only static property の初期化。
30                 static StrokeStyleT()
31                 {
32                         // フォルダ変数を真っ先に登録する。(ほかのメンバのコンストラクタでフォルダ変数を利用できるようにするため。)
33                         StrokeStyleT.bs_フォルダ = new SST.フォルダ();
34                         SST.フォルダ.フォルダ変数を追加する( "Static", StrokeStyleT.フォルダ.StaticFolder );
35                         SST.フォルダ.フォルダ変数を追加する( "AppData", StrokeStyleT.フォルダ.AppDataFolder );
36                         SST.フォルダ.フォルダ変数を追加する( "User", null );
37
38                         // その他の static の生成。
39                         StrokeStyleT.bs_ユーザ管理 = new ユーザ.ユーザ管理();
40                         StrokeStyleT.bs_曲ツリー管理 = new 曲.曲ツリー管理();
41                 }
42
43                 public override void 初期化する()
44                 {
45                         Debug.Assert( null == this.デバイスリソース );  // 作成される前である。
46                         FDK.Log.現在のスレッドに名前をつける( "GUI" );
47
48                         this.Text = $"StrokeStyle<T> {System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()}";
49                         this.ClientSize = new System.Drawing.Size( 1920, 1080 );        // 物理画面サイズpx
50                         this.設計画面サイズdpx = new SharpDX.Size2F( 1920, 1080 ); // 設計画面サイズdpx
51
52                         #region " System.Stopwatch が高解像度タイマを使わないならエラー。"
53                         //-----------------
54                         if( false == System.Diagnostics.Stopwatch.IsHighResolution )
55                                 throw new SSTException( "このシステムは、高解像度タイマをサポートしていません。" );
56                         //-----------------
57                         #endregion
58                         #region " MediaFoundation を起動する。"
59                         //-----------------
60                         SharpDX.MediaFoundation.MediaManager.Startup();
61                         //-----------------
62                         #endregion
63                         #region " Sleep 精度を上げる。"
64                         //-----------------
65                         StrokeStyleT.timeBeginPeriod( 1 );
66                         //-----------------
67                         #endregion
68
69                         #region " ユーザを初期化する。"
70                         //-----------------
71                         FDK.Log.Info( "ユーザ情報を初期化します。" );
72                         StrokeStyleT.ユーザ管理.UsersXmlを読み込む();
73
74                         // ユーザ別の初期化。
75                         foreach( var ユーザ in StrokeStyleT.ユーザ管理.ユーザリスト )
76                                 ユーザ.SourcesXmlを読み込む();
77                         //-----------------
78                         #endregion
79                         #region " WASAPI デバイスを初期化する。"
80                         //----------------
81                         StrokeStyleT.bs_Wasapiデバイス = new FDK.メディア.サウンド.WASAPI排他.ExclusiveDevice();
82                         StrokeStyleT.bs_Wasapiデバイス.初期化する( 7.0f );
83                         //----------------
84                         #endregion
85                         #region " キーボード入力 を初期化する。"
86                         //-----------------
87                         FDK.Log.Info( "キーボード入力デバイスを初期化します。" );
88                         StrokeStyleT.bs_キーボード入力 = new FDK.入力.Keyboard( this.Handle );
89                         //-----------------
90                         #endregion
91                         #region " MIDI入力 を初期化する。"
92                         //-----------------
93                         FDK.Log.Info( "MIDI入力デバイスを初期化します。" );
94                         StrokeStyleT.bs_MIDI入力 = new FDK.入力.MidiIn();
95                         //-----------------
96                         #endregion
97
98                         this.現在のステージ = this.最初のダミーステージ;
99                 }
100                 public override void 終了する()
101                 {
102                         Debug.Assert( null == this.デバイスリソース );  // 解放された後である。
103
104                         #region " ステージを終了し、解放する。"
105                         //----------------
106                         //if( this.現在のステージ.活性化している )
107                         //      this.現在のステージ.非活性化する( this.デバイスリソース ); → デバイスリソース解放後なので、ここで非活性化してはならない。
108                         this.最初のダミーステージ  = null;
109                         this.起動ステージ = null;
110                         this.タイトルステージ = null;
111                         this.ログインステージ = null;
112                         this.選曲ステージ = null;
113                         this.曲読込ステージ = null;
114                         this.演奏ステージ = null;
115                         this.結果ステージ = null;
116                         //----------------
117                         #endregion
118                         #region " MIDI入力 を解放する。"
119                         //-----------------
120                         FDK.Log.Info( "MIDI入力デバイスを解放します。" );
121                         FDK.Utilities.解放する( ref StrokeStyleT.bs_MIDI入力 );
122                         //-----------------
123                         #endregion
124                         #region " キーボード入力 を解放する。"
125                         //-----------------
126                         FDK.Log.Info( "キーボード入力デバイスを解放します。" );
127                         FDK.Utilities.解放する( ref StrokeStyleT.bs_キーボード入力 );
128                         //-----------------
129                         #endregion
130                         #region " WASAPIデバイスを解放する。"
131                         //----------------
132                         FDK.Utilities.解放する( ref StrokeStyleT.bs_Wasapiデバイス );
133                         //----------------
134                         #endregion
135                         #region " MediaFoundation を終了する。"
136                         //-----------------
137                         SharpDX.MediaFoundation.MediaManager.Shutdown();
138                         //-----------------
139                         #endregion
140                 }
141                 public override void シーンを描画する()
142                 {
143                         #region " 描画を準備する。"
144                         //----------------
145                         var d3dDevice = (SharpDX.Direct3D11.Device) null;
146                         using( var d3dLock = new FDK.同期.AutoD3DDeviceLock( this.デバイスリソース.DXGIDeviceManager, out d3dDevice ) )
147                         using( d3dDevice )
148                         using( var d3dContext = d3dDevice.ImmediateContext )
149                         {
150                                 // 既定のD3Dレンダーターゲットビューを黒でクリアする。
151                                 d3dContext.ClearRenderTargetView(
152                                         this.デバイスリソース.D3DRenderTargetView, SharpDX.Color4.Black );
153
154                                 // 深度バッファを 1.0f でクリアする。
155                                 d3dContext.ClearDepthStencilView(
156                                         this.デバイスリソース.D3DDepthStencilView,
157                                         SharpDX.Direct3D11.DepthStencilClearFlags.Depth,
158                                         depth: 1.0f,
159                                         stencil: 0 );
160                         }
161                         //----------------
162                         #endregion
163
164                         // 現在のステージを進行描画する。
165                         this.現在のステージ?.進行描画する( this.デバイスリソース );
166
167                         // 表示する。
168                         this.デバイスリソース.SwapChain.Present( 1, SharpDX.DXGI.PresentFlags.None );
169
170                         if( null != this.現在のステージ )
171                         {
172                                 // ステージの状態チェックを行い、必要あれば遷移、またはアプリを終了する。
173                                 switch( this.現在のステージ.GetType().Name )
174                                 {
175                                         case nameof( ステージ.ステージ ):
176                                                 #region " 最初のダミーステージ "
177                                                 //----------------
178                                                 // 起動ステージへ遷移する。
179                                                 this.起動ステージ.活性化する( this.デバイスリソース );
180                                                 this.現在のステージ = this.起動ステージ;
181                                                 //----------------
182                                                 #endregion
183                                                 break;
184
185                                         case nameof( ステージ.起動.起動ステージ ):
186                                                 #region " 終了 → タイトルステージへ。"
187                                                 //---------------
188                                                 if( this.起動ステージ.現在のフェーズ == ステージ.起動.起動ステージ.フェーズ.終了 )
189                                                 {
190                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
191                                                         this.現在のステージ = this.タイトルステージ;
192                                                         this.現在のステージ.活性化する( this.デバイスリソース );
193                                                 }
194                                                 //---------------
195                                                 #endregion
196                                                 break;
197
198                                         case nameof( ステージ.タイトル.タイトルステージ ):
199                                                 #region " 確定 → ログインステージへ。"
200                                                 //---------------
201                                                 if( this.タイトルステージ.現在のフェーズ == ステージ.タイトル.タイトルステージ.フェーズ.確定 )
202                                                 {
203                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
204                                                         this.現在のステージ = this.ログインステージ;
205                                                         this.現在のステージ.活性化する( this.デバイスリソース );
206                                                 }
207                                                 //---------------
208                                                 #endregion
209                                                 #region " キャンセル → アプリを終了する。"
210                                                 //---------------
211                                                 else if( this.タイトルステージ.現在のフェーズ == ステージ.タイトル.タイトルステージ.フェーズ.キャンセル )
212                                                 {
213                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
214                                                         this.現在のステージ = null;
215                                                         this.アプリを終了せよ = true;
216                                                 }
217                                                 //---------------
218                                                 #endregion
219                                                 break;
220
221                                         case nameof( ステージ.ログイン.ログインステージ ):
222                                                 #region " 確定 → ログイン処理を行って、選曲ステージへ。"
223                                                 //---------------
224                                                 if( this.ログインステージ.現在のフェーズ == ステージ.ログイン.ログインステージ.フェーズ.確定 )
225                                                 {
226                                                         // ログイン処理(1) ユーザリストに対してユーザを選択する。
227                                                         StrokeStyleT.ユーザ管理.ユーザを選択する( this.ログインステージ.ユーザインデックス );
228                                                         FDK.Log.Info( $"ユーザが選択されました。[{StrokeStyleT.ユーザ管理.現在選択されているユーザ.名前}]" );
229
230                                                         // ログイン処理(2) 選択ユーザの曲ツリーを構築する。
231                                                         StrokeStyleT.ユーザ管理.現在選択されているユーザ.曲を検索して曲ツリーを構築する();
232
233                                                         // ログイン処理(3) 構築した曲ツリーを曲リスト管理に登録する。
234                                                         StrokeStyleT.曲ツリー管理.現在の管理対象ツリー = StrokeStyleT.ユーザ管理.現在選択されているユーザ.曲ツリーのルートノード;
235
236                                                         // 選曲ステージへ。
237                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
238                                                         this.現在のステージ = this.選曲ステージ;
239                                                         this.現在のステージ.活性化する( this.デバイスリソース );
240                                                 }
241                                                 //---------------
242                                                 #endregion
243                                                 #region " キャンセル → タイトルステージへ。"
244                                                 //---------------
245                                                 else if( this.ログインステージ.現在のフェーズ == ステージ.ログイン.ログインステージ.フェーズ.キャンセル )
246                                                 {
247                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
248                                                         this.現在のステージ = this.タイトルステージ;
249                                                         this.現在のステージ.活性化する( this.デバイスリソース );
250                                                 }
251                                                 //---------------
252                                                 #endregion
253                                                 break;
254
255                                         case nameof( ステージ.選曲.選曲ステージ ):
256                                                 #region " 曲確定 → 曲読込ステージへ。"
257                                                 //---------------
258                                                 if( this.選曲ステージ.現在のフェーズ == ステージ.選曲.選曲ステージ.フェーズ.曲確定 )
259                                                 {
260                                                         // 曲ノードが選択されていることを確認。
261                                                         Trace.Assert( null != StrokeStyleT.曲ツリー管理.現在選択されているノード, "[バグあり] 選択曲が null です。" );
262                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
263                                                         this.現在のステージ = this.曲読込ステージ;
264                                                         this.現在のステージ.活性化する( this.デバイスリソース );
265                                                 }
266                                                 //---------------
267                                                 #endregion
268                                                 #region " キャンセル → アプリを終了する。"
269                                                 //---------------
270                                                 else if( this.選曲ステージ.現在のフェーズ == ステージ.選曲.選曲ステージ.フェーズ.キャンセル )
271                                                 {
272                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
273                                                         this.現在のステージ = null;
274                                                         this.アプリを終了せよ = true;
275                                                 }
276                                                 //---------------
277                                                 #endregion
278                                                 break;
279
280                                         case nameof( ステージ.曲読込.曲読込ステージ ):
281                                                 #region " 終了 → 演奏ステージへ。"
282                                                 //--------------------
283                                                 if( this.曲読込ステージ.現在のフェーズ == ステージ.曲読込.曲読込ステージ.フェーズ.終了 )
284                                                 {
285                                                         // 演奏スコアインスタンスが作成されていることを確認。
286                                                         Debug.Assert( null != StrokeStyleT.演奏スコア );
287
288                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
289                                                         this.現在のステージ = this.演奏ステージ;
290                                                         this.現在のステージ.活性化する( this.デバイスリソース );
291                                                 }
292                                                 //--------------------
293                                                 #endregion
294                                                 break;
295
296                                         case nameof( ステージ.演奏.演奏ステージ ):
297                                                 #region " 演奏終了 → 結果ステージへ。"
298                                                 //--------------------
299                                                 if( this.演奏ステージ.現在のフェーズ == ステージ.演奏.演奏ステージ.フェーズ.クリアor失敗 )
300                                                 {
301                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
302                                                         this.現在のステージ = this.結果ステージ;
303                                                         this.現在のステージ.活性化する( this.デバイスリソース );
304                                                 }
305                                                 //--------------------
306                                                 #endregion
307                                                 #region " キャンセル → 選曲ステージへ。"
308                                                 //--------------------
309                                                 if( this.演奏ステージ.現在のフェーズ == ステージ.演奏.演奏ステージ.フェーズ.キャンセル )
310                                                 {
311                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
312                                                         this.現在のステージ = this.選曲ステージ;
313                                                         this.現在のステージ.活性化する( this.デバイスリソース );
314                                                 }
315                                                 //--------------------
316                                                 #endregion
317                                                 break;
318
319                                         case nameof( ステージ.結果.結果ステージ ):
320                                                 #region " 終了 → 選曲ステージへ。"
321                                                 //--------------------
322                                                 if( this.結果ステージ.現在のフェーズ == ステージ.結果.結果ステージ.フェーズ.終了 )
323                                                 {
324                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
325                                                         this.現在のステージ = this.選曲ステージ;
326                                                         this.現在のステージ.活性化する( this.デバイスリソース );
327                                                 }
328                                                 //--------------------
329                                                 #endregion
330                                                 break;
331                                 }
332                         }
333                 }
334                 protected override void OnClosing( CancelEventArgs e )
335                 {
336                         if( ( null != this.現在のステージ ) && ( this.現在のステージ.活性化している ) )
337                                 this.現在のステージ.非活性化する( this.デバイスリソース );
338
339                         base.OnClosing( e );
340                 }
341                 protected override void OnSizeChanged( EventArgs e )
342                 {
343                         // すべてのサイズ依存リソースを解放する。
344                         this.現在のステージ?.非活性化する( this.デバイスリソース );
345
346                         // デバイス等の変更処理を行う。
347                         base.OnSizeChanged( e );
348
349                         // すべてのサイズ依存リソースを作成し直す。
350                         this.現在のステージ?.活性化する( this.デバイスリソース );
351                 }
352
353                 // 各ステージの、唯一のインスタンス。
354                 protected SST.ステージ.ステージ 最初のダミーステージ = new ステージ.ステージ();
355                 protected SST.ステージ.起動.起動ステージ 起動ステージ = new ステージ.起動.起動ステージ();
356                 protected SST.ステージ.タイトル.タイトルステージ タイトルステージ = new ステージ.タイトル.タイトルステージ();
357                 protected SST.ステージ.ログイン.ログインステージ ログインステージ = new ステージ.ログイン.ログインステージ();
358                 protected SST.ステージ.選曲.選曲ステージ 選曲ステージ = new ステージ.選曲.選曲ステージ();
359                 protected SST.ステージ.曲読込.曲読込ステージ 曲読込ステージ = new ステージ.曲読込.曲読込ステージ();
360                 protected SST.ステージ.演奏.演奏ステージ 演奏ステージ = new ステージ.演奏.演奏ステージ();
361                 protected SST.ステージ.結果.結果ステージ 結果ステージ = new ステージ.結果.結果ステージ();
362
363                 private SST.ステージ.ステージ 現在のステージ = null;
364
365                 #region " バックストア。"
366                 //----------------
367                 private static SST.フォルダ bs_フォルダ = null;
368                 private static FDK.入力.Keyboard bs_キーボード入力 = null;
369                 private static FDK.入力.MidiIn bs_MIDI入力 = null;
370                 private static FDK.メディア.サウンド.WASAPI排他.ExclusiveDevice bs_Wasapiデバイス = null;
371                 private static readonly Random bs_乱数 = new Random( DateTime.Now.Millisecond );
372                 private static SST.ユーザ.ユーザ管理 bs_ユーザ管理 = null;
373                 private static SST.曲.曲ツリー管理 bs_曲ツリー管理 = null;
374                 //----------------
375                 #endregion
376
377                 #region " Win32 API "
378                 //-----------------
379                 [System.Runtime.InteropServices.DllImport( "winmm.dll", EntryPoint = "timeBeginPeriod" )]
380                 private static extern uint timeBeginPeriod( uint uMilliseconds );
381
382                 [System.Runtime.InteropServices.DllImport( "winmm.dll", EntryPoint = "timeEndPeriod" )]
383                 private static extern uint timeEndPeriod( uint uMilliseconds );
384                 //-----------------
385                 #endregion
386         }
387 }