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

150 lines
4.8 KiB
C#
Raw Normal View History

// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2024-02-03 21:01:53 +01:00
// Copyright © 2024 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;
2024-05-22 11:25:32 +02:00
#if UWP
2024-05-26 20:32:29 +02:00
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;
2024-05-22 11:25:32 +02:00
#else
2024-05-26 20:32:29 +02:00
using Microsoft.UI.Xaml;
2024-05-22 11:25:32 +02:00
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Shapes;
2021-06-14 21:41:37 +02:00
#endif
namespace MapControl
{
2024-05-26 20:32:29 +02:00
public partial class MapGraticule : MapPanel
{
2024-05-26 20:32:29 +02:00
public static readonly DependencyProperty ForegroundProperty =
DependencyPropertyHelper.Register<MapGraticule, Brush>(nameof(Foreground));
public static readonly DependencyProperty FontFamilyProperty =
DependencyPropertyHelper.Register<MapGraticule, FontFamily>(nameof(FontFamily));
public static readonly DependencyProperty FontSizeProperty =
DependencyPropertyHelper.Register<MapGraticule, double>(nameof(FontSize), 12d);
public static readonly DependencyProperty StrokeThicknessProperty =
DependencyPropertyHelper.Register<MapGraticule, double>(nameof(StrokeThickness), 0.5);
2022-04-27 18:00:40 +02:00
private readonly Path path = new Path { Data = new PathGeometry() };
2024-05-26 20:32:29 +02:00
public Brush Foreground
{
get => (Brush)GetValue(ForegroundProperty);
set => SetValue(ForegroundProperty, value);
}
public FontFamily FontFamily
{
get => (FontFamily)GetValue(FontFamilyProperty);
set => SetValue(FontFamilyProperty, value);
}
public double FontSize
{
2024-05-26 20:32:29 +02:00
get => (double)GetValue(FontSizeProperty);
set => SetValue(FontSizeProperty, value);
}
public double StrokeThickness
{
get => (double)GetValue(StrokeThicknessProperty);
set => SetValue(StrokeThicknessProperty, value);
}
protected override void SetParentMap(MapBase map)
{
if (map != null && Foreground == null)
{
SetBinding(ForegroundProperty, map.CreateBinding(nameof(Foreground)));
}
base.SetParentMap(map);
}
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)
{
2024-05-26 20:32:29 +02:00
path.SetBinding(Shape.StrokeProperty, this.CreateBinding(nameof(Foreground)));
2024-05-24 18:24:44 +02:00
path.SetBinding(Shape.StrokeThicknessProperty, this.CreateBinding(nameof(StrokeThickness)));
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() };
2024-05-24 18:24:44 +02:00
textBlock.SetBinding(TextBlock.FontSizeProperty, this.CreateBinding(nameof(FontSize)));
textBlock.SetBinding(TextBlock.ForegroundProperty, this.CreateBinding(nameof(Foreground)));
2022-04-27 18:00:40 +02:00
if (FontFamily != null)
{
2024-05-24 18:24:44 +02:00
textBlock.SetBinding(TextBlock.FontFamilyProperty, this.CreateBinding(nameof(FontFamily)));
2022-04-27 18:00:40 +02:00
}
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;
}
}
}