Browse Source

More Aseprite mappings

Krzysztof Krysiński 2 years ago
parent
commit
bf42031a64

+ 15 - 35
src/PixiEditor/Data/ShortcutActionMaps/AsepriteShortcutMap.json

@@ -583,15 +583,15 @@
       },
       "Parameters": []
     },
-    "": {
+    "Options": {
       "Command": "PixiEditor.Window.OpenSettingsWindow",
       "DefaultShortcut": {
-        "key": "None",
-        "modifiers": null
+        "key": "K",
+        "modifiers": ["Ctrl"]
       },
       "Parameters": []
     },
-    "": {
+    "Home": {
       "Command": "PixiEditor.Window.OpenStartupWindow",
       "DefaultShortcut": {
         "key": "None",
@@ -599,18 +599,22 @@
       },
       "Parameters": []
     },
-    "": {
+    "KeyboardShortcuts": {
       "Command": "PixiEditor.Window.OpenShortcutWindow",
       "DefaultShortcut": {
-        "key": "None",
-        "modifiers": null
+        "key": "F7",
+        "modifiers": [
+            "Ctrl",
+            "Alt",
+            "Shift"
+        ]
       },
       "Parameters": []
     },
-    "": {
+    "TogglePreview": {
       "Command": "PixiEditor.Window.OpenNavigationWindow",
       "DefaultShortcut": {
-        "key": "None",
+        "key": "F7",
         "modifiers": null
       },
       "Parameters": []
@@ -623,7 +627,7 @@
       },
       "Parameters": []
     },
-    "": {
+    "SymmetryMode.orientation=vertical": {
       "Command": "PixiEditor.Document.ToggleVerticalSymmetryAxis",
       "DefaultShortcut": {
         "key": "None",
@@ -631,7 +635,7 @@
       },
       "Parameters": []
     },
-    "": {
+    "SymmetryMode.orientation=horizontal": {
       "Command": "PixiEditor.Document.ToggleHorizontalSymmetryAxis",
       "DefaultShortcut": {
         "key": "None",
@@ -639,30 +643,6 @@
       },
       "Parameters": []
     },
-    "": {
-      "Command": "PixiEditor.Document.DragSymmetry",
-      "DefaultShortcut": {
-        "key": "None",
-        "modifiers": null
-      },
-      "Parameters": []
-    },
-    "": {
-      "Command": "PixiEditor.Document.StartDragSymmetry",
-      "DefaultShortcut": {
-        "key": "None",
-        "modifiers": null
-      },
-      "Parameters": []
-    },
-    "": {
-      "Command": "PixiEditor.Document.EndDragSymmetry",
-      "DefaultShortcut": {
-        "key": "None",
-        "modifiers": null
-      },
-      "Parameters": []
-    },
     "Clear": {
       "Command": "PixiEditor.Document.DeletePixels",
       "DefaultShortcut": {

+ 75 - 1
src/PixiEditor/Models/Commands/Templates/Providers/Parsers/AsepriteKeysParser.cs

@@ -1,4 +1,5 @@
 using System.IO;
+using System.Text;
 using System.Xml;
 
 namespace PixiEditor.Models.Commands.Templates.Parsers;
@@ -47,12 +48,85 @@ public class AsepriteKeysParser : KeysParser
         
         List<KeyDefinition> keyDefinitions = new List<KeyDefinition>(); // DefaultShortcut is actually mapped shortcut.
 
+        LoadCommands(doc, keyDefinitions);
         LoadTools(doc, keyDefinitions);
         
         ShortcutsTemplate template = ShortcutsTemplate.FromKeyDefinitions(keyDefinitions);
         return template;
     }
 
+    private void LoadCommands(XmlDocument document, List<KeyDefinition> keyDefinitions)
+    {
+        XmlNodeList commands = document.SelectNodes("keyboard/commands/key");
+        if(commands == null)
+        {
+            ApplyDefaults(keyDefinitions, "PixiEditor");
+            return;
+        }
+        
+        foreach (XmlNode commandNode in commands)
+        {
+            if(commandNode.Attributes == null) continue;
+            
+            XmlAttribute command = commandNode.Attributes["command"];
+            XmlAttribute shortcut = commandNode.Attributes["shortcut"];
+            XmlNodeList paramNodes = commandNode.SelectNodes("param");
+
+            if(command == null || shortcut == null) continue;
+
+            string commandName = $"{command.Value}{GetParamString(paramNodes)}";
+            string shortcutValue = shortcut.Value;
+            
+            if (!Map.ContainsKey(commandName))
+            {
+                continue;
+            }
+
+            var mappedEntry = Map[commandName];
+            commandName = mappedEntry.Command;
+            
+            HumanReadableKeyCombination combination;
+            
+            XmlAttribute removed = commandNode.Attributes["removed"];
+            if (removed is { Value: "true" })
+            {
+                combination = new HumanReadableKeyCombination("None");
+            }
+            else
+            {
+                combination = HumanReadableKeyCombination.FromStringCombination(shortcutValue);
+            }
+
+            // We should override existing entry, because aseprite-keys file can contain multiple entries for the same command.
+            // Last one is the one that should be used.
+            keyDefinitions.RemoveAll(x => x.Command == commandName);
+            
+            keyDefinitions.Add(new KeyDefinition(commandName, combination));
+        }
+    }
+
+    private string GetParamString(XmlNodeList paramNodes)
+    {
+        if(paramNodes == null || paramNodes.Count == 0) return string.Empty;
+        
+        StringBuilder builder = new StringBuilder();
+        foreach (XmlNode paramNode in paramNodes)
+        {
+            if(paramNode.Attributes == null) continue;
+            
+            XmlAttribute paramName = paramNode.Attributes["name"];
+            XmlAttribute paramValue = paramNode.Attributes["value"];
+            if(paramName == null || paramValue == null) continue;
+
+            builder.Append('.');
+            builder.Append(paramName.Value);
+            builder.Append('=');
+            builder.Append(paramValue.Value);
+        }
+
+        return builder.ToString();
+    }
+
     /// <summary>
     ///     Tools are stored in keyboard > tools section of aseprite-keys file.
     /// Each tool entry is a key XML node with command attribute under 'tool' parameter and shortcut under 'shortcut' parameter.
@@ -75,7 +149,7 @@ public class AsepriteKeysParser : KeysParser
             XmlAttribute shortcut = tool.Attributes["shortcut"];
 
             if(command == null || shortcut == null) continue;
-            
+
             string commandName = command.Value;
             string shortcutValue = shortcut.Value;
 

+ 2 - 24
src/PixiEditor/Models/Commands/Templates/Providers/Parsers/KeyDefinition.cs

@@ -1,5 +1,4 @@
 using System.Windows.Input;
-using Newtonsoft.Json;
 using PixiEditor.Models.DataHolders;
 
 namespace PixiEditor.Models.Commands.Templates.Parsers;
@@ -26,7 +25,8 @@ public record HumanReadableKeyCombination(string key, string[] modifiers = null)
     {
         Key parsedKey = Key.None;
         ModifierKeys parsedModifiers = ModifierKeys.None;
-        if (TryParseSpecial(key, out parsedKey) || Enum.TryParse(key, true, out parsedKey))
+
+        if (KeyParser.TryParseSpecial(key, out parsedKey) || Enum.TryParse(key, true, out parsedKey))
         {
             parsedModifiers = ParseModifiers(modifiers);
         }
@@ -38,28 +38,6 @@ public record HumanReadableKeyCombination(string key, string[] modifiers = null)
         return new KeyCombination(parsedKey, parsedModifiers);
     }
 
-    private bool TryParseSpecial(string key, out Key parsed)
-    {
-        switch (key.ToLower())
-        {
-            case "shift":
-                parsed = Key.LeftShift;
-                return true;
-            case "ctrl":
-                parsed = Key.LeftCtrl;
-                return true;
-            case "alt":
-                parsed = Key.LeftAlt;
-                return true;
-            case "win":
-                parsed = Key.LWin;
-                return true;
-            default:
-                parsed = Key.None;
-                return false;
-        }
-    }
-
     private ModifierKeys ParseModifiers(string[] strings)
     {
         if(strings == null || strings.Length == 0)

+ 109 - 0
src/PixiEditor/Models/Commands/Templates/Providers/Parsers/KeyParser.cs

@@ -0,0 +1,109 @@
+using System.Windows.Input;
+
+namespace PixiEditor.Models.Commands.Templates.Parsers;
+
+public static class KeyParser
+{
+    public static bool TryParseSpecial(string key, out Key parsed)
+    {
+        switch (key.ToLower())
+        {
+            case "shift":
+                parsed = Key.LeftShift;
+                return true;
+            case "ctrl":
+                parsed = Key.LeftCtrl;
+                return true;
+            case "alt":
+                parsed = Key.LeftAlt;
+                return true;
+            case "win":
+                parsed = Key.LWin;
+                return true;
+            case ".":
+                parsed = Key.OemPeriod;
+                return true;
+            case ",":
+                parsed = Key.OemComma;
+                return true;
+            case "+":
+                parsed = Key.OemPlus;
+                return true;
+            case "-":
+                parsed = Key.OemMinus;
+                return true;
+            case "/":
+                parsed = Key.OemQuestion;
+                return true;
+            case "*":
+                parsed = Key.Multiply;
+                return true;
+            case "\\":
+                parsed = Key.Oem5;
+                return true;
+            case "'":
+                parsed = Key.OemQuotes;
+                return true;
+            case "\"":
+                parsed = Key.Oem7;
+                return true;
+            case ";":
+                parsed = Key.OemSemicolon;
+                return true;
+            case ":":
+                parsed = Key.Oem1;
+                return true;
+            case "<":
+                parsed = Key.Oem102;
+                return true;
+            case ">":
+                parsed = Key.Oem102;
+                return true;
+            case "~":
+                parsed = Key.Oem3;
+                return true;
+            case "!":
+                parsed = Key.D1;
+                return true;
+            case "@":
+                parsed = Key.D2;
+                return true;
+            case "#":
+                parsed = Key.D3;
+                return true;
+            case "$":
+                parsed = Key.D4;
+                return true;
+            case "%":
+                parsed = Key.D5;
+                return true;
+            case "^":
+                parsed = Key.D6;
+                return true;
+            case "&":
+                parsed = Key.D7;
+                return true;
+            case "[" or "{":
+                parsed = Key.OemOpenBrackets;
+                return true;
+            case "]" or "}":
+                parsed = Key.OemCloseBrackets;
+                return true;
+            case "_":
+                parsed = Key.OemMinus;
+                return true;
+            case "=":
+                parsed = Key.OemPlus;
+                return true;
+            case "(":
+                parsed = Key.D9;
+                return true;
+            case ")":
+                parsed = Key.D0;
+                return true;
+            default:
+                parsed = Key.None;
+                return false;
+        }
+    }
+}