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

113 lines
4.2 KiB
C#
Raw Normal View History

// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2023-01-03 15:12:53 +01:00
// Copyright © 2023 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using Windows.Foundation;
2022-04-27 18:00:40 +02:00
using System.Collections.Generic;
using System.Linq;
2021-06-14 21:41:37 +02:00
#if WINUI
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Shapes;
#else
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;
2021-06-14 21:41:37 +02:00
#endif
namespace MapControl
{
public partial class MapGraticule
{
2022-04-27 18:00:40 +02:00
private readonly Path path = new Path { Data = new PathGeometry() };
public MapGraticule()
{
StrokeThickness = 0.5;
}
protected override void OnViewportChanged(ViewportChangedEventArgs e)
{
2022-04-28 18:34:30 +02:00
var labels = DrawGraticule(((PathGeometry)path.Data).Figures);
2022-04-27 18:00:40 +02:00
if (Children.Count == 0)
{
path.SetBinding(Shape.StrokeProperty, this.GetOrCreateBinding(StrokeProperty, nameof(Stroke)));
path.SetBinding(Shape.StrokeThicknessProperty, this.GetOrCreateBinding(StrokeThicknessProperty, nameof(StrokeThickness)));
path.SetBinding(Shape.StrokeDashArrayProperty, this.GetOrCreateBinding(StrokeDashArrayProperty, nameof(StrokeDashArray)));
path.SetBinding(Shape.StrokeDashOffsetProperty, this.GetOrCreateBinding(StrokeDashOffsetProperty, nameof(StrokeDashOffset)));
path.SetBinding(Shape.StrokeDashCapProperty, this.GetOrCreateBinding(StrokeDashCapProperty, nameof(StrokeDashCap)));
2022-04-27 18:00:40 +02:00
Children.Add(path);
}
2022-04-27 18:00:40 +02:00
var childrenCount = 1;
2022-04-27 18:00:40 +02:00
foreach (var label in labels)
{
TextBlock textBlock;
2022-04-27 18:00:40 +02:00
if (childrenCount < Children.Count)
{
2022-04-27 18:00:40 +02:00
textBlock = (TextBlock)Children[childrenCount];
}
else
{
textBlock = new TextBlock { RenderTransform = new MatrixTransform() };
textBlock.SetBinding(TextBlock.FontSizeProperty, this.GetOrCreateBinding(FontSizeProperty, nameof(FontSize)));
textBlock.SetBinding(TextBlock.FontStyleProperty, this.GetOrCreateBinding(FontStyleProperty, nameof(FontStyle)));
textBlock.SetBinding(TextBlock.FontStretchProperty, this.GetOrCreateBinding(FontStretchProperty, nameof(FontStretch)));
textBlock.SetBinding(TextBlock.FontWeightProperty, this.GetOrCreateBinding(FontWeightProperty, nameof(FontWeight)));
textBlock.SetBinding(TextBlock.ForegroundProperty, this.GetOrCreateBinding(ForegroundProperty, nameof(Foreground)));
if (FontFamily != null)
{
2022-04-27 18:00:40 +02:00
textBlock.SetBinding(TextBlock.FontFamilyProperty, this.GetOrCreateBinding(FontFamilyProperty, nameof(FontFamily)));
}
2022-04-27 18:00:40 +02:00
Children.Add(textBlock);
}
2022-04-27 18:00:40 +02:00
textBlock.Text = label.LatitudeText + "\n" + label.LongitudeText;
textBlock.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
2022-04-27 18:00:40 +02:00
var matrix = new Matrix(1, 0, 0, 1, 0, 0);
2022-04-27 18:00:40 +02:00
matrix.Translate(StrokeThickness / 2d + 2d, -textBlock.DesiredSize.Height / 2d);
matrix.Rotate(label.Rotation);
2022-04-28 18:34:30 +02:00
matrix.Translate(label.X, label.Y);
2022-04-27 18:00:40 +02:00
((MatrixTransform)textBlock.RenderTransform).Matrix = matrix;
2022-04-27 18:00:40 +02:00
childrenCount++;
}
2022-04-27 18:00:40 +02:00
while (Children.Count > childrenCount)
{
Children.RemoveAt(Children.Count - 1);
}
2022-04-27 18:00:40 +02:00
base.OnViewportChanged(e);
}
2022-04-28 18:34:30 +02:00
private static PathFigure CreatePolylineFigure(IEnumerable<Point> points)
2022-04-27 18:00:40 +02:00
{
var figure = new PathFigure
{
StartPoint = points.First(),
IsFilled = false
};
2022-04-27 18:00:40 +02:00
var polyline = new PolyLineSegment();
2022-04-27 18:00:40 +02:00
foreach (var p in points.Skip(1))
{
2022-04-27 18:00:40 +02:00
polyline.Points.Add(p);
}
2022-04-27 18:00:40 +02:00
figure.Segments.Add(polyline);
return figure;
}
}
}