2 using System.Collections.Generic;
3 using System.Diagnostics;
6 using System.Windows.Forms;
7 using Microsoft.VisualBasic.ApplicationServices;
8 using FDK; // for string 拡張
12 class StrokeStyleT : FDK.ApplicationBase
15 public static SST.フォルダ フォルダ => ( StrokeStyleT.bs_フォルダ );
16 public static FDK.入力.Keyboard キーボード入力 => ( StrokeStyleT.bs_キーボード入力 );
17 public static FDK.入力.MidiIn MIDI入力 => ( StrokeStyleT.bs_MIDI入力 );
18 public static FDK.メディア.サウンド.WASAPI排他.ExclusiveDevice Wasapiデバイス => ( StrokeStyleT.bs_Wasapiデバイス );
19 public static Random 乱数 => ( StrokeStyleT.bs_乱数 );
20 public static SST.ユーザ.ユーザ管理 ユーザ管理 => ( StrokeStyleT.bs_ユーザ管理 );
21 public static SST.曲.曲ツリー管理 曲ツリー管理 => ( StrokeStyleT.bs_曲ツリー管理 );
22 public static SSTFormat.スコア 演奏スコア { get; set; } = null;
23 public static SST.設定.Config Config => ( StrokeStyleT.bs_Config );
26 /// ビュアーモード用のプロパティ。(static)
27 /// 各プロパティの値は、起動時のコマンドライン引数を解析して得られる。
29 public class ViewerMode
31 public bool ビュアーモードである => ( this.曲ファイルパス.Nullでも空でもない() );
32 public string 曲ファイルパス { get; set; } = null;
33 public int 演奏開始小節番号 { get; set; } = 0;
34 public bool 演奏停止 { get; set; } = false;
35 public bool ドラム音を使う { get; set; } = false;
37 public static ViewerMode ビュアーモード => ( StrokeStyleT.bs_ビュアーモード );
39 public static void すべての入力デバイスをポーリングする()
41 // hack: 追加の入力デバイスができたら、ここにポーリングコードを追加すること。
42 StrokeStyleT.キーボード入力?.ポーリングする();
43 StrokeStyleT.MIDI入力?.ポーリングする();
46 // get only static property の初期化。
49 // フォルダ変数を真っ先に登録する。(ほかのメンバのコンストラクタでフォルダ変数を利用できるようにするため。)
50 StrokeStyleT.bs_フォルダ = new SST.フォルダ();
51 SST.フォルダ.フォルダ変数を追加する( "Static", StrokeStyleT.フォルダ.StaticFolder );
52 SST.フォルダ.フォルダ変数を追加する( "AppData", StrokeStyleT.フォルダ.AppDataFolder );
53 SST.フォルダ.フォルダ変数を追加する( "User", null );
56 StrokeStyleT.bs_ユーザ管理 = new ユーザ.ユーザ管理();
57 StrokeStyleT.bs_曲ツリー管理 = new 曲.曲ツリー管理();
60 protected override void 初期化する()
62 FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
66 Debug.Assert( null == this.デバイスリソース, "デバイスリソースの作成前であること。" );
68 StrokeStyleT.bs_ビュアーモード = this.コマンドライン引数を解析する(
69 Environment.GetCommandLineArgs().Skip( 1 ) ); // 最初の要素は exe ファイル名なのでスキップする。
71 this.MainForm.Text = $"StrokeStyle<T> {System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()}";
72 this.設計画面サイズdpx = new SharpDX.Size2F( 1920, 1080 ); // 設計画面サイズdpx(固定)
74 #region " コンフィグ を初期化する。"
76 FDK.Log.Info( "コンフィグを初期化します。" );
77 StrokeStyleT.bs_Config = new 設定.Config();
78 StrokeStyleT.bs_Config.ConfigXmlを読み込む();
81 #region " コンフィグで指定されたウィンドウサイズに変更する。"
83 this.MainForm.ClientSize = new System.Drawing.Size( StrokeStyleT.Config.物理画面サイズpx.Width, StrokeStyleT.Config.物理画面サイズpx.Height );
87 #region " System.Stopwatch が高解像度タイマを使わないならエラー。"
89 if( false == System.Diagnostics.Stopwatch.IsHighResolution )
90 throw new SSTException( "このシステムは、高解像度タイマをサポートしていません。" );
93 #region " MediaFoundation を起動する。"
95 SharpDX.MediaFoundation.MediaManager.Startup();
98 #region " Sleep 精度を上げる。"
100 StrokeStyleT.timeBeginPeriod( 1 );
104 #region " ステージのActionを接続する。"
106 this.結果ステージ.演奏ステージインスタンスを取得する = () => ( this.演奏ステージ );
107 this.結果ステージ.BGMを終了する = () => { this.演奏ステージ.BGMを解放する(); };
110 #region " ユーザを初期化する。"
112 FDK.Log.Info( "ユーザ情報を初期化します。" );
113 StrokeStyleT.ユーザ管理.UsersXmlを読み込む();
116 foreach( var ユーザ in StrokeStyleT.ユーザ管理.ユーザリスト )
117 ユーザ.SourcesXmlを読み込む();
120 #region " WASAPI デバイスを初期化する。"
122 StrokeStyleT.bs_Wasapiデバイス = new FDK.メディア.サウンド.WASAPI排他.ExclusiveDevice();
123 StrokeStyleT.bs_Wasapiデバイス.初期化する( 15.0f );
126 #region " キーボード入力 を初期化する。"
128 FDK.Log.Info( "キーボード入力デバイスを初期化します。" );
129 StrokeStyleT.bs_キーボード入力 = new FDK.入力.Keyboard( this.MainForm.Handle );
132 #region " MIDI入力 を初期化する。"
134 FDK.Log.Info( "MIDI入力デバイスを初期化します。" );
135 StrokeStyleT.bs_MIDI入力 = new FDK.入力.MidiIn();
139 this.現在のステージ = this.最初のダミーステージ;
141 //#warning 全画面モード切替えを KeyDown で仮実装。
142 this.MainForm.KeyDown += ( target, arg ) => {
143 // Alt+Enter → 画面モードの切り替え
144 // if( ( arg.KeyCode == System.Windows.Forms.Keys.Return ) && ( arg.Modifiers == Keys.Alt ) )
145 // this.全画面モードとウィンドウモードを切り替える();
149 FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
151 protected override void 終了する()
153 FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
157 Debug.Assert( null != this.デバイスリソース, "デバイスリソースが解放される前であること。" );
159 #region " ステージを終了し、解放する。"
161 if( ( null != this.現在のステージ ) && ( this.現在のステージ.活性化している ) ) // 念のため
162 this.現在のステージ.非活性化する( this.デバイスリソース );
164 this.最初のダミーステージ = null;
166 this.タイトルステージ = null;
167 this.ログインステージ = null;
172 this.ビュアーステージ = null;
175 #region " MIDI入力 を解放する。"
177 FDK.Log.Info( "MIDI入力デバイスを解放します。" );
178 FDK.Utilities.解放する( ref StrokeStyleT.bs_MIDI入力 );
181 #region " キーボード入力 を解放する。"
183 FDK.Log.Info( "キーボード入力デバイスを解放します。" );
184 FDK.Utilities.解放する( ref StrokeStyleT.bs_キーボード入力 );
187 #region " WASAPIデバイスを解放する。"
189 FDK.Log.Info( "WASAPIデバイスを解放します。" );
190 FDK.Utilities.解放する( ref StrokeStyleT.bs_Wasapiデバイス );
193 #region " コンフィグを解放する。"
195 FDK.Log.Info( "コンフィグを解放します。" );
196 StrokeStyleT.bs_Config = null;
199 #region " MediaFoundation を終了する。"
201 SharpDX.MediaFoundation.MediaManager.Shutdown();
206 FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
208 protected override void シーンを描画する()
210 // このメソッドは、GUIスレッドではなく進行描画スレッドから呼び出されるので注意。(FDK.ApplicationBase.進行描画スレッド処理() を参照。)
217 var d3dDevice = (SharpDX.Direct3D11.Device) null;
218 using( var d3dLock = new FDK.同期.AutoD3DDeviceLock( this.デバイスリソース.DXGIDeviceManager, out d3dDevice ) )
220 using( var d3dContext = d3dDevice.ImmediateContext )
222 // 既定のD3Dレンダーターゲットビューを黒でクリアする。
223 d3dContext.ClearRenderTargetView( this.デバイスリソース.D3DRenderTargetView, SharpDX.Color4.Black );
225 // 深度バッファを 1.0f でクリアする。
226 d3dContext.ClearDepthStencilView(
227 this.デバイスリソース.D3DDepthStencilView,
228 SharpDX.Direct3D11.DepthStencilClearFlags.Depth,
235 this.現在のステージ?.進行描画する( this.デバイスリソース );
238 // スワップチェーンを表示する。垂直帰線待ちなどで時間がかかるので、この部分はスレッド排他領域の外に配置すること。
239 this.デバイスリソース.SwapChain.Present(
240 ( StrokeStyleT.Config.垂直帰線待ちを行う ) ? 1 : 0,
241 SharpDX.DXGI.PresentFlags.None );
243 // ステージの状態をチェックし、必要あれば遷移またはアプリを終了する。
244 bool アプリを終了せよ = false;
247 if( null != this.現在のステージ )
249 switch( this.現在のステージ.GetType().Name )
251 case nameof( ステージ.ステージ ):
252 #region " ビュアーモード → ビュアーステージへ。"
254 if( StrokeStyleT.ビュアーモード.ビュアーモードである )
256 this.ビュアーステージ.活性化する( this.デバイスリソース );
257 this.現在のステージ = this.ビュアーステージ;
261 #region " 通常モード → 起動ステージへ。"
265 this.起動ステージ.活性化する( this.デバイスリソース );
266 this.現在のステージ = this.起動ステージ;
272 case nameof( ステージ.起動.起動ステージ ):
273 #region " 終了 → タイトルステージへ。"
275 if( this.起動ステージ.現在のフェーズ == ステージ.起動.起動ステージ.フェーズ.終了 )
277 this.現在のステージ.非活性化する( this.デバイスリソース );
278 this.現在のステージ = this.タイトルステージ;
279 this.現在のステージ.活性化する( this.デバイスリソース );
285 case nameof( ステージ.タイトル.タイトルステージ ):
286 #region " 確定 → ログインステージへ。"
288 if( this.タイトルステージ.現在のフェーズ == ステージ.タイトル.タイトルステージ.フェーズ.確定 )
290 this.現在のステージ.非活性化する( this.デバイスリソース );
291 this.現在のステージ = this.ログインステージ;
292 this.現在のステージ.活性化する( this.デバイスリソース );
296 #region " キャンセル → アプリを終了する。"
298 else if( this.タイトルステージ.現在のフェーズ == ステージ.タイトル.タイトルステージ.フェーズ.キャンセル )
300 this.現在のステージ.非活性化する( this.デバイスリソース );
308 case nameof( ステージ.ログイン.ログインステージ ):
309 #region " 確定 → ログイン処理を行って、選曲ステージへ。"
311 if( this.ログインステージ.現在のフェーズ == ステージ.ログイン.ログインステージ.フェーズ.確定 )
313 StrokeStyleT.ユーザ管理.ユーザを選択する( this.ログインステージ.ユーザインデックス );
314 FDK.Log.Info( $"ユーザが選択されました。[{StrokeStyleT.ユーザ管理.現在選択されているユーザ.名前}]" );
315 StrokeStyleT.ユーザ管理.現在選択されているユーザ.曲を検索して曲ツリーを構築する();
316 StrokeStyleT.曲ツリー管理.現在の管理対象ツリー = StrokeStyleT.ユーザ管理.現在選択されているユーザ.曲ツリーのルートノード;
319 this.現在のステージ.非活性化する( this.デバイスリソース );
320 this.現在のステージ = this.選曲ステージ;
321 this.現在のステージ.活性化する( this.デバイスリソース );
325 #region " キャンセル → タイトルステージへ。"
327 else if( this.ログインステージ.現在のフェーズ == ステージ.ログイン.ログインステージ.フェーズ.キャンセル )
329 this.現在のステージ.非活性化する( this.デバイスリソース );
330 this.現在のステージ = this.タイトルステージ;
331 this.現在のステージ.活性化する( this.デバイスリソース );
337 case nameof( ステージ.選曲.選曲ステージ ):
338 #region " 曲確定 → 曲読込ステージへ。"
340 if( this.選曲ステージ.現在のフェーズ == ステージ.選曲.選曲ステージ.フェーズ.曲確定 )
342 // 曲ノードが選択されていることを確認。
343 Trace.Assert( null != StrokeStyleT.曲ツリー管理.現在選択されているノード, "[バグあり] 選択曲が null です。" );
344 this.現在のステージ.非活性化する( this.デバイスリソース );
345 this.現在のステージ = this.曲読込ステージ;
346 this.現在のステージ.活性化する( this.デバイスリソース );
350 #region " キャンセル → アプリを終了する。"
352 else if( this.選曲ステージ.現在のフェーズ == ステージ.選曲.選曲ステージ.フェーズ.キャンセル )
354 this.現在のステージ.非活性化する( this.デバイスリソース );
362 case nameof( ステージ.曲読込.曲読込ステージ ):
363 #region " 終了 → 演奏ステージへ。"
364 //--------------------
365 if( this.曲読込ステージ.現在のフェーズ == ステージ.曲読込.曲読込ステージ.フェーズ.終了 )
367 // 演奏スコアインスタンスが作成されていることを確認。
368 Debug.Assert( null != StrokeStyleT.演奏スコア );
370 this.現在のステージ.非活性化する( this.デバイスリソース );
371 this.現在のステージ = this.演奏ステージ;
372 this.現在のステージ.活性化する( this.デバイスリソース );
374 //--------------------
378 case nameof( ステージ.演奏.演奏ステージ ):
379 #region " 演奏終了 → 結果ステージへ。"
380 //--------------------
381 if( this.演奏ステージ.現在のフェーズ == ステージ.演奏.演奏ステージ.フェーズ.クリアor失敗 )
383 this.現在のステージ.非活性化する( this.デバイスリソース );
384 this.現在のステージ = this.結果ステージ;
385 this.現在のステージ.活性化する( this.デバイスリソース );
387 //--------------------
389 #region " キャンセル → 選曲ステージへ。"
390 //--------------------
391 if( this.演奏ステージ.現在のフェーズ == ステージ.演奏.演奏ステージ.フェーズ.キャンセル )
393 #warning 結果ステージのデバッグのため、演奏キャンセル時も結果ステージへ遷移する。
394 this.現在のステージ.非活性化する( this.デバイスリソース );
395 this.現在のステージ = this.結果ステージ;
396 this.現在のステージ.活性化する( this.デバイスリソース );
397 //this.現在のステージ.非活性化する( this.スレッド排他領域.デバイスリソース );
398 //this.現在のステージ = this.選曲ステージ;
399 //this.現在のステージ.活性化する( this.スレッド排他領域.デバイスリソース );
401 //--------------------
405 case nameof( ステージ.結果.結果ステージ ):
406 #region " 終了 → 選曲ステージへ。"
407 //--------------------
408 if( this.結果ステージ.現在のフェーズ == ステージ.結果.結果ステージ.フェーズ.終了 )
410 this.現在のステージ.非活性化する( this.デバイスリソース );
411 this.現在のステージ = this.選曲ステージ;
412 this.現在のステージ.活性化する( this.デバイスリソース );
414 //--------------------
418 case nameof( ステージ.演奏.ビュアーステージ ):
419 #region " 終了 → アプリを終了する。"
421 if( this.ビュアーステージ.現在のフェーズ == ステージ.演奏.ビュアーステージ.フェーズ.終了 )
423 this.現在のステージ.非活性化する( this.デバイスリソース );
436 // GUIスレッド上で、ウィンドウを閉じる。
437 this.MainForm.BeginInvoke( new Action( () => { this.MainForm.Close(); } ) );
440 protected override void デバイス依存リソースを解放する()
442 FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
446 Debug.Assert( null != this.デバイスリソース ); // 解放前であること。
447 this.現在のステージ?.デバイス依存リソースを解放する( this.デバイスリソース );
450 FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
452 protected override void デバイス依存リソースを再構築する()
454 FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
458 Debug.Assert( null != this.デバイスリソース ); // 再生成済みであること。
459 this.現在のステージ?.デバイス依存リソースを作成する( this.デバイスリソース );
462 FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
465 /// アプリが二重起動されたときに発生するイベント。
468 /// 後続のインスタンスは起動せず、既存のインスタンスに対してこのイベントが発生する。
469 /// eventArg.CommandLine で、後続のインスタンスのコマンドライン引数を確認することができる。
471 protected override void OnStartupNextInstance( StartupNextInstanceEventArgs eventArgs )
473 FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
477 if( StrokeStyleT.ビュアーモード.ビュアーモードである )
479 this.新しいビュアーモード = this.コマンドライン引数を解析する( eventArgs.CommandLine );
480 this.ビュアーモードアクションを実行せよ.状態 = FDK.同期.TriStateEvent.状態種別.ON;
484 FDK.Log.ERROR( "現在、ビュアーモードではありません。" );
488 FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
491 // 各ステージの、唯一のインスタンス。最終的に null を代入するので、readonlyにはしない。
492 protected SST.ステージ.ステージ 最初のダミーステージ = new ステージ.ステージ();
493 protected SST.ステージ.起動.起動ステージ 起動ステージ = new ステージ.起動.起動ステージ();
494 protected SST.ステージ.タイトル.タイトルステージ タイトルステージ = new ステージ.タイトル.タイトルステージ();
495 protected SST.ステージ.ログイン.ログインステージ ログインステージ = new ステージ.ログイン.ログインステージ();
496 protected SST.ステージ.選曲.選曲ステージ 選曲ステージ = new ステージ.選曲.選曲ステージ();
497 protected SST.ステージ.曲読込.曲読込ステージ 曲読込ステージ = new ステージ.曲読込.曲読込ステージ();
498 protected SST.ステージ.演奏.演奏ステージ 演奏ステージ = new ステージ.演奏.演奏ステージ();
499 protected SST.ステージ.結果.結果ステージ 結果ステージ = new ステージ.結果.結果ステージ();
500 protected SST.ステージ.演奏.ビュアーステージ ビュアーステージ = new ステージ.演奏.ビュアーステージ();
502 private SST.ステージ.ステージ 現在のステージ = null;
505 private ViewerMode 新しいビュアーモード = null;
506 private FDK.同期.TriStateEvent ビュアーモードアクションを実行せよ = new FDK.同期.TriStateEvent( FDK.同期.TriStateEvent.状態種別.OFF );
510 private static SST.フォルダ bs_フォルダ = null;
511 private static FDK.入力.Keyboard bs_キーボード入力 = null;
512 private static FDK.入力.MidiIn bs_MIDI入力 = null;
513 private static FDK.メディア.サウンド.WASAPI排他.ExclusiveDevice bs_Wasapiデバイス = null;
514 private static readonly Random bs_乱数 = new Random( DateTime.Now.Millisecond );
515 private static SST.ユーザ.ユーザ管理 bs_ユーザ管理 = null;
516 private static SST.曲.曲ツリー管理 bs_曲ツリー管理 = null;
517 private static SST.設定.Config bs_Config = null;
518 private static SST.StrokeStyleT.ViewerMode bs_ビュアーモード = new ViewerMode();
523 /// コマンドライン引数を解析して、ビュアーモードの設定があればそれを返す。
525 /// <param name="args">コマンドライン引数の列挙。exeファイル名は含まない。</param>
526 /// <return>引数を反映したビュアーモード変数。</return>
527 private ViewerMode コマンドライン引数を解析する( IEnumerable<string> args )
529 FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
531 var vmode = new ViewerMode() {
540 if( 0 == args.Count() )
542 FDK.Log.Info( "引数は指定されていません。" ); // ビュアーモードではない。
549 var optionSet = new Mono.Options.OptionSet() {
550 "Usage: StrokeStyleT [File] [OPTIONS]+",
551 " File\t\t\t\tビュアーモードで表示する曲ファイル名です。",
552 { "p=|part=", "ビュアーモードで起動し、指定された小節番号から演奏を開始します。小節番号を省略すると、先頭から再生します。", (int v) => { vmode.演奏開始小節番号 = v; } },
553 { "s|stop", "ビュアーモードで演奏中であれば、演奏を停止します。", v => { vmode.演奏停止 = ( v != null ); } },
554 { "d|drums", "ビュアーモードで、チップヒット時に内蔵のドラム音を再生します。", v => { vmode.ドラム音を使う = ( v != null ); } },
558 List<string> ファイルパスs = optionSet.Parse( args );
563 // (A) 演奏停止(曲ファイルパスは省略可。)
564 FDK.Log.Info( "演奏停止" );
568 // (B) 演奏開始(曲ファイルパスは必須。)
569 if( 0 < ファイルパスs.Count )
571 if( File.Exists( ファイルパスs[ 0 ] ) )
573 vmode.曲ファイルパス = ファイルパスs[ 0 ];
577 vmode.曲ファイルパス = null; // ファイルが存在しなかったら null 。
578 throw new Mono.Options.OptionException( $"ファイルが存在しません。[{FDK.フォルダ.絶対パスをフォルダ変数付き絶対パスに変換して返す( ファイルパスs[ 0 ] )}]", "File" );
583 throw new Mono.Options.OptionException( "ファイルの指定がありません。", "File" );
586 FDK.Log.Info( $"曲ファイルパス: {vmode.曲ファイルパス}" );
587 FDK.Log.Info( $"開始小節番号: {vmode.演奏開始小節番号}" );
588 FDK.Log.Info( $"ドラム音: {vmode.ドラム音を使う}" );
591 catch( Mono.Options.OptionException e )
593 FDK.Log.ERROR( $"{e.Message}" );
600 FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
604 #region " Win32 API "
606 [ System.Runtime.InteropServices.DllImport( "winmm.dll", EntryPoint = "timeBeginPeriod" )]
607 private static extern uint timeBeginPeriod( uint uMilliseconds );
609 [System.Runtime.InteropServices.DllImport( "winmm.dll", EntryPoint = "timeEndPeriod" )]
610 private static extern uint timeEndPeriod( uint uMilliseconds );