2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Diagnostics;
6 using System.Windows.Forms;
7 using Microsoft.VisualBasic.ApplicationServices;
11 class StrokeStyleT : FDK.ApplicationBase
14 public static SST.フォルダ フォルダ => ( StrokeStyleT.bs_フォルダ );
15 public static FDK.入力.Keyboard キーボード入力 => ( StrokeStyleT.bs_キーボード入力 );
16 public static FDK.入力.MidiIn MIDI入力 => ( StrokeStyleT.bs_MIDI入力 );
17 public static FDK.メディア.サウンド.WASAPI排他.ExclusiveDevice Wasapiデバイス => ( StrokeStyleT.bs_Wasapiデバイス );
18 public static Random 乱数 => ( StrokeStyleT.bs_乱数 );
19 public static SST.ユーザ.ユーザ管理 ユーザ管理 => ( StrokeStyleT.bs_ユーザ管理 );
20 public static SST.曲.曲ツリー管理 曲ツリー管理 => ( StrokeStyleT.bs_曲ツリー管理 );
21 public static SSTFormat.スコア 演奏スコア { get; set; } = null;
22 public static SST.設定.Config Config => ( StrokeStyleT.bs_Config );
23 public static bool ビュアーモード => ( StrokeStyleT.bs_ビュアーモード );
24 public static string ビューファイル => ( StrokeStyleT.bs_ビューファイル );
26 public static void すべての入力デバイスをポーリングする()
28 // hack: 追加の入力デバイスができたら、ここにポーリングコードを追加すること。
29 StrokeStyleT.キーボード入力?.ポーリングする();
30 StrokeStyleT.MIDI入力?.ポーリングする();
33 // get only static property の初期化。
36 // フォルダ変数を真っ先に登録する。(ほかのメンバのコンストラクタでフォルダ変数を利用できるようにするため。)
37 StrokeStyleT.bs_フォルダ = new SST.フォルダ();
38 SST.フォルダ.フォルダ変数を追加する( "Static", StrokeStyleT.フォルダ.StaticFolder );
39 SST.フォルダ.フォルダ変数を追加する( "AppData", StrokeStyleT.フォルダ.AppDataFolder );
40 SST.フォルダ.フォルダ変数を追加する( "User", null );
43 StrokeStyleT.bs_ユーザ管理 = new ユーザ.ユーザ管理();
44 StrokeStyleT.bs_曲ツリー管理 = new 曲.曲ツリー管理();
47 protected override void 初期化する()
49 FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
50 Debug.Assert( null == this.スレッド排他領域.デバイスリソース, "デバイスリソースの作成前であること。" );
52 this.コマンドライン引数を処理する( Environment.GetCommandLineArgs().Skip( 1 ) ); // 最初の要素は exe ファイル名なのでスキップする。
54 this.MainForm.Text = $"StrokeStyle<T> {System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()}";
55 this.設計画面サイズdpx = new SharpDX.Size2F( 1920, 1080 ); // 設計画面サイズdpx(固定)
57 #region " コンフィグ を初期化する。"
59 FDK.Log.Info( "コンフィグを初期化します。" );
60 StrokeStyleT.bs_Config = new 設定.Config();
61 StrokeStyleT.bs_Config.ConfigXmlを読み込む();
64 #region " コンフィグで指定されたウィンドウサイズに変更。"
66 this.MainForm.ClientSize = new System.Drawing.Size( StrokeStyleT.Config.物理画面サイズpx.Width, StrokeStyleT.Config.物理画面サイズpx.Height );
70 #region " System.Stopwatch が高解像度タイマを使わないならエラー。"
72 if( false == System.Diagnostics.Stopwatch.IsHighResolution )
73 throw new SSTException( "このシステムは、高解像度タイマをサポートしていません。" );
76 #region " MediaFoundation を起動する。"
78 SharpDX.MediaFoundation.MediaManager.Startup();
81 #region " Sleep 精度を上げる。"
83 StrokeStyleT.timeBeginPeriod( 1 );
87 #region " ステージのActionを接続する。"
89 this.結果ステージ.演奏ステージインスタンスを取得する = () => this.演奏ステージ;
94 FDK.Log.Info( "ユーザ情報を初期化します。" );
95 StrokeStyleT.ユーザ管理.UsersXmlを読み込む();
98 foreach( var ユーザ in StrokeStyleT.ユーザ管理.ユーザリスト )
99 ユーザ.SourcesXmlを読み込む();
102 #region " WASAPI デバイスを初期化する。"
104 StrokeStyleT.bs_Wasapiデバイス = new FDK.メディア.サウンド.WASAPI排他.ExclusiveDevice();
105 StrokeStyleT.bs_Wasapiデバイス.初期化する( 15.0f );
108 #region " キーボード入力 を初期化する。"
110 FDK.Log.Info( "キーボード入力デバイスを初期化します。" );
111 StrokeStyleT.bs_キーボード入力 = new FDK.入力.Keyboard( this.MainForm.Handle );
114 #region " MIDI入力 を初期化する。"
116 FDK.Log.Info( "MIDI入力デバイスを初期化します。" );
117 StrokeStyleT.bs_MIDI入力 = new FDK.入力.MidiIn();
121 this.現在のステージ = this.最初のダミーステージ;
123 //#warning 全画面モード切替えを KeyDown で仮実装。
124 this.MainForm.KeyDown += ( target, arg ) => {
125 // Alt+Enter → 画面モードの切り替え
126 // if( ( arg.KeyCode == System.Windows.Forms.Keys.Return ) && ( arg.Modifiers == Keys.Alt ) )
127 // this.全画面モードとウィンドウモードを切り替える();
130 FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
132 protected override void 終了する()
134 FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
136 Debug.Assert( null != this.スレッド排他領域.デバイスリソース, "デバイスリソースが解放される前であること。" );
138 #region " ステージを終了し、解放する。"
140 if( ( null != this.現在のステージ ) && ( this.現在のステージ.活性化している ) )
141 this.現在のステージ.非活性化する( this.スレッド排他領域.デバイスリソース );
143 this.最初のダミーステージ = null;
145 this.タイトルステージ = null;
146 this.ログインステージ = null;
153 #region " MIDI入力 を解放する。"
155 FDK.Log.Info( "MIDI入力デバイスを解放します。" );
156 FDK.Utilities.解放する( ref StrokeStyleT.bs_MIDI入力 );
159 #region " キーボード入力 を解放する。"
161 FDK.Log.Info( "キーボード入力デバイスを解放します。" );
162 FDK.Utilities.解放する( ref StrokeStyleT.bs_キーボード入力 );
165 #region " WASAPIデバイスを解放する。"
167 FDK.Log.Info( "WASAPIデバイスを解放します。" );
168 FDK.Utilities.解放する( ref StrokeStyleT.bs_Wasapiデバイス );
171 #region " コンフィグを解放する。"
173 FDK.Log.Info( "コンフィグを解放します。" );
174 StrokeStyleT.bs_Config = null;
177 #region " MediaFoundation を終了する。"
179 SharpDX.MediaFoundation.MediaManager.Shutdown();
183 FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
185 protected override void シーンを描画する()
187 // このメソッドは、GUIスレッドではなく進行描画スレッドから呼び出されるので注意。
189 this.スレッド排他領域.WriteLock( () => {
193 var dr = this.スレッド排他領域.デバイスリソース;
194 var d3dDevice = (SharpDX.Direct3D11.Device) null;
195 using( var d3dLock = new FDK.同期.AutoD3DDeviceLock( dr.DXGIDeviceManager, out d3dDevice ) )
197 using( var d3dContext = d3dDevice.ImmediateContext )
199 // 既定のD3Dレンダーターゲットビューを黒でクリアする。
200 d3dContext.ClearRenderTargetView( dr.D3DRenderTargetView, SharpDX.Color4.Black );
202 // 深度バッファを 1.0f でクリアする。
203 d3dContext.ClearDepthStencilView(
204 dr.D3DDepthStencilView,
205 SharpDX.Direct3D11.DepthStencilClearFlags.Depth,
212 this.現在のステージ?.進行描画する( this.スレッド排他領域.デバイスリソース );
216 // スワップチェーンを表示する。垂直帰線待ちなどで時間がかかるので、この部分はスレッド排他領域の外に配置すること。
217 if( false == this.スレッド排他領域.アプリを終了せよ )
219 this.スレッド排他領域.デバイスリソース.SwapChain.Present(
220 ( StrokeStyleT.Config.垂直帰線待ちを行う ) ? 1 : 0,
221 SharpDX.DXGI.PresentFlags.None );
224 // ステージの状態をチェックし、必要あれば遷移またはアプリを終了する。
225 this.スレッド排他領域.WriteLock( () => {
227 if( null != this.現在のステージ )
229 switch( this.現在のステージ.GetType().Name )
231 case nameof( ステージ.ステージ ):
232 #region " 最初のダミーステージ → 起動ステージへ。"
234 this.起動ステージ.活性化する( this.スレッド排他領域.デバイスリソース );
235 this.現在のステージ = this.起動ステージ;
240 case nameof( ステージ.起動.起動ステージ ):
241 #region " 終了 → タイトルステージへ。"
243 if( this.起動ステージ.現在のフェーズ == ステージ.起動.起動ステージ.フェーズ.終了 )
245 this.現在のステージ.非活性化する( this.スレッド排他領域.デバイスリソース );
246 this.現在のステージ = this.タイトルステージ;
247 this.現在のステージ.活性化する( this.スレッド排他領域.デバイスリソース );
253 case nameof( ステージ.タイトル.タイトルステージ ):
254 #region " 確定 → ログインステージへ。"
256 if( this.タイトルステージ.現在のフェーズ == ステージ.タイトル.タイトルステージ.フェーズ.確定 )
258 this.現在のステージ.非活性化する( this.スレッド排他領域.デバイスリソース );
259 this.現在のステージ = this.ログインステージ;
260 this.現在のステージ.活性化する( this.スレッド排他領域.デバイスリソース );
264 #region " キャンセル → アプリを終了する。"
266 else if( this.タイトルステージ.現在のフェーズ == ステージ.タイトル.タイトルステージ.フェーズ.キャンセル )
268 this.現在のステージ.非活性化する( this.スレッド排他領域.デバイスリソース );
270 this.スレッド排他領域.アプリを終了せよ = true;
276 case nameof( ステージ.ログイン.ログインステージ ):
277 #region " 確定 → ログイン処理を行って、選曲ステージへ。"
279 if( this.ログインステージ.現在のフェーズ == ステージ.ログイン.ログインステージ.フェーズ.確定 )
281 // ログイン処理(1) ユーザリストに対してユーザを選択する。
282 StrokeStyleT.ユーザ管理.ユーザを選択する( this.ログインステージ.ユーザインデックス );
283 FDK.Log.Info( $"ユーザが選択されました。[{StrokeStyleT.ユーザ管理.現在選択されているユーザ.名前}]" );
285 // ログイン処理(2) 選択ユーザの曲ツリーを構築する。
286 StrokeStyleT.ユーザ管理.現在選択されているユーザ.曲を検索して曲ツリーを構築する();
288 // ログイン処理(3) 構築した曲ツリーを曲リスト管理に登録する。
289 StrokeStyleT.曲ツリー管理.現在の管理対象ツリー = StrokeStyleT.ユーザ管理.現在選択されているユーザ.曲ツリーのルートノード;
292 this.現在のステージ.非活性化する( this.スレッド排他領域.デバイスリソース );
293 this.現在のステージ = this.選曲ステージ;
294 this.現在のステージ.活性化する( this.スレッド排他領域.デバイスリソース );
298 #region " キャンセル → タイトルステージへ。"
300 else if( this.ログインステージ.現在のフェーズ == ステージ.ログイン.ログインステージ.フェーズ.キャンセル )
302 this.現在のステージ.非活性化する( this.スレッド排他領域.デバイスリソース );
303 this.現在のステージ = this.タイトルステージ;
304 this.現在のステージ.活性化する( this.スレッド排他領域.デバイスリソース );
310 case nameof( ステージ.選曲.選曲ステージ ):
311 #region " 曲確定 → 曲読込ステージへ。"
313 if( this.選曲ステージ.現在のフェーズ == ステージ.選曲.選曲ステージ.フェーズ.曲確定 )
315 // 曲ノードが選択されていることを確認。
316 Trace.Assert( null != StrokeStyleT.曲ツリー管理.現在選択されているノード, "[バグあり] 選択曲が null です。" );
317 this.現在のステージ.非活性化する( this.スレッド排他領域.デバイスリソース );
318 this.現在のステージ = this.曲読込ステージ;
319 this.現在のステージ.活性化する( this.スレッド排他領域.デバイスリソース );
323 #region " キャンセル → アプリを終了する。"
325 else if( this.選曲ステージ.現在のフェーズ == ステージ.選曲.選曲ステージ.フェーズ.キャンセル )
327 this.現在のステージ.非活性化する( this.スレッド排他領域.デバイスリソース );
329 this.スレッド排他領域.アプリを終了せよ = true;
335 case nameof( ステージ.曲読込.曲読込ステージ ):
336 #region " 終了 → 演奏ステージへ。"
337 //--------------------
338 if( this.曲読込ステージ.現在のフェーズ == ステージ.曲読込.曲読込ステージ.フェーズ.終了 )
340 // 演奏スコアインスタンスが作成されていることを確認。
341 Debug.Assert( null != StrokeStyleT.演奏スコア );
343 this.現在のステージ.非活性化する( this.スレッド排他領域.デバイスリソース );
344 this.現在のステージ = this.演奏ステージ;
345 this.現在のステージ.活性化する( this.スレッド排他領域.デバイスリソース );
347 //--------------------
351 case nameof( ステージ.演奏.演奏ステージ ):
352 #region " 演奏終了 → 結果ステージへ。"
353 //--------------------
354 if( this.演奏ステージ.現在のフェーズ == ステージ.演奏.演奏ステージ.フェーズ.クリアor失敗 )
356 this.現在のステージ.非活性化する( this.スレッド排他領域.デバイスリソース );
357 this.現在のステージ = this.結果ステージ;
358 this.現在のステージ.活性化する( this.スレッド排他領域.デバイスリソース );
360 //--------------------
362 #region " キャンセル → 選曲ステージへ。"
363 //--------------------
364 if( this.演奏ステージ.現在のフェーズ == ステージ.演奏.演奏ステージ.フェーズ.キャンセル )
366 #warning 結果ステージのデバッグのため、演奏キャンセル時も結果ステージへ遷移する。
367 this.現在のステージ.非活性化する( this.スレッド排他領域.デバイスリソース );
368 this.現在のステージ = this.結果ステージ;
369 this.現在のステージ.活性化する( this.スレッド排他領域.デバイスリソース );
370 //this.現在のステージ.非活性化する( this.スレッド排他領域.デバイスリソース );
371 //this.現在のステージ = this.選曲ステージ;
372 //this.現在のステージ.活性化する( this.スレッド排他領域.デバイスリソース );
374 //--------------------
378 case nameof( ステージ.結果.結果ステージ ):
379 #region " 終了 → 選曲ステージへ。"
380 //--------------------
381 if( this.結果ステージ.現在のフェーズ == ステージ.結果.結果ステージ.フェーズ.終了 )
383 this.現在のステージ.非活性化する( this.スレッド排他領域.デバイスリソース );
384 this.現在のステージ = this.選曲ステージ;
385 this.現在のステージ.活性化する( this.スレッド排他領域.デバイスリソース );
387 //--------------------
395 protected override void デバイス依存リソースを再構築する()
397 FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
399 this.現在のステージ?.デバイス依存リソースを作成する( this.スレッド排他領域.デバイスリソース );
401 FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
403 protected override void デバイス依存リソースを解放する()
405 FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
407 this.現在のステージ?.デバイス依存リソースを解放する( this.スレッド排他領域.デバイスリソース );
409 FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
412 /// アプリが二重起動されたときに発生するイベント。
415 /// 後続のインスタンスは起動せず、既存のインスタンスに対してこのイベントが発生する。
416 /// eventArg.CommandLine で、後続のインスタンスのコマンドライン引数を確認することができる。
418 protected override void OnStartupNextInstance( StartupNextInstanceEventArgs eventArgs )
420 FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
422 this.コマンドライン引数を処理する( eventArgs.CommandLine );
424 FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
427 // 各ステージの、唯一のインスタンス。最終的に null を代入するので、readonlyにはしない。
428 protected SST.ステージ.ステージ 最初のダミーステージ = new ステージ.ステージ();
429 protected SST.ステージ.起動.起動ステージ 起動ステージ = new ステージ.起動.起動ステージ();
430 protected SST.ステージ.タイトル.タイトルステージ タイトルステージ = new ステージ.タイトル.タイトルステージ();
431 protected SST.ステージ.ログイン.ログインステージ ログインステージ = new ステージ.ログイン.ログインステージ();
432 protected SST.ステージ.選曲.選曲ステージ 選曲ステージ = new ステージ.選曲.選曲ステージ();
433 protected SST.ステージ.曲読込.曲読込ステージ 曲読込ステージ = new ステージ.曲読込.曲読込ステージ();
434 protected SST.ステージ.演奏.演奏ステージ 演奏ステージ = new ステージ.演奏.演奏ステージ();
435 protected SST.ステージ.結果.結果ステージ 結果ステージ = new ステージ.結果.結果ステージ();
437 private SST.ステージ.ステージ 現在のステージ = null;
441 private static SST.フォルダ bs_フォルダ = null;
442 private static FDK.入力.Keyboard bs_キーボード入力 = null;
443 private static FDK.入力.MidiIn bs_MIDI入力 = null;
444 private static FDK.メディア.サウンド.WASAPI排他.ExclusiveDevice bs_Wasapiデバイス = null;
445 private static readonly Random bs_乱数 = new Random( DateTime.Now.Millisecond );
446 private static SST.ユーザ.ユーザ管理 bs_ユーザ管理 = null;
447 private static SST.曲.曲ツリー管理 bs_曲ツリー管理 = null;
448 private static SST.設定.Config bs_Config = null;
450 private static bool bs_ビュアーモード = false;
451 private static string bs_ビューファイル = null;
455 /// <param name="args">コマンドライン引数の列挙。exeファイル名は含まない。</param>
456 private void コマンドライン引数を処理する( IEnumerable<string> args )
458 FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
459 FDK.Log.Info( $"args.Count = {args.Count()}" );
460 //Mono.Options.OptionSet
465 FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
469 #region " Win32 API "
471 [ System.Runtime.InteropServices.DllImport( "winmm.dll", EntryPoint = "timeBeginPeriod" )]
472 private static extern uint timeBeginPeriod( uint uMilliseconds );
474 [System.Runtime.InteropServices.DllImport( "winmm.dll", EntryPoint = "timeEndPeriod" )]
475 private static extern uint timeEndPeriod( uint uMilliseconds );