2025-02-27 18:46:32 +01:00
|
|
|
|
using System.Collections.Generic;
|
2015-08-25 20:04:33 +02:00
|
|
|
|
using System.Globalization;
|
2022-03-04 22:28:18 +01:00
|
|
|
|
using System.Linq;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
using System.Windows;
|
2024-05-26 20:32:29 +02:00
|
|
|
|
using System.Windows.Controls;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2024-05-26 20:32:29 +02:00
|
|
|
|
public partial class MapGraticule : Control, IMapElement
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2024-05-26 20:32:29 +02:00
|
|
|
|
private MapBase parentMap;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Implements IMapElement.ParentMap.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public MapBase ParentMap
|
2013-04-12 19:59:16 +02:00
|
|
|
|
{
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2013-04-12 19:59:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-26 20:32:29 +02:00
|
|
|
|
private void OnViewportChanged(object sender, ViewportChangedEventArgs e)
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2013-04-12 19:59:16 +02:00
|
|
|
|
InvalidateVisual();
|
|
|
|
|
|
}
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2013-04-12 19:59:16 +02:00
|
|
|
|
protected override void OnRender(DrawingContext drawingContext)
|
|
|
|
|
|
{
|
2024-05-26 20:32:29 +02:00
|
|
|
|
if (parentMap != null)
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2022-04-27 18:00:40 +02:00
|
|
|
|
var pathGeometry = new PathGeometry();
|
2018-02-11 19:46:31 +01:00
|
|
|
|
|
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;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|