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

116 lines
3.5 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 System;
2024-05-23 18:08:14 +02:00
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;
namespace MapControl
{
public partial class MapPath : Shape, IWeakEventListener
{
2023-01-24 16:22:02 +01:00
public MapPath()
{
Stretch = Stretch.None;
}
2024-05-23 18:08:14 +02:00
public static readonly DependencyProperty DataProperty =
DependencyPropertyHelper.AddOwner<MapPath, Geometry>(Path.DataProperty,
(path, oldValue, newValue) => path.DataPropertyChanged(oldValue, newValue));
public Geometry Data
{
2022-08-06 10:40:59 +02:00
get => (Geometry)GetValue(DataProperty);
set => SetValue(DataProperty, value);
}
2022-08-06 10:40:59 +02:00
protected override Geometry DefiningGeometry => Data;
2024-05-23 18:08:14 +02:00
private void DataPropertyChanged(Geometry oldData, Geometry newData)
2022-11-03 20:10:17 +01:00
{
2023-01-25 14:29:27 +01:00
// Check if data is actually a new Geometry.
2022-11-30 22:18:45 +01:00
//
2024-05-23 18:08:14 +02:00
if (newData != null && !ReferenceEquals(newData, oldData))
2022-11-03 20:10:17 +01:00
{
2024-05-23 18:08:14 +02:00
if (newData.IsFrozen)
2022-11-03 20:10:17 +01:00
{
2024-05-23 18:08:14 +02:00
Data = newData.Clone(); // DataPropertyChanged called again
2022-11-03 20:10:17 +01:00
}
else
{
2024-05-23 18:08:14 +02:00
UpdateData();
2022-11-03 20:10:17 +01:00
}
}
}
2023-01-23 18:42:10 +01:00
private void SetMapTransform(Matrix matrix)
{
if (Data.Transform is MatrixTransform transform && !transform.IsFrozen)
{
transform.Matrix = matrix;
}
else
{
Data.Transform = new MatrixTransform(matrix);
}
}
#region Methods used only by derived classes MapPolyline, MapPolygon and MapMultiPolygon
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
CollectionChangedEventManager.RemoveListener(oldCollection, this);
}
2024-05-23 18:08:14 +02:00
if (newValue is INotifyCollectionChanged newCollection)
{
2020-04-16 23:15:03 +02:00
CollectionChangedEventManager.AddListener(newCollection, this);
}
UpdateData();
}
2020-05-13 18:17:28 +02:00
bool IWeakEventListener.ReceiveWeakEvent(Type managerType, object sender, EventArgs e)
{
UpdateData();
return true;
}
protected void SetPathFigures(PathFigureCollection pathFigures)
{
((PathGeometry)Data).Figures = pathFigures;
}
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);
var figure = new PathFigure
{
StartPoint = points.First(),
IsClosed = closed,
2023-01-24 16:22:02 +01:00
IsFilled = true
};
figure.Segments.Add(new PolyLineSegment(points.Skip(1), true));
2020-05-13 18:17:28 +02:00
pathFigures.Add(figure);
}
}
2020-05-13 18:17:28 +02:00
#endregion
}
}