Browse Source

MaxSize as constant

tomaszkot 3 years ago
parent
commit
da5a03ea54

+ 4 - 0
PixiEditor/Helpers/SizeCalculator.cs

@@ -9,5 +9,9 @@
             var newHeight = currentSize.Height * percFactor;
             return new System.Drawing.Size((int)newWidth, (int)newHeight);
         }
+        public static int CalcPercentageFromAbsolute(int initAbsoluteSize, int currentAbsoluteSize)
+        {
+            return (int)((float)currentAbsoluteSize*100) / initAbsoluteSize;
+        }
     }
 }

+ 9 - 0
PixiEditor/Models/Constants.cs

@@ -0,0 +1,9 @@
+namespace PixiEditor.Models
+{
+    internal class Constants
+    {
+        public const int DefaultSize = 64;
+        public const int MaxWidth = 2048;
+        public const int MaxHeight = 2048;
+    }
+}

+ 4 - 6
PixiEditor/Models/DataHolders/RecentlyOpenedDocument.cs

@@ -105,18 +105,16 @@ namespace PixiEditor.Models.DataHolders
                 {
                     corrupt = true;
                 }
-
-                const int MaxWidthInPixels = 2048;
-                const int MaxHeightInPixels = 2048;
+                                
                 ImageFileMaxSizeChecker imageFileMaxSizeChecker = new ImageFileMaxSizeChecker()
                 {
-                    MaxAllowedWidthInPixels = MaxWidthInPixels,
-                    MaxAllowedHeightInPixels = MaxHeightInPixels,
+                    MaxAllowedWidthInPixels = Constants.MaxWidth,
+                    MaxAllowedHeightInPixels = Constants.MaxHeight,
                 };
 
                 return imageFileMaxSizeChecker.IsFileUnderMaxSize(bitmap) ?
                     bitmap
-                    : bitmap.Resize(width: MaxWidthInPixels, height: MaxHeightInPixels, WriteableBitmapExtensions.Interpolation.Bilinear);
+                    : bitmap.Resize(width: Constants.MaxWidth, height: Constants.MaxHeight, WriteableBitmapExtensions.Interpolation.Bilinear);
             }
 
             return null;

+ 2 - 4
PixiEditor/Models/Dialogs/NewFileDialog.cs

@@ -5,11 +5,9 @@ namespace PixiEditor.Models.Dialogs
 {
     public class NewFileDialog : CustomDialog
     {
-        public const int defaultSize = 64;
+        private int height = IPreferences.Current.GetPreference("DefaultNewFileHeight", Constants.DefaultSize);
 
-        private int height = IPreferences.Current.GetPreference("DefaultNewFileHeight", defaultSize);
-
-        private int width = IPreferences.Current.GetPreference("DefaultNewFileWidth", defaultSize);
+        private int width = IPreferences.Current.GetPreference("DefaultNewFileWidth", Constants.DefaultSize);
 
         public int Width
         {

+ 4 - 3
PixiEditor/ViewModels/SubViewModels/UserPreferences/Settings/FileSettings.cs

@@ -1,4 +1,5 @@
-using PixiEditor.Models.Dialogs;
+using PixiEditor.Models;
+using PixiEditor.Models.Dialogs;
 
 namespace PixiEditor.ViewModels.SubViewModels.UserPreferences.Settings
 {
@@ -12,7 +13,7 @@ namespace PixiEditor.ViewModels.SubViewModels.UserPreferences.Settings
             set => RaiseAndUpdatePreference(ref showStartupWindow, value);
         }
 
-        private int defaultNewFileWidth = GetPreference("DefaultNewFileWidth", NewFileDialog.defaultSize);
+        private int defaultNewFileWidth = GetPreference("DefaultNewFileWidth", Constants.DefaultSize);
 
         public int DefaultNewFileWidth
         {
@@ -25,7 +26,7 @@ namespace PixiEditor.ViewModels.SubViewModels.UserPreferences.Settings
             }
         }
 
-        private int defaultNewFileHeight = GetPreference("DefaultNewFileHeight", NewFileDialog.defaultSize);
+        private int defaultNewFileHeight = GetPreference("DefaultNewFileHeight", Constants.DefaultSize);
 
         public int DefaultNewFileHeight
         {

+ 4 - 2
PixiEditor/Views/UserControls/SizePicker.xaml.cs

@@ -1,4 +1,5 @@
 using PixiEditor.Helpers;
+using PixiEditor.Models;
 using PixiEditor.Models.Enums;
 using PixiEditor.ViewModels;
 using System;
@@ -43,9 +44,10 @@ namespace PixiEditor.Views
 
             var newValue = (int)e.NewValue;
             var newSize = SizeCalculator.CalcAbsoluteFromPercentage(newValue, sizePicker.initSize.Value);
-            if (newSize.Width > sizePicker.MaxWidth || newSize.Width > sizePicker.MaxHeight)
+            if (newSize.Width > Constants.MaxWidth || newSize.Width > Constants.MaxHeight)
             {
-                newSize = new System.Drawing.Size(newSize.Width, newSize.Height);
+                newSize = new System.Drawing.Size(Constants.MaxWidth, Constants.MaxHeight);
+                d.SetValue(ChosenPercentageSizeProperty, SizeCalculator.CalcPercentageFromAbsolute(sizePicker.initSize.Value.Width, newSize.Width));
             }
             
             d.SetValue(ChosenWidthProperty, newSize.Width);

+ 11 - 1
PixiEditorTests/HelpersTests/SizeCalculatorTest.cs

@@ -9,11 +9,21 @@ namespace PixiEditorTests.HelpersTests
         [InlineData(50, 64, 64, 32, 32)]
         [InlineData(100, 64, 64, 64, 64)]
         [InlineData(200, 128, 128, 256, 256)]
-        public void TestCalculationOfPercentsWorks(int percent, int currentWidth, int currentHeight, int expectedWidth, int expectedHeight)
+        public void TestCalculationOfAbsoluteFromPercentageWorks(int percent, int currentWidth, int currentHeight, int expectedWidth, int expectedHeight)
         {
             var newSize = SizeCalculator.CalcAbsoluteFromPercentage(percent, new System.Drawing.Size(currentWidth, currentHeight));
             Assert.Equal(expectedWidth, newSize.Width);
             Assert.Equal(expectedHeight, newSize.Height);
         }
+
+        [Theory]
+        [InlineData(32, 64, 50)]
+        [InlineData(32, 32, 100)]
+        [InlineData(64, 32, 200)]
+        public void TestCalculationOfPercentageFromAbsoluteWorks(int currentSize, int initSize, int expectedPerc)
+        {
+            var perc = SizeCalculator.CalcPercentageFromAbsolute(initSize, currentSize);
+            Assert.Equal(perc, expectedPerc);
+        }
     }
 }