OSDN Git Service

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