OSDN Git Service

SSTサービスが切断されてからビュアーを再度起動したときに例外が発生してたミスを修正。
[strokestylet/CsWin10Desktop3.git] / StrokeStyleT / StrokeStyleT.cs
1 using System;
2 using System.Collections.Concurrent;
3 using System.Collections.Generic;
4 using System.Diagnostics;
5 using System.Linq;
6 using System.ServiceModel;
7 using System.Windows.Forms;
8 using FDK;
9
10 namespace SST
11 {
12         [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]     // サービスインターフェースをシングルスレッドで呼び出す。
13         class StrokeStyleT : SST.IStrokeStyleTService
14         {
15                 // グローバルリソース (static) 
16
17                 public static SST.フォルダ フォルダ
18                 {
19                         get { return StrokeStyleT.bs_フォルダ; }
20                 }
21                 public static FDK.メディア.サウンド.WASAPI排他.ExclusiveDevice Wasapiデバイス
22                 {
23                         get { return StrokeStyleT.bs_Wasapiデバイス; }
24                 }
25                 public static System.Random 乱数
26                 {
27                         get { return StrokeStyleT.bs_乱数; }
28                 }
29                 public static SST.ユーザ.ユーザ管理 ユーザ管理
30                 {
31                         get { return StrokeStyleT.bs_ユーザ管理; }
32                 }
33                 public static SST.曲.曲ツリー管理 曲ツリー管理
34                 {
35                         get { return StrokeStyleT.bs_曲ツリー管理; }
36                 }
37                 public static SSTFormat.スコア 演奏スコア
38                 {
39                         get;
40                         set;
41                 } = null;
42                 public static SST.設定.Config Config
43                 {
44                         get { return StrokeStyleT.bs_Config; }
45                 }
46                 public static bool ビュアーモードである
47                 {
48                         get;
49                         set;
50                 } = false;
51                 public static bool ビュアーモードではない
52                 {
53                         get { return !StrokeStyleT.ビュアーモードである; }
54                         set { StrokeStyleT.ビュアーモードである = !value; }
55                 }
56                 public static SST.ViewerMessage 最後に取得したビュアーメッセージ
57                 {
58                         get;
59                         protected set;
60                 } = null;
61                 public static FDK.入力.Keyboard キーボード入力
62                 {
63                         get { return StrokeStyleT.bs_キーボード入力; }
64                 }
65                 public static FDK.入力.MidiIn MIDI入力
66                 {
67                         get { return StrokeStyleT.bs_MIDI入力; }
68                 }
69
70                 public static void すべての入力デバイスをポーリングする()
71                 {
72                         // hack: 追加の入力デバイスができたら、ここにポーリングコードを追加すること。
73                         StrokeStyleT.キーボード入力?.ポーリングする();
74                         StrokeStyleT.MIDI入力?.ポーリングする();
75                 }
76
77                 static StrokeStyleT()
78                 {
79                         // フォルダ変数を真っ先に登録する。(ほかのメンバのコンストラクタでフォルダ変数を利用できるようにするため。)
80                         StrokeStyleT.bs_フォルダ = new SST.フォルダ();
81                         SST.フォルダ.フォルダ変数を追加する( "Static", StrokeStyleT.フォルダ.StaticFolder );
82                         SST.フォルダ.フォルダ変数を追加する( "AppData", StrokeStyleT.フォルダ.AppDataFolder );
83                         SST.フォルダ.フォルダ変数を追加する( "User", null );
84                 }
85                 public StrokeStyleT()
86                 {
87                         #region " ビュアーモードかどうかを確認する。"
88                         //----------------
89                         foreach( var arg in Environment.GetCommandLineArgs() )
90                         {
91                                 if( ( "-v" == arg.ToLower() ) || ( "-viewer" == arg.ToLower() ) )
92                                 {
93                                         StrokeStyleT.ビュアーモードである = true;
94                                         break;
95                                 }
96                         }
97                         //----------------
98                         #endregion
99
100                         this.State = ApplicationState.起動;
101
102                         this.MainForm = new RenderForm();
103                         this.MainForm.BackColor = System.Drawing.Color.Black;
104                         this.MainForm.AllowUserResizing = false;        // ユーザはフォームサイズを変更できない。
105                         this.MainForm.UserResized += フォームサイズが変更された;
106                         this.MainForm.FormClosing += ( sender, e ) => {
107                                 this.終了する();
108                         };
109                 }
110
111                 public void Run()
112                 {
113                         Debug.Assert( null != this.MainForm );
114
115                         SharpDX.Windows.RenderLoop.Run( this.MainForm, () => {
116
117                                 // アプリケーションの状態に応じて処理分岐。
118                                 switch( this.State )
119                                 {
120                                         case ApplicationState.起動:
121                                                 this.起動する();
122                                                 this.State = ApplicationState.初期化;
123                                                 break;  // 初期化に移る前に一度このラムダ式を出て、ウィンドウメッセージ処理を行わせる(画面を再描画させる)。
124
125                                         case ApplicationState.初期化:
126                                                 this.初期化する();
127                                                 this.State = ApplicationState.進行描画;
128                                                 break;
129
130                                         case ApplicationState.進行描画:
131                                                 this.進行描画する();
132                                                 if( this.State == ApplicationState.終了 )
133                                                         this.終了する();
134                                                 break;
135
136                                         case ApplicationState.終了:
137                                                 // 何もしない
138                                                 break;
139                                 }
140
141                         } );
142                 }
143
144                 #region " WCF サービスインターフェースの実装 "
145                 //----------------
146                 // ・このサービスインターフェースは、シングルスレッド(GUIスレッド)で同期実行される。(StrokeStyleT クラスの ServiceBehavior属性を参照。)
147                 // ・このサービスホストはシングルトンであり、すべてのクライアントセッションは同一(単一)のサービスインスタンスへ接続される。(Program.Main() を参照。)
148
149                 public void ViewerPlay( string path, int startPart = 0, bool drumsSound = true )
150                 {
151                         if( StrokeStyleT.ビュアーモードではない )
152                                 return;
153
154                         this.ビュアーメッセージキュー.Enqueue( new ViewerMessage() {
155                                 種別 = ViewerMessageType.演奏開始,
156                                 曲ファイルパス = path,
157                                 演奏開始小節番号 = startPart,
158                                 ドラムチップ発声 = drumsSound,
159                         } );
160                 }
161                 public void ViewerStop()
162                 {
163                         if( StrokeStyleT.ビュアーモードではない )
164                                 return;
165
166                         this.ビュアーメッセージキュー.Enqueue( new ViewerMessage() {
167                                 種別 = ViewerMessageType.演奏停止,
168                         } );
169                 }
170                 public float GetSoundDelay()
171                 {
172                         if( StrokeStyleT.ビュアーモードではない )
173                                 return 0f;
174
175                         return ( null != StrokeStyleT.Wasapiデバイス ) ? StrokeStyleT.Wasapiデバイス.遅延ms : 0f;
176                 }
177                 //----------------
178                 #endregion
179
180                 protected enum ApplicationState { 起動, 初期化, 進行描画, 終了 }
181                 protected ApplicationState State;
182                 protected SST.RenderForm MainForm = null;
183                 protected SharpDX.Size2F 設計画面サイズdpx = SharpDX.Size2F.Empty;
184                 protected ConcurrentQueue<ViewerMessage> ビュアーメッセージキュー = new ConcurrentQueue<ViewerMessage>();
185                 protected FDK.メディア.デバイスリソース デバイスリソース = null;
186                 protected SST.ステージ.ステージ 最初のダミーステージ = null;
187                 protected SST.ステージ.起動.起動ステージ 起動ステージ = null;
188                 protected SST.ステージ.タイトル.タイトルステージ タイトルステージ = null;
189                 protected SST.ステージ.ログイン.ログインステージ ログインステージ = null;
190                 protected SST.ステージ.選曲.選曲ステージ 選曲ステージ = null;
191                 protected SST.ステージ.曲読込.曲読込ステージ 曲読込ステージ = null;
192                 protected SST.ステージ.演奏.演奏ステージ 演奏ステージ = null;
193                 protected SST.ステージ.結果.結果ステージ 結果ステージ = null;
194                 protected SST.ステージ.ステージ 現在のステージ = null;
195
196                 protected void 起動する()
197                 {
198                         FDK.Log.現在のスレッドに名前をつける( "Render" );
199                         FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
200
201                         #region " コンフィグ を初期化する。"
202                         //----------------
203                         StrokeStyleT.bs_Config = new 設定.Config();
204                         StrokeStyleT.bs_Config.ConfigXmlを読み込む();
205                         //----------------
206                         #endregion
207
208                         #region " コンフィグで指定されたウィンドウサイズに変更する。"
209                         //----------------
210                         this.MainForm.ClientSize = new System.Drawing.Size( StrokeStyleT.Config.物理画面サイズpx.Width, StrokeStyleT.Config.物理画面サイズpx.Height );
211                         //----------------
212                         #endregion
213                         #region " フォームタイトルと設計画面サイズを設定する。"
214                         //----------------
215                         this.MainForm.Text = $"StrokeStyle<T> {System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()}";
216                         if( StrokeStyleT.ビュアーモードである )
217                                 this.MainForm.Text += " (Viewer)";
218
219                         // 設計画面サイズは、フォームサイズ(物理画面サイズ)とは独立して固定。
220                         this.設計画面サイズdpx = new SharpDX.Size2F( 1920f, 1080f );
221                         //----------------
222                         #endregion
223
224                         FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
225                 }
226                 protected void 初期化する()
227                 {
228                         FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
229
230                         // 開始条件チェック。
231                         Debug.Assert( null == this.デバイスリソース, "デバイスリソースの作成前であること。" );
232
233                         StrokeStyleT.bs_ユーザ管理 = new ユーザ.ユーザ管理();
234                         StrokeStyleT.bs_曲ツリー管理 = new 曲.曲ツリー管理();
235
236                         #region " 高解像度タイマを使えないならエラー。"
237                         //-----------------
238                         if( false == System.Diagnostics.Stopwatch.IsHighResolution )
239                                 throw new SSTException( "このシステムは、高解像度タイマをサポートしていません。" );
240                         //-----------------
241                         #endregion
242                         #region " MediaFoundation を起動する。"
243                         //-----------------
244                         SharpDX.MediaFoundation.MediaManager.Startup();
245                         //-----------------
246                         #endregion
247                         #region " Sleep 精度を上げる。"
248                         //-----------------
249                         Win32.timeBeginPeriod( 1 );
250                         //-----------------
251                         #endregion
252
253                         #region " デバイスリソースを作成する。"
254                         //----------------
255                         FDK.Log.Info( $"設計画面サイズ: {this.設計画面サイズdpx}" );
256                         FDK.Log.Info( $"物理画面サイズ: {this.MainForm.ClientSize}" );
257                         this.デバイスリソース = new FDK.メディア.デバイスリソース( this.設計画面サイズdpx );
258                         this.デバイスリソース.すべてのリソースを作成する( this.MainForm.ClientSize, this.MainForm.Handle );
259                         //----------------
260                         #endregion
261
262                         #region " ステージを生成する。"
263                         //----------------
264                         this.最初のダミーステージ = new ステージ.ステージ();
265                         this.起動ステージ = new ステージ.起動.起動ステージ();
266                         this.タイトルステージ = new ステージ.タイトル.タイトルステージ();
267                         this.ログインステージ = new ステージ.ログイン.ログインステージ();
268                         this.選曲ステージ = new ステージ.選曲.選曲ステージ();
269                         this.曲読込ステージ = new ステージ.曲読込.曲読込ステージ();
270                         this.演奏ステージ = new ステージ.演奏.演奏ステージ();
271                         this.結果ステージ = new ステージ.結果.結果ステージ();
272
273                         // 外部依存アクションを接続する。
274                         this.曲読込ステージ.読込曲のファイルパスを取得する = () => ( ( StrokeStyleT.曲ツリー管理.現在選択されているノード as SST.曲.MusicNode )?.sstfファイルパス );
275                         this.結果ステージ.演奏ステージインスタンスを取得する = () => ( this.演奏ステージ );
276                         this.結果ステージ.BGMを終了する = () => { this.演奏ステージ.BGMを解放する(); };
277                         //----------------
278                         #endregion
279                         #region " ユーザを初期化する。"
280                         //-----------------
281                         // Users.xml を読み込む。
282                         StrokeStyleT.ユーザ管理.UsersXmlを読み込む();
283
284                         // ユーザ別の初期化。
285                         foreach( var ユーザ in StrokeStyleT.ユーザ管理.ユーザリスト )
286                                 ユーザ.SourcesXmlを読み込む();
287                         //-----------------
288                         #endregion
289                         #region " WASAPI デバイスを初期化する。"
290                         //----------------
291                         StrokeStyleT.bs_Wasapiデバイス = new FDK.メディア.サウンド.WASAPI排他.ExclusiveDevice();
292                         StrokeStyleT.bs_Wasapiデバイス.初期化する( 15.0f );
293                         //----------------
294                         #endregion
295                         #region " キーボード入力 を初期化する。"
296                         //-----------------
297                         StrokeStyleT.bs_キーボード入力 = new FDK.入力.Keyboard( this.MainForm.Handle );
298                         //-----------------
299                         #endregion
300                         #region " MIDI入力 を初期化する。"
301                         //-----------------
302                         StrokeStyleT.bs_MIDI入力 = new FDK.入力.MidiIn();
303                         //-----------------
304                         #endregion
305
306                         FDK.Log.Info( "最初のダミーステージを開始します。" );
307                         this.現在のステージ = this.最初のダミーステージ;
308
309                         //#warning 全画面モード切替えを仮実装。
310                         this.MainForm.KeyDown += ( target, arg ) => {
311
312                                 // F11 → 画面モードの切り替え
313                                 if( arg.KeyCode == System.Windows.Forms.Keys.F11 )
314                                 {
315                                         this.全画面モードとウィンドウモードを切り替える();
316                                         arg.Handled = true;
317                                 }
318                         };
319
320                         // 終了条件チェック。
321                         Debug.Assert( SharpDX.Size2F.Empty != this.設計画面サイズdpx, "設計画面サイズが設定されていません。" );
322
323                         FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
324                 }
325                 protected void 終了する()
326                 {
327                         FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
328                         Debug.Assert( null != this.デバイスリソース, "デバイスリソースが解放される前であること。" );
329
330                         this.State = ApplicationState.終了;
331
332                         #region " ステージを終了し、解放する。"
333                         //----------------
334                         if( ( null != this.現在のステージ ) && ( this.現在のステージ.活性化している ) )      // 念のため
335                                 this.現在のステージ.非活性化する( this.デバイスリソース );
336
337                         this.演奏ステージ.BGMを解放する();
338
339                         this.最初のダミーステージ = null;
340                         this.起動ステージ = null;
341                         this.タイトルステージ = null;
342                         this.ログインステージ = null;
343                         this.選曲ステージ = null;
344                         this.曲読込ステージ = null;
345                         this.演奏ステージ = null;
346                         this.結果ステージ = null;
347                         //----------------
348                         #endregion
349
350                         #region " デバイスリソースを解放する。"
351                         //----------------
352                         FDK.Utilities.解放する( ref this.デバイスリソース );
353                         //----------------
354                         #endregion
355
356                         #region " MIDI入力デバイスを解放する。"
357                         //-----------------
358                         FDK.Log.Info( "MIDI入力デバイスを解放します。" );
359                         FDK.Utilities.解放する( ref StrokeStyleT.bs_MIDI入力 );
360                         //-----------------
361                         #endregion
362                         #region " キーボード入力デバイスを解放する。"
363                         //-----------------
364                         FDK.Log.Info( "キーボード入力デバイスを解放します。" );
365                         FDK.Utilities.解放する( ref StrokeStyleT.bs_キーボード入力 );
366                         //-----------------
367                         #endregion
368                         #region " WASAPIデバイスを解放する。"
369                         //----------------
370                         FDK.Log.Info( "WASAPIデバイスを解放します。" );
371                         FDK.Utilities.解放する( ref StrokeStyleT.bs_Wasapiデバイス );
372                         //----------------
373                         #endregion
374                         
375                         #region " コンフィグを保存し、解放する。"
376                         //----------------
377                         FDK.Log.Info( "コンフィグを解放します。" );
378                         StrokeStyleT.bs_Config.ConfigXmlを保存する();
379                         StrokeStyleT.bs_Config = null;
380                         //----------------
381                         #endregion
382                         #region " フォームを閉じる。"
383                         //----------------
384                         FDK.Utilities.解放する( ref this.MainForm );
385                         //----------------
386                         #endregion
387
388                         #region " MediaFoundation を終了する。"
389                         //-----------------
390                         SharpDX.MediaFoundation.MediaManager.Shutdown();
391                         //-----------------
392                         #endregion
393
394                         FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
395                 }
396                 protected void 進行描画する()
397                 {
398                         #region " D3Dデバイスが消失していれば再構築する。"
399                         //----------------
400                         bool 異常発生 = false;
401                         this.デバイスリソース.D3Dデバイスが消失していれば再構築する( out 異常発生 );
402
403                         // 再構築不可能な異常の場合は、即終了する。
404                         if( 異常発生 )
405                         {
406                                 this.State = ApplicationState.終了;
407                                 return;
408                         }
409                         //----------------
410                         #endregion
411                         #region " 描画の前処理を行う。"
412                         //----------------
413                         {
414                                 var d3dDevice = (SharpDX.Direct3D11.Device) null;
415                                 using( var d3dLock = new FDK.同期.AutoD3DDeviceLock( this.デバイスリソース.DXGIDeviceManager, out d3dDevice ) )
416                                 using( d3dDevice )
417                                 using( var d3dContext = d3dDevice.ImmediateContext )
418                                 {
419                                         // 既定のD3Dレンダーターゲットビューを黒でクリアする。
420                                         d3dContext.ClearRenderTargetView( this.デバイスリソース.D3DRenderTargetView, SharpDX.Color4.Black );
421
422                                         // 深度バッファを 1.0f でクリアする。
423                                         d3dContext.ClearDepthStencilView(
424                                                 this.デバイスリソース.D3DDepthStencilView,
425                                                 SharpDX.Direct3D11.DepthStencilClearFlags.Depth,
426                                                 depth: 1.0f,
427                                                 stencil: 0 );
428                                 }
429                         }
430                         //----------------
431                         #endregion
432                         #region " 現在のステージを進行描画する。"
433                         //----------------
434                         this.現在のステージ?.進行描画する( this.デバイスリソース );
435                         //----------------
436                         #endregion
437                         #region " スワップチェーンを表示する。"
438                         //----------------
439                         if( StrokeStyleT.Config.垂直帰線待ちを行う )
440                         {
441                                 // We recommend that you use Flush when the CPU waits for an arbitrary amount of time
442                                 // (such as when you call the Sleep function). 
443                                 // → https://msdn.microsoft.com/ja-jp/library/windows/desktop/ff476425(v=vs.85).aspx
444
445                                 var d3dDevice = (SharpDX.Direct3D11.Device) null;
446                                 using( var d3dLock = new FDK.同期.AutoD3DDeviceLock( this.デバイスリソース.DXGIDeviceManager, out d3dDevice ) )
447                                 using( d3dDevice )
448                                 using( var d3dContext = d3dDevice.ImmediateContext )
449                                 {
450                                         d3dContext.Flush();
451                                 }
452                         }
453
454                         this.デバイスリソース.SwapChain1.Present(
455                                 ( StrokeStyleT.Config.垂直帰線待ちを行う ) ? 1 : 0,
456                                 SharpDX.DXGI.PresentFlags.None );
457                         //----------------
458                         #endregion
459
460                         #region " ステージの状態をチェックし、必要あれば遷移またはアプリを終了する。"
461                         //----------------
462                         if( null != this.現在のステージ )
463                         {
464                                 switch( this.現在のステージ.GetType().Name )
465                                 {
466                                         case nameof( ステージ.ステージ ):
467                                                 #region " ビュアーモード → AutoPlayerでログインして演奏ステージへ。"
468                                                 //----------------
469                                                 if( StrokeStyleT.ビュアーモードである )
470                                                 {
471                                                         // ビュアー用ユーザでログインする。
472                                                         FDK.Log.Info( "ビュアーモード: AutoPlayer ユーザでログインします。" );
473                                                         this.ログインする( Properties.Resources.AUTOPLAYER );
474
475                                                         // 曲読込ステージ向けの初期設定。
476                                                         StrokeStyleT.曲ツリー管理.現在の管理対象ツリー = null;
477                                                         StrokeStyleT.曲ツリー管理.現在選択されているノード = null;
478                                                         
479                                                         // 演奏ステージ向けの初期設定。
480                                                         StrokeStyleT.演奏スコア = null;
481
482                                                         // 演奏ステージへ。
483                                                         this.演奏ステージ.活性化する( this.デバイスリソース );
484                                                         this.現在のステージ = this.演奏ステージ;
485                                                 }
486                                                 //----------------
487                                                 #endregion
488                                                 #region " 通常モード → 起動ステージへ。"
489                                                 //----------------
490                                                 else
491                                                 {
492                                                         this.起動ステージ.活性化する( this.デバイスリソース );
493                                                         this.現在のステージ = this.起動ステージ;
494                                                 }
495                                                 //----------------
496                                                 #endregion
497                                                 break;
498
499                                         case nameof( ステージ.起動.起動ステージ ):
500                                                 #region " 終了 → タイトルステージへ。"
501                                                 //---------------
502                                                 if( this.起動ステージ.現在のフェーズ == ステージ.起動.起動ステージ.フェーズ.終了 )
503                                                 {
504                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
505                                                         this.現在のステージ = this.タイトルステージ;
506                                                         this.現在のステージ.活性化する( this.デバイスリソース );
507                                                 }
508                                                 //---------------
509                                                 #endregion
510                                                 break;
511
512                                         case nameof( ステージ.タイトル.タイトルステージ ):
513                                                 #region " 確定 → ログインステージへ。"
514                                                 //---------------
515                                                 if( this.タイトルステージ.現在のフェーズ == ステージ.タイトル.タイトルステージ.フェーズ.確定 )
516                                                 {
517                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
518                                                         this.現在のステージ = this.ログインステージ;
519                                                         this.現在のステージ.活性化する( this.デバイスリソース );
520                                                 }
521                                                 //---------------
522                                                 #endregion
523                                                 #region " キャンセル → アプリを終了する。"
524                                                 //---------------
525                                                 else if( this.タイトルステージ.現在のフェーズ == ステージ.タイトル.タイトルステージ.フェーズ.キャンセル )
526                                                 {
527                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
528                                                         this.現在のステージ = null;
529                                                         this.State = ApplicationState.終了;
530                                                 }
531                                                 //---------------
532                                                 #endregion
533                                                 break;
534
535                                         case nameof( ステージ.ログイン.ログインステージ ):
536                                                 #region " 確定 → ログイン処理を行って、選曲ステージへ。"
537                                                 //---------------
538                                                 if( this.ログインステージ.現在のフェーズ == ステージ.ログイン.ログインステージ.フェーズ.確定 )
539                                                 {
540                                                         var user = StrokeStyleT.ユーザ管理.現在選択されているユーザ;
541
542                                                         if( null != user )
543                                                         {
544                                                                 foreach( var path in user.曲の検索元フォルダパスのリスト )
545                                                                         SST.曲.曲ツリー管理.フォルダから曲を再帰的に検索して子ノードリストに追加する( user.曲ツリーのルートノード, path );
546
547                                                                 StrokeStyleT.曲ツリー管理.現在の管理対象ツリー = StrokeStyleT.ユーザ管理.現在選択されているユーザ.曲ツリーのルートノード;
548                                                         }
549
550                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
551                                                         this.現在のステージ = this.選曲ステージ;
552                                                         this.現在のステージ.活性化する( this.デバイスリソース );
553                                                 }
554                                                 //---------------
555                                                 #endregion
556                                                 #region " キャンセル → タイトルステージへ。"
557                                                 //---------------
558                                                 else if( this.ログインステージ.現在のフェーズ == ステージ.ログイン.ログインステージ.フェーズ.キャンセル )
559                                                 {
560                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
561                                                         this.現在のステージ = this.タイトルステージ;
562                                                         this.現在のステージ.活性化する( this.デバイスリソース );
563                                                 }
564                                                 //---------------
565                                                 #endregion
566                                                 break;
567
568                                         case nameof( ステージ.選曲.選曲ステージ ):
569                                                 #region " 曲確定 → 曲読込ステージへ。"
570                                                 //---------------
571                                                 if( this.選曲ステージ.現在のフェーズ == ステージ.選曲.選曲ステージ.フェーズ.曲確定 )
572                                                 {
573                                                         // 曲ノードが選択されていることを確認。
574                                                         Trace.Assert( null != StrokeStyleT.曲ツリー管理.現在選択されているノード, "[バグあり] 選択曲が null です。" );
575                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
576                                                         this.現在のステージ = this.曲読込ステージ;
577                                                         this.現在のステージ.活性化する( this.デバイスリソース );
578                                                 }
579                                                 //---------------
580                                                 #endregion
581                                                 #region " キャンセル → アプリを終了する。"
582                                                 //---------------
583                                                 else if( this.選曲ステージ.現在のフェーズ == ステージ.選曲.選曲ステージ.フェーズ.キャンセル )
584                                                 {
585                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
586                                                         this.現在のステージ = null;
587                                                         this.State = ApplicationState.終了;
588                                                 }
589                                                 //---------------
590                                                 #endregion
591                                                 break;
592
593                                         case nameof( ステージ.曲読込.曲読込ステージ ):
594                                                 #region " 終了 → 演奏ステージへ。"
595                                                 //--------------------
596                                                 if( this.曲読込ステージ.現在のフェーズ == ステージ.曲読込.曲読込ステージ.フェーズ.終了 )
597                                                 {
598                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
599                                                         this.現在のステージ = this.演奏ステージ;
600                                                         this.現在のステージ.活性化する( this.デバイスリソース );
601                                                 }
602                                                 //--------------------
603                                                 #endregion
604                                                 break;
605
606                                         case nameof( ステージ.演奏.演奏ステージ ):
607                                                 if( StrokeStyleT.ビュアーモードである )
608                                                 {
609                                                         // (A) ビュアーモード
610
611                                                         #region " ビュアーメッセージがあれば処理する。"
612                                                         //----------------
613                                                         var msg = this.ビュアーメッセージを取得する();
614
615                                                         StrokeStyleT.最後に取得したビュアーメッセージ = msg;    // 保存する。(演奏ステージから参照する)
616
617                                                         if( null != msg )
618                                                         {
619                                                                 #region " (A) 演奏開始 "
620                                                                 //----------------
621                                                                 if( msg.種別 == ViewerMessageType.演奏開始 && (
622                                                                         this.演奏ステージ.現在のフェーズ.Value == ステージ.演奏.演奏ステージ.フェーズ.ビュアーメッセージ待機中 ||
623                                                                         this.演奏ステージ.現在のフェーズ.Value == ステージ.演奏.演奏ステージ.フェーズ.演奏中 )
624                                                                         )
625                                                                 {
626                                                                         try
627                                                                         {
628                                                                                 // MusicNode を作成し、現在選択されているノードとして登録する。
629                                                                                 StrokeStyleT.曲ツリー管理.現在選択されているノード = new 曲.MusicNode( msg.曲ファイルパス );
630
631                                                                                 // 演奏を停止する。
632                                                                                 this.演奏ステージ.非活性化する( this.デバイスリソース );
633                                                                                 this.演奏ステージ.BGMを解放する();
634                                                                                  
635                                                                                 // 曲読込ステージへ。
636                                                                                 this.現在のステージ = this.曲読込ステージ;
637                                                                                 this.現在のステージ.活性化する( this.デバイスリソース );
638                                                                         }
639                                                                         catch
640                                                                         {
641                                                                                 FDK.Log.ERROR( $"MusicNode の作成に失敗しました。" );
642                                                                         }
643                                                                 }
644                                                                 //----------------
645                                                                 #endregion
646                                                                 #region " (B) 演奏停止 "
647                                                                 //----------------
648                                                                 else if( msg.種別 == ViewerMessageType.演奏停止 && (
649                                                                         this.演奏ステージ.現在のフェーズ.Value == ステージ.演奏.演奏ステージ.フェーズ.ビュアーメッセージ待機中 ||
650                                                                         this.演奏ステージ.現在のフェーズ.Value == ステージ.演奏.演奏ステージ.フェーズ.演奏中 )
651                                                                         )
652                                                                 {
653                                                                         this.演奏ステージ.演奏を停止する();
654                                                                         this.演奏ステージ.現在のフェーズ.Value = ステージ.演奏.演奏ステージ.フェーズ.ビュアーメッセージ待機中;
655                                                                 }
656                                                                 //----------------
657                                                                 #endregion
658                                                                 #region " その他 "
659                                                                 //----------------
660                                                                 else
661                                                                 {
662                                                                         // その他のメッセージは無視。
663                                                                 }
664                                                                 //----------------
665                                                                 #endregion
666                                                         }
667                                                         //----------------
668                                                         #endregion
669                                                         #region " クリア/キャンセル → メッセージ待機へ。"
670                                                         //----------------
671                                                         if( this.演奏ステージ.現在のフェーズ.Value == ステージ.演奏.演奏ステージ.フェーズ.クリアor失敗 ||
672                                                                 this.演奏ステージ.現在のフェーズ.Value == ステージ.演奏.演奏ステージ.フェーズ.キャンセル )
673                                                         {
674                                                                 // フェーズのみ変更。BGM は止めない。
675                                                                 this.演奏ステージ.現在のフェーズ.Value = ステージ.演奏.演奏ステージ.フェーズ.ビュアーメッセージ待機中;
676                                                         }
677                                                         //----------------
678                                                         #endregion
679                                                 }
680                                                 else
681                                                 {
682                                                         // (B) 通常モード
683
684                                                         #region " 演奏終了 → 結果ステージへ。"
685                                                         //--------------------
686                                                         if( this.演奏ステージ.現在のフェーズ.Value == ステージ.演奏.演奏ステージ.フェーズ.クリアor失敗 )
687                                                         {
688                                                                 this.現在のステージ.非活性化する( this.デバイスリソース );
689                                                                 this.現在のステージ = this.結果ステージ;
690                                                                 this.現在のステージ.活性化する( this.デバイスリソース );
691                                                         }
692                                                         //--------------------
693                                                         #endregion
694                                                         #region " キャンセル → 選曲ステージへ。"
695                                                         //--------------------
696                                                         if( this.演奏ステージ.現在のフェーズ.Value == ステージ.演奏.演奏ステージ.フェーズ.キャンセル )
697                                                         {
698                                                                 this.現在のステージ.非活性化する( this.デバイスリソース );
699                                                                 this.現在のステージ = this.選曲ステージ;
700                                                                 this.現在のステージ.活性化する( this.デバイスリソース );
701                                                         }
702                                                         //--------------------
703                                                         #endregion
704                                                 }
705                                                 break;
706
707                                         case nameof( ステージ.結果.結果ステージ ):
708                                                 #region " 終了 → 選曲ステージへ。"
709                                                 //--------------------
710                                                 if( this.結果ステージ.現在のフェーズ == ステージ.結果.結果ステージ.フェーズ.終了 )
711                                                 {
712                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
713                                                         this.現在のステージ = this.選曲ステージ;
714                                                         this.現在のステージ.活性化する( this.デバイスリソース );
715                                                 }
716                                                 //--------------------
717                                                 #endregion
718                                                 break;
719                                 }
720                         }
721                         //----------------
722                         #endregion
723                 }
724                 protected void デバイス依存リソースを解放する()
725                 {
726                         FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
727                         Debug.Assert( null != this.デバイスリソース );  // 解放前であること。
728
729                         this.現在のステージ?.デバイス依存リソースを解放する( this.デバイスリソース );
730
731                         FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
732                 }
733                 protected void デバイス依存リソースを再構築する()
734                 {
735                         FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
736                         Debug.Assert( null != this.デバイスリソース );  // 再生成済みであること。
737
738                         this.現在のステージ?.デバイス依存リソースを作成する( this.デバイスリソース );
739
740                         FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
741                 }
742
743                 private void フォームサイズが変更された( object sender, EventArgs e )
744                 {
745                         FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
746                         FDK.Log.Info( $"新しいクライアントサイズ = {this.MainForm.ClientSize}" );
747
748                         #region " 実行条件チェック。"
749                         //----------------
750                         if( null == this.デバイスリソース )
751                         {
752                                 FDK.Log.Info( " まだ初期化されてないので、何もしません。" );
753                                 FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
754                                 return;
755                         }
756                         if( this.MainForm.WindowState == FormWindowState.Minimized )
757                         {
758                                 FDK.Log.Info( "最小化されました。" );
759                                 FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
760                                 return; // 何もしない
761                         }
762                         if( this.State != ApplicationState.進行描画 )
763                         {
764                                 FDK.Log.Info( " アプリケーションの状態が進行描画じゃないので、何もしません。" );
765                                 FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
766                                 return;
767                         }
768                         //----------------
769                         #endregion
770
771                         Debug.Assert( null != this.デバイスリソース, "デバイスリソースが作成済みであること。" );
772
773                         // 現在の画面モードを取得しておく。(Alt+TABなど、勝手に全画面を解除されることもあるので。)
774                         SharpDX.Mathematics.Interop.RawBool fullscreen;
775                         SharpDX.DXGI.Output outputTarget;
776                         this.デバイスリソース.SwapChain1.GetFullscreenState( out fullscreen, out outputTarget );
777                         this.MainForm.IsFullscreen = fullscreen;
778                         outputTarget?.Dispose();
779                         FDK.Log.Info( $"現在、全画面モードである = {this.MainForm.IsFullscreen}" );
780
781                         // (1) リソースを解放して、
782                         this.デバイス依存リソースを解放する();
783                         this.デバイスリソース.サイズに依存するリソースを解放する();
784
785                         // (2) 物理画面サイズを変更して、
786                         this.デバイスリソース.物理画面サイズpx = new SharpDX.Size2F( this.MainForm.ClientSize.Width, this.MainForm.ClientSize.Height );
787
788                         // (3) リソースを再構築する。
789                         this.デバイスリソース.サイズに依存するリソースを作成する();
790                         this.デバイス依存リソースを再構築する();
791
792                         FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
793                 }
794                 private void 全画面モードとウィンドウモードを切り替える()
795                 {
796                         FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
797
798                         this.MainForm.IsFullscreen = !this.MainForm.IsFullscreen;   // 切り替え
799                         this.デバイスリソース.SwapChain1.SetFullscreenState( this.MainForm.IsFullscreen, null );
800
801                         FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
802                 }
803                 private void ログインする( string ユーザ名 )
804                 {
805                         StrokeStyleT.ユーザ管理.ユーザを選択する( ユーザ名 );
806                         FDK.Log.Info( $"ユーザが選択されました。[{StrokeStyleT.ユーザ管理.現在選択されているユーザ.名前}]" );
807                 }
808                 /// <returns>メッセージがない場合は null を返す。</returns>
809                 private ViewerMessage ビュアーメッセージを取得する()
810                 {
811                         var msg = (ViewerMessage) null;
812
813                         if( StrokeStyleT.ビュアーモードである &&
814                                 ( 0 < this.ビュアーメッセージキュー.Count ) &&
815                                 this.ビュアーメッセージキュー.TryDequeue( out msg ) )
816                         {
817                                 return msg;
818                         }
819
820                         return null;
821                 }
822
823                 #region " バックストア。"
824                 //----------------
825                 private static SST.フォルダ bs_フォルダ = null;
826                 private static FDK.入力.Keyboard bs_キーボード入力 = null;
827                 private static FDK.入力.MidiIn bs_MIDI入力 = null;
828                 private static FDK.メディア.サウンド.WASAPI排他.ExclusiveDevice bs_Wasapiデバイス = null;
829                 private static readonly System.Random bs_乱数 = new Random( DateTime.Now.Millisecond );
830                 private static SST.ユーザ.ユーザ管理 bs_ユーザ管理 = null;
831                 private static SST.曲.曲ツリー管理 bs_曲ツリー管理 = null;
832                 private static SST.設定.Config bs_Config = null;
833                 //----------------
834                 #endregion
835         }
836 }