|
@@ -8,18 +8,12 @@ namespace YZWater.Avalonia.Controls;
|
|
|
|
|
|
|
|
public class GaugeControl : Control
|
|
public class GaugeControl : Control
|
|
|
{
|
|
{
|
|
|
- public static readonly StyledProperty<double> ValueProperty =
|
|
|
|
|
- AvaloniaProperty.Register<GaugeControl, double>(nameof(Value), 0.0);
|
|
|
|
|
- public static readonly StyledProperty<double> MinValueProperty =
|
|
|
|
|
- AvaloniaProperty.Register<GaugeControl, double>(nameof(MinValue), 0.0);
|
|
|
|
|
- public static readonly StyledProperty<double> MaxValueProperty =
|
|
|
|
|
- AvaloniaProperty.Register<GaugeControl, double>(nameof(MaxValue), 100.0);
|
|
|
|
|
- public static readonly StyledProperty<string> TitleProperty =
|
|
|
|
|
- AvaloniaProperty.Register<GaugeControl, string>(nameof(Title), "娴侀噺");
|
|
|
|
|
- public static readonly StyledProperty<string> UnitProperty =
|
|
|
|
|
- AvaloniaProperty.Register<GaugeControl, string>(nameof(Unit), "m鲁/h");
|
|
|
|
|
- public static readonly StyledProperty<IBrush> ValueColorProperty =
|
|
|
|
|
- AvaloniaProperty.Register<GaugeControl, IBrush>(nameof(ValueColor));
|
|
|
|
|
|
|
+ public static readonly StyledProperty<double> ValueProperty = AvaloniaProperty.Register<GaugeControl, double>(nameof(Value), 0.0);
|
|
|
|
|
+ public static readonly StyledProperty<double> MinValueProperty = AvaloniaProperty.Register<GaugeControl, double>(nameof(MinValue), 0.0);
|
|
|
|
|
+ public static readonly StyledProperty<double> MaxValueProperty = AvaloniaProperty.Register<GaugeControl, double>(nameof(MaxValue), 100.0);
|
|
|
|
|
+ public static readonly StyledProperty<string> TitleProperty = AvaloniaProperty.Register<GaugeControl, string>(nameof(Title), "娴侀噺");
|
|
|
|
|
+ public static readonly StyledProperty<string> UnitProperty = AvaloniaProperty.Register<GaugeControl, string>(nameof(Unit), "m鲁/h");
|
|
|
|
|
+ public static readonly StyledProperty<IBrush> ValueColorProperty = AvaloniaProperty.Register<GaugeControl, IBrush>(nameof(ValueColor));
|
|
|
|
|
|
|
|
public double Value { get => GetValue(ValueProperty); set => SetValue(ValueProperty, value); }
|
|
public double Value { get => GetValue(ValueProperty); set => SetValue(ValueProperty, value); }
|
|
|
public double MinValue { get => GetValue(MinValueProperty); set => SetValue(MinValueProperty, value); }
|
|
public double MinValue { get => GetValue(MinValueProperty); set => SetValue(MinValueProperty, value); }
|
|
@@ -30,6 +24,10 @@ public class GaugeControl : Control
|
|
|
|
|
|
|
|
static GaugeControl() { AffectsRender<GaugeControl>(ValueProperty, MinValueProperty, MaxValueProperty, TitleProperty, UnitProperty, ValueColorProperty); }
|
|
static GaugeControl() { AffectsRender<GaugeControl>(ValueProperty, MinValueProperty, MaxValueProperty, TitleProperty, UnitProperty, ValueColorProperty); }
|
|
|
|
|
|
|
|
|
|
+ protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) { base.OnAttachedToVisualTree(e); ThemeHelper.ThemeChanged += OnThemeChanged; }
|
|
|
|
|
+ protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e) { ThemeHelper.ThemeChanged -= OnThemeChanged; base.OnDetachedFromVisualTree(e); }
|
|
|
|
|
+ private void OnThemeChanged() => InvalidateVisual();
|
|
|
|
|
+
|
|
|
public override void Render(DrawingContext context)
|
|
public override void Render(DrawingContext context)
|
|
|
{
|
|
{
|
|
|
base.Render(context);
|
|
base.Render(context);
|
|
@@ -38,20 +36,14 @@ public class GaugeControl : Control
|
|
|
var radius = Math.Min(bounds.Width, bounds.Height) / 2 - 20;
|
|
var radius = Math.Min(bounds.Width, bounds.Height) / 2 - 20;
|
|
|
var typeface = new Typeface("Microsoft YaHei", FontStyle.Normal, FontWeight.Bold);
|
|
var typeface = new Typeface("Microsoft YaHei", FontStyle.Normal, FontWeight.Bold);
|
|
|
|
|
|
|
|
- // 鑳屾櫙
|
|
|
|
|
context.DrawEllipse(ThemeHelper.PanelBg(), null, new Rect(cx - radius - 5, cy - radius - 5, (radius + 5) * 2, (radius + 5) * 2));
|
|
context.DrawEllipse(ThemeHelper.PanelBg(), null, new Rect(cx - radius - 5, cy - radius - 5, (radius + 5) * 2, (radius + 5) * 2));
|
|
|
|
|
+ DrawArc(context, new Pen(ThemeHelper.Border(), 8), cx, cy, radius, 135, 270);
|
|
|
|
|
|
|
|
- // 寮х嚎鑳屾櫙
|
|
|
|
|
- var arcBg = new Pen(ThemeHelper.Border(), 8);
|
|
|
|
|
- DrawArc(context, arcBg, cx, cy, radius, 135, 270);
|
|
|
|
|
-
|
|
|
|
|
- // 鍊煎姬绾
|
|
|
|
|
var normalized = Math.Max(0, Math.Min(1, (Value - MinValue) / (MaxValue - MinValue)));
|
|
var normalized = Math.Max(0, Math.Min(1, (Value - MinValue) / (MaxValue - MinValue)));
|
|
|
var valueAngle = 135 + normalized * 270;
|
|
var valueAngle = 135 + normalized * 270;
|
|
|
var valueColor = ValueColor ?? ThemeHelper.Success();
|
|
var valueColor = ValueColor ?? ThemeHelper.Success();
|
|
|
DrawArc(context, new Pen(valueColor, 8), cx, cy, radius, 135, valueAngle - 135);
|
|
DrawArc(context, new Pen(valueColor, 8), cx, cy, radius, 135, valueAngle - 135);
|
|
|
|
|
|
|
|
- // 鍒诲害
|
|
|
|
|
for (int i = 0; i <= 10; i++)
|
|
for (int i = 0; i <= 10; i++)
|
|
|
{
|
|
{
|
|
|
var angle = 135 + (270.0 * i / 10);
|
|
var angle = 135 + (270.0 * i / 10);
|
|
@@ -63,21 +55,17 @@ public class GaugeControl : Control
|
|
|
new Point(cx + Math.Cos(rad) * radius, cy + Math.Sin(rad) * radius));
|
|
new Point(cx + Math.Cos(rad) * radius, cy + Math.Sin(rad) * radius));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 鎸囬拡
|
|
|
|
|
var pAngle = valueAngle * Math.PI / 180;
|
|
var pAngle = valueAngle * Math.PI / 180;
|
|
|
var pLen = radius * 0.7;
|
|
var pLen = radius * 0.7;
|
|
|
context.DrawLine(new Pen(valueColor, 3), new Point(cx, cy), new Point(cx + Math.Cos(pAngle) * pLen, cy + Math.Sin(pAngle) * pLen));
|
|
context.DrawLine(new Pen(valueColor, 3), new Point(cx, cy), new Point(cx + Math.Cos(pAngle) * pLen, cy + Math.Sin(pAngle) * pLen));
|
|
|
context.DrawEllipse(valueColor, null, new Rect(cx - 6, cy - 6, 12, 12));
|
|
context.DrawEllipse(valueColor, null, new Rect(cx - 6, cy - 6, 12, 12));
|
|
|
|
|
|
|
|
- // 鏍囬
|
|
|
|
|
var titleText = new FormattedText(Title, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, typeface, 12, ThemeHelper.TextPrimary());
|
|
var titleText = new FormattedText(Title, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, typeface, 12, ThemeHelper.TextPrimary());
|
|
|
context.DrawText(titleText, new Point(cx - titleText.Width / 2, bounds.Y + 5));
|
|
context.DrawText(titleText, new Point(cx - titleText.Width / 2, bounds.Y + 5));
|
|
|
|
|
|
|
|
- // 鏁板
|
|
|
|
|
var valText = new FormattedText($"{Value:F1}", CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("Microsoft YaHei", FontStyle.Normal, FontWeight.Bold), 20, ThemeHelper.TextPrimary());
|
|
var valText = new FormattedText($"{Value:F1}", CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("Microsoft YaHei", FontStyle.Normal, FontWeight.Bold), 20, ThemeHelper.TextPrimary());
|
|
|
context.DrawText(valText, new Point(cx - valText.Width / 2, cy + radius * 0.3));
|
|
context.DrawText(valText, new Point(cx - valText.Width / 2, cy + radius * 0.3));
|
|
|
|
|
|
|
|
- // 鍗曚綅
|
|
|
|
|
var unitText = new FormattedText(Unit, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("Microsoft YaHei"), 10, ThemeHelper.TextDisabled());
|
|
var unitText = new FormattedText(Unit, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("Microsoft YaHei"), 10, ThemeHelper.TextDisabled());
|
|
|
context.DrawText(unitText, new Point(cx - unitText.Width / 2, cy + radius * 0.3 + valText.Height + 2));
|
|
context.DrawText(unitText, new Point(cx - unitText.Width / 2, cy + radius * 0.3 + valText.Height + 2));
|
|
|
}
|
|
}
|