|
@@ -22,7 +22,7 @@ namespace PixiEditor.Views
|
|
DependencyProperty.Register(nameof(ChosenHeight), typeof(int), typeof(SizePicker), new PropertyMetadata(1));
|
|
DependencyProperty.Register(nameof(ChosenHeight), typeof(int), typeof(SizePicker), new PropertyMetadata(1));
|
|
|
|
|
|
public static readonly DependencyProperty ChosenPercentageSizeProperty =
|
|
public static readonly DependencyProperty ChosenPercentageSizeProperty =
|
|
- DependencyProperty.Register(nameof(ChosenPercentageSize), typeof(int), typeof(SizePicker), new PropertyMetadata(1, InputSizeChanged));
|
|
|
|
|
|
+ DependencyProperty.Register(nameof(ChosenPercentageSize), typeof(float), typeof(SizePicker), new PropertyMetadata(100f));
|
|
|
|
|
|
public static readonly DependencyProperty SelectedUnitProperty =
|
|
public static readonly DependencyProperty SelectedUnitProperty =
|
|
DependencyProperty.Register(nameof(SelectedUnit), typeof(SizeUnit), typeof(SizePicker), new PropertyMetadata(SizeUnit.Pixel));
|
|
DependencyProperty.Register(nameof(SelectedUnit), typeof(SizeUnit), typeof(SizePicker), new PropertyMetadata(SizeUnit.Pixel));
|
|
@@ -32,24 +32,6 @@ namespace PixiEditor.Views
|
|
|
|
|
|
System.Drawing.Size? initSize = null;
|
|
System.Drawing.Size? initSize = null;
|
|
|
|
|
|
- private static void InputSizeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
|
|
|
- {
|
|
|
|
- var sizePicker = d as SizePicker;
|
|
|
|
- if (!sizePicker.initSize.HasValue)
|
|
|
|
- return;
|
|
|
|
-
|
|
|
|
- var newValue = (int)e.NewValue;
|
|
|
|
- var newSize = SizeCalculator.CalcAbsoluteFromPercentage(newValue, sizePicker.initSize.Value);
|
|
|
|
- if (newSize.Width > Constants.MaxCanvasWidth || newSize.Height > Constants.MaxCanvasHeight)
|
|
|
|
- {
|
|
|
|
- newSize = new System.Drawing.Size(Constants.MaxCanvasWidth, Constants.MaxCanvasHeight);
|
|
|
|
- d.SetValue(ChosenPercentageSizeProperty, SizeCalculator.CalcPercentageFromAbsolute(sizePicker.initSize.Value.Width, newSize.Width));
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- d.SetValue(ChosenWidthProperty, newSize.Width);
|
|
|
|
- d.SetValue(ChosenHeightProperty, newSize.Height);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
public bool EditingEnabled
|
|
public bool EditingEnabled
|
|
{
|
|
{
|
|
get => (bool)GetValue(EditingEnabledProperty);
|
|
get => (bool)GetValue(EditingEnabledProperty);
|
|
@@ -68,9 +50,9 @@ namespace PixiEditor.Views
|
|
set => SetValue(ChosenHeightProperty, value);
|
|
set => SetValue(ChosenHeightProperty, value);
|
|
}
|
|
}
|
|
|
|
|
|
- public int ChosenPercentageSize
|
|
|
|
|
|
+ public float ChosenPercentageSize
|
|
{
|
|
{
|
|
- get => (int)GetValue(ChosenPercentageSizeProperty);
|
|
|
|
|
|
+ get => (float)GetValue(ChosenPercentageSizeProperty);
|
|
set => SetValue(ChosenPercentageSizeProperty, value);
|
|
set => SetValue(ChosenPercentageSizeProperty, value);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -95,12 +77,14 @@ namespace PixiEditor.Views
|
|
public RelayCommand LoadedCommand { get; private set; }
|
|
public RelayCommand LoadedCommand { get; private set; }
|
|
public RelayCommand WidthLostFocusCommand { get; private set; }
|
|
public RelayCommand WidthLostFocusCommand { get; private set; }
|
|
public RelayCommand HeightLostFocusCommand { get; private set; }
|
|
public RelayCommand HeightLostFocusCommand { get; private set; }
|
|
|
|
+ public RelayCommand PercentageLostFocusCommand { get; private set; }
|
|
|
|
|
|
public SizePicker()
|
|
public SizePicker()
|
|
{
|
|
{
|
|
LoadedCommand = new(AfterLoaded);
|
|
LoadedCommand = new(AfterLoaded);
|
|
WidthLostFocusCommand = new(WidthLostFocus);
|
|
WidthLostFocusCommand = new(WidthLostFocus);
|
|
HeightLostFocusCommand = new(HeightLostFocus);
|
|
HeightLostFocusCommand = new(HeightLostFocus);
|
|
|
|
+ PercentageLostFocusCommand = new(PercentageLostFocus);
|
|
InitializeComponent();
|
|
InitializeComponent();
|
|
}
|
|
}
|
|
|
|
|
|
@@ -118,6 +102,46 @@ namespace PixiEditor.Views
|
|
private void WidthLostFocus(object param) => OnSizeUpdate(true);
|
|
private void WidthLostFocus(object param) => OnSizeUpdate(true);
|
|
private void HeightLostFocus(object param) => OnSizeUpdate(false);
|
|
private void HeightLostFocus(object param) => OnSizeUpdate(false);
|
|
|
|
|
|
|
|
+ private void PercentageLostFocus(object param)
|
|
|
|
+ {
|
|
|
|
+ if (!initSize.HasValue)
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ float targetPercentage = GetTargetPercentage(initSize.Value, ChosenPercentageSize);
|
|
|
|
+ var newSize = SizeCalculator.CalcAbsoluteFromPercentage(targetPercentage, initSize.Value);
|
|
|
|
+
|
|
|
|
+ //this shouldn't ever be necessary but just in case
|
|
|
|
+ newSize.Width = Math.Clamp(newSize.Width, 1, Constants.MaxCanvasSize);
|
|
|
|
+ newSize.Height = Math.Clamp(newSize.Height, 1, Constants.MaxCanvasSize);
|
|
|
|
+
|
|
|
|
+ ChosenPercentageSize = targetPercentage;
|
|
|
|
+ ChosenWidth = newSize.Width;
|
|
|
|
+ ChosenHeight = newSize.Height;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static float GetTargetPercentage(System.Drawing.Size initSize, float desiredPercentage)
|
|
|
|
+ {
|
|
|
|
+ var potentialSize = SizeCalculator.CalcAbsoluteFromPercentage(desiredPercentage, initSize);
|
|
|
|
+ // all good
|
|
|
|
+ if (potentialSize.Width > 0 && potentialSize.Height > 0 && potentialSize.Width <= Constants.MaxCanvasSize && potentialSize.Height <= Constants.MaxCanvasSize)
|
|
|
|
+ return desiredPercentage;
|
|
|
|
+
|
|
|
|
+ // canvas too small
|
|
|
|
+ if (potentialSize.Width <= 0 || potentialSize.Height <= 0)
|
|
|
|
+ {
|
|
|
|
+ if (potentialSize.Width < potentialSize.Height)
|
|
|
|
+ return 100f / initSize.Width;
|
|
|
|
+ else
|
|
|
|
+ return 100f / initSize.Height;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // canvas too big
|
|
|
|
+ if (potentialSize.Width > potentialSize.Height)
|
|
|
|
+ return Constants.MaxCanvasSize * 100f / initSize.Width;
|
|
|
|
+ else
|
|
|
|
+ return Constants.MaxCanvasSize * 100f / initSize.Height;
|
|
|
|
+ }
|
|
|
|
+
|
|
private void OnSizeUpdate(bool widthUpdated)
|
|
private void OnSizeUpdate(bool widthUpdated)
|
|
{
|
|
{
|
|
if (!initSize.HasValue || !PreserveAspectRatio)
|
|
if (!initSize.HasValue || !PreserveAspectRatio)
|