Browse Source

Now shortcuts work

Frytek 5 years ago
parent
commit
0bee3b5728

+ 28 - 0
PixiEditor/Models/Controllers/Shortcuts/Shortcut.cs

@@ -0,0 +1,28 @@
+using System.Windows.Input;
+
+namespace PixiEditor.Models.Controllers
+{
+    public class Shortcut
+    {
+        public Key ShortcutKey { get; set; }
+        public ModifierKeys Modifier { get; set; }
+        public ICommand Command { get; set; }
+        public object CommandParameter { get; set; }
+
+        public Shortcut(Key shortcutKey, ICommand command, object commandParameter = null, ModifierKeys modifier = ModifierKeys.None)
+        {
+            ShortcutKey = shortcutKey;
+            Modifier = modifier;
+            Command = command;
+            CommandParameter = commandParameter;
+        }
+
+        public void Execute()
+        {
+            if(Command.CanExecute(CommandParameter))
+            {
+                Command.Execute(CommandParameter);
+            }
+        }
+    }
+}

+ 33 - 0
PixiEditor/Models/Controllers/Shortcuts/ShortcutController.cs

@@ -0,0 +1,33 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Windows.Input;
+
+namespace PixiEditor.Models.Controllers
+{
+    public class ShortcutController
+    {
+        public static bool BlockShortcutExecution { get; set; }
+
+        public List<Shortcut> Shortcuts { get; set; }       
+
+        public ShortcutController()
+        {
+            Shortcuts = new List<Shortcut>();
+            
+        }
+
+        public void KeyPressed(Key key)
+        {
+            if (!BlockShortcutExecution)
+            {
+                Shortcut shortcut = Shortcuts.Find(x => x.ShortcutKey == key);
+                if (shortcut == null) return;
+                if (Keyboard.Modifiers.HasFlag(shortcut.Modifier))
+                {
+                    shortcut.Execute();
+                }
+            }
+        }
+    }
+}

+ 1 - 0
PixiEditor/PixiEditor.csproj

@@ -17,6 +17,7 @@
     <PackageReference Include="Extended.Wpf.Toolkit">
       <Version>3.8.1</Version>
     </PackageReference>
+    <PackageReference Include="MvvmLightLibs" Version="5.4.1.1" />
     <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
     <PackageReference Include="System.Drawing.Common" Version="4.7.0" />
     <PackageReference Include="WriteableBitmapEx">

+ 36 - 1
PixiEditor/ViewModels/ViewModelMain.cs

@@ -23,6 +23,7 @@ namespace PixiEditor.ViewModels
         public RelayCommand GenerateDrawAreaCommand { get; set; } //Command that generates draw area
         public RelayCommand MouseMoveCommand { get; set; } //Command that is used to draw
         public RelayCommand MouseDownCommand { get; set; }
+        public RelayCommand KeyDownCommand { get; set; }
         public RelayCommand SaveFileCommand { get; set; } //Command that is used to save file
         public RelayCommand UndoCommand { get; set; }
         public RelayCommand RedoCommand { get; set; }
@@ -35,6 +36,7 @@ namespace PixiEditor.ViewModels
         public RelayCommand DeleteLayerCommand { get; set; }
         public RelayCommand MoveToBackCommand { get; set; }
         public RelayCommand MoveToFrontCommand { get; set; }
+        public RelayCommand SwapColorsCommand { get; set; }
 
         private double _mouseXonCanvas;
 
@@ -126,6 +128,8 @@ namespace PixiEditor.ViewModels
         public BitmapOperationsUtility BitmapUtility { get; set; }
         public PixelChangesController ChangesController { get; set; }
 
