Browse Source

Implement keyboard control for select tool modes (normal/add/subtract) (Resolve #445)

Equbuxu 2 years ago
parent
commit
4ba1b1958a

+ 1 - 1
src/PixiEditor/Models/DocumentModels/UpdateableChangeExecutors/LassoToolExecutor.cs

@@ -12,7 +12,7 @@ internal sealed class LassoToolExecutor : UpdateableChangeExecutor
     
     public override ExecutionState Start()
     {
-        mode = ViewModelMain.Current?.ToolsSubViewModel.GetTool<LassoToolViewModel>()?.SelectMode;
+        mode = ViewModelMain.Current?.ToolsSubViewModel.GetTool<LassoToolViewModel>()?.ResultingSelectionMode;
 
         if (mode is null)
             return ExecutionState.Error;

+ 1 - 1
src/PixiEditor/Models/DocumentModels/UpdateableChangeExecutors/SelectToolExecutor.cs

@@ -31,7 +31,7 @@ internal class SelectToolExecutor : UpdateableChangeExecutor
         
         startPos = controller!.LastPixelPosition;
         selectShape = toolViewModel.SelectShape;
-        selectMode = toolViewModel.SelectMode;
+        selectMode = toolViewModel.ResultingSelectionMode;
 
         IAction action = CreateUpdateAction(selectShape, new RectI(startPos, new(0)), selectMode);
         internals!.ActionAccumulator.AddActions(action);

+ 25 - 1
src/PixiEditor/ViewModels/SubViewModels/Tools/Tools/LassoToolViewModel.cs

@@ -10,10 +10,34 @@ namespace PixiEditor.ViewModels.SubViewModels.Tools.Tools;
 [Command.ToolAttribute(Key = Key.Q)]
 internal class LassoToolViewModel : ToolViewModel
 {
+    private string defaultActionDisplay = "Click and move to select pixels inside of the lasso. Hold Shift to add to existing selection. Hold Ctrl to subtract from it.";
+
     public LassoToolViewModel()
     {
         Toolbar = ToolbarFactory.Create<LassoToolViewModel>();
-        ActionDisplay = "Click and move to select pixels inside of lasso.";
+        ActionDisplay = defaultActionDisplay;
+    }
+
+    private SelectionMode modifierKeySelectionMode = SelectionMode.New;
+    public SelectionMode ResultingSelectionMode => modifierKeySelectionMode != SelectionMode.New ? modifierKeySelectionMode : SelectMode;
+
+    public override void UpdateActionDisplay(bool ctrlIsDown, bool shiftIsDown, bool altIsDown)
+    {
+        if (shiftIsDown)
+        {
+            ActionDisplay = "Click and move to add pixels inside of the lasso to the selection.";
+            modifierKeySelectionMode = SelectionMode.Add;
+        }
+        else if (ctrlIsDown)
+        {
+            ActionDisplay = "Click and move to subtract pixels inside of the lasso from the selection.";
+            modifierKeySelectionMode = SelectionMode.Subtract;
+        }
+        else
+        {
+            ActionDisplay = defaultActionDisplay;
+            modifierKeySelectionMode = SelectionMode.New;
+        }
     }
 
     public override string Tooltip => $"Lasso. ({Shortcut})";

+ 25 - 1
src/PixiEditor/ViewModels/SubViewModels/Tools/Tools/SelectToolViewModel.cs

@@ -12,13 +12,37 @@ namespace PixiEditor.ViewModels.SubViewModels.Tools.Tools;
 [Command.Tool(Key = Key.M)]
 internal class SelectToolViewModel : ToolViewModel
 {
+    private string defaultActionDisplay = "Click and move to select an area. Hold Shift to add to existing selection. Hold Ctrl to subtract from it.";
+
     public SelectToolViewModel()
     {
-        ActionDisplay = "Click and move to select an area.";
+        ActionDisplay = defaultActionDisplay;
         Toolbar = ToolbarFactory.Create<SelectToolViewModel>();
         Cursor = Cursors.Cross;
     }
 
+    private SelectionMode modifierKeySelectionMode = SelectionMode.New;
+    public SelectionMode ResultingSelectionMode => modifierKeySelectionMode != SelectionMode.New ? modifierKeySelectionMode : SelectMode;
+
+    public override void UpdateActionDisplay(bool ctrlIsDown, bool shiftIsDown, bool altIsDown)
+    {
+        if (shiftIsDown)
+        {
+            ActionDisplay = "Click and move to add to the current selection.";
+            modifierKeySelectionMode = SelectionMode.Add;
+        }
+        else if (ctrlIsDown)
+        {
+            ActionDisplay = "Click and move to subtract from the current selection.";
+            modifierKeySelectionMode = SelectionMode.Subtract;
+        }
+        else
+        {
+            ActionDisplay = defaultActionDisplay;
+            modifierKeySelectionMode = SelectionMode.New;
+        }
+    }
+
     [Settings.Enum("Mode")]
     public SelectionMode SelectMode => GetValue<SelectionMode>();