Browse Source

Preserve aspect ratio works, implement to all popups wip

flabbet 5 years ago
parent
commit
890b750fd8

+ 2 - 1
PixiEditor/Helpers/Behaviours/TextBoxFocusBehavior.cs

@@ -90,7 +90,8 @@ namespace PixiEditor.Helpers.Behaviours
         private void ConvertValue()
         {
             if (_valueConverted == true || FillSize == false) return;
-            if (int.TryParse(Regex.Replace(AssociatedObject.Text, "\\p{L}", ""), out _) == true)
+            
+            if (int.TryParse(Regex.Replace(AssociatedObject.Text, "\\p{L}", ""), out int result) == true && result > 0)
             {
                 AssociatedObject.Text = string.Format("{0} {1}", AssociatedObject.Text, "px");
             }

+ 16 - 0
PixiEditor/Helpers/Validators/SizeValidationRule.cs

@@ -0,0 +1,16 @@
+using System;
+using System.Collections.Generic;
+using System.Globalization;
+using System.Text;
+using System.Windows.Controls;
+
+namespace PixiEditor.Helpers.Validators
+{
+    public class SizeValidationRule : ValidationRule
+    {
+        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
+        {
+            return new ValidationResult(int.Parse(((string)value).Split(' ')[0]) > 0, null); //Size is greater than 0
+        }
+    }
+}

+ 2 - 0
PixiEditor/Models/DataHolders/Document.cs

@@ -66,6 +66,8 @@ namespace PixiEditor.Models.DataHolders
             Height = height;
         }
 
