NumberInput.xaml.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Input;
  6. using PixiEditor.Models.Controllers.Shortcuts;
  7. namespace PixiEditor.Views
  8. {
  9. /// <summary>
  10. /// Interaction logic for NumerInput.xaml.
  11. /// </summary>
  12. public partial class NumberInput : UserControl
  13. {
  14. // Using a DependencyProperty as the backing store for Value. This enables animation, styling, binding, etc...
  15. public static readonly DependencyProperty ValueProperty =
  16. DependencyProperty.Register(
  17. nameof(Value),
  18. typeof(float),
  19. typeof(NumberInput),
  20. new PropertyMetadata(0f, OnValueChanged));
  21. // Using a DependencyProperty as the backing store for Min. This enables animation, styling, binding, etc...
  22. public static readonly DependencyProperty MinProperty =
  23. DependencyProperty.Register(
  24. nameof(Min),
  25. typeof(float),
  26. typeof(NumberInput),
  27. new PropertyMetadata(float.NegativeInfinity));
  28. // Using a DependencyProperty as the backing store for Max. This enables animation, styling, binding, etc...
  29. public static readonly DependencyProperty MaxProperty =
  30. DependencyProperty.Register(
  31. nameof(Max),
  32. typeof(float),
  33. typeof(NumberInput),
  34. new PropertyMetadata(float.PositiveInfinity));
  35. private readonly Regex regex = new Regex("^[.][0-9]+$|^[0-9]*[.]{0,1}[0-9]*$", RegexOptions.Compiled);
  36. public NumberInput()
  37. {
  38. InitializeComponent();
  39. }
  40. public float Value
  41. {
  42. get => (float)GetValue(ValueProperty);
  43. set => SetValue(ValueProperty, value);
  44. }
  45. public float Min
  46. {
  47. get => (float)GetValue(MinProperty);
  48. set => SetValue(MinProperty, value);
  49. }
  50. public float Max
  51. {
  52. get => (float)GetValue(MaxProperty);
  53. set => SetValue(MaxProperty, value);
  54. }
  55. private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  56. {
  57. NumberInput input = (NumberInput)d;
  58. input.Value = Math.Clamp((float)e.NewValue, input.Min, input.Max);
  59. }
  60. private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
  61. {
  62. e.Handled = !regex.IsMatch((sender as TextBox).Text.Insert((sender as TextBox).SelectionStart, e.Text));
  63. }
  64. private void TextBox_MouseWheel(object sender, MouseWheelEventArgs e)
  65. {
  66. int step = e.Delta / 100;
  67. if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
  68. {
  69. float multiplier = (Max - Min) * 0.1f;
  70. Value += step * multiplier;
  71. }
  72. else if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
  73. {
  74. Value += step / 2f;
  75. }
  76. else
  77. {
  78. Value += step;
  79. }
  80. }
  81. }
  82. }