OSDN Git Service

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