OSDN Git Service

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