+        public ShortcutController ShortcutController { get; set; }
+
         public ViewModelMain()
         {
             PixiFilesManager.InitializeTempDirectories();
@@ -148,14 +152,38 @@ namespace PixiEditor.ViewModels
             DeleteLayerCommand = new RelayCommand(DeleteLayer, CanDeleteLayer);
             MoveToBackCommand = new RelayCommand(MoveLayerToBack, CanMoveToBack);
             MoveToFrontCommand = new RelayCommand(MoveLayerToFront, CanMoveToFront);
+            SwapColorsCommand = new RelayCommand(SwapColors);
+            KeyDownCommand = new RelayCommand(KeyDown);
             ToolSet = new List<Tool> { new PixiTools.PenTool(), new PixiTools.FloodFill(), new PixiTools.LineTool(),
-            new PixiTools.CircleTool(), new PixiTools.RectangleTool(), new PixiTools.EarserTool(), new PixiTools.BrightnessTool() };       
+            new PixiTools.CircleTool(), new PixiTools.RectangleTool(), new PixiTools.EarserTool(), new PixiTools.BrightnessTool() };
+            ShortcutController = new ShortcutController
+            {
+                Shortcuts = new List<Shortcut> { 
+                    new Shortcut(Key.B, SelectToolCommand, ToolType.Pen),
+                    new Shortcut(Key.X, SwapColorsCommand),
+                    new Shortcut(Key.O, OpenFileCommand, ModifierKeys.Control),
+                    new Shortcut(Key.E, SelectToolCommand, ToolType.Earser),
+                    new Shortcut(Key.O, SelectToolCommand, ToolType.ColorPicker),
+                    new Shortcut(Key.C, SelectToolCommand, ToolType.Rectangle),
+                    new Shortcut(Key.L, SelectToolCommand, ToolType.Line),
+                    new Shortcut(Key.G, SelectToolCommand, ToolType.Bucket),
+                    new Shortcut(Key.Y, RedoCommand, ModifierKeys.Control),
+                    new Shortcut(Key.Z, UndoCommand),
+                    new Shortcut(Key.S, UndoCommand, ModifierKeys.Control),
+                    new Shortcut(Key.N, GenerateDrawAreaCommand, ModifierKeys.Control),
+                }
+            };
             UndoManager.SetMainRoot(this);
             SetActiveTool(ToolType.Pen);
             BitmapUtility.PrimaryColor = PrimaryColor;
             ToolSize = 1;
         }
 
+        public void KeyDown(object parameter)
+        {
+            ShortcutController.KeyPressed(((KeyEventArgs)parameter).Key);
+        }
+
         private void MouseController_StoppedRecordingChanges(object sender, EventArgs e)
         {
             Tuple<LayerChanges, LayerChanges> changes = ChangesController.PopChanges();
@@ -168,6 +196,13 @@ namespace PixiEditor.ViewModels
                 new LayerChanges(e.OldPixelsValues, e.ChangedLayerIndex));
         }
 
+        public void SwapColors(object parameter)
+        {
+            var tmp = PrimaryColor;
+            PrimaryColor = SecondaryColor;
+            SecondaryColor = tmp;
+        }
+
         public void MoveLayerToFront(object parameter)
         {
             int oldIndex = (int)parameter;

+ 17 - 4
PixiEditor/Views/EditableTextBlock.xaml.cs

@@ -1,4 +1,5 @@
 using PixiEditor.Helpers;
+using PixiEditor.Models.Controllers;
 using System;
 using System.Collections.Generic;
 using System.Text;
@@ -49,12 +50,24 @@ namespace PixiEditor.Views
             set { SetValue(TextProperty, value); }
         }
 
+        private void EnableEditing()
+        {
+            ShortcutController.BlockShortcutExecution = true;
+            TextBlockVisibility = Visibility.Hidden;
+        }
+
+        private void DisableEditing()
+        {
+            TextBlockVisibility = Visibility.Visible;
+            ShortcutController.BlockShortcutExecution = false;
+        }
+
 
         private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
         {
             if(e.ClickCount == 2)
             {
-                TextBlockVisibility = Visibility.Hidden;
+                EnableEditing();
             }
         }
 
@@ -62,18 +75,18 @@ namespace PixiEditor.Views
         {
             if(e.Key == Key.Enter)
             {
-                TextBlockVisibility = Visibility.Visible;
+                DisableEditing();
             }
         }
 
         private void TextBox_LostFocus(object sender, RoutedEventArgs e)
         {
-             TextBlockVisibility = Visibility.Visible;
+            DisableEditing();
         }
 
         private void textBox_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
         {
-               TextBlockVisibility = Visibility.Visible;
+            DisableEditing();
         }
     }
 }

+ 8 - 61
PixiEditor/Views/MainWindow.xaml

@@ -11,43 +11,22 @@
         xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
         xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
         xmlns:xcad="http://schemas.xceed.com/wpf/xaml/avalondock"
-        xmlns:ui="clr-namespace:PixiEditor.Helpers.UI"      
+        xmlns:ui="clr-namespace:PixiEditor.Helpers.UI"
+        xmlns:cmd="http://www.galasoft.ch/mvvmlight"
         mc:Ignorable="d"
