PipeLineControl.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Media;
  4. using Avalonia.Threading;
  5. using System;
  6. namespace YZWater.Avalonia.Controls;
  7. /// <summary>
  8. /// 绠¢亾鎺т欢 - 妯℃嫙 PipeLine
  9. /// </summary>
  10. public class PipeLineControl : Control
  11. {
  12. public static readonly StyledProperty<bool> IsFlowProperty =
  13. AvaloniaProperty.Register<PipeLineControl, bool>(nameof(IsFlow), false);
  14. public static readonly StyledProperty<double> FlowSpeedProperty =
  15. AvaloniaProperty.Register<PipeLineControl, double>(nameof(FlowSpeed), 1.0);
  16. public static readonly StyledProperty<IBrush> PipeColorProperty =
  17. AvaloniaProperty.Register<PipeLineControl, IBrush>(nameof(PipeColor), new SolidColorBrush(Color.Parse("#4CAF50")));
  18. public static readonly StyledProperty<IBrush> WaterColorProperty =
  19. AvaloniaProperty.Register<PipeLineControl, IBrush>(nameof(WaterColor), new SolidColorBrush(Color.Parse("#2196F3")));
  20. public static readonly StyledProperty<double> PipeWidthProperty =
  21. AvaloniaProperty.Register<PipeLineControl, double>(nameof(PipeWidth), 20.0);
  22. public static readonly StyledProperty<bool> IsHorizontalProperty =
  23. AvaloniaProperty.Register<PipeLineControl, bool>(nameof(IsHorizontal), true);
  24. public bool IsFlow
  25. {
  26. get => GetValue(IsFlowProperty);
  27. set => SetValue(IsFlowProperty, value);
  28. }
  29. public double FlowSpeed
  30. {
  31. get => GetValue(FlowSpeedProperty);
  32. set => SetValue(FlowSpeedProperty, value);
  33. }
  34. public IBrush PipeColor
  35. {
  36. get => GetValue(PipeColorProperty);
  37. set => SetValue(PipeColorProperty, value);
  38. }
  39. public IBrush WaterColor
  40. {
  41. get => GetValue(WaterColorProperty);
  42. set => SetValue(WaterColorProperty, value);
  43. }
  44. public double PipeWidth
  45. {
  46. get => GetValue(PipeWidthProperty);
  47. set => SetValue(PipeWidthProperty, value);
  48. }
  49. public bool IsHorizontal
  50. {
  51. get => GetValue(IsHorizontalProperty);
  52. set => SetValue(IsHorizontalProperty, value);
  53. }
  54. private double _flowOffset = 0;
  55. private IDisposable? _timerSubscription;
  56. static PipeLineControl()
  57. {
  58. AffectsRender<PipeLineControl>(IsFlowProperty, FlowSpeedProperty, PipeColorProperty, WaterColorProperty, PipeWidthProperty, IsHorizontalProperty);
  59. }
  60. protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
  61. {
  62. base.OnAttachedToVisualTree(e);
  63. if (IsFlow)
  64. {
  65. StartAnimation();
  66. }
  67. }
  68. protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
  69. {
  70. base.OnDetachedFromVisualTree(e);
  71. StopAnimation();
  72. }
  73. protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
  74. {
  75. base.OnPropertyChanged(change);
  76. if (change.Property == IsFlowProperty)
  77. {
  78. if (IsFlow)
  79. {
  80. StartAnimation();
  81. }
  82. else
  83. {
  84. StopAnimation();
  85. _flowOffset = 0;
  86. }
  87. }
  88. }
  89. private void StartAnimation()
  90. {
  91. StopAnimation();
  92. _timerSubscription = DispatcherTimer.Run(() =>
  93. {
  94. _flowOffset += FlowSpeed;
  95. if (_flowOffset >= 20) _flowOffset -= 20;
  96. InvalidateVisual();
  97. return true; // 缁х画杩愯
  98. }, TimeSpan.FromMilliseconds(50));
  99. }
  100. private void StopAnimation()
  101. {
  102. _timerSubscription?.Dispose();
  103. _timerSubscription = null;
  104. }
  105. public override void Render(DrawingContext context)
  106. {
  107. base.Render(context);
  108. var bounds = new Rect(Bounds.Size);
  109. var pipeWidth = PipeWidth;
  110. if (IsHorizontal)
  111. {
  112. // 缁樺埗姘村钩绠¢亾
  113. var pipeRect = new Rect(0, (bounds.Height - pipeWidth) / 2, bounds.Width, pipeWidth);
  114. context.DrawRectangle(PipeColor, null, pipeRect);
  115. // 缁樺埗娴佸姩鏁堟灉
  116. if (IsFlow)
  117. {
  118. var dashPen = new Pen(WaterColor, pipeWidth * 0.6, new DashStyle(new double[] { 8, 12 }, _flowOffset));
  119. var y = bounds.Height / 2;
  120. context.DrawLine(dashPen, new Point(0, y), new Point(bounds.Width, y));
  121. }
  122. }
  123. else
  124. {
  125. // 缁樺埗鍨傜洿绠¢亾
  126. var pipeRect = new Rect((bounds.Width - pipeWidth) / 2, 0, pipeWidth, bounds.Height);
  127. context.DrawRectangle(PipeColor, null, pipeRect);
  128. // 缁樺埗娴佸姩鏁堟灉
  129. if (IsFlow)
  130. {
  131. var dashPen = new Pen(WaterColor, pipeWidth * 0.6, new DashStyle(new double[] { 8, 12 }, _flowOffset));
  132. var x = bounds.Width / 2;
  133. context.DrawLine(dashPen, new Point(x, 0), new Point(x, bounds.Height));
  134. }
  135. }
  136. }
  137. protected override Size MeasureOverride(Size availableSize)
  138. {
  139. return IsHorizontal ? new Size(80, 30) : new Size(30, 80);
  140. }
  141. }