SizeInput.xaml.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using PixiEditor.Models.Enums;
  2. using System;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Input;
  6. namespace PixiEditor.Views
  7. {
  8. /// <summary>
  9. /// Interaction logic for SizeInput.xaml.
  10. /// </summary>
  11. public partial class SizeInput : UserControl
  12. {
  13. public static readonly DependencyProperty SizeProperty =
  14. DependencyProperty.Register(nameof(Size), typeof(int), typeof(SizeInput), new PropertyMetadata(1, InputSizeChanged));
  15. public static readonly DependencyProperty MaxSizeProperty =
  16. DependencyProperty.Register(nameof(MaxSize), typeof(int), typeof(SizeInput), new PropertyMetadata(int.MaxValue));
  17. public static readonly DependencyProperty BehaveLikeSmallEmbeddedFieldProperty =
  18. DependencyProperty.Register(nameof(BehaveLikeSmallEmbeddedField), typeof(bool), typeof(SizeInput), new PropertyMetadata(true));
  19. public static readonly DependencyProperty UnitProperty =
  20. DependencyProperty.Register(nameof(Unit), typeof(SizeUnit), typeof(SizeInput), new PropertyMetadata(SizeUnit.Pixel));
  21. public SizeInput()
  22. {
  23. InitializeComponent();
  24. }
  25. private void SizeInput_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
  26. {
  27. textBox.Focus();
  28. }
  29. public int Size
  30. {
  31. get => (int)GetValue(SizeProperty);
  32. set => SetValue(SizeProperty, value);
  33. }
  34. public int MaxSize
  35. {
  36. get => (int)GetValue(MaxSizeProperty);
  37. set => SetValue(MaxSizeProperty, value);
  38. }
  39. public bool BehaveLikeSmallEmbeddedField
  40. {
  41. get => (bool)GetValue(BehaveLikeSmallEmbeddedFieldProperty);
  42. set => SetValue(BehaveLikeSmallEmbeddedFieldProperty, value);
  43. }
  44. public void FocusAndSelect()
  45. {
  46. textBox.Focus();
  47. textBox.SelectAll();
  48. }
  49. private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  50. {
  51. if (!textBox.IsFocused)
  52. textBox.Focus();
  53. Point pos = Mouse.GetPosition(textBox);
  54. int charIndex = textBox.GetCharacterIndexFromPoint(pos, true);
  55. var charRect = textBox.GetRectFromCharacterIndex(charIndex);
  56. double middleX = (charRect.Left + charRect.Right) / 2;
  57. if (pos.X > middleX)
  58. textBox.CaretIndex = charIndex + 1;
  59. else
  60. textBox.CaretIndex = charIndex;
  61. e.Handled = true;
  62. }
  63. public SizeUnit Unit
  64. {
  65. get => (SizeUnit)GetValue(UnitProperty);
  66. set => SetValue(UnitProperty, value);
  67. }
  68. private static void InputSizeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  69. {
  70. int newValue = (int)e.NewValue;
  71. int maxSize = (int)d.GetValue(MaxSizeProperty);
  72. if (newValue > maxSize)
  73. {
  74. d.SetValue(SizeProperty, maxSize);
  75. return;
  76. }
  77. else if (newValue <= 0)
  78. {
  79. d.SetValue(SizeProperty, 1);
  80. return;
  81. }
  82. }
  83. private void Border_MouseWheel(object sender, MouseWheelEventArgs e)
  84. {
  85. int step = e.Delta / 100;
  86. if (Keyboard.IsKeyDown(Key.LeftShift))
  87. {
  88. Size += step * 2;
  89. }
  90. else if (Keyboard.IsKeyDown(Key.LeftCtrl))
  91. {
  92. if (step < 0)
  93. {
  94. Size /= 2;
  95. }
  96. else
  97. {
  98. Size *= 2;
  99. }
  100. }
  101. else
  102. {
  103. Size += step;
  104. }
  105. }
  106. }
  107. }