ValveControl.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Media;
  4. using System;
  5. using System.Globalization;
  6. namespace YZWater.Avalonia.Controls;
  7. /// <summary>
  8. /// 阀门状态枚举
  9. /// </summary>
  10. public enum ValveStatus
  11. {
  12. /// <summary>
  13. /// 关闭
  14. /// </summary>
  15. Closed,
  16. /// <summary>
  17. /// 中间位置
  18. /// </summary>
  19. Middle,
  20. /// <summary>
  21. /// 打开
  22. /// </summary>
  23. Open
  24. }
  25. /// <summary>
  26. /// 阀门控件 - 模拟 HslSwitch
  27. /// </summary>
  28. public class ValveControl : Control
  29. {
  30. public static readonly StyledProperty<ValveStatus> StatusProperty =
  31. AvaloniaProperty.Register<ValveControl, ValveStatus>(nameof(Status), ValveStatus.Middle);
  32. public static readonly StyledProperty<string> TextProperty =
  33. AvaloniaProperty.Register<ValveControl, string>(nameof(Text), "阀门");
  34. public static readonly StyledProperty<IBrush> ValveColorProperty =
  35. AvaloniaProperty.Register<ValveControl, IBrush>(nameof(ValveColor), new SolidColorBrush(Color.Parse("#FF5722")));
  36. public static readonly StyledProperty<IBrush> BackgroundColorProperty =
  37. AvaloniaProperty.Register<ValveControl, IBrush>(nameof(BackgroundColor), new SolidColorBrush(Color.Parse("#333333")));
  38. public ValveStatus Status
  39. {
  40. get => GetValue(StatusProperty);
  41. set => SetValue(StatusProperty, value);
  42. }
  43. public string Text
  44. {
  45. get => GetValue(TextProperty);
  46. set => SetValue(TextProperty, value);
  47. }
  48. public IBrush ValveColor
  49. {
  50. get => GetValue(ValveColorProperty);
  51. set => SetValue(ValveColorProperty, value);
  52. }
  53. public IBrush BackgroundColor
  54. {
  55. get => GetValue(BackgroundColorProperty);
  56. set => SetValue(BackgroundColorProperty, value);
  57. }
  58. static ValveControl()
  59. {
  60. AffectsRender<ValveControl>(StatusProperty, TextProperty, ValveColorProperty, BackgroundColorProperty);
  61. }
  62. public override void Render(DrawingContext context)
  63. {
  64. base.Render(context);
  65. var bounds = new Rect(Bounds.Size);
  66. var centerX = bounds.Width / 2;
  67. var centerY = bounds.Height / 2;
  68. var size = Math.Min(bounds.Width, bounds.Height) * 0.8;
  69. var typeface = new Typeface("Microsoft YaHei");
  70. // 绘制背景圆
  71. var backgroundRadius = size / 2;
  72. context.DrawEllipse(BackgroundColor, null, new Rect(
  73. centerX - backgroundRadius,
  74. centerY - backgroundRadius,
  75. backgroundRadius * 2,
  76. backgroundRadius * 2));
  77. // 绘制阀门手柄
  78. var handleLength = size * 0.35;
  79. var handleWidth = size * 0.15;
  80. var angle = Status switch
  81. {
  82. ValveStatus.Open => 0,
  83. ValveStatus.Middle => 45,
  84. ValveStatus.Closed => 90,
  85. _ => 45
  86. };
  87. // 简化绘制 - 不使用旋转,直接绘制矩形
  88. var handleRect = new Rect(
  89. centerX - handleWidth / 2,
  90. centerY - handleLength,
  91. handleWidth,
  92. handleLength * 2);
  93. context.DrawRectangle(ValveColor, null, handleRect);
  94. // 绘制状态指示器
  95. var indicatorSize = size * 0.15;
  96. var indicatorColor = Status switch
  97. {
  98. ValveStatus.Open => new SolidColorBrush(Colors.Green),
  99. ValveStatus.Middle => new SolidColorBrush(Colors.Yellow),
  100. ValveStatus.Closed => new SolidColorBrush(Colors.Red),
  101. _ => new SolidColorBrush(Colors.Gray)
  102. };
  103. context.DrawEllipse(indicatorColor, null, new Rect(
  104. centerX - indicatorSize / 2,
  105. centerY - backgroundRadius - indicatorSize - 5,
  106. indicatorSize,
  107. indicatorSize));
  108. // 绘制文字
  109. if (!string.IsNullOrEmpty(Text))
  110. {
  111. var foreground = new SolidColorBrush(Colors.White);
  112. var text = new FormattedText(Text, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, typeface, 10, foreground);
  113. var textPoint = new Point(
  114. centerX - text.Width / 2,
  115. bounds.Bottom - text.Height - 2);
  116. context.DrawText(text, textPoint);
  117. }
  118. }
  119. protected override Size MeasureOverride(Size availableSize)
  120. {
  121. return new Size(50, 70);
  122. }
  123. }