Browse Source

Added shortcut window shortcut and wrapped both GeneratePreviewBitmap in a private GeneratePrewiewBitmap

CPKreuz 4 years ago
parent
commit
d55097c111

+ 62 - 30
PixiEditor/Models/ImageManipulation/BitmapUtils.cs

@@ -90,40 +90,30 @@ namespace PixiEditor.Models.ImageManipulation
         /// <returns>WriteableBitmap image.</returns>
         public static WriteableBitmap GeneratePreviewBitmap(Document document, int maxPreviewWidth, int maxPreviewHeight)
         {
-            WriteableBitmap previewBitmap = BitmapFactory.New(document.Width, document.Height);
-
-            // 0.8 because blit doesn't take into consideration layer opacity. Small images with opacity > 80% are simillar enough.
-            foreach (var layer in document.Layers.Where(x => x.IsVisible && x.Opacity > 0.8f))
-            {
-                previewBitmap.Blit(
-                    new Rect(layer.OffsetX, layer.OffsetY, layer.Width, layer.Height),
-                    layer.LayerBitmap,
-                    new Rect(0, 0, layer.Width, layer.Height));
-            }
-
-            int width = document.Width >= document.Height ? maxPreviewWidth : (int)Math.Ceiling(document.Width / ((float)document.Height / maxPreviewHeight));
-            int height = document.Height > document.Width ? maxPreviewHeight : (int)Math.Ceiling(document.Height / ((float)document.Width / maxPreviewWidth));
-
-            return previewBitmap.Resize(width, height, WriteableBitmapExtensions.Interpolation.NearestNeighbor);
+            var opacityLayers = document.Layers.Where(x => x.IsVisible && x.Opacity > 0.8f);
+
+            return GeneratePreviewBitmap(
+                opacityLayers.Select(x => x.LayerBitmap),
+                opacityLayers.Select(x => x.OffsetX),
+                opacityLayers.Select(x => x.OffsetY),
+                document.Width,
+                document.Height,
+                maxPreviewWidth,
+                maxPreviewHeight);
         }
 
         public static WriteableBitmap GeneratePreviewBitmap(IEnumerable<SerializableLayer> layers, int width, int height, int maxPreviewWidth, int maxPreviewHeight)
         {
-            WriteableBitmap previewBitmap = BitmapFactory.New(width, height);
-
-            // 0.8 because blit doesn't take into consideration layer opacity. Small images with opacity > 80% are simillar enough.
-            foreach (var layer in layers.Where(x => x.IsVisible && x.Opacity > 0.8f))
-            {
-                previewBitmap.Blit(
-                    new Rect(layer.OffsetX, layer.OffsetY, layer.Width, layer.Height),
-                    BitmapUtils.BytesToWriteableBitmap(layer.Width, layer.Height, layer.BitmapBytes),
-                    new Rect(0, 0, layer.Width, layer.Height));
-            }
-
-            int resizeWidth = width >= height ? maxPreviewWidth : (int)Math.Ceiling(width / ((float)height / maxPreviewHeight));
-            int resizeHeight = height > width ? maxPreviewHeight : (int)Math.Ceiling(height / ((float)width / maxPreviewWidth));
-
-            return previewBitmap.Resize(resizeWidth, resizeHeight, WriteableBitmapExtensions.Interpolation.NearestNeighbor);
+            var opacityLayers = layers.Where(x => x.IsVisible && x.Opacity > 0.8f);
+
+            return GeneratePreviewBitmap(
+                opacityLayers.Select(x => BytesToWriteableBitmap(x.Width, x.Height, x.BitmapBytes)),
+                opacityLayers.Select(x => x.OffsetX),
+                opacityLayers.Select(x => x.OffsetY),
+                width,
+                height,
+                maxPreviewWidth,
+                maxPreviewHeight);
         }
 
         public static Dictionary<Guid, Color[]> GetPixelsForSelection(Layer[] layers, Coordinates[] selection)
@@ -154,5 +144,47 @@ namespace PixiEditor.Models.ImageManipulation
 
             return result;
         }
+
+        private static WriteableBitmap GeneratePreviewBitmap(
+            IEnumerable<WriteableBitmap> layerBitmaps,
+            IEnumerable<int> offsetsX,
+            IEnumerable<int> offsetsY,
+            int width,
+            int height,
+            int maxPreviewWidth,
+            int maxPreviewHeight)
+        {
+            int count = layerBitmaps.Count();
+
+            if (count != offsetsX.Count() || count != offsetsY.Count())
+            {
+                throw new ArgumentException("There were not the same amount of bitmaps and offsets", nameof(layerBitmaps));
+            }
+
+            WriteableBitmap previewBitmap = BitmapFactory.New(width, height);
+
+            var layerBitmapsEnumerator = layerBitmaps.GetEnumerator();
+            var offsetsXEnumerator = offsetsX.GetEnumerator();
+            var offsetsYEnumerator = offsetsY.GetEnumerator();
+
+            while (layerBitmapsEnumerator.MoveNext())
+            {
+                offsetsXEnumerator.MoveNext();
+                offsetsYEnumerator.MoveNext();
+
+                var bitmap = layerBitmapsEnumerator.Current;
+                var offsetX = offsetsXEnumerator.Current;
+                var offsetY = offsetsYEnumerator.Current;
+
+                previewBitmap.Blit(
+                    new Rect(offsetX, offsetY, bitmap.Width, bitmap.Height),
+                    bitmap,
+                    new Rect(0, 0, bitmap.Width, bitmap.Height));
+            }
+
+            int newWidth = width >= height ? maxPreviewWidth : (int)Math.Ceiling(width / ((float)height / maxPreviewHeight));
+            int newHeight = height > width ? maxPreviewHeight : (int)Math.Ceiling(height / ((float)width / maxPreviewWidth));
+            return previewBitmap.Resize(newWidth, newHeight, WriteableBitmapExtensions.Interpolation.NearestNeighbor);
+        }
     }
 }

+ 6 - 0
PixiEditor/ViewModels/ViewModelMain.cs

@@ -165,6 +165,12 @@ namespace PixiEditor.ViewModels
 
             MiscSubViewModel = new MiscViewModel(this);
 
+            // Add F1 shortcut after MiscSubViewModel is constructed
+            ShortcutController.ShortcutGroups.Add(
+                    new ShortcutGroup(
+                        "Misc",
+                        new Shortcut(Key.F1, MiscSubViewModel.OpenShortcutWindowCommand, "Open the shortcut window", true)));
+
             BitmapManager.PrimaryColor = ColorsSubViewModel.PrimaryColor;
         }