PumpControl.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Media;
  4. using Avalonia.Threading;
  5. using System;
  6. using System.Globalization;
  7. namespace YZWater.Avalonia.Controls;
  8. /// <summary>
  9. /// 娉垫帶浠 - 妯℃嫙 HslPumpOne锛堜紭鍖栫増锛
  10. /// </summary>
  11. public class PumpControl : Control
  12. {
  13. public static readonly StyledProperty<bool> IsRunningProperty =
  14. AvaloniaProperty.Register<PumpControl, bool>(nameof(IsRunning), false);
  15. public static readonly StyledProperty<double> SpeedProperty =
  16. AvaloniaProperty.Register<PumpControl, double>(nameof(Speed), 0.0);
  17. public static readonly StyledProperty<double> FrequencyProperty =
  18. AvaloniaProperty.Register<PumpControl, double>(nameof(Frequency), 50.0);
  19. public static readonly StyledProperty<string> TextProperty =
  20. AvaloniaProperty.Register<PumpControl, string>(nameof(Text), string.Empty);
  21. public static readonly StyledProperty<IBrush> RunningColorProperty =
  22. AvaloniaProperty.Register<PumpControl, IBrush>(nameof(RunningColor), new SolidColorBrush(Color.Parse("#4CAF50")));
  23. public static readonly StyledProperty<IBrush> StoppedColorProperty =
  24. AvaloniaProperty.Register<PumpControl, IBrush>(nameof(StoppedColor), new SolidColorBrush(Color.Parse("#607D8B")));
  25. public static readonly StyledProperty<IBrush> FaultColorProperty =
  26. AvaloniaProperty.Register<PumpControl, IBrush>(nameof(FaultColor), new SolidColorBrush(Color.Parse("#F44336")));
  27. public bool IsRunning
  28. {
  29. get => GetValue(IsRunningProperty);
  30. set => SetValue(IsRunningProperty, value);
  31. }
  32. public double Speed
  33. {
  34. get => GetValue(SpeedProperty);
  35. set => SetValue(SpeedProperty, value);
  36. }
  37. public double Frequency
  38. {
  39. get => GetValue(FrequencyProperty);
  40. set => SetValue(FrequencyProperty, value);
  41. }
  42. public string Text
  43. {
  44. get => GetValue(TextProperty);
  45. set => SetValue(TextProperty, value);
  46. }
  47. public IBrush RunningColor
  48. {
  49. get => GetValue(RunningColorProperty);
  50. set => SetValue(RunningColorProperty, value);
  51. }
  52. public IBrush StoppedColor
  53. {
  54. get => GetValue(StoppedColorProperty);
  55. set => SetValue(StoppedColorProperty, value);
  56. }
  57. public IBrush FaultColor
  58. {
  59. get => GetValue(FaultColorProperty);
  60. set => SetValue(FaultColorProperty, value);
  61. }
  62. private double _rotationAngle = 0;
  63. private IDisposable? _timerSubscription;
  64. static PumpControl()
  65. {
  66. AffectsRender<PumpControl>(IsRunningProperty, SpeedProperty, FrequencyProperty,
  67. TextProperty, RunningColorProperty, StoppedColorProperty, FaultColorProperty);
  68. }
  69. protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
  70. {
  71. base.OnAttachedToVisualTree(e);
  72. if (IsRunning)
  73. {
  74. StartAnimation();
  75. }
  76. }
  77. protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
  78. {
  79. base.OnDetachedFromVisualTree(e);
  80. StopAnimation();
  81. }
  82. protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
  83. {
  84. base.OnPropertyChanged(change);
  85. if (change.Property == IsRunningProperty)
  86. {
  87. if (IsRunning)
  88. {
  89. StartAnimation();
  90. }
  91. else
  92. {
  93. StopAnimation();
  94. _rotationAngle = 0;
  95. }
  96. InvalidateVisual();
  97. }
  98. }
  99. private void StartAnimation()
  100. {
  101. StopAnimation();
  102. _timerSubscription = DispatcherTimer.Run(() =>
  103. {
  104. _rotationAngle += Frequency / 20.0;
  105. if (_rotationAngle >= 360) _rotationAngle -= 360;
  106. InvalidateVisual();
  107. return true;
  108. }, TimeSpan.FromMilliseconds(30));
  109. }
  110. private void StopAnimation()
  111. {
  112. _timerSubscription?.Dispose();
  113. _timerSubscription = null;
  114. }
  115. public override void Render(DrawingContext context)
  116. {
  117. base.Render(context);
  118. var bounds = new Rect(Bounds.Size);
  119. var centerX = bounds.Width / 2;
  120. var centerY = bounds.Height / 2 - 5;
  121. var radius = Math.Min(bounds.Width, bounds.Height) / 2 - 15;
  122. var typeface = new Typeface("Microsoft YaHei", FontStyle.Normal, FontWeight.Bold);
  123. // 纭畾鐘舵侀鑹
  124. var statusColor = IsRunning ? RunningColor : StoppedColor;
  125. var statusBrush = statusColor as SolidColorBrush ?? new SolidColorBrush(Colors.Gray);
  126. // 缁樺埗澶栧湀鍙戝厜鏁堟灉锛堣繍琛屾椂锛
  127. if (IsRunning)
  128. {
  129. var glowBrush = new SolidColorBrush(statusBrush.Color, 0.2);
  130. context.DrawEllipse(glowBrush, null, new Rect(
  131. centerX - radius - 8,
  132. centerY - radius - 8,
  133. (radius + 8) * 2,
  134. (radius + 8) * 2));
  135. }
  136. // 缁樺埗澶栧3
  137. var outerPen = new Pen(new SolidColorBrush(Color.Parse("#455A64")), 3);
  138. context.DrawEllipse(null, outerPen, new Rect(
  139. centerX - radius,
  140. centerY - radius,
  141. radius * 2,
  142. radius * 2));
  143. // 缁樺埗鍐呴儴鑳屾櫙
  144. var innerBg = new SolidColorBrush(Color.Parse("#1E272E"));
  145. context.DrawEllipse(innerBg, null, new Rect(
  146. centerX - radius + 3,
  147. centerY - radius + 3,
  148. (radius - 3) * 2,
  149. (radius - 3) * 2));
  150. // 缁樺埗娉靛彾鐗
  151. var bladeCount = 6;
  152. var bladeLength = radius * 0.65;
  153. var bladeWidth = radius * 0.18;
  154. for (int i = 0; i < bladeCount; i++)
  155. {
  156. var angle = _rotationAngle + (360.0 / bladeCount) * i;
  157. var radians = angle * Math.PI / 180;
  158. var bladeX = centerX + Math.Cos(radians) * bladeLength * 0.5;
  159. var bladeY = centerY + Math.Sin(radians) * bladeLength * 0.5;
  160. // 缁樺埗鍙剁墖锛堝甫娓愬彉鏁堟灉锛
  161. var bladeBrush = new SolidColorBrush(IsRunning
  162. ? Color.FromArgb(200, statusBrush.Color.R, statusBrush.Color.G, statusBrush.Color.B)
  163. : Color.Parse("#546E7A"));
  164. var bladePath = new StreamGeometry();
  165. using (var ctx = bladePath.Open())
  166. {
  167. ctx.BeginFigure(new Point(bladeX - bladeWidth / 2, bladeY - bladeLength / 2), true);
  168. ctx.LineTo(new Point(bladeX + bladeWidth / 2, bladeY - bladeLength / 2));
  169. ctx.LineTo(new Point(bladeX + bladeWidth / 2, bladeY + bladeLength / 2));
  170. ctx.LineTo(new Point(bladeX - bladeWidth / 2, bladeY + bladeLength / 2));
  171. ctx.EndFigure(true);
  172. }
  173. // 鏃嬭浆鍙剁墖 - 浣跨敤鐭╅樀鍙樻崲
  174. var rotationRadians = angle * Math.PI / 180;
  175. var cos = Math.Cos(rotationRadians);
  176. var sin = Math.Sin(rotationRadians);
  177. var transform = new Matrix(cos, sin, -sin, cos, bladeX - bladeX * cos + bladeY * sin, bladeY - bladeX * sin - bladeY * cos);
  178. using (context.PushTransform(transform))
  179. {
  180. context.DrawGeometry(bladeBrush, null, bladePath);
  181. }
  182. }
  183. // 缁樺埗涓績鍦嗭紙甯︽笎鍙橈級
  184. var centerRadius = radius * 0.25;
  185. var centerBrush = new SolidColorBrush(IsRunning ? statusBrush.Color : Color.Parse("#455A64"));
  186. context.DrawEllipse(centerBrush, null, new Rect(
  187. centerX - centerRadius,
  188. centerY - centerRadius,
  189. centerRadius * 2,
  190. centerRadius * 2));
  191. // 涓績楂樺厜
  192. var highlightBrush = new SolidColorBrush(Colors.White, 0.3);
  193. context.DrawEllipse(highlightBrush, null, new Rect(
  194. centerX - centerRadius * 0.5,
  195. centerY - centerRadius * 0.5,
  196. centerRadius,
  197. centerRadius));
  198. // 缁樺埗鐘舵佹寚绀虹伅
  199. var indicatorSize = 8;
  200. var indicatorX = bounds.Width - 20;
  201. var indicatorY = 10;
  202. var indicatorBrush = IsRunning
  203. ? new SolidColorBrush(Color.Parse("#4CAF50"))
  204. : new SolidColorBrush(Color.Parse("#78909C"));
  205. // 鎸囩ず鐏彂鍏
  206. if (IsRunning)
  207. {
  208. context.DrawEllipse(new SolidColorBrush(indicatorBrush.Color, 0.3), null,
  209. new Rect(indicatorX - 4, indicatorY - 4, 16, 16));
  210. }
  211. context.DrawEllipse(indicatorBrush, null,
  212. new Rect(indicatorX, indicatorY, indicatorSize, indicatorSize));
  213. // 缁樺埗鏂囧瓧鏍囩
  214. if (!string.IsNullOrEmpty(Text))
  215. {
  216. var textBrush = new SolidColorBrush(Colors.White);
  217. var text = new FormattedText(Text, CultureInfo.CurrentCulture,
  218. FlowDirection.LeftToRight, typeface, 11, textBrush);
  219. var textPoint = new Point(
  220. centerX - text.Width / 2,
  221. bounds.Bottom - text.Height - 5);
  222. context.DrawText(text, textPoint);
  223. }
  224. // 缁樺埗棰戠巼鏄剧ず
  225. if (IsRunning)
  226. {
  227. var freqStr = $"{Frequency:F0}Hz";
  228. var freqText = new FormattedText(freqStr, CultureInfo.CurrentCulture,
  229. FlowDirection.LeftToRight, new Typeface("Microsoft YaHei"), 9,
  230. new SolidColorBrush(statusBrush.Color));
  231. context.DrawText(freqText, new Point(centerX - freqText.Width / 2, centerY + radius + 5));
  232. }
  233. }
  234. protected override Size MeasureOverride(Size availableSize)
  235. {
  236. return new Size(70, 80);
  237. }
  238. }