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

106 lines
3.4 KiB
C#
Raw Normal View History

2025-02-27 18:46:32 +01:00
using System.Collections.Generic;
using System.Globalization;
2022-03-04 22:28:18 +01:00
using System.Linq;
using System.Windows;
2024-05-26 20:32:29 +02:00
using System.Windows.Controls;
using System.Windows.Media;
namespace MapControl
{
2024-05-26 20:32:29 +02:00
public partial class MapGraticule : Control, IMapElement
{
2024-05-26 20:32:29 +02:00
public static readonly DependencyProperty StrokeThicknessProperty =
DependencyPropertyHelper.Register<MapGraticule, double>(nameof(StrokeThickness), 0.5);
public double StrokeThickness
{
get => (double)GetValue(StrokeThicknessProperty);
set => SetValue(StrokeThicknessProperty, value);
}
private MapBase parentMap;
/// <summary>
/// Implements IMapElement.ParentMap.
/// </summary>
public MapBase ParentMap
{
2024-05-26 20:32:29 +02:00
get => parentMap;
set
{
if (parentMap != null)
{
parentMap.ViewportChanged -= OnViewportChanged;
}
parentMap = value;
if (parentMap != null)
{
parentMap.ViewportChanged += OnViewportChanged;
}
}
}
2024-05-26 20:32:29 +02:00
private void OnViewportChanged(object sender, ViewportChangedEventArgs e)
{
InvalidateVisual();
}
protected override void OnRender(DrawingContext drawingContext)
{
2024-05-26 20:32:29 +02:00
if (parentMap != null)
{
2022-04-27 18:00:40 +02:00
var pathGeometry = new PathGeometry();
2022-04-28 18:34:30 +02:00
var labels = DrawGraticule(pathGeometry.Figures);
2022-03-04 22:28:18 +01:00
2024-05-26 20:32:29 +02:00
var pen = new Pen
{
Brush = Foreground,
Thickness = StrokeThickness,
};
drawingContext.DrawGeometry(null, pen, pathGeometry);
2022-03-04 22:28:18 +01:00
2022-04-28 18:34:30 +02:00
if (labels.Count > 0)
2022-03-04 22:28:18 +01:00
{
2022-04-28 18:34:30 +02:00
var typeface = new Typeface(FontFamily, FontStyle, FontWeight, FontStretch);
var pixelsPerDip = VisualTreeHelper.GetDpi(this).PixelsPerDip;
foreach (var label in labels)
{
var latText = new FormattedText(label.LatitudeText,
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground, pixelsPerDip);
2022-03-04 22:28:18 +01:00
2022-04-28 18:34:30 +02:00
var lonText = new FormattedText(label.LongitudeText,
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground, pixelsPerDip);
2022-03-04 22:28:18 +01:00
2022-04-28 18:34:30 +02:00
var x = label.X + StrokeThickness / 2d + 2d;
var y1 = label.Y - StrokeThickness / 2d - latText.Height;
var y2 = label.Y + StrokeThickness / 2d;
2022-03-04 22:28:18 +01:00
2022-04-28 18:34:30 +02:00
drawingContext.PushTransform(new RotateTransform(label.Rotation, label.X, label.Y));
drawingContext.DrawText(latText, new Point(x, y1));
drawingContext.DrawText(lonText, new Point(x, y2));
drawingContext.Pop();
}
2022-03-05 18:40:57 +01:00
}
2022-03-04 22:28:18 +01:00
}
}
2022-04-28 18:34:30 +02:00
private static PathFigure CreatePolylineFigure(IEnumerable<Point> points)
2022-03-04 22:28:18 +01:00
{
2022-04-27 18:00:40 +02:00
var figure = new PathFigure
2022-03-04 22:28:18 +01:00
{
2022-04-27 18:00:40 +02:00
StartPoint = points.First(),
2024-05-29 12:02:55 +02:00
IsClosed = false,
2022-04-27 18:00:40 +02:00
IsFilled = false
};
2022-03-04 22:28:18 +01:00
2022-04-27 18:00:40 +02:00
figure.Segments.Add(new PolyLineSegment(points.Skip(1), true));
return figure;
}
}
}