Browse Source

Improved checker background + changed array types for faster operations

flabbet 4 years ago
parent
commit
75a60f3bfc

+ 26 - 0
PixiEditor/Helpers/Converters/ZoomToViewportConverter.cs

@@ -0,0 +1,26 @@
+using System;
+using System.Globalization;
+using System.Windows;
+using System.Windows.Data;
+
+namespace PixiEditor.Helpers.Converters
+{
+    public class ZoomToViewportConverter : IValueConverter
+    {
+        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
+        {
+            if(value is double scale)
+            {
+                double newSize = Math.Clamp((double)parameter / scale, 1, 9999);
+                return new Rect(0, 0, newSize, newSize);
+            }
+
+            return Binding.DoNothing;
+        }
+
+        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
+        {
+            throw new NotImplementedException();
+        }
+    }
+}

+ 15 - 9
PixiEditor/Models/Controllers/BitmapOperationsUtility.cs

@@ -121,20 +121,26 @@ namespace PixiEditor.Models.Controllers
         {
             SizeSetting sizeSetting = tool.Toolbar.GetSetting<SizeSetting>("ToolSize");
             int thickness = sizeSetting != null ? sizeSetting.Value : 1;
-            bool mouseInLine = MouseCordsNotInLine(mouseMoveCords, thickness);
+
             bool shiftDown = Keyboard.IsKeyDown(Key.LeftShift);
-            if (shiftDown && !mouseInLine)
-            {
-                mouseMoveCords = GetSquareCoordiantes(mouseMoveCords);
-            }
-            else if (shiftDown && mouseInLine)
+
+            if (shiftDown)
             {
-                mouseMoveCords = GetLineCoordinates(mouseMoveCords, thickness);
+                bool mouseInLine = MouseCordsNotInLine(mouseMoveCords, thickness);
+
+                if (!mouseInLine)
+                {
+                    mouseMoveCords = GetSquareCoordiantes(mouseMoveCords);
+                }
+                else
+                {
+                    mouseMoveCords = GetLineCoordinates(mouseMoveCords, thickness);
+                }
             }
 
             if (!tool.RequiresPreviewLayer)
             {
-                LayerChange[] modifiedLayers = tool.Use(Manager.ActiveLayer, mouseMoveCords.ToArray(), color);
+                LayerChange[] modifiedLayers = tool.Use(Manager.ActiveLayer, mouseMoveCords, color);
                 LayerChange[] oldPixelsValues = new LayerChange[modifiedLayers.Length];
                 for (int i = 0; i < modifiedLayers.Length; i++)
                 {
@@ -257,7 +263,7 @@ namespace PixiEditor.Models.Controllers
 
                 modifiedLayers = ((BitmapOperationTool)Manager.SelectedTool).Use(
                     Manager.ActiveDocument.ActiveLayer,
-                    mouseMove.ToArray(),
+                    mouseMove,
                     Manager.PrimaryColor);
 
                 BitmapPixelChanges[] changes = modifiedLayers.Select(x => x.PixelChanges).ToArray();

+ 3 - 1
PixiEditor/Models/Tools/BitmapOperationTool.cs

@@ -1,4 +1,6 @@
 using System;
+using System.Collections.Generic;
+using System.Windows.Documents;
 using System.Windows.Media;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.Layers;
@@ -16,7 +18,7 @@ namespace PixiEditor.Models.Tools
 
         private readonly LayerChange[] onlyLayerArr = new LayerChange[] { new LayerChange(BitmapPixelChanges.Empty, Guid.Empty) };
 
-        public abstract LayerChange[] Use(Layer layer, Coordinates[] mouseMove, Color color);
+        public abstract LayerChange[] Use(Layer layer, List<Coordinates> mouseMove, Color color);
 
         protected LayerChange[] Only(BitmapPixelChanges changes, Layer layer)
         {

+ 1 - 1
PixiEditor/Models/Tools/ShapeTool.cs

@@ -19,7 +19,7 @@ namespace PixiEditor.Models.Tools
         }
 
         // TODO: Add cache for lines 31, 32 (hopefully it would speed up calculation)
-        public abstract override LayerChange[] Use(Layer layer, Coordinates[] coordinates, Color color);
+        public abstract override LayerChange[] Use(Layer layer, List<Coordinates> coordinates, Color color);
 
         protected IEnumerable<Coordinates> GetThickShape(IEnumerable<Coordinates> shape, int thickness)
         {

+ 1 - 1
PixiEditor/Models/Tools/Tools/BrightnessTool.cs

@@ -49,7 +49,7 @@ namespace PixiEditor.Models.Tools.Tools
             }
         }
 
-        public override LayerChange[] Use(Layer layer, Coordinates[] coordinates, Color color)
+        public override LayerChange[] Use(Layer layer, List<Coordinates> coordinates, Color color)
         {
             int toolSize = Toolbar.GetSetting<SizeSetting>("ToolSize").Value;
             float correctionFactor = Toolbar.GetSetting<FloatSetting>("CorrectionFactor").Value;

+ 1 - 1
PixiEditor/Models/Tools/Tools/CircleTool.cs

@@ -35,7 +35,7 @@ namespace PixiEditor.Models.Tools.Tools
             }
         }
 
-        public override LayerChange[] Use(Layer layer, Coordinates[] coordinates, Color color)
+        public override LayerChange[] Use(Layer layer, List<Coordinates> coordinates, Color color)
         {
             int thickness = Toolbar.GetSetting<SizeSetting>("ToolSize").Value;
             DoubleCords fixedCoordinates = CalculateCoordinatesForShapeRotation(coordinates[^1], coordinates[0]);

+ 5 - 4
PixiEditor/Models/Tools/Tools/EraserTool.cs

@@ -1,4 +1,5 @@
-using System.Windows.Media;
+using System.Collections.Generic;
+using System.Windows.Media;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Position;
@@ -18,14 +19,14 @@ namespace PixiEditor.Models.Tools.Tools
             Toolbar = new BasicToolbar();
         }
 
-        public override LayerChange[] Use(Layer layer, Coordinates[] coordinates, Color color)
+        public override LayerChange[] Use(Layer layer, List<Coordinates> coordinates, Color color)
         {
             return Erase(layer, coordinates, Toolbar.GetSetting<SizeSetting>("ToolSize").Value);
         }
 
-        public LayerChange[] Erase(Layer layer, Coordinates[] coordinates, int toolSize)
+        public LayerChange[] Erase(Layer layer, List<Coordinates> coordinates, int toolSize)
         {
-            Coordinates startingCords = coordinates.Length > 1 ? coordinates[1] : coordinates[0];
+            Coordinates startingCords = coordinates.Count > 1 ? coordinates[1] : coordinates[0];
             BitmapPixelChanges pixels = pen.Draw(startingCords, coordinates[0], System.Windows.Media.Colors.Transparent, toolSize);
             return Only(pixels, layer);
         }

+ 1 - 1
PixiEditor/Models/Tools/Tools/FloodFill.cs

@@ -15,7 +15,7 @@ namespace PixiEditor.Models.Tools.Tools
             Tooltip = "Fills area with color. (G)";
         }
 
