SizePicker.xaml.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using PixiEditor.Helpers;
  2. using System;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. namespace PixiEditor.Views
  6. {
  7. public partial class SizePicker : UserControl
  8. {
  9. public static readonly DependencyProperty EditingEnabledProperty =
  10. DependencyProperty.Register(nameof(EditingEnabled), typeof(bool), typeof(SizePicker), new PropertyMetadata(true));
  11. public static readonly DependencyProperty PreserveAspectRatioProperty =
  12. DependencyProperty.Register(nameof(PreserveAspectRatio), typeof(bool), typeof(SizePicker), new PropertyMetadata(true));
  13. public static readonly DependencyProperty ChosenWidthProperty =
  14. DependencyProperty.Register(nameof(ChosenWidth), typeof(int), typeof(SizePicker), new PropertyMetadata(1));
  15. public static readonly DependencyProperty ChosenHeightProperty =
  16. DependencyProperty.Register(nameof(ChosenHeight), typeof(int), typeof(SizePicker), new PropertyMetadata(1));
  17. public bool EditingEnabled
  18. {
  19. get => (bool)GetValue(EditingEnabledProperty);
  20. set => SetValue(EditingEnabledProperty, value);
  21. }
  22. public int ChosenWidth
  23. {
  24. get => (int)GetValue(ChosenWidthProperty);
  25. set => SetValue(ChosenWidthProperty, value);
  26. }
  27. public int ChosenHeight
  28. {
  29. get => (int)GetValue(ChosenHeightProperty);
  30. set => SetValue(ChosenHeightProperty, value);
  31. }
  32. public bool PreserveAspectRatio
  33. {
  34. get => (bool)GetValue(PreserveAspectRatioProperty);
  35. set => SetValue(PreserveAspectRatioProperty, value);
  36. }
  37. public RelayCommand LoadedCommand { get; private set; }
  38. public RelayCommand WidthLostFocusCommand { get; private set; }
  39. public RelayCommand HeightLostFocusCommand { get; private set; }
  40. private bool initialValuesLoaded = false;
  41. private int initW;
  42. private int initH;
  43. public SizePicker()
  44. {
  45. LoadedCommand = new(AfterLoaded);
  46. WidthLostFocusCommand = new(WidthLostFocus);
  47. HeightLostFocusCommand = new(HeightLostFocus);
  48. InitializeComponent();
  49. }
  50. public void FocusWidthPicker()
  51. {
  52. WidthPicker.FocusAndSelect();
  53. }
  54. private void AfterLoaded(object parameter)
  55. {
  56. initW = ChosenWidth;
  57. initH = ChosenHeight;
  58. initialValuesLoaded = true;
  59. }
  60. private void WidthLostFocus(object param) => OnSizeUpdate(true);
  61. private void HeightLostFocus(object param) => OnSizeUpdate(false);
  62. private void OnSizeUpdate(bool widthUpdated)
  63. {
  64. if (!initialValuesLoaded || !PreserveAspectRatio)
  65. return;
  66. if (widthUpdated)
  67. {
  68. ChosenHeight = Math.Clamp(ChosenWidth * initH / initW, 1, HeightPicker.MaxSize);
  69. }
  70. else
  71. {
  72. ChosenWidth = Math.Clamp(ChosenHeight * initW / initH, 1, WidthPicker.MaxSize);
  73. }
  74. }
  75. }
  76. }