OSDN Git Service

Users.xml が存在しない場合にこれを生成する際、AutoPlayer が追加されなかったミスを修正。
[strokestylet/CsWin10Desktop3.git] / StrokeStyleT / StrokeStyleT.cs
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Diagnostics;
5 using System.Linq;
6 using System.Windows.Forms;
7
8 namespace SST
9 {
10         class StrokeStyleT : FDK.ApplicationFormBase
11         {
12                 // グローバルリソース (static) 
13                 public static SST.フォルダ フォルダ => StrokeStyleT.bs_フォルダ;
14                 public static FDK.入力.Keyboard キーボード入力 => StrokeStyleT.bs_キーボード入力;
15                 public static FDK.入力.MidiIn MIDI入力 => StrokeStyleT.bs_MIDI入力;
16                 public static FDK.メディア.サウンド.WASAPI排他.ExclusiveDevice Wasapiデバイス => StrokeStyleT.bs_Wasapiデバイス;
17                 public static Random 乱数 => StrokeStyleT.bs_乱数;
18                 public static SST.ユーザ.ユーザ管理 ユーザ管理 => StrokeStyleT.bs_ユーザ管理;
19                 public static SST.曲.曲ツリー管理 曲ツリー管理 => StrokeStyleT.bs_曲ツリー管理;
20                 public static SSTFormat.スコア 演奏スコア { get; set; } = null;
21
22                 public static void すべての入力デバイスをポーリングする()
23                 {
24                         // hack: 追加の入力デバイスができたら、ここにポーリングコードを追加すること。
25                         StrokeStyleT.キーボード入力?.ポーリングする();
26                         StrokeStyleT.MIDI入力?.ポーリングする();
27                 }
28
29                 // get only static property の初期化。
30                 static StrokeStyleT()
31                 {
32                         // フォルダ変数を真っ先に登録する。(ほかのメンバのコンストラクタでフォルダ変数を利用できるようにするため。)
33                         StrokeStyleT.bs_フォルダ = new SST.フォルダ();
34                         SST.フォルダ.フォルダ変数を追加する( "Static", StrokeStyleT.フォルダ.StaticFolder );
35                         SST.フォルダ.フォルダ変数を追加する( "AppData", StrokeStyleT.フォルダ.AppDataFolder );
36                         SST.フォルダ.フォルダ変数を追加する( "User", null );
37
38                         // その他の static の生成。
39                         StrokeStyleT.bs_ユーザ管理 = new ユーザ.ユーザ管理();
40                         StrokeStyleT.bs_曲ツリー管理 = new 曲.曲ツリー管理();
41                 }
42
43                 public bool 垂直帰線待ちを行う { get; set; } = true;
44
45                 public override void 初期化する()
46                 {
47                         Debug.Assert( null == this.デバイスリソース );  // 作成される前である。
48                         FDK.Log.現在のスレッドに名前をつける( "GUI" );
49
50                         this.Text = $"StrokeStyle<T> {System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()}";
51                         this.ClientSize = new System.Drawing.Size( 1920, 1080 );    // 物理画面サイズpx
52                         this.設計画面サイズdpx = new SharpDX.Size2F( 1920, 1080 ); // 設計画面サイズdpx
53
54                         #region " System.Stopwatch が高解像度タイマを使わないならエラー。"
55                         //-----------------
56                         if( false == System.Diagnostics.Stopwatch.IsHighResolution )
57                                 throw new SSTException( "このシステムは、高解像度タイマをサポートしていません。" );
58                         //-----------------
59                         #endregion
60                         #region " MediaFoundation を起動する。"
61                         //-----------------
62                         SharpDX.MediaFoundation.MediaManager.Startup();
63                         //-----------------
64                         #endregion
65                         #region " Sleep 精度を上げる。"
66                         //-----------------
67                         StrokeStyleT.timeBeginPeriod( 1 );
68                         //-----------------
69                         #endregion
70
71                         #region " ユーザを初期化する。"
72                         //-----------------
73                         FDK.Log.Info( "ユーザ情報を初期化します。" );
74                         StrokeStyleT.ユーザ管理.UsersXmlを読み込む();
75
76                         // ユーザ別の初期化。
77                         foreach( var ユーザ in StrokeStyleT.ユーザ管理.ユーザリスト )
78                                 ユーザ.SourcesXmlを読み込む();
79                         //-----------------
80                         #endregion
81                         #region " WASAPI デバイスを初期化する。"
82                         //----------------
83                         StrokeStyleT.bs_Wasapiデバイス = new FDK.メディア.サウンド.WASAPI排他.ExclusiveDevice();
84                         StrokeStyleT.bs_Wasapiデバイス.初期化する( 15.0f );
85                         //----------------
86                         #endregion
87                         #region " キーボード入力 を初期化する。"
88                         //-----------------
89                         FDK.Log.Info( "キーボード入力デバイスを初期化します。" );
90                         StrokeStyleT.bs_キーボード入力 = new FDK.入力.Keyboard( this.Handle );
91                         //-----------------
92                         #endregion
93                         #region " MIDI入力 を初期化する。"
94                         //-----------------
95                         FDK.Log.Info( "MIDI入力デバイスを初期化します。" );
96                         StrokeStyleT.bs_MIDI入力 = new FDK.入力.MidiIn();
97                         //-----------------
98                         #endregion
99
100                         this.現在のステージ = this.最初のダミーステージ;
101
102 #warning テストコード。
103                         this.KeyDown += ( target, arg ) => {
104                                 // Alt+Enter → 画面モードの切り替え
105                                 if( ( arg.KeyCode == System.Windows.Forms.Keys.Return ) && ( arg.Modifiers == Keys.Alt ) )
106                                         this.全画面モードとウィンドウモードを切り替える();
107                         };
108                 }
109                 public override void 終了する()
110                 {
111                         Debug.Assert( null == this.デバイスリソース );  // 解放された後である。
112
113                         #region " ステージを終了し、解放する。"
114                         //----------------
115                         //if( this.現在のステージ.活性化している )
116                         //      this.現在のステージ.非活性化する( this.デバイスリソース ); → デバイスリソース解放後なので、ここで非活性化してはならない。
117                         this.最初のダミーステージ  = null;
118                         this.起動ステージ = null;
119                         this.タイトルステージ = null;
120                         this.ログインステージ = null;
121                         this.選曲ステージ = null;
122                         this.曲読込ステージ = null;
123                         this.演奏ステージ = null;
124                         this.結果ステージ = null;
125                         //----------------
126                         #endregion
127                         #region " MIDI入力 を解放する。"
128                         //-----------------
129                         FDK.Log.Info( "MIDI入力デバイスを解放します。" );
130                         FDK.Utilities.解放する( ref StrokeStyleT.bs_MIDI入力 );
131                         //-----------------
132                         #endregion
133                         #region " キーボード入力 を解放する。"
134                         //-----------------
135                         FDK.Log.Info( "キーボード入力デバイスを解放します。" );
136                         FDK.Utilities.解放する( ref StrokeStyleT.bs_キーボード入力 );
137                         //-----------------
138                         #endregion
139                         #region " WASAPIデバイスを解放する。"
140                         //----------------
141                         FDK.Utilities.解放する( ref StrokeStyleT.bs_Wasapiデバイス );
142                         //----------------
143                         #endregion
144                         #region " MediaFoundation を終了する。"
145                         //-----------------
146                         SharpDX.MediaFoundation.MediaManager.Shutdown();
147                         //-----------------
148                         #endregion
149                 }
150                 public override void シーンを描画する()
151                 {
152                         #region " 描画を準備する。"
153                         //----------------
154                         var d3dDevice = (SharpDX.Direct3D11.Device) null;
155                         using( var d3dLock = new FDK.同期.AutoD3DDeviceLock( this.デバイスリソース.DXGIDeviceManager, out d3dDevice ) )
156                         using( d3dDevice )
157                         using( var d3dContext = d3dDevice.ImmediateContext )
158                         {
159                                 // 既定のD3Dレンダーターゲットビューを黒でクリアする。
160                                 d3dContext.ClearRenderTargetView(
161                                         this.デバイスリソース.D3DRenderTargetView, SharpDX.Color4.Black );
162
163                                 // 深度バッファを 1.0f でクリアする。
164                                 d3dContext.ClearDepthStencilView(
165                                         this.デバイスリソース.D3DDepthStencilView,
166                                         SharpDX.Direct3D11.DepthStencilClearFlags.Depth,
167                                         depth: 1.0f,
168                                         stencil: 0 );
169                         }
170                         //----------------
171                         #endregion
172
173                         // 現在のステージを進行描画する。
174                         this.現在のステージ?.進行描画する( this.デバイスリソース );
175
176                         // 表示する。
177                         this.デバイスリソース.SwapChain.Present(
178                                 (this.垂直帰線待ちを行う) ? 1 : 0,
179                                 SharpDX.DXGI.PresentFlags.None );
180
181                         if( null != this.現在のステージ )
182                         {
183                                 // ステージの状態チェックを行い、必要あれば遷移、またはアプリを終了する。
184                                 switch( this.現在のステージ.GetType().Name )
185                                 {
186                                         case nameof( ステージ.ステージ ):
187                                                 #region " 最初のダミーステージ "
188                                                 //----------------
189                                                 // 起動ステージへ遷移する。
190                                                 this.起動ステージ.活性化する( this.デバイスリソース );
191                                                 this.現在のステージ = this.起動ステージ;
192                                                 //----------------
193                                                 #endregion
194                                                 break;
195
196                                         case nameof( ステージ.起動.起動ステージ ):
197                                                 #region " 終了 → タイトルステージへ。"
198                                                 //---------------
199                                                 if( this.起動ステージ.現在のフェーズ == ステージ.起動.起動ステージ.フェーズ.終了 )
200                                                 {
201                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
202                                                         this.現在のステージ = this.タイトルステージ;
203                                                         this.現在のステージ.活性化する( this.デバイスリソース );
204                                                 }
205                                                 //---------------
206                                                 #endregion
207                                                 break;
208
209                                         case nameof( ステージ.タイトル.タイトルステージ ):
210                                                 #region " 確定 → ログインステージへ。"
211                                                 //---------------
212                                                 if( this.タイトルステージ.現在のフェーズ == ステージ.タイトル.タイトルステージ.フェーズ.確定 )
213                                                 {
214                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
215                                                         this.現在のステージ = this.ログインステージ;
216                                                         this.現在のステージ.活性化する( this.デバイスリソース );
217                                                 }
218                                                 //---------------
219                                                 #endregion
220                                                 #region " キャンセル → アプリを終了する。"
221                                                 //---------------
222                                                 else if( this.タイトルステージ.現在のフェーズ == ステージ.タイトル.タイトルステージ.フェーズ.キャンセル )
223                                                 {
224                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
225                                                         this.現在のステージ = null;
226                                                         this.アプリを終了せよ = true;
227                                                 }
228                                                 //---------------
229                                                 #endregion
230                                                 break;
231
232                                         case nameof( ステージ.ログイン.ログインステージ ):
233                                                 #region " 確定 → ログイン処理を行って、選曲ステージへ。"
234                                                 //---------------
235                                                 if( this.ログインステージ.現在のフェーズ == ステージ.ログイン.ログインステージ.フェーズ.確定 )
236                                                 {
237                                                         // ログイン処理(1) ユーザリストに対してユーザを選択する。
238                                                         StrokeStyleT.ユーザ管理.ユーザを選択する( this.ログインステージ.ユーザインデックス );
239                                                         FDK.Log.Info( $"ユーザが選択されました。[{StrokeStyleT.ユーザ管理.現在選択されているユーザ.名前}]" );
240
241                                                         // ログイン処理(2) 選択ユーザの曲ツリーを構築する。
242                                                         StrokeStyleT.ユーザ管理.現在選択されているユーザ.曲を検索して曲ツリーを構築する();
243
244                                                         // ログイン処理(3) 構築した曲ツリーを曲リスト管理に登録する。
245                                                         StrokeStyleT.曲ツリー管理.現在の管理対象ツリー = StrokeStyleT.ユーザ管理.現在選択されているユーザ.曲ツリーのルートノード;
246
247                                                         // 選曲ステージへ。
248                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
249                                                         this.現在のステージ = this.選曲ステージ;
250                                                         this.現在のステージ.活性化する( this.デバイスリソース );
251                                                 }
252                                                 //---------------
253                                                 #endregion
254                                                 #region " キャンセル → タイトルステージへ。"
255                                                 //---------------
256                                                 else if( this.ログインステージ.現在のフェーズ == ステージ.ログイン.ログインステージ.フェーズ.キャンセル )
257                                                 {
258                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
259                                                         this.現在のステージ = this.タイトルステージ;
260                                                         this.現在のステージ.活性化する( this.デバイスリソース );
261                                                 }
262                                                 //---------------
263                                                 #endregion
264                                                 break;
265
266                                         case nameof( ステージ.選曲.選曲ステージ ):
267                                                 #region " 曲確定 → 曲読込ステージへ。"
268                                                 //---------------
269                                                 if( this.選曲ステージ.現在のフェーズ == ステージ.選曲.選曲ステージ.フェーズ.曲確定 )
270                                                 {
271                                                         // 曲ノードが選択されていることを確認。
272                                                         Trace.Assert( null != StrokeStyleT.曲ツリー管理.現在選択されているノード, "[バグあり] 選択曲が null です。" );
273                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
274                                                         this.現在のステージ = this.曲読込ステージ;
275                                                         this.現在のステージ.活性化する( this.デバイスリソース );
276                                                 }
277                                                 //---------------
278                                                 #endregion
279                                                 #region " キャンセル → アプリを終了する。"
280                                                 //---------------
281                                                 else if( this.選曲ステージ.現在のフェーズ == ステージ.選曲.選曲ステージ.フェーズ.キャンセル )
282                                                 {
283                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
284                                                         this.現在のステージ = null;
285                                                         this.アプリを終了せよ = true;
286                                                 }
287                                                 //---------------
288                                                 #endregion
289                                                 break;
290
291                                         case nameof( ステージ.曲読込.曲読込ステージ ):
292                                                 #region " 終了 → 演奏ステージへ。"
293                                                 //--------------------
294                                                 if( this.曲読込ステージ.現在のフェーズ == ステージ.曲読込.曲読込ステージ.フェーズ.終了 )
295                                                 {
296                                                         // 演奏スコアインスタンスが作成されていることを確認。
297                                                         Debug.Assert( null != StrokeStyleT.演奏スコア );
298
299                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
300                                                         this.現在のステージ = this.演奏ステージ;
301                                                         this.現在のステージ.活性化する( this.デバイスリソース );
302                                                 }
303                                                 //--------------------
304                                                 #endregion
305                                                 break;
306
307                                         case nameof( ステージ.演奏.演奏ステージ ):
308                                                 #region " 演奏終了 → 結果ステージへ。"
309                                                 //--------------------
310                                                 if( this.演奏ステージ.現在のフェーズ == ステージ.演奏.演奏ステージ.フェーズ.クリアor失敗 )
311                                                 {
312                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
313                                                         this.現在のステージ = this.結果ステージ;
314                                                         this.現在のステージ.活性化する( this.デバイスリソース );
315                                                 }
316                                                 //--------------------
317                                                 #endregion
318                                                 #region " キャンセル → 選曲ステージへ。"
319                                                 //--------------------
320                                                 if( this.演奏ステージ.現在のフェーズ == ステージ.演奏.演奏ステージ.フェーズ.キャンセル )
321                                                 {
322                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
323                                                         this.現在のステージ = this.選曲ステージ;
324                                                         this.現在のステージ.活性化する( this.デバイスリソース );
325                                                 }
326                                                 //--------------------
327                                                 #endregion
328                                                 break;
329
330                                         case nameof( ステージ.結果.結果ステージ ):
331                                                 #region " 終了 → 選曲ステージへ。"
332                                                 //--------------------
333                                                 if( this.結果ステージ.現在のフェーズ == ステージ.結果.結果ステージ.フェーズ.終了 )
334                                                 {
335                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
336                                                         this.現在のステージ = this.選曲ステージ;
337                                                         this.現在のステージ.活性化する( this.デバイスリソース );
338                                                 }
339                                                 //--------------------
340                                                 #endregion
341                                                 break;
342                                 }
343                         }
344                 }
345                 protected override void OnClosing( CancelEventArgs e )
346                 {
347                         if( ( null != this.現在のステージ ) && ( this.現在のステージ.活性化している ) )
348                                 this.現在のステージ.非活性化する( this.デバイスリソース );
349
350                         base.OnClosing( e );
351                 }
352                 protected override void OnSizeChanged( EventArgs e )
353                 {
354                         // すべてのサイズ依存リソースを解放する。
355                         this.現在のステージ?.デバイス依存リソースを解放する( this.デバイスリソース );
356
357                         // デバイス等の変更処理を行う。
358                         base.OnSizeChanged( e );
359
360                         // すべてのサイズ依存リソースを作成し直す。
361                         this.現在のステージ?.デバイス依存リソースを作成する( this.デバイスリソース );
362                 }
363
364                 // 各ステージの、唯一のインスタンス。
365                 protected SST.ステージ.ステージ 最初のダミーステージ = new ステージ.ステージ();
366                 protected SST.ステージ.起動.起動ステージ 起動ステージ = new ステージ.起動.起動ステージ();
367                 protected SST.ステージ.タイトル.タイトルステージ タイトルステージ = new ステージ.タイトル.タイトルステージ();
368                 protected SST.ステージ.ログイン.ログインステージ ログインステージ = new ステージ.ログイン.ログインステージ();
369                 protected SST.ステージ.選曲.選曲ステージ 選曲ステージ = new ステージ.選曲.選曲ステージ();
370                 protected SST.ステージ.曲読込.曲読込ステージ 曲読込ステージ = new ステージ.曲読込.曲読込ステージ();
371                 protected SST.ステージ.演奏.演奏ステージ 演奏ステージ = new ステージ.演奏.演奏ステージ();
372                 protected SST.ステージ.結果.結果ステージ 結果ステージ = new ステージ.結果.結果ステージ();
373
374                 private SST.ステージ.ステージ 現在のステージ = null;
375
376                 #region " バックストア。"
377                 //----------------
378                 private static SST.フォルダ bs_フォルダ = null;
379                 private static FDK.入力.Keyboard bs_キーボード入力 = null;
380                 private static FDK.入力.MidiIn bs_MIDI入力 = null;
381                 private static FDK.メディア.サウンド.WASAPI排他.ExclusiveDevice bs_Wasapiデバイス = null;
382                 private static readonly Random bs_乱数 = new Random( DateTime.Now.Millisecond );
383                 private static SST.ユーザ.ユーザ管理 bs_ユーザ管理 = null;
384                 private static SST.曲.曲ツリー管理 bs_曲ツリー管理 = null;
385                 //----------------
386                 #endregion
387
388                 #region " Win32 API "
389                 //-----------------
390                 [System.Runtime.InteropServices.DllImport( "winmm.dll", EntryPoint = "timeBeginPeriod" )]
391                 private static extern uint timeBeginPeriod( uint uMilliseconds );
392
393                 [System.Runtime.InteropServices.DllImport( "winmm.dll", EntryPoint = "timeEndPeriod" )]
394                 private static extern uint timeEndPeriod( uint uMilliseconds );
395                 //-----------------
396                 #endregion
397         }
398 }