123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- using PixiEditor.Helpers;
- using PixiEditor.Models.Enums;
- using PixiEditor.ViewModels;
- using System.Windows;
- using System.Windows.Controls;
- namespace PixiEditor.Views
- {
- /// <summary>
- /// Interaction logic for SizePicker.xaml.
- /// </summary>
- public partial class SizePicker : UserControl
- {
- public static readonly DependencyProperty EditingEnabledProperty =
- DependencyProperty.Register(nameof(EditingEnabled), typeof(bool), typeof(SizePicker), new PropertyMetadata(true));
- public static readonly DependencyProperty ChosenWidthProperty =
- DependencyProperty.Register(nameof(ChosenWidth), typeof(int), typeof(SizePicker), new PropertyMetadata(1));
- public static readonly DependencyProperty ChosenHeightProperty =
- DependencyProperty.Register(nameof(ChosenHeight), typeof(int), typeof(SizePicker), new PropertyMetadata(1));
- public static readonly DependencyProperty NextControlProperty =
- DependencyProperty.Register(nameof(NextControl), typeof(FrameworkElement), typeof(SizePicker));
- public static readonly DependencyProperty ChosenPercentageSizeProperty =
- DependencyProperty.Register(nameof(ChosenPercentageSize), typeof(int), typeof(SizePicker), new PropertyMetadata(1, InputSizeChanged));
- public static readonly DependencyProperty SelectedUnitProperty =
- DependencyProperty.Register(nameof(SelectedUnit), typeof(SizeUnit), typeof(SizePicker), new PropertyMetadata(SizeUnit.Pixel));
- public static readonly DependencyProperty SizeUnitSelectionVisibilityProperty =
- DependencyProperty.Register(nameof(SizeUnitSelectionVisibility), typeof(Visibility), typeof(SizePicker), new PropertyMetadata(Visibility.Collapsed));
- System.Drawing.Size? initSize = null;
- public SizePicker()
- {
- InitializeComponent();
- EnableSizeEditors();
- }
- void OnLoad(object sender, RoutedEventArgs e)
- {
- var sizePicker = sender as SizePicker;
- sizePicker.initSize = new System.Drawing.Size(sizePicker.ChosenWidth, sizePicker.ChosenHeight);
- }
- 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 > sizePicker.MaxWidth || newSize.Width > sizePicker.MaxHeight)
- {
- newSize = new System.Drawing.Size(newSize.Width, newSize.Height);
- }
-
- d.SetValue(ChosenWidthProperty, newSize.Width);
- d.SetValue(ChosenHeightProperty, newSize.Height);
- }
- public bool EditingEnabled
- {
- get => (bool)GetValue(EditingEnabledProperty);
- set => SetValue(EditingEnabledProperty, value);
- }
- public int ChosenWidth
- {
- get => (int)GetValue(ChosenWidthProperty);
- set => SetValue(ChosenWidthProperty, value);
- }
- public int ChosenHeight
- {
- get => (int)GetValue(ChosenHeightProperty);
- set => SetValue(ChosenHeightProperty, value);
- }
- public int ChosenPercentageSize
- {
- get => (int)GetValue(ChosenPercentageSizeProperty);
- set => SetValue(ChosenPercentageSizeProperty, value);
- }
- public SizeUnit SelectedUnit
- {
- get => (SizeUnit)GetValue(SelectedUnitProperty);
- set => SetValue(SelectedUnitProperty, value);
- }
- public Visibility SizeUnitSelectionVisibility
- {
- get => (Visibility)GetValue(SizeUnitSelectionVisibilityProperty);
- set => SetValue(SizeUnitSelectionVisibilityProperty, value);
- }
-
- public FrameworkElement NextControl
- {
- get => (FrameworkElement)GetValue(NextControlProperty);
- set => SetValue(NextControlProperty, value);
- }
- public void FocusWidthPicker()
- {
- WidthPicker.Focus();
- }
-
- private void PercentageRb_Checked(object sender, RoutedEventArgs e)
- {
- EnableSizeEditors();
- }
- private void AbsoluteRb_Checked(object sender, RoutedEventArgs e)
- {
- EnableSizeEditors();
- }
- private void EnableSizeEditors()
- {
- if (PercentageSizePicker != null)
- PercentageSizePicker.IsEnabled = EditingEnabled && PercentageRb.IsChecked.Value;
- if (WidthPicker != null)
- WidthPicker.IsEnabled = EditingEnabled && !PercentageRb.IsChecked.Value;
- if (HeightPicker != null)
- HeightPicker.IsEnabled = EditingEnabled && !PercentageRb.IsChecked.Value;
- }
- }
- }
|