SizeInput.xaml.cs 3.2 KB

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