+        //Resize methods below can be probably reduced in count
+
         public void Crop(int x, int y, int width, int height)
         {
             object[] reverseArgs = new object[] { x, y, 0, 0, Width, Height, width, height};

+ 6 - 15
PixiEditor/Views/ResizeCanvasPopup.xaml

@@ -8,7 +8,7 @@
         xmlns:behaviors="clr-namespace:PixiEditor.Helpers.Behaviours" 
         xmlns:converters="clr-namespace:PixiEditor.Helpers"
         mc:Ignorable="d" Name="window"
-        Title="ResizeCanvasPopup" Topmost="True" ShowInTaskbar="False" WindowStartupLocation="CenterScreen" Height="370" Width="400" WindowStyle="None">
+        Title="ResizeCanvasPopup" Topmost="True" ShowInTaskbar="False" WindowStartupLocation="CenterScreen" Height="390" Width="400" WindowStyle="None">
 
     <Window.Resources>
         <converters:ToolSizeToIntConverter x:Key="ToolSizeToIntConverter"/>
@@ -33,26 +33,17 @@
                             Command="{x:Static SystemCommands.CloseWindowCommand}"/>
         </DockPanel>
         <Label Grid.Row="1" VerticalAlignment="Top" Foreground="White" FontSize="24" HorizontalAlignment="Center" Content="Resize Canvas"/>
-        <StackPanel HorizontalAlignment="Center" Margin="0,50,0,0" Background="{StaticResource MainColor}" VerticalAlignment="Top" Grid.Row="1" Width="300" Height="230">
+        <StackPanel HorizontalAlignment="Center" Margin="0,50,0,0" Background="{StaticResource MainColor}" VerticalAlignment="Top" Grid.Row="1" Width="300" Height="250">
             <DockPanel Margin="50,35,0,0">
                 <TextBlock Height="30" Foreground="Snow" Text="Height:" TextAlignment="Center" FontSize="16" HorizontalAlignment="Center" VerticalAlignment="Center"/>
-                <TextBox Height="30" Width="150" Style="{StaticResource DarkTextBoxStyle}" FontSize="16"
-                             HorizontalAlignment="Left" TextAlignment="Center" 
-                             Margin="5,0,0,0" Text="{Binding ElementName=window, Converter={StaticResource ToolSizeToIntConverter}, Path=NewHeight, Mode=TwoWay}" MaxLength="4">
-                    <i:Interaction.Behaviors>
-                        <behaviors:TextBoxFocusBehavior FillSize="True"/>
-                    </i:Interaction.Behaviors>
-                </TextBox>
+                <local:SizeInput Margin="5,0,0,0" PreserveAspectRatio="{Binding Path=IsChecked, ElementName=aspectRatio}" AspectRatioValue="{Binding Path=NewWidth, ElementName=window}" HorizontalAlignment="Left" Width="150" Height="30" Size="{Binding NewHeight, ElementName=window, Mode=TwoWay}"/>
             </DockPanel>
             <DockPanel Margin="50,10,0,0">
                 <TextBlock Height="30" Foreground="Snow" Text="Width:" TextAlignment="Center" FontSize="16" HorizontalAlignment="Center" VerticalAlignment="Center"/>
-                <TextBox Height="30" Width="150" Style="{StaticResource DarkTextBoxStyle}" FontSize="16" HorizontalAlignment="Left" Margin="10,0,0,0" TextAlignment="Center"
-                             Text="{Binding ElementName=window, Converter={StaticResource ToolSizeToIntConverter}, Path=NewWidth, Mode=TwoWay}" MaxLength="4">
-                    <i:Interaction.Behaviors>
-                        <behaviors:TextBoxFocusBehavior FillSize="True"/>
-                    </i:Interaction.Behaviors>
-                </TextBox>
+                <local:SizeInput Width="150" Height="30" PreserveAspectRatio="{Binding Path=IsChecked, ElementName=aspectRatio}" AspectRatioValue="{Binding Path=NewHeight, ElementName=window}"
+                                 HorizontalAlignment="Left" Margin="10,0,0,0" Size="{Binding Path=NewWidth, ElementName=window, Mode=TwoWay}"/>
             </DockPanel>
+            <CheckBox Name="aspectRatio" Content="Preserve aspect ratio" Foreground="White" HorizontalAlignment="Left" IsChecked="True" Margin="50,10,0,0"/>
             <Separator Margin="10,20,10,0" Background="{StaticResource AccentColor}" Height="1"/>
             <Label Content="Anchor point:" Foreground="White" Margin="10,5,0,0" HorizontalAlignment="Left" FontSize="16"/>
             <local:AnchorPointPicker AnchorPoint="{Binding Path=SelectedAnchorPoint, Mode=TwoWay, ElementName=window}" Width="78" Margin="45,-25,0,0" Height="78"/>

+ 27 - 0
PixiEditor/Views/SizeInput.xaml

@@ -0,0 +1,27 @@
+<UserControl x:Class="PixiEditor.Views.SizeInput"
+             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" 
+             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
+             xmlns:local="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" xmlns:validators="clr-namespace:PixiEditor.Helpers.Validators"
+             mc:Ignorable="d" 
+             d:DesignHeight="30" d:DesignWidth="160" Name="uc" LayoutUpdated="uc_LayoutUpdated">
+    <UserControl.Resources>
+        <converters:ToolSizeToIntConverter x:Key="ToolSizeToIntConverter"/>
+    </UserControl.Resources>
+    <TextBox HorizontalContentAlignment="Center" Style="{StaticResource DarkTextBoxStyle}" FontSize="16" MaxLength="4">
+        <TextBox.Text>
+            <Binding ElementName="uc"
+                     Path="Size" Mode="TwoWay" 
+                     Converter="{StaticResource ToolSizeToIntConverter}">
+                <Binding.ValidationRules>
+                    <validators:SizeValidationRule ValidatesOnTargetUpdated="True"/>
+                </Binding.ValidationRules>
+            </Binding>
+        </TextBox.Text>
+        <i:Interaction.Behaviors>
+            <behaviors:TextBoxFocusBehavior FillSize="True"/>
+        </i:Interaction.Behaviors>
+    </TextBox>
+</UserControl>

+ 84 - 0
PixiEditor/Views/SizeInput.xaml.cs

@@ -0,0 +1,84 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Data;
+using System.Windows.Documents;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Imaging;
+using System.Windows.Navigation;
+using System.Windows.Shapes;
+
+namespace PixiEditor.Views
+{
+    /// <summary>
+    /// Interaction logic for SizeInput.xaml
+    /// </summary>
+    public partial class SizeInput : UserControl
+    {
+
+        private int _loadedSize = -1;
+        private int _loadedAspectRatioSize = -1;
+
+        public SizeInput()
+        {
+            InitializeComponent();
+        }
+
+
+
+        public int Size
+        {
+            get { return (int)GetValue(SizeProperty); }
+            set { SetValue(SizeProperty, value); }
+        }
+
+        // Using a DependencyProperty as the backing store for Size.  This enables animation, styling, binding, etc...
+        public static readonly DependencyProperty SizeProperty =
+            DependencyProperty.Register("Size", typeof(int), typeof(SizeInput), new PropertyMetadata(1));
+
+        public bool PreserveAspectRatio
+        {
+            get { return (bool)GetValue(PreserveAspectRatioProperty); }
+            set { SetValue(PreserveAspectRatioProperty, value); }
+        }
+
+        // Using a DependencyProperty as the backing store for PreserveAspectRatio.  This enables animation, styling, binding, etc...
+        public static readonly DependencyProperty PreserveAspectRatioProperty =
+            DependencyProperty.Register("PreserveAspectRatio", typeof(bool), typeof(SizeInput), new PropertyMetadata(false));
+
+
+        public int AspectRatioValue
+        {
+            get { return (int)GetValue(AspectRatioValueProperty); }
+            set { SetValue(AspectRatioValueProperty, value); }
+        }
+
+        // Using a DependencyProperty as the backing store for AspectRatioValue.  This enables animation, styling, binding, etc...
+        public static readonly DependencyProperty AspectRatioValueProperty =
+            DependencyProperty.Register("AspectRatioValue", typeof(int), typeof(SizeInput), new PropertyMetadata(1, AspectRatioValChanged));
+
+        private static void AspectRatioValChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
+        {
+            SizeInput input = (SizeInput)d;
+
+            if (input.PreserveAspectRatio && input._loadedSize != -1)
+            {
+                int newVal = (int)e.NewValue;
+                float ratio = newVal / Math.Clamp(input._loadedAspectRatioSize, 1f, float.MaxValue);
+                input.Size = (int)(input._loadedSize * ratio);
+            }
+        }
+
+        private void uc_LayoutUpdated(object sender, EventArgs e)
+        {
+            if(_loadedSize == -1)
+            {
+                _loadedSize = Size;
+                _loadedAspectRatioSize = AspectRatioValue;
+            }
+        }
+    }
+}