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

141 lines
4.3 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)
2024-05-23 18:08:14 +02:00
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
2024-05-22 11:25:32 +02:00
#if UWP
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;
2024-05-22 11:25:32 +02:00
#else
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Shapes;
2021-06-14 21:41:37 +02:00
#endif
namespace MapControl
{
public partial class MapPath : Path
{
2021-01-17 00:31:30 +01:00
public MapPath()
{
2023-01-24 16:22:02 +01:00
Stretch = Stretch.None;
2021-01-17 00:31:30 +01:00
MapPanel.InitMapElement(this);
}
2023-01-23 18:42:10 +01:00
private void SetMapTransform(Matrix matrix)
{
if (Data.Transform is MatrixTransform transform)
{
transform.Matrix = matrix;
}
else
{
Data.Transform = new MatrixTransform { Matrix = matrix };
}
}
#region Methods used only by derived classes MapPolyline and MapPolygon
2024-05-23 18:08:14 +02:00
protected void DataCollectionPropertyChanged(IEnumerable oldValue, IEnumerable newValue)
{
2024-05-23 18:08:14 +02:00
if (oldValue is INotifyCollectionChanged oldCollection)
{
2020-04-16 23:15:03 +02:00
oldCollection.CollectionChanged -= DataCollectionChanged;
}
2024-05-23 18:08:14 +02:00
if (newValue is INotifyCollectionChanged newCollection)
{
2020-04-16 23:15:03 +02:00
newCollection.CollectionChanged += DataCollectionChanged;
}
UpdateData();
}
2020-05-13 18:17:28 +02:00
protected void DataCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
2020-05-13 18:17:28 +02:00
UpdateData();
}
2023-12-13 14:55:49 +01:00
protected void AddPolylinePoints(PathFigureCollection pathFigures, IEnumerable<Location> locations, double longitudeOffset, bool closed)
2020-05-13 18:17:28 +02:00
{
if (locations.Count() >= 2)
{
var points = locations
.Select(location => LocationToView(location, longitudeOffset))
.Where(point => point.HasValue)
.Select(point => point.Value);
2020-05-13 18:17:28 +02:00
if (closed)
{
var segment = new PolyLineSegment();
foreach (var point in points.Skip(1))
{
segment.Points.Add(point);
}
var figure = new PathFigure
{
StartPoint = points.First(),
IsClosed = closed,
2023-01-24 16:22:02 +01:00
IsFilled = true
};
figure.Segments.Add(segment);
pathFigures.Add(figure);
}
else
{
var pointList = points.ToList();
if (closed)
{
pointList.Add(pointList[0]);
}
var viewport = new Rect(0, 0, ParentMap.RenderSize.Width, ParentMap.RenderSize.Height);
PathFigure figure = null;
PolyLineSegment segment = null;
for (int i = 1; i < pointList.Count; i++)
{
var p1 = pointList[i - 1];
var p2 = pointList[i];
var inside = Intersections.GetIntersections(ref p1, ref p2, viewport);
if (inside)
{
if (figure == null)
{
figure = new PathFigure
{
StartPoint = p1,
IsClosed = false,
2023-01-24 16:22:02 +01:00
IsFilled = true
};
segment = new PolyLineSegment();
figure.Segments.Add(segment);
pathFigures.Add(figure);
}
segment.Points.Add(p2);
}
if (!inside || p2 != pointList[i])
{
figure = null;
}
}
}
}
}
2020-05-13 18:17:28 +02:00
2021-06-14 21:41:37 +02:00
#endregion
}
}