| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- using Avalonia;
- using Avalonia.Controls;
- using Avalonia.Media;
- using System;
- using System.Globalization;
- namespace YZWater.Avalonia.Controls;
- /// <summary>
- /// 闃闂ㄧ姸鎬佹灇涓
- /// </summary>
- public enum ValveStatus
- {
- /// <summary>
- /// 鍏抽棴
- /// </summary>
- Closed,
- /// <summary>
- /// 涓棿浣嶇疆
- /// </summary>
- Middle,
- /// <summary>
- /// 鎵撳紑
- /// </summary>
- Open
- }
- /// <summary>
- /// 闃闂ㄦ帶浠 - 妯℃嫙 HslSwitch
- /// </summary>
- public class ValveControl : Control
- {
- public static readonly StyledProperty<ValveStatus> StatusProperty =
- AvaloniaProperty.Register<ValveControl, ValveStatus>(nameof(Status), ValveStatus.Middle);
- public static readonly StyledProperty<string> TextProperty =
- AvaloniaProperty.Register<ValveControl, string>(nameof(Text), "闃闂");
- public static readonly StyledProperty<IBrush> ValveColorProperty =
- AvaloniaProperty.Register<ValveControl, IBrush>(nameof(ValveColor), new SolidColorBrush(Color.Parse("#FF5722")));
- public static readonly StyledProperty<IBrush> BackgroundColorProperty =
- AvaloniaProperty.Register<ValveControl, IBrush>(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<ValveControl>(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);
- }
- }
|