OSDN Git Service

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