-        public override LayerChange[] Use(Layer layer, Coordinates[] coordinates, Color color)
+        public override LayerChange[] Use(Layer layer, List<Coordinates> coordinates, Color color)
         {
             return Only(ForestFire(layer, coordinates[0], color), layer);
         }

+ 3 - 3
PixiEditor/Models/Tools/Tools/LineTool.cs

@@ -39,7 +39,7 @@ namespace PixiEditor.Models.Tools.Tools
             }
         }
 
-        public override LayerChange[] Use(Layer layer, Coordinates[] coordinates, Color color)
+        public override LayerChange[] Use(Layer layer, List<Coordinates> coordinates, Color color)
         {
             BitmapPixelChanges pixels =
                 BitmapPixelChanges.FromSingleColoredArray(
@@ -58,10 +58,10 @@ namespace PixiEditor.Models.Tools.Tools
 
         public IEnumerable<Coordinates> CreateLine(Coordinates start, Coordinates end, int thickness, CapType startCap, CapType endCap)
         {
-            return CreateLine(new[] { end, start }, thickness, startCap, endCap);
+            return CreateLine(new() { end, start }, thickness, startCap, endCap);
         }
 
-        private IEnumerable<Coordinates> CreateLine(Coordinates[] coordinates, int thickness, CapType startCap, CapType endCap)
+        private IEnumerable<Coordinates> CreateLine(List<Coordinates> coordinates, int thickness, CapType startCap, CapType endCap)
         {
             Coordinates startingCoordinates = coordinates[^1];
             Coordinates latestCoordinates = coordinates[0];

+ 3 - 3
PixiEditor/Models/Tools/Tools/MoveTool.cs

@@ -133,7 +133,7 @@ namespace PixiEditor.Models.Tools.Tools
             startingOffsets = GetOffsets(affectedLayers);
         }
 
-        public override LayerChange[] Use(Layer layer, Coordinates[] mouseMove, Color color)
+        public override LayerChange[] Use(Layer layer, List<Coordinates> mouseMove, Color color)
         {
             LayerChange[] result = new LayerChange[affectedLayers.Length];
             var end = mouseMove[0];
@@ -163,9 +163,9 @@ namespace PixiEditor.Models.Tools.Tools
             return result;
         }
 
-        public BitmapPixelChanges MoveSelection(Layer layer, Coordinates[] mouseMove)
+        public BitmapPixelChanges MoveSelection(Layer layer, IEnumerable<Coordinates> mouseMove)
         {
-            Coordinates end = mouseMove[0];
+            Coordinates end = mouseMove.First();
 
             currentSelection = TranslateSelection(end);
             if (updateViewModelSelection)

+ 2 - 2
PixiEditor/Models/Tools/Tools/PenTool.cs

@@ -46,9 +46,9 @@ namespace PixiEditor.Models.Tools.Tools
             confirmedPixels.Clear();
         }
 
-        public override LayerChange[] Use(Layer layer, Coordinates[] coordinates, Color color)
+        public override LayerChange[] Use(Layer layer, List<Coordinates> coordinates, Color color)
         {
-            Coordinates startingCords = coordinates.Length > 1 ? coordinates[1] : coordinates[0];
+            Coordinates startingCords = coordinates.Count > 1 ? coordinates[1] : coordinates[0];
             BitmapPixelChanges pixels = Draw(
                 startingCords,
                 coordinates[0],

+ 3 - 3
PixiEditor/Models/Tools/Tools/RectangleTool.cs

@@ -37,7 +37,7 @@ namespace PixiEditor.Models.Tools.Tools
             }
         }
 
-        public override LayerChange[] Use(Layer layer, Coordinates[] coordinates, Color color)
+        public override LayerChange[] Use(Layer layer, List<Coordinates> coordinates, Color color)
         {
             int thickness = Toolbar.GetSetting<SizeSetting>("ToolSize").Value;
             BitmapPixelChanges pixels =
@@ -54,7 +54,7 @@ namespace PixiEditor.Models.Tools.Tools
             return new[] { new LayerChange(pixels, layer) };
         }
 
-        public IEnumerable<Coordinates> CreateRectangle(Coordinates[] coordinates, int thickness)
+        public IEnumerable<Coordinates> CreateRectangle(List<Coordinates> coordinates, int thickness)
         {
             DoubleCords fixedCoordinates = CalculateCoordinatesForShapeRotation(coordinates[^1], coordinates[0]);
             List<Coordinates> output = new List<Coordinates>();
@@ -80,7 +80,7 @@ namespace PixiEditor.Models.Tools.Tools
 
         public IEnumerable<Coordinates> CreateRectangle(Coordinates start, Coordinates end, int thickness)
         {
-            return CreateRectangle(new[] { end, start }, thickness);
+            return CreateRectangle(new() { end, start }, thickness);
         }
 
         public IEnumerable<Coordinates> CalculateFillForRectangle(Coordinates start, Coordinates end, int thickness)

+ 2 - 2
PixiEditor/Views/MainWindow.xaml

@@ -4,7 +4,7 @@
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:vm="clr-namespace:PixiEditor.ViewModels"
-        xmlns:vws="clr-namespace:PixiEditor.Views"
+        xmlns:dataHolders="clr-namespace:PixiEditor.Models.DataHolders"
         xmlns:converters="clr-namespace:PixiEditor.Helpers.Converters"
         xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
         xmlns:ui="clr-namespace:PixiEditor.Helpers.UI"
@@ -201,7 +201,7 @@
                     <DockingManager.LayoutItemTemplateSelector>
                         <ui:DocumentsTemplateSelector>
                             <ui:DocumentsTemplateSelector.DocumentsViewTemplate>
-                                <DataTemplate DataType="{x:Type vm:ViewModelMain}">
+                                <DataTemplate DataType="{x:Type dataHolders:Document}">
                                     <usercontrols:DrawingViewPort
                                         ZoomPercentage="{Binding ZoomPercentage}"
                                         RecenterZoombox="{Binding RecenterZoombox}"

+ 32 - 25
PixiEditor/Views/UserControls/DrawingViewPort.xaml

@@ -1,4 +1,4 @@
-<UserControl x:Class="PixiEditor.Views.UserControls.DrawingViewPort"
+<UserControl
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
@@ -8,39 +8,49 @@
              xmlns:vws="clr-namespace:PixiEditor.Views" 
              xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
              xmlns:behaviors="clr-namespace:PixiEditor.Helpers.Behaviours" xmlns:converters="clr-namespace:PixiEditor.Helpers.Converters"
+             xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit" xmlns:sys="clr-namespace:System;assembly=System.Runtime" x:Class="PixiEditor.Views.UserControls.DrawingViewPort"
              mc:Ignorable="d" 
-             d:DesignHeight="450" d:DesignWidth="800" Name="uc">
+             d:DesignHeight="450" d:DesignWidth="800" x:Name="uc">
     <UserControl.Resources>
         <converters:BoolToIntConverter x:Key="BoolToIntConverter" />
         <BooleanToVisibilityConverter x:Key="BoolToVisibilityConverter" />
         <converters:IntToViewportRectConverter x:Key="IntToViewportRectConverter" />
+        <converters:ZoomToViewportConverter x:Key="ZoomToViewportConverter"/>
     </UserControl.Resources>
-    <vws:MainDrawingPanel ZoomPercentage="{Binding ZoomPercentage, Mode=TwoWay, ElementName=uc}"
-                          Center="{Binding RecenterZoombox, Mode=TwoWay, ElementName=uc}" 
+    <vws:MainDrawingPanel ZoomPercentage="{Binding ZoomPercentage, ElementName=uc, Mode=TwoWay}"
+                          Center="{Binding RecenterZoombox, ElementName=uc, Mode=TwoWay}" 
                           x:Name="DrawingPanel"
                           CenterOnStart="True" Cursor="{Binding Cursor, ElementName=uc}" 
                           MiddleMouseClickedCommand="{Binding MiddleMouseClickedCommand, ElementName=uc}" 
                           MiddleMouseClickedCommandParameter="{x:Type tools:MoveViewportTool}"
                           ViewportPosition="{Binding ViewportPosition, ElementName=uc, Mode=TwoWay}">
-            <i:Interaction.Triggers>
-                <i:EventTrigger EventName="MouseMove">
-                    <i:InvokeCommandAction Command="{Binding MouseMoveCommand, ElementName=uc}" />
-                </i:EventTrigger>
-                <i:EventTrigger EventName="MouseDown">
-                    <i:InvokeCommandAction Command="{Binding MouseDownCommand, ElementName=uc}"/>
-                </i:EventTrigger>
-            </i:Interaction.Triggers>
-            <i:Interaction.Behaviors>
-                <behaviors:MouseBehavior RelativeTo="{Binding ElementName=DrawingPanel, Path=Item}"
-                                                  MouseX="{Binding MouseXOnCanvas, Mode=TwoWay, ElementName=uc}"
-                                                  MouseY="{Binding MouseYOnCanvas, Mode=TwoWay, ElementName=uc}" />
-            </i:Interaction.Behaviors>
+        <i:Interaction.Triggers>
+            <i:EventTrigger EventName="MouseMove">
+                <i:InvokeCommandAction Command="{Binding MouseMoveCommand, ElementName=uc}" />
+            </i:EventTrigger>
+            <i:EventTrigger EventName="MouseDown">
+                <i:InvokeCommandAction Command="{Binding MouseDownCommand, ElementName=uc}"/>
+            </i:EventTrigger>
+        </i:Interaction.Triggers>
+        <i:Interaction.Behaviors>
+            <behaviors:MouseBehavior RelativeTo="{Binding Item, ElementName=DrawingPanel}"
+                                                  MouseX="{Binding MouseXOnCanvas, ElementName=uc, Mode=TwoWay}"
+                                                  MouseY="{Binding MouseYOnCanvas, ElementName=uc, Mode=TwoWay}" />
+        </i:Interaction.Behaviors>
         <vws:MainDrawingPanel.Item>
             <Canvas Width="{Binding Width}"
                                 Height="{Binding Height}" VerticalAlignment="Center"
                                 HorizontalAlignment="Center" RenderOptions.BitmapScalingMode="NearestNeighbor">
                 <Canvas.Background>
-                    <ImageBrush ImageSource="/Images/CheckerTile.png" TileMode="Tile" ViewportUnits="Absolute" Viewport="0, 0, 16, 16" />
+                    <ImageBrush ImageSource="/Images/CheckerTile.png" TileMode="Tile" ViewportUnits="Absolute">
+                        <ImageBrush.Viewport>
+                            <Binding Path="Scale" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type xctk:Zoombox}}" Converter="{StaticResource ZoomToViewportConverter}">
+                                <Binding.ConverterParameter>
+                                    <sys:Double>16</sys:Double>
+                                </Binding.ConverterParameter>
+                            </Binding>
+                        </ImageBrush.Viewport>
+                    </ImageBrush>
                 </Canvas.Background>
                 <Image Source="{Binding PreviewLayer.LayerBitmap}" Panel.ZIndex="2"
                                    RenderOptions.BitmapScalingMode="NearestNeighbor" Stretch="Uniform"
@@ -70,8 +80,7 @@
                                    Height="{Binding ActiveSelection.SelectionLayer.Height}" 
                                    Margin="{Binding ActiveSelection.SelectionLayer.Offset}" />
                 <Grid ShowGridLines="True" Width="{Binding Width}" Height="{Binding Height}" Panel.ZIndex="10" 
-                      Visibility="{Binding GridLinesVisible, Converter={StaticResource BoolToVisibilityConverter},
-                    RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:DrawingViewPort}}}">
+                      Visibility="{Binding GridLinesVisible, Converter={StaticResource BoolToVisibilityConverter}, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:DrawingViewPort}}}">
                     <Rectangle Focusable="False">
                         <Rectangle.Fill>
                             <VisualBrush Viewport="{Binding Height, Converter={StaticResource IntToViewportRectConverter}}" ViewboxUnits="Absolute" TileMode="Tile" >
