XAML-Map-Control/MapControl/Avalonia/MapPolypoint.Avalonia.cs

138 lines
4.3 KiB
C#
Raw Normal View History

2024-05-26 09:49:00 +02:00
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// Copyright © 2024 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
2024-07-16 10:41:44 +02:00
using System;
2024-05-26 09:49:00 +02:00
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
namespace MapControl
{
/// <summary>
/// Base class of MapPolyline and MapPolygon.
/// </summary>
public class MapPolypoint : MapPath
{
public static readonly StyledProperty<FillRule> FillRuleProperty =
DependencyPropertyHelper.Register<MapPolygon, FillRule>(nameof(FillRule), FillRule.EvenOdd,
(polypoint, oldValue, newValue) => ((PathGeometry)polypoint.Data).FillRule = newValue);
public FillRule FillRule
{
get => GetValue(FillRuleProperty);
set => SetValue(FillRuleProperty, value);
}
protected MapPolypoint()
{
Data = new PathGeometry();
}
protected void DataCollectionPropertyChanged(IEnumerable oldValue, IEnumerable newValue)
{
if (oldValue is INotifyCollectionChanged oldCollection)
{
oldCollection.CollectionChanged -= DataCollectionChanged;
}
if (newValue is INotifyCollectionChanged newCollection)
{
newCollection.CollectionChanged += DataCollectionChanged;
}
UpdateData();
}
protected void DataCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
UpdateData();
}
protected void UpdateData(IEnumerable<Location> locations, bool closed)
{
2024-07-16 21:29:25 +02:00
var figures = new PathFigures();
2024-05-26 09:49:00 +02:00
2024-07-15 13:41:52 +02:00
if (ParentMap != null && locations != null)
2024-05-26 09:49:00 +02:00
{
2024-07-15 13:41:52 +02:00
var longitudeOffset = GetLongitudeOffset(Location ?? locations.FirstOrDefault());
2024-05-26 09:49:00 +02:00
2024-07-16 21:29:25 +02:00
AddPolylinePoints(figures, locations, longitudeOffset, closed);
2024-05-26 09:49:00 +02:00
}
2024-07-16 21:29:25 +02:00
SetPathFigures(figures);
2024-07-16 13:59:00 +02:00
}
2024-07-16 21:29:25 +02:00
protected void UpdateData(IEnumerable<IEnumerable<Location>> polygons)
2024-07-16 13:59:00 +02:00
{
2024-07-16 21:29:25 +02:00
var figures = new PathFigures();
if (ParentMap != null && polygons != null)
{
var longitudeOffset = GetLongitudeOffset(Location);
foreach (var locations in polygons)
{
AddPolylinePoints(figures, locations, longitudeOffset, true);
}
}
SetPathFigures(figures);
}
private void AddPolylinePoints(PathFigures figures, IEnumerable<Location> locations, double longitudeOffset, bool closed)
2024-05-26 09:49:00 +02:00
{
2024-07-15 09:09:15 +02:00
var points = locations
.Select(location => LocationToView(location, longitudeOffset))
.Where(point => point.HasValue)
.Select(point => point.Value);
2024-07-15 13:41:52 +02:00
if (points.Any())
{
2024-07-16 21:29:25 +02:00
var start = points.First();
var polyline = new PolyLineSegment(points.Skip(1));
var minX = start.X;
var maxX = start.X;
var minY = start.Y;
var maxY = start.Y;
foreach (var point in polyline.Points)
2024-07-15 13:41:52 +02:00
{
2024-07-16 10:41:44 +02:00
minX = Math.Min(minX, point.X);
maxX = Math.Max(maxX, point.X);
minY = Math.Min(minY, point.Y);
maxY = Math.Max(maxY, point.Y);
}
if (maxX >= 0 && minX <= ParentMap.ActualWidth &&
maxY >= 0 && minY <= ParentMap.ActualHeight)
{
2024-07-16 21:29:25 +02:00
var figure = new PathFigure
2024-07-16 10:41:44 +02:00
{
2024-07-16 21:29:25 +02:00
StartPoint = start,
2024-07-16 10:41:44 +02:00
IsClosed = closed,
IsFilled = true
};
2024-07-16 21:29:25 +02:00
figure.Segments.Add(polyline);
figures.Add(figure);
2024-07-16 10:41:44 +02:00
}
2024-07-15 13:41:52 +02:00
}
2024-05-26 09:49:00 +02:00
}
2024-07-16 21:34:16 +02:00
private void SetPathFigures(PathFigures figures)
{
if (figures.Count == 0)
{
// Avalonia Shape seems to ignore PathGeometry with empty Figures collection
figures.Add(new PathFigure { StartPoint = new Point(-1000, -1000) });
}
((PathGeometry)Data).Figures = figures;
InvalidateGeometry();
}
2024-05-26 09:49:00 +02:00
}
}