Browse Source

Fixed not working overlay modifiers due to transients

Krzysztof Krysiński 8 months ago
parent
commit
08f19f92de

+ 1 - 0
src/PixiEditor/Models/Commands/Attributes/Commands/ToolAttribute.cs

@@ -8,6 +8,7 @@ internal partial class Command
     internal class ToolAttribute : CommandAttribute
     {
         public Key Transient { get; set; }
+        public bool TransientImmediate { get; set; } = false;
 
         public ToolAttribute() : base(null, null, null)
         {

+ 1 - 0
src/PixiEditor/Models/Commands/CommandController.cs

@@ -214,6 +214,7 @@ internal class CommandController
                 Icon = toolInstance.DefaultIcon,
                 IconEvaluator = IconEvaluator.Default,
                 TransientKey = toolAttr.Transient,
+                TransientImmediate = toolAttr.TransientImmediate,
                 DefaultShortcut = toolAttr.GetShortcut(),
                 Shortcut = GetShortcut(internalName, toolAttr.GetShortcut(), template),
                 ToolType = type,

+ 1 - 0
src/PixiEditor/Models/Commands/Commands/ToolCommand.cs

@@ -10,6 +10,7 @@ internal partial class Command
         public Type ToolType { get; init; }
 
         public Key TransientKey { get; init; }
+        public bool TransientImmediate { get; init; } = false;
 
         public override object GetParameter() => ToolType;
     }

+ 32 - 8
src/PixiEditor/ViewModels/SubViewModels/IoViewModel.cs

@@ -32,6 +32,8 @@ internal class IoViewModel : SubViewModel<ViewModelMain>
     private bool? drawingWithRight;
     private bool startedWithEraser;
 
+    private Key? queuedTransientKey;
+
     public RelayCommand<MouseOnCanvasEventArgs> MouseMoveCommand { get; set; }
     public RelayCommand<MouseOnCanvasEventArgs> MouseDownCommand { get; set; }
     public RelayCommand PreviewMouseMiddleButtonCommand { get; set; }
@@ -125,19 +127,28 @@ internal class IoViewModel : SubViewModel<ViewModelMain>
         Owner.DocumentManagerSubViewModel.ActiveDocument?.EventInlet.OnKeyDown(args.Key);
     }
 
-    private void HandleTransientKey(Key transientKey)
+    private bool HandleTransientKey(Key transientKey, bool executeOnlyImmediate)
     {
         if (ShortcutController.ShortcutExecutionBlocked)
         {
-            return;
+            return false;
         }
 
         var tool = GetTransientTool(transientKey);
 
-        if (tool is not null)
+        if (tool is null)
+        {
+            return false;
+        }
+
+        if (!tool.TransientImmediate && executeOnlyImmediate)
         {
-            Owner.ToolsSubViewModel.SetActiveTool(tool.ToolType, true);
+            return false;
         }
+
+        Owner.ToolsSubViewModel.SetActiveTool(tool.ToolType, true);
+
+        return true;
     }
 
     private static Command.ToolCommand? GetTransientTool(Key transientKey)
@@ -152,11 +163,18 @@ internal class IoViewModel : SubViewModel<ViewModelMain>
     {
         if (argsModifiers == KeyModifiers.None && !isRepeat)
         {
-            HandleTransientKey(key);
+            if (!HandleTransientKey(key, true))
+            {
+                queuedTransientKey = key;
+            }
+        }
+        else
+        {
+            queuedTransientKey = null;
         }
 
         if (isRepeat && Owner.ShortcutController.LastCommands != null &&
-            Owner.ShortcutController.LastCommands.Any(x => x is Command.ToolCommand))
+            Owner.ShortcutController.LastCommands.Any(x => x is Command.ToolCommand cmd && cmd.Shortcut == new KeyCombination(key, argsModifiers)))
         {
             Owner.ToolsSubViewModel.HandleToolRepeatShortcutDown();
         }
@@ -186,6 +204,12 @@ internal class IoViewModel : SubViewModel<ViewModelMain>
 
     private void OnMouseDown(object? sender, MouseOnCanvasEventArgs args)
     {
+        if (args.Button == MouseButton.Left && queuedTransientKey != null)
+        {
+            HandleTransientKey(queuedTransientKey.Value, false);
+            queuedTransientKey = null;
+        }
+
         if (drawingWithRight != null || args.Button is not (MouseButton.Left or MouseButton.Right))
             return;
 
@@ -199,8 +223,8 @@ internal class IoViewModel : SubViewModel<ViewModelMain>
 
         drawingWithRight = args.Button == MouseButton.Right;
         activeDocument.EventInlet.OnCanvasLeftMouseButtonDown(args);
-        if(args.Handled) return;
-        
+        if (args.Handled) return;
+
         Owner.ToolsSubViewModel.UseToolEventInlet(args.PositionOnCanvas, args.Button);
 
         if (args.Button == MouseButton.Right)

+ 1 - 1
src/PixiEditor/ViewModels/Tools/Tools/MoveToolViewModel.cs

@@ -13,7 +13,7 @@ using PixiEditor.Views.Overlays.BrushShapeOverlay;
 
 namespace PixiEditor.ViewModels.Tools.Tools;
 
-[Command.Tool(Key = Key.V, Transient = Key.Space)]
+[Command.Tool(Key = Key.V)]
 internal class MoveToolViewModel : ToolViewModel, IMoveToolHandler
 {
     private string defaultActionDisplay = "MOVE_TOOL_ACTION_DISPLAY";

+ 1 - 1
src/PixiEditor/ViewModels/Tools/Tools/MoveViewportToolViewModel.cs

@@ -6,7 +6,7 @@ using PixiEditor.Views.Overlays.BrushShapeOverlay;
 
 namespace PixiEditor.ViewModels.Tools.Tools;
 
-[Command.Tool(Key = Key.H, Transient = Key.Space)]
+[Command.Tool(Key = Key.H, Transient = Key.Space, TransientImmediate = true)]
 internal class MoveViewportToolViewModel : ToolViewModel
 {
     public override string ToolNameLocalizationKey => "MOVE_VIEWPORT_TOOL";