OSDN Git Service

ドラムでのカーソル移動の割り当てを、左:Snare, 上:Tom1, 下:Tom2, 右:Tom3 に変更。感覚的に。
[strokestylet/CsWin10Desktop3.git] / StrokeStyleT / 入力 / キーバインディング.cs
index 955a3a3..dd9ca13 100644 (file)
@@ -4,213 +4,217 @@ using System.Diagnostics;
 using System.IO;
 using System.Linq;
 using System.Runtime.Serialization;
-using System.Xml;
 using SharpDX.DirectInput;
+using FDK;
 
 namespace SST.入力
 {
-       [DataContract( Name = "キーバインディング", Namespace = "" )]
+       [DataContract( Name = "KeyBindings", Namespace = "" )]
        class キーバインディング : IExtensibleDataObject
        {
+               /// <summary>
+               ///             入力コードのマッピング用 Dictionary のキーとなる型。
+               /// </summary>
+               /// <remarks>
+               ///             入力は、デバイスID(入力デバイスの内部識別用ID; FDKのIInputEvent.DeviceIDと同じ)と、
+               ///             キー(キーコード、ノート番号などデバイスから得られる入力値)の組で定義される。
+               /// </remarks>
+               [DataContract( Name = "IDとキー", Namespace = "" )]
+               public struct IdKey
+               {
+                       [DataMember]
+                       public int deviceId;
+
+                       [DataMember]
+                       public int key;
+
+                       public IdKey( int deviceId, int key )
+                       {
+                               this.deviceId = deviceId;
+                               this.key = key;
+                       }
+                       public IdKey( FDK.入力.InputEvent ie )
+                       {
+                               this.deviceId = ie.DeviceID;
+                               this.key = ie.Key;
+                       }
+               }
+
+
+               /// <summary>
+               ///             MIDI番号(0~7)とMIDIデバイス名のマッピング用 Dictionary。
+               /// </summary>
                [DataMember]
-               public Dictionary<操作コマンド種別, ドラム様式入力種別[][]> 操作コマンドtoストロークシーケンス { get; protected set; }
+               public Dictionary<int, string> MIDIデバイス番号toデバイス名 { get; protected set; }
 
+               /// <summary>
+               ///             キーボードの入力(DirectInputのKey値)からドラム入力へのマッピング用 Dictionary 。
+               /// </summary>
                [DataMember]
-               public Dictionary<int, ドラム様式入力種別> キーボードto入力 { get; protected set; }
+               public Dictionary<IdKey, ドラム入力種別> キーボードtoドラム { get; protected set; }
 
                /// <summary>
-               ///             Key は、MIDI-ID(0~7) × 1000 + Note 。
+               ///             MIDI入力の入力(MIDIノート番号)からドラム入力へのマッピング用 Dictionary 。
                /// </summary>
                [DataMember]
-               public Dictionary<int, ドラム様式入力種別> MIDIto入力 { get; protected set; }
+               public Dictionary<IdKey, ドラム入力種別> MIDItoドラム { get; protected set; }
+
 
                /// <summary>
                ///             コンストラクタ。
                /// </summary>
                public キーバインディング()
                {
-                       this._初期化する( new StreamingContext() );
-               }
-
-               public int MidiID( int midiKey )
-               {
-                       return ( midiKey / 1000 );
-               }
-               public int MidiNote( int midiKey )
-               {
-                       return ( midiKey % 1000 );
-               }
-               public int MidiKey( int midiID, int midiNote )
-               {
-                       return ( midiID * 1000 + midiNote );
+                       using( Log.Block( FDKUtilities.現在のメソッド名 ) )
+                       {
+                               this._既定値で初期化する( new StreamingContext() );
+                       }
                }
 
                /// <summary>
-               ///             DataContract オブジェクトをシリアル化してXMLファイルに保存する。
+               ///             ファイルに保存する。
                /// </summary>
-               public void 保存する( string XMLファイルパス )
+               public void 保存する()
                {
-                       string xmlPath = フォルダ.絶対パスに含まれるフォルダ変数を展開して返す( XMLファイルパス );
-
-                       try
-                       {
-                               var serializer = new DataContractSerializer( typeof( キーバインディング ) );
-
-                               var settings = new XmlWriterSettings() {
-                                       Encoding = new System.Text.UTF8Encoding( false ),   // UTF-8, BOMなし
-                                       NewLineChars = Environment.NewLine,                 // 改行コード
-                                       NewLineHandling = NewLineHandling.Replace,          // 改行コードを統一(上書き)
-                                       Indent = true,                                      // 改行&インデントを使う(falseにすると改行もされなくなる)
-                               };
-
-                               using( var writer = XmlWriter.Create( xmlPath, settings ) )
-                               {
-                                       serializer.WriteObject( writer, this );
-                               }
-                       }
-                       catch( Exception e )
+                       using( Log.Block( FDKUtilities.現在のメソッド名 ) )
                        {
-                               FDK.Log.ERROR( $"キーバインディングファイルの保存に失敗しました。[{e.Message}][{XMLファイルパス}]" );
+                               var path = SST.IO.Folder.絶対パスに含まれるフォルダ変数を展開して返す( _KeyBindingsファイルパス );
+                               FDKUtilities.保存する( this, path, false ); // キーが struct なので、SimplayDictionary を使うとおかしくなる。
                        }
                }
 
                /// <summary>
-               ///             XMLファイルを逆シリアル化して、DataContract オブジェクトを生成する。
+               ///             ファイルから復元する。
                /// </summary>
-               /// <remarks>
-               ///             DataContract 属性を持つ型の逆シリアル時には、コンストラクタは呼び出されないので注意。
-               ///             各メンバは 0 または null になるが、それがイヤなら OnDeserializing コールバックなどを用意して、個別に初期化すること。
-               /// </remarks>
-               public static キーバインディング 読み込む( string XMLファイルパス )
+               public static キーバインディング 復元する()
                {
-                       var keyBinding = new キーバインディング();
-
-                       string xmlPath = FDK.フォルダ.絶対パスに含まれるフォルダ変数を展開して返す( XMLファイルパス );
-
-                       if( File.Exists( xmlPath ) )
+                       using( Log.Block( FDKUtilities.現在のメソッド名 ) )
                        {
-                               try
-                               {
-                                       var serializer = new DataContractSerializer( typeof( キーバインディング ) );
-
-                                       using( var reader = XmlReader.Create( xmlPath ) )
-                                       {
-                                               keyBinding = (キーバインディング) serializer.ReadObject( reader );
-                                       }
-                               }
-                               catch( Exception e )
-                               {
-                                       FDK.Log.ERROR( $"キーバインディングファイルの読み込みに失敗しました。[{e.Message}][{XMLファイルパス}]" );
-                                       return new キーバインディング();
-                               }
+                               var path = SST.IO.Folder.絶対パスに含まれるフォルダ変数を展開して返す( _KeyBindingsファイルパス );
+                               return FDKUtilities.復元または新規作成する<キーバインディング>( path, false );      // キーが struct なので、SimplayDictionary を使うとおかしくなる。
                        }
-                       else
-                       {
-                               FDK.Log.WARNING( $"キーバインディングファイルが存在しません。作成します。[{XMLファイルパス}]" );
-                               keyBinding.保存する( XMLファイルパス );
-                       }
-
-                       return keyBinding;
                }
 
                /// <summary>
+               ///             キーバインディングを保存するファイルのパス。
+               /// </summary>
+               private static readonly string _KeyBindingsファイルパス = @"$(AppData)\KeyBindings.json";
+
+               /// <summary>
                ///             コンストラクタ時または逆シリアル化時のメンバの既定値を設定する。
                /// </summary>
-               /// <param name="sc"></param>
+               /// <param name="sc">未使用。</param>
+               /// <remarks>
+               ///             .NET 既定の初期値だと支障のある(逆シリアル化対象の)メンバがあれば、ここで初期化しておくこと。
+               /// </remarks>
                [OnDeserializing]
-               private void _初期化する( StreamingContext context )
+               private void _既定値で初期化する( StreamingContext context )
                {
-                       this.操作コマンドtoストロークシーケンス = new Dictionary<操作コマンド種別, ドラム様式入力種別[][]>() {
-
-                               // 決定: シンバル類
-                               { 操作コマンド種別.決定, new [] {
-                                       new [] { ドラム様式入力種別.LeftCrash },
-                                       new [] { ドラム様式入力種別.Ride },
-                                       new [] { ドラム様式入力種別.Ride_Cup },
-                                       new [] { ドラム様式入力種別.China },
-                                       new [] { ドラム様式入力種別.Splash }
-                               } },
-
-                               // キャンセル: Bass×2
-                               { 操作コマンド種別.キャンセル, new [] { new [] { ドラム様式入力種別.Bass, ドラム様式入力種別.Bass } } },
-
-                               // 上下左右: Snare,Tom
-                               { 操作コマンド種別.上, new [] { new [] { ドラム様式入力種別.Tom2 } } },
-                               { 操作コマンド種別.下, new [] { new [] { ドラム様式入力種別.Snare } } },
-                               { 操作コマンド種別.左, new [] { new [] { ドラム様式入力種別.Tom1 } } },
-                               { 操作コマンド種別.右, new [] { new [] { ドラム様式入力種別.Tom3 } } },
-                       };
-
-                       this.キーボードto入力 = new Dictionary<int, ドラム様式入力種別>() {
-                               { (int) Key.Q, ドラム様式入力種別.LeftCrash },
-                               { (int) Key.Return, ドラム様式入力種別.LeftCrash },
-                               { (int) Key.A, ドラム様式入力種別.HiHat_Open },
-                               { (int) Key.Z, ドラム様式入力種別.HiHat_Close },
-                               { (int) Key.X, ドラム様式入力種別.Snare },
-                               { (int) Key.C, ドラム様式入力種別.Bass },
-                               { (int) Key.Space, ドラム様式入力種別.Bass },
-                               { (int) Key.V, ドラム様式入力種別.Tom1 },
-                               { (int) Key.B, ドラム様式入力種別.Tom2 },
-                               { (int) Key.N, ドラム様式入力種別.Tom3 },
-                               { (int) Key.M, ドラム様式入力種別.RightCrash },
-                               { (int) Key.K, ドラム様式入力種別.Ride },
-                       };
-
-                       this.MIDIto入力 = new Dictionary<int, ドラム様式入力種別>() {
-
-                               // とりあえず、DTXMania からベタ移植。
-
-                               { this.MidiKey( 0, 42 ), ドラム様式入力種別.HiHat_Close },
-                               { this.MidiKey( 0, 93 ), ドラム様式入力種別.HiHat_Close },
-                               { this.MidiKey( 0, 46 ), ドラム様式入力種別.HiHat_Open },
-                               { this.MidiKey( 0, 92 ), ドラム様式入力種別.HiHat_Open },
-                               { this.MidiKey( 0, 44 ), ドラム様式入力種別.HiHat_Foot },
-                               { this.MidiKey( 0, 255 ), ドラム様式入力種別.HiHat_Control },  // FDK の MidiIn クラスは、FootControl を ノート 255 として扱う。
-                               { this.MidiKey( 0, 25 ), ドラム様式入力種別.Snare },
-                               { this.MidiKey( 0, 26 ), ドラム様式入力種別.Snare },
-                               { this.MidiKey( 0, 27 ), ドラム様式入力種別.Snare },
-                               { this.MidiKey( 0, 28 ), ドラム様式入力種別.Snare },
-                               { this.MidiKey( 0, 29 ), ドラム様式入力種別.Snare },
-                               { this.MidiKey( 0, 31 ), ドラム様式入力種別.Snare },
-                               { this.MidiKey( 0, 32 ), ドラム様式入力種別.Snare },
-                               { this.MidiKey( 0, 34 ), ドラム様式入力種別.Snare },
-                               { this.MidiKey( 0, 37 ), ドラム様式入力種別.Snare },
-                               { this.MidiKey( 0, 38 ), ドラム様式入力種別.Snare },
-                               { this.MidiKey( 0, 40 ), ドラム様式入力種別.Snare },
-                               { this.MidiKey( 0, 113 ), ドラム様式入力種別.Snare },
-                               { this.MidiKey( 0, 33 ), ドラム様式入力種別.Bass },
-                               { this.MidiKey( 0, 35 ), ドラム様式入力種別.Bass },
-                               { this.MidiKey( 0, 36 ), ドラム様式入力種別.Bass },
-                               { this.MidiKey( 0, 112 ), ドラム様式入力種別.Bass },
-                               { this.MidiKey( 0, 48 ), ドラム様式入力種別.Tom1 },
-                               { this.MidiKey( 0, 50 ), ドラム様式入力種別.Tom1 },
-                               { this.MidiKey( 0, 47 ), ドラム様式入力種別.Tom2 },
-                               { this.MidiKey( 0, 41 ), ドラム様式入力種別.Tom3 },
-                               { this.MidiKey( 0, 43 ), ドラム様式入力種別.Tom3 },
-                               { this.MidiKey( 0, 45 ), ドラム様式入力種別.Tom3 },
-                               { this.MidiKey( 0, 49 ), ドラム様式入力種別.RightCrash },
-                               { this.MidiKey( 0, 52 ), ドラム様式入力種別.RightCrash },
-                               { this.MidiKey( 0, 55 ), ドラム様式入力種別.RightCrash },
-                               { this.MidiKey( 0, 57 ), ドラム様式入力種別.RightCrash },
-                               { this.MidiKey( 0, 91 ), ドラム様式入力種別.RightCrash },
-                               { this.MidiKey( 0, 51 ), ドラム様式入力種別.Ride },
-                               { this.MidiKey( 0, 53 ), ドラム様式入力種別.Ride },
-                               { this.MidiKey( 0, 59 ), ドラム様式入力種別.Ride },
-                               { this.MidiKey( 0, 89 ), ドラム様式入力種別.Ride },
-                       };
+                       using( Log.Block( FDKUtilities.現在のメソッド名 ) )
+                       {
+                               this.MIDIデバイス番号toデバイス名 = new Dictionary<int, string>();
+
+                               this.キーボードtoドラム = new Dictionary<IdKey, ドラム入力種別>() {
+                                       { new IdKey( 0, (int) Key.Q ), ドラム入力種別.LeftCrash },
+                                       { new IdKey( 0, (int) Key.Return ), ドラム入力種別.LeftCrash },
+                                       { new IdKey( 0, (int) Key.A ), ドラム入力種別.HiHat_Open },
+                                       { new IdKey( 0, (int) Key.Z ), ドラム入力種別.HiHat_Close },
+                                       { new IdKey( 0, (int) Key.X ), ドラム入力種別.Snare },
+                                       { new IdKey( 0, (int) Key.C ), ドラム入力種別.Bass },
+                                       { new IdKey( 0, (int) Key.Space ), ドラム入力種別.Bass },
+                                       { new IdKey( 0, (int) Key.V ), ドラム入力種別.Tom1 },
+                                       { new IdKey( 0, (int) Key.B ), ドラム入力種別.Tom2 },
+                                       { new IdKey( 0, (int) Key.N ), ドラム入力種別.Tom3 },
+                                       { new IdKey( 0, (int) Key.M ), ドラム入力種別.RightCrash },
+                                       { new IdKey( 0, (int) Key.K ), ドラム入力種別.Ride },
+                               };
+
+                               this.MIDItoドラム = new Dictionary<IdKey, ドラム入力種別>() {
+                                       // うちの環境(2017.6.11)
+                                       { new IdKey( 1, 36 ), ドラム入力種別.Bass },
+                                       { new IdKey( 1, 30 ), ドラム入力種別.RightCrash },
+                                       { new IdKey( 1, 29 ), ドラム入力種別.RightCrash },
+                                       { new IdKey( 0, 51 ), ドラム入力種別.RightCrash },
+                                       { new IdKey( 0, 52 ), ドラム入力種別.RightCrash },
+                                       { new IdKey( 0, 57 ), ドラム入力種別.RightCrash },
+                                       { new IdKey( 1, 52 ), ドラム入力種別.RightCrash },
+                                       { new IdKey( 1, 43 ), ドラム入力種別.Tom3 },
+                                       { new IdKey( 1, 58 ), ドラム入力種別.Tom3 },
+                                       { new IdKey( 1, 42 ), ドラム入力種別.HiHat_Close },
+                                       { new IdKey( 1, 22 ), ドラム入力種別.HiHat_Close },
+                                       { new IdKey( 1, 26 ), ドラム入力種別.HiHat_Open },
+                                       { new IdKey( 1, 46 ), ドラム入力種別.HiHat_Open },
+                                       { new IdKey( 1, 255 ), ドラム入力種別.HiHat_Control },   // FDK の MidiIn クラスは、FootControl を ノート 255 として扱う。
+                                       { new IdKey( 1, 48 ), ドラム入力種別.Tom1 },
+                                       { new IdKey( 1, 50 ), ドラム入力種別.Tom1 },
+                                       { new IdKey( 1, 49 ), ドラム入力種別.LeftCrash },
+                                       { new IdKey( 1, 55 ), ドラム入力種別.LeftCrash },
+                                       { new IdKey( 0, 48 ), ドラム入力種別.LeftCrash },
+                                       { new IdKey( 0, 49 ), ドラム入力種別.LeftCrash },
+                                       { new IdKey( 0, 59 ), ドラム入力種別.LeftCrash },
+                                       { new IdKey( 1, 45 ), ドラム入力種別.Tom2 },
+                                       { new IdKey( 1, 47 ), ドラム入力種別.Tom2 },
+                                       { new IdKey( 1, 51 ), ドラム入力種別.Ride },
+                                       { new IdKey( 1, 59 ), ドラム入力種別.Ride },
+                                       { new IdKey( 1, 38 ), ドラム入力種別.Snare },
+                                       { new IdKey( 1, 40 ), ドラム入力種別.Snare },
+                                       { new IdKey( 1, 37 ), ドラム入力種別.Snare },
+
+                                       // とりあえず、DTXMania からベタ移植。
+                                       //{ new IdKey( 0, 42 ), ドラム入力種別.HiHat_Close },
+                                       //{ new IdKey( 0, 93 ), ドラム入力種別.HiHat_Close },
+                                       //{ new IdKey( 0, 46 ), ドラム入力種別.HiHat_Open },
+                                       //{ new IdKey( 0, 92 ), ドラム入力種別.HiHat_Open },
+                                       //{ new IdKey( 0, 255 ), ドラム入力種別.HiHat_Control }, // FDK の MidiIn クラスは、FootControl を ノート 255 として扱う。
+                                       //{ new IdKey( 0, 25 ), ドラム入力種別.Snare },
+                                       //{ new IdKey( 0, 26 ), ドラム入力種別.Snare },
+                                       //{ new IdKey( 0, 27 ), ドラム入力種別.Snare },
+                                       //{ new IdKey( 0, 28 ), ドラム入力種別.Snare },
+                                       //{ new IdKey( 0, 29 ), ドラム入力種別.Snare },
+                                       //{ new IdKey( 0, 31 ), ドラム入力種別.Snare },
+                                       //{ new IdKey( 0, 32 ), ドラム入力種別.Snare },
+                                       //{ new IdKey( 0, 34 ), ドラム入力種別.Snare },
+                                       //{ new IdKey( 0, 37 ), ドラム入力種別.Snare },
+                                       //{ new IdKey( 0, 38 ), ドラム入力種別.Snare },
+                                       //{ new IdKey( 0, 40 ), ドラム入力種別.Snare },
+                                       //{ new IdKey( 0, 113 ), ドラム入力種別.Snare },
+                                       //{ new IdKey( 0, 33 ), ドラム入力種別.Bass },
+                                       //{ new IdKey( 0, 35 ), ドラム入力種別.Bass },
+                                       //{ new IdKey( 0, 36 ), ドラム入力種別.Bass },
+                                       //{ new IdKey( 0, 112 ), ドラム入力種別.Bass },
+                                       //{ new IdKey( 0, 48 ), ドラム入力種別.Tom1 },
+                                       //{ new IdKey( 0, 50 ), ドラム入力種別.Tom1 },
+                                       //{ new IdKey( 0, 47 ), ドラム入力種別.Tom2 },
+                                       //{ new IdKey( 0, 41 ), ドラム入力種別.Tom3 },
+                                       //{ new IdKey( 0, 43 ), ドラム入力種別.Tom3 },
+                                       //{ new IdKey( 0, 45 ), ドラム入力種別.Tom3 },
+                                       //{ new IdKey( 0, 49 ), ドラム入力種別.RightCrash },
+                                       //{ new IdKey( 0, 52 ), ドラム入力種別.RightCrash },
+                                       //{ new IdKey( 0, 55 ), ドラム入力種別.RightCrash },
+                                       //{ new IdKey( 0, 57 ), ドラム入力種別.RightCrash },
+                                       //{ new IdKey( 0, 91 ), ドラム入力種別.RightCrash },
+                                       //{ new IdKey( 0, 51 ), ドラム入力種別.Ride },
+                                       //{ new IdKey( 0, 53 ), ドラム入力種別.Ride },
+                                       //{ new IdKey( 0, 59 ), ドラム入力種別.Ride },
+                                       //{ new IdKey( 0, 89 ), ドラム入力種別.Ride },
+                               };
+                       }
                }
 
+
                #region " IExtensibleDataObject の実装 "
                //----------------
                private ExtensionDataObject _ExData;
 
                public virtual ExtensionDataObject ExtensionData
                {
-                       get { return _ExData; }
-                       set { _ExData = value; }
+                       get
+                               => this._ExData;
+
+                       set
+                               => this._ExData = value;
                }
                //----------------
                #endregion
        }
 }
-