-        Title="PixiEditor" Height="1000" Width="1600" Background="#FF252424" WindowStartupLocation="CenterScreen"  WindowState="Maximized" DataContext="{DynamicResource ViewModelMain}">
+        Title="PixiEditor" Name="mainWindow" Height="1000" Width="1600" Background="#FF252424" WindowStartupLocation="CenterScreen"  WindowState="Maximized" DataContext="{DynamicResource ViewModelMain}">
     <Window.Resources>
         <vm:ViewModelMain x:Key="ViewModelMain"/>
         <BooleanToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
         <helpers:ToolSizeToIntConverter x:Key="ToolSizeToIntConverter"/>
         <converters:BoolToColorConverter x:Key="BoolToColorConverter"/>
     </Window.Resources>
-    <Window.InputBindings>
-        <KeyBinding 
-            Modifiers="Ctrl" 
-            Key="N"                  
-            Command="{Binding GenerateDrawAreaCommand}"/>
-        <KeyBinding
-            Modifiers="Ctrl"
-            Key="S"
-            Command="{Binding SaveFileCommand}"/>
-        <KeyBinding
-            Modifiers="Ctrl"
-            Key="Z"
-            Command="{Binding UndoCommand}"/>
-        <KeyBinding 
-            Modifiers="Ctrl"
-            Key="Y"
-            Command="{Binding RedoCommand}"/>
-        <KeyBinding
-            Modifiers="Ctrl"
-            Key="Z"
-            Command="{Binding UndoCommand}"/>
-        <KeyBinding 
-            Modifiers="Ctrl"
-            Key="Y"
-            Command="{Binding RedoCommand}"/>
 
-
-    </Window.InputBindings>
+    <i:Interaction.Triggers>
+        <i:EventTrigger EventName="KeyDown">
+            <cmd:EventToCommand Command="{Binding KeyDownCommand}" PassEventArgsToCommand="True"/>
+        </i:EventTrigger>
+    </i:Interaction.Triggers>
     <Grid>
         <Grid.ColumnDefinitions>
             <ColumnDefinition Width="60*"/>
@@ -91,38 +70,6 @@
         <Grid Grid.Column="1" Grid.Row="1" Background="#303030" Margin="0,5,5,0">
             <Grid>
                 <vws:MainDrawingPanel CenterOnStart="True">
-                    <vws:MainDrawingPanel.InputBindings>
-                        <KeyBinding            
-            Key="B"
-            Command="{Binding SelectToolCommand}"
-            CommandParameter="Pen"/>
-                        <KeyBinding
-            Key="G"
-            Command="{Binding SelectToolCommand}"
-            CommandParameter="Bucket"/>
-                        <KeyBinding
-            Key="L"
-            Command="{Binding SelectToolCommand}"
-            CommandParameter="Line"/>
-                        <KeyBinding
-            Key="C"
-            Command="{Binding SelectToolCommand}"
-            CommandParameter="Circle"/>
-                        <KeyBinding Key="R"
-                    Command="{Binding SelectToolCommand}"
-                    CommandParameter="Rectangle"/>
-                        <KeyBinding Key="O"
-                    Command="{Binding SelectToolCommand}"
-                    CommandParameter="ColorPicker"/>
-                        <KeyBinding Key="E"
-                    Command="{Binding SelectToolCommand}"
-                    CommandParameter="Earser"/>
-                        <KeyBinding Key="U"
-                    Command="{Binding SelectToolCommand}"
-                    CommandParameter="Lighten"/>
-                        <KeyBinding Key="O" Modifiers="Ctrl"
-                    Command="{Binding OpenFileCommand}"/>
-                    </vws:MainDrawingPanel.InputBindings>
                     <vws:MainDrawingPanel.Item>
                         <Canvas Width="{Binding BitmapUtility.ActiveLayer.Width}" Height="{Binding BitmapUtility.ActiveLayer.Height}" VerticalAlignment="Center" HorizontalAlignment="Center">
                             <i:Interaction.Triggers>

+ 2 - 1
PixiEditor/Views/MainWindow.xaml.cs

@@ -1,4 +1,5 @@
-using PixiEditor.ViewModels;
+using PixiEditor.Models.Controllers;
+using PixiEditor.ViewModels;
 using System;
 using System.Collections.Generic;
 using System.Linq;