OSDN Git Service

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