XAML-Map-Control/MapControl/Avalonia/MapGraticule.Avalonia.cs

113 lines
3.6 KiB
C#
Raw Normal View History

2025-08-19 19:43:02 +02:00
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Documents;
using Avalonia.Media;
using System.Collections.Generic;
2024-05-26 20:32:29 +02:00
using System.Globalization;
using System.Linq;
namespace MapControl
{
2025-06-10 09:27:10 +02:00
public partial class MapGraticule : Control, IMapElement
2024-05-26 20:32:29 +02:00
{
2025-06-09 22:34:05 +02:00
static MapGraticule()
2024-05-26 20:32:29 +02:00
{
2025-06-10 09:27:10 +02:00
AffectsRender<MapGraticule>(ForegroundProperty);
}
public static readonly StyledProperty<IBrush> ForegroundProperty =
TextElement.ForegroundProperty.AddOwner<MapGraticule>();
2025-06-10 09:27:10 +02:00
public static readonly StyledProperty<FontFamily> FontFamilyProperty =
TextElement.FontFamilyProperty.AddOwner<MapGraticule>();
2025-06-10 09:27:10 +02:00
public static readonly StyledProperty<double> FontSizeProperty =
TextElement.FontSizeProperty.AddOwner<MapGraticule>(new StyledPropertyMetadata<double>(12d));
2024-05-26 20:32:29 +02:00
private MapBase parentMap;
/// <summary>
/// Implements IMapElement.ParentMap.
/// </summary>
public MapBase ParentMap
{
get => parentMap;
set
{
if (parentMap != null)
{
parentMap.ViewportChanged -= OnViewportChanged;
}
parentMap = value;
if (parentMap != null)
{
parentMap.ViewportChanged += OnViewportChanged;
}
}
}
private void OnViewportChanged(object sender, ViewportChangedEventArgs e)
{
InvalidateVisual();
}
public override void Render(DrawingContext drawingContext)
{
if (parentMap != null)
{
var pathGeometry = new PathGeometry();
var labels = DrawGraticule(pathGeometry.Figures);
var pen = new Pen
{
Brush = Foreground,
Thickness = StrokeThickness,
};
drawingContext.DrawGeometry(null, pen, pathGeometry);
if (labels.Count > 0)
{
2025-06-10 09:27:10 +02:00
var typeface = new Typeface(FontFamily, FontStyle.Normal, FontWeight.Normal, FontStretch.Normal);
2024-05-26 20:32:29 +02:00
foreach (var label in labels)
{
var latText = new FormattedText(label.LatitudeText,
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground);
var lonText = new FormattedText(label.LongitudeText,
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground);
var x = StrokeThickness / 2d + 2d;
var y1 = -StrokeThickness / 2d - latText.Height;
var y2 = StrokeThickness / 2d;
using var pushState = drawingContext.PushTransform(
Matrix.CreateRotation(Matrix.ToRadians(label.Rotation)) *
Matrix.CreateTranslation(label.X, label.Y));
drawingContext.DrawText(latText, new Point(x, y1));
drawingContext.DrawText(lonText, new Point(x, y2));
}
}
}
}
private static PathFigure CreatePolylineFigure(IEnumerable<Point> points)
{
var figure = new PathFigure
{
StartPoint = points.First(),
2024-05-29 12:02:55 +02:00
IsClosed = false,
2024-05-26 20:32:29 +02:00
IsFilled = false
};
figure.Segments.Add(new PolyLineSegment(points.Skip(1)));
return figure;
}
}
}