SizePicker.xaml.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using PixiEditor.Helpers;
  2. using PixiEditor.Models.Enums;
  3. using PixiEditor.ViewModels;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. namespace PixiEditor.Views
  7. {
  8. /// <summary>
  9. /// Interaction logic for SizePicker.xaml.
  10. /// </summary>
  11. public partial class SizePicker : UserControl
  12. {
  13. public static readonly DependencyProperty EditingEnabledProperty =
  14. DependencyProperty.Register(nameof(EditingEnabled), typeof(bool), typeof(SizePicker), new PropertyMetadata(true));
  15. public static readonly DependencyProperty ChosenWidthProperty =
  16. DependencyProperty.Register(nameof(ChosenWidth), typeof(int), typeof(SizePicker), new PropertyMetadata(1));
  17. public static readonly DependencyProperty ChosenHeightProperty =
  18. DependencyProperty.Register(nameof(ChosenHeight), typeof(int), typeof(SizePicker), new PropertyMetadata(1));
  19. public static readonly DependencyProperty NextControlProperty =
  20. DependencyProperty.Register(nameof(NextControl), typeof(FrameworkElement), typeof(SizePicker));
  21. public static readonly DependencyProperty ChosenPercentageSizeProperty =
  22. DependencyProperty.Register(nameof(ChosenPercentageSize), typeof(int), typeof(SizePicker), new PropertyMetadata(1, InputSizeChanged));
  23. public static readonly DependencyProperty SelectedUnitProperty =
  24. DependencyProperty.Register(nameof(SelectedUnit), typeof(SizeUnit), typeof(SizePicker), new PropertyMetadata(SizeUnit.Pixel));
  25. public static readonly DependencyProperty SizeUnitSelectionVisibilityProperty =
  26. DependencyProperty.Register(nameof(SizeUnitSelectionVisibility), typeof(Visibility), typeof(SizePicker), new PropertyMetadata(Visibility.Collapsed));
  27. System.Drawing.Size? initSize = null;
  28. public SizePicker()
  29. {
  30. InitializeComponent();
  31. EnableSizeEditors();
  32. }
  33. void OnLoad(object sender, RoutedEventArgs e)
  34. {
  35. var sizePicker = sender as SizePicker;
  36. sizePicker.initSize = new System.Drawing.Size(sizePicker.ChosenWidth, sizePicker.ChosenHeight);
  37. }
  38. private static void InputSizeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  39. {
  40. var sizePicker = d as SizePicker;
  41. if (!sizePicker.initSize.HasValue)
  42. return;
  43. var newValue = (int)e.NewValue;
  44. var newSize = SizeCalculator.CalcAbsoluteFromPercentage(newValue, sizePicker.initSize.Value);
  45. if (newSize.Width > sizePicker.MaxWidth || newSize.Width > sizePicker.MaxHeight)
  46. {
  47. newSize = new System.Drawing.Size(newSize.Width, newSize.Height);
  48. }
  49. d.SetValue(ChosenWidthProperty, newSize.Width);
  50. d.SetValue(ChosenHeightProperty, newSize.Height);
  51. }
  52. public bool EditingEnabled
  53. {
  54. get => (bool)GetValue(EditingEnabledProperty);
  55. set => SetValue(EditingEnabledProperty, value);
  56. }
  57. public int ChosenWidth
  58. {
  59. get => (int)GetValue(ChosenWidthProperty);
  60. set => SetValue(ChosenWidthProperty, value);
  61. }
  62. public int ChosenHeight
  63. {
  64. get => (int)GetValue(ChosenHeightProperty);
  65. set => SetValue(ChosenHeightProperty, value);
  66. }
  67. public int ChosenPercentageSize
  68. {
  69. get => (int)GetValue(ChosenPercentageSizeProperty);
  70. set => SetValue(ChosenPercentageSizeProperty, value);
  71. }
  72. public SizeUnit SelectedUnit
  73. {
  74. get => (SizeUnit)GetValue(SelectedUnitProperty);
  75. set => SetValue(SelectedUnitProperty, value);
  76. }
  77. public Visibility SizeUnitSelectionVisibility
  78. {
  79. get => (Visibility)GetValue(SizeUnitSelectionVisibilityProperty);
  80. set => SetValue(SizeUnitSelectionVisibilityProperty, value);
  81. }
  82. public FrameworkElement NextControl
  83. {
  84. get => (FrameworkElement)GetValue(NextControlProperty);
  85. set => SetValue(NextControlProperty, value);
  86. }
  87. public void FocusWidthPicker()
  88. {
  89. WidthPicker.Focus();
  90. }
  91. private void PercentageRb_Checked(object sender, RoutedEventArgs e)
  92. {
  93. EnableSizeEditors();
  94. }
  95. private void AbsoluteRb_Checked(object sender, RoutedEventArgs e)
  96. {
  97. EnableSizeEditors();
  98. }
  99. private void EnableSizeEditors()
  100. {
  101. if (PercentageSizePicker != null)
  102. PercentageSizePicker.IsEnabled = EditingEnabled && PercentageRb.IsChecked.Value;
  103. if (WidthPicker != null)
  104. WidthPicker.IsEnabled = EditingEnabled && !PercentageRb.IsChecked.Value;
  105. if (HeightPicker != null)
  106. HeightPicker.IsEnabled = EditingEnabled && !PercentageRb.IsChecked.Value;
  107. }
  108. }
  109. }