@@ -83,8 +92,7 @@
                     </Rectangle>
                     <Rectangle Focusable="False">
                         <Rectangle.Fill>
-                            <VisualBrush Viewport="{Binding Width, Converter={StaticResource IntToViewportRectConverter}, 
-                            ConverterParameter=vertical}" ViewboxUnits="Absolute" TileMode="Tile" >
+                            <VisualBrush Viewport="{Binding Width, Converter={StaticResource IntToViewportRectConverter}, ConverterParameter=vertical}" ViewboxUnits="Absolute" TileMode="Tile" >
                                 <VisualBrush.Visual>
                                     <Line  X1="0" Y1="0" X2="0" Y2="1" Stroke="Black" StrokeThickness="0.01"/>
                                 </VisualBrush.Visual>
@@ -102,8 +110,7 @@
                     </Rectangle>
                     <Rectangle Focusable="False">
                         <Rectangle.Fill>
-                            <VisualBrush Viewport="{Binding Width, Converter={StaticResource IntToViewportRectConverter}, 
-                            ConverterParameter=vertical}" ViewboxUnits="Absolute" TileMode="Tile" >
+                            <VisualBrush Viewport="{Binding Width, Converter={StaticResource IntToViewportRectConverter}, ConverterParameter=vertical}" ViewboxUnits="Absolute" TileMode="Tile" >
                                 <VisualBrush.Visual>
                                     <Line  X1="0" Y1="0" X2="0" Y2="1" Stroke="White" StrokeThickness="0.01"/>
                                 </VisualBrush.Visual>
@@ -113,5 +120,5 @@
                 </Grid>
             </Canvas>
         </vws:MainDrawingPanel.Item>
-        </vws:MainDrawingPanel>
+    </vws:MainDrawingPanel>
 </UserControl>

+ 3 - 1
PixiEditor/Views/UserControls/DrawingViewPort.xaml.cs

@@ -1,4 +1,6 @@
-using System.Windows;
+using System;
+using System.Diagnostics;
+using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Input;