using Avalonia;
using Avalonia.Controls;
using Avalonia.Media;
using System;
using System.Globalization;
namespace YZWater.Avalonia.Controls;
///
/// 阀门状态枚举
///
public enum ValveStatus
{
///
/// 关闭
///
Closed,
///
/// 中间位置
///
Middle,
///
/// 打开
///
Open
}
///
/// 阀门控件 - 模拟 HslSwitch
///
public class ValveControl : Control
{
public static readonly StyledProperty StatusProperty =
AvaloniaProperty.Register(nameof(Status), ValveStatus.Middle);
public static readonly StyledProperty TextProperty =
AvaloniaProperty.Register(nameof(Text), "阀门");
public static readonly StyledProperty ValveColorProperty =
AvaloniaProperty.Register(nameof(ValveColor), new SolidColorBrush(Color.Parse("#FF5722")));
public static readonly StyledProperty BackgroundColorProperty =
AvaloniaProperty.Register(nameof(BackgroundColor), new SolidColorBrush(Color.Parse("#333333")));
public ValveStatus Status
{
get => GetValue(StatusProperty);
set => SetValue(StatusProperty, value);
}
public string Text
{
get => GetValue(TextProperty);
set => SetValue(TextProperty, value);
}
public IBrush ValveColor
{
get => GetValue(ValveColorProperty);
set => SetValue(ValveColorProperty, value);
}
public IBrush BackgroundColor
{
get => GetValue(BackgroundColorProperty);
set => SetValue(BackgroundColorProperty, value);
}
static ValveControl()
{
AffectsRender(StatusProperty, TextProperty, ValveColorProperty, BackgroundColorProperty);
}
public override void Render(DrawingContext context)
{
base.Render(context);
var bounds = new Rect(Bounds.Size);
var centerX = bounds.Width / 2;
var centerY = bounds.Height / 2;
var size = Math.Min(bounds.Width, bounds.Height) * 0.8;
var typeface = new Typeface("Microsoft YaHei");
// 绘制背景圆
var backgroundRadius = size / 2;
context.DrawEllipse(BackgroundColor, null, new Rect(
centerX - backgroundRadius,
centerY - backgroundRadius,
backgroundRadius * 2,
backgroundRadius * 2));
// 绘制阀门手柄
var handleLength = size * 0.35;
var handleWidth = size * 0.15;
var angle = Status switch
{
ValveStatus.Open => 0,
ValveStatus.Middle => 45,
ValveStatus.Closed => 90,
_ => 45
};
// 简化绘制 - 不使用旋转,直接绘制矩形
var handleRect = new Rect(
centerX - handleWidth / 2,
centerY - handleLength,
handleWidth,
handleLength * 2);
context.DrawRectangle(ValveColor, null, handleRect);
// 绘制状态指示器
var indicatorSize = size * 0.15;
var indicatorColor = Status switch
{
ValveStatus.Open => new SolidColorBrush(Colors.Green),
ValveStatus.Middle => new SolidColorBrush(Colors.Yellow),
ValveStatus.Closed => new SolidColorBrush(Colors.Red),
_ => new SolidColorBrush(Colors.Gray)
};
context.DrawEllipse(indicatorColor, null, new Rect(
centerX - indicatorSize / 2,
centerY - backgroundRadius - indicatorSize - 5,
indicatorSize,
indicatorSize));
// 绘制文字
if (!string.IsNullOrEmpty(Text))
{
var foreground = new SolidColorBrush(Colors.White);
var text = new FormattedText(Text, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, typeface, 10, foreground);
var textPoint = new Point(
centerX - text.Width / 2,
bounds.Bottom - text.Height - 2);
context.DrawText(text, textPoint);
}
}
protected override Size MeasureOverride(Size availableSize)
{
return new Size(50, 70);
}
}