2025-06-09 22:34:05 +02:00
|
|
|
|
using Avalonia.Controls.Primitives;
|
2024-05-26 20:32:29 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2025-06-09 22:34:05 +02:00
|
|
|
|
public partial class MapGraticule : TemplatedControl, 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-09 22:34:05 +02:00
|
|
|
|
ForegroundProperty.Changed.AddClassHandler<MapGraticule, IBrush>((graticule, e) => graticule.InvalidateVisual());
|
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-09 22:34:05 +02:00
|
|
|
|
var typeface = new Typeface(FontFamily, FontStyle, FontWeight, FontStretch);
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|