XAML-Map-Control/MapControl/Shared/MapPanel.cs

423 lines
13 KiB
C#
Raw Normal View History

2025-05-08 19:51:31 +02:00
#if WPF
2024-05-22 11:25:32 +02:00
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
#elif UWP
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
#elif WINUI
using Windows.Foundation;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
#endif
2012-04-25 22:02:53 +02:00
2024-05-20 23:24:34 +02:00
/// <summary>
/// Arranges child elements on a Map at positions specified by the attached property Location,
/// or in rectangles specified by the attached property BoundingBox.
/// </summary>
2012-04-25 22:02:53 +02:00
namespace MapControl
{
2017-09-05 20:57:17 +02:00
/// <summary>
2021-01-16 18:32:31 +01:00
/// Optional interface to hold the value of the attached property MapPanel.ParentMap.
2017-09-05 20:57:17 +02:00
/// </summary>
public interface IMapElement
{
MapBase ParentMap { get; set; }
}
public partial class MapPanel : Panel, IMapElement
2012-04-25 22:02:53 +02:00
{
2024-05-20 23:24:34 +02:00
private static readonly DependencyProperty ViewPositionProperty =
DependencyPropertyHelper.RegisterAttached<Point?>("ViewPosition", typeof(MapPanel));
2024-05-20 23:24:34 +02:00
private static readonly DependencyProperty ParentMapProperty =
DependencyPropertyHelper.RegisterAttached<MapBase>("ParentMap", typeof(MapPanel), null,
2024-05-25 15:55:31 +02:00
(element, oldValue, newValue) =>
{
if (element is IMapElement mapElement)
{
mapElement.ParentMap = newValue;
}
},
true); // inherits
2020-07-14 17:03:36 +02:00
2025-06-10 23:50:58 +02:00
public MapPanel()
{
UseLayoutRounding = false;
if (this is MapBase)
{
SetValue(ParentMapProperty, this);
}
2025-06-13 17:38:06 +02:00
#if UWP || WINUI
2025-06-10 23:50:58 +02:00
else
{
2025-06-13 17:38:06 +02:00
InitMapElement(this);
2025-06-10 23:50:58 +02:00
}
2025-06-13 17:38:06 +02:00
#endif
2025-06-10 23:50:58 +02:00
}
2017-09-05 20:57:17 +02:00
private MapBase parentMap;
/// <summary>
/// Implements IMapElement.ParentMap.
/// </summary>
2020-07-14 17:03:36 +02:00
public MapBase ParentMap
{
2022-08-06 10:40:59 +02:00
get => parentMap;
set => SetParentMap(value);
2020-07-14 17:03:36 +02:00
}
2021-01-13 00:08:55 +01:00
/// <summary>
/// Gets a value that controls whether an element's Visibility is automatically
/// set to Collapsed when it is located outside the visible viewport area.
/// </summary>
public static bool GetAutoCollapse(FrameworkElement element)
{
return (bool)element.GetValue(AutoCollapseProperty);
}
/// <summary>
/// Sets the AutoCollapse property.
/// </summary>
public static void SetAutoCollapse(FrameworkElement element, bool value)
{
element.SetValue(AutoCollapseProperty, value);
}
/// <summary>
2024-04-16 19:55:59 +02:00
/// Gets the Location of an element.
2021-01-13 00:08:55 +01:00
/// </summary>
public static Location GetLocation(FrameworkElement element)
2012-04-25 22:02:53 +02:00
{
2012-05-04 12:52:20 +02:00
return (Location)element.GetValue(LocationProperty);
2012-04-25 22:02:53 +02:00
}
2021-01-13 00:08:55 +01:00
/// <summary>
2024-04-16 19:55:59 +02:00
/// Sets the Location of an element.
2021-01-13 00:08:55 +01:00
/// </summary>
public static void SetLocation(FrameworkElement element, Location value)
2012-04-25 22:02:53 +02:00
{
element.SetValue(LocationProperty, value);
}
2021-01-13 00:08:55 +01:00
/// <summary>
/// Gets the BoundingBox of an element.
/// </summary>
public static BoundingBox GetBoundingBox(FrameworkElement element)
{
return (BoundingBox)element.GetValue(BoundingBoxProperty);
}
2021-01-13 00:08:55 +01:00
/// <summary>
/// Sets the BoundingBox of an element.
/// </summary>
public static void SetBoundingBox(FrameworkElement element, BoundingBox value)
{
element.SetValue(BoundingBoxProperty, value);
}
2021-01-13 00:08:55 +01:00
/// <summary>
2023-01-14 18:41:10 +01:00
/// Gets the view position of an element with Location.
2021-01-13 00:08:55 +01:00
/// </summary>
public static Point? GetViewPosition(FrameworkElement element)
{
return (Point?)element.GetValue(ViewPositionProperty);
}
2024-05-20 23:24:34 +02:00
/// <summary>
/// Sets the attached ViewPosition property of an element. The method is called during
/// ArrangeOverride and may be overridden to modify the actual view position value.
/// An overridden method should call this method to set the attached property.
/// </summary>
protected virtual void SetViewPosition(FrameworkElement element, ref Point? position)
{
element.SetValue(ViewPositionProperty, position);
}
protected virtual void SetParentMap(MapBase map)
{
if (parentMap != null && parentMap != this)
{
parentMap.ViewportChanged -= OnViewportChanged;
}
parentMap = map;
if (parentMap != null && parentMap != this)
{
parentMap.ViewportChanged += OnViewportChanged;
OnViewportChanged(new ViewportChangedEventArgs());
}
}
private void OnViewportChanged(object sender, ViewportChangedEventArgs e)
{
OnViewportChanged(e);
}
protected virtual void OnViewportChanged(ViewportChangedEventArgs e)
{
InvalidateArrange();
}
protected override Size MeasureOverride(Size availableSize)
{
availableSize = new Size(double.PositiveInfinity, double.PositiveInfinity);
2024-05-20 23:24:34 +02:00
foreach (var element in ChildElements)
{
element.Measure(availableSize);
}
return new Size();
}
2012-04-25 22:02:53 +02:00
protected override Size ArrangeOverride(Size finalSize)
{
if (parentMap != null)
2012-04-25 22:02:53 +02:00
{
2024-05-20 23:24:34 +02:00
foreach (var element in ChildElements)
{
2025-04-01 10:09:57 +02:00
ArrangeChildElement(element, finalSize);
}
}
return finalSize;
}
2024-09-08 14:49:10 +02:00
private Point? GetViewPosition(Location location)
2022-11-05 00:19:43 +01:00
{
2024-08-29 23:56:29 +02:00
var position = parentMap.LocationToView(location);
if (parentMap.MapProjection.Type <= MapProjectionType.NormalCylindrical &&
2024-08-30 17:35:30 +02:00
position.HasValue && !parentMap.InsideViewBounds(position.Value))
2022-11-05 00:19:43 +01:00
{
2024-08-29 23:56:29 +02:00
var coercedPosition = parentMap.LocationToView(
new Location(location.Latitude, parentMap.CoerceLongitude(location.Longitude)));
2024-08-29 21:35:58 +02:00
2024-08-30 10:02:27 +02:00
if (coercedPosition.HasValue)
2024-08-29 21:35:58 +02:00
{
2024-08-29 23:56:29 +02:00
position = coercedPosition;
2024-08-29 21:35:58 +02:00
}
2022-11-05 00:19:43 +01:00
}
2024-08-29 23:56:29 +02:00
return position;
2022-11-05 00:19:43 +01:00
}
2024-09-08 14:49:10 +02:00
private Rect GetViewRect(Rect mapRect)
2022-11-05 00:19:43 +01:00
{
2024-08-29 10:56:41 +02:00
var center = new Point(mapRect.X + mapRect.Width / 2d, mapRect.Y + mapRect.Height / 2d);
2024-08-29 23:56:29 +02:00
var position = parentMap.ViewTransform.MapToView(center);
2025-01-06 15:33:12 +01:00
if (parentMap.MapProjection.Type <= MapProjectionType.NormalCylindrical &&
!parentMap.InsideViewBounds(position))
{
var location = parentMap.MapProjection.MapToLocation(center);
if (location != null)
{
var coercedPosition = parentMap.LocationToView(
new Location(location.Latitude, parentMap.CoerceLongitude(location.Longitude)));
if (coercedPosition.HasValue)
{
position = coercedPosition.Value;
}
}
}
2024-08-29 23:56:29 +02:00
var width = mapRect.Width * parentMap.ViewTransform.Scale;
var height = mapRect.Height * parentMap.ViewTransform.Scale;
2022-11-05 00:19:43 +01:00
var x = position.X - width / 2d;
var y = position.Y - height / 2d;
2024-09-02 19:41:44 +02:00
return new Rect(x, y, width, height);
2024-08-28 14:39:49 +02:00
}
2024-05-22 15:14:46 +02:00
private void ArrangeChildElement(FrameworkElement element, Size panelSize)
{
var location = GetLocation(element);
2024-08-28 20:25:36 +02:00
if (location != null)
2024-05-22 15:14:46 +02:00
{
2024-08-28 20:25:36 +02:00
var position = GetViewPosition(location);
SetViewPosition(element, ref position);
if (GetAutoCollapse(element))
{
2024-08-30 17:35:30 +02:00
SetVisible(element, position.HasValue && parentMap.InsideViewBounds(position.Value));
2024-08-28 20:25:36 +02:00
}
2024-05-22 15:14:46 +02:00
2024-08-28 23:33:19 +02:00
if (position.HasValue)
{
ArrangeElement(element, position.Value);
}
2024-05-22 15:14:46 +02:00
}
else
{
2024-08-28 20:25:36 +02:00
element.ClearValue(ViewPositionProperty);
2024-05-22 15:14:46 +02:00
var boundingBox = GetBoundingBox(element);
if (boundingBox != null)
{
2024-09-05 21:27:04 +02:00
ArrangeElement(element, boundingBox);
2024-05-22 15:14:46 +02:00
}
else
{
ArrangeElement(element, panelSize);
}
}
}
2024-09-05 21:27:04 +02:00
private void ArrangeElement(FrameworkElement element, BoundingBox boundingBox)
2024-08-28 23:37:08 +02:00
{
2025-03-24 20:59:11 +01:00
Rect? mapRect = null;
var rotation = 0d;
if (boundingBox is LatLonBox latLonBox)
{
var rotatedRect = parentMap.MapProjection.LatLonBoxToMap(latLonBox);
if (rotatedRect != null)
{
mapRect = rotatedRect.Item1;
rotation = -rotatedRect.Item2;
}
}
else
{
mapRect = parentMap.MapProjection.BoundingBoxToMap(boundingBox);
}
2024-08-28 23:37:08 +02:00
2025-03-24 20:59:11 +01:00
if (mapRect.HasValue)
2024-08-28 23:37:08 +02:00
{
2025-03-24 20:59:11 +01:00
var viewRect = GetViewRect(mapRect.Value);
2024-09-05 21:27:04 +02:00
2025-03-24 20:59:11 +01:00
element.Width = viewRect.Width;
element.Height = viewRect.Height;
element.Arrange(viewRect);
2024-09-05 21:27:04 +02:00
2025-03-24 20:59:11 +01:00
rotation += parentMap.ViewTransform.Rotation;
2024-09-05 21:27:04 +02:00
if (element.RenderTransform is RotateTransform rotateTransform)
{
rotateTransform.Angle = rotation;
}
else if (rotation != 0d)
{
SetRenderTransform(element, new RotateTransform { Angle = rotation }, 0.5, 0.5);
}
2024-08-28 23:37:08 +02:00
}
}
2023-01-13 18:42:08 +01:00
private static void ArrangeElement(FrameworkElement element, Point position)
{
2023-01-21 16:08:23 +01:00
var size = GetDesiredSize(element);
var x = position.X;
var y = position.Y;
switch (element.HorizontalAlignment)
{
case HorizontalAlignment.Center:
x -= size.Width / 2d;
break;
case HorizontalAlignment.Right:
x -= size.Width;
break;
2012-04-25 22:02:53 +02:00
default:
break;
}
2012-04-25 22:02:53 +02:00
switch (element.VerticalAlignment)
2012-04-25 22:02:53 +02:00
{
case VerticalAlignment.Center:
y -= size.Height / 2d;
break;
case VerticalAlignment.Bottom:
y -= size.Height;
break;
default:
break;
}
element.Arrange(new Rect(x, y, size.Width, size.Height));
}
2024-05-22 15:14:46 +02:00
private static void ArrangeElement(FrameworkElement element, Size panelSize)
{
2023-01-21 16:08:23 +01:00
var size = GetDesiredSize(element);
var x = 0d;
var y = 0d;
var width = size.Width;
var height = size.Height;
switch (element.HorizontalAlignment)
{
case HorizontalAlignment.Center:
2024-05-22 15:14:46 +02:00
x = (panelSize.Width - size.Width) / 2d;
break;
case HorizontalAlignment.Right:
2024-05-22 15:14:46 +02:00
x = panelSize.Width - size.Width;
break;
case HorizontalAlignment.Stretch:
2024-05-22 15:14:46 +02:00
width = panelSize.Width;
break;
2012-12-07 17:04:44 +01:00
default:
break;
}
switch (element.VerticalAlignment)
2012-12-07 17:04:44 +01:00
{
case VerticalAlignment.Center:
2024-05-22 15:14:46 +02:00
y = (panelSize.Height - size.Height) / 2d;
break;
2012-12-07 17:04:44 +01:00
case VerticalAlignment.Bottom:
2024-05-22 15:14:46 +02:00
y = panelSize.Height - size.Height;
break;
case VerticalAlignment.Stretch:
2024-05-22 15:14:46 +02:00
height = panelSize.Height;
break;
2012-12-07 17:04:44 +01:00
default:
break;
}
2012-12-07 17:04:44 +01:00
element.Arrange(new Rect(x, y, width, height));
2020-07-15 16:40:33 +02:00
}
2024-05-20 23:24:34 +02:00
internal static Size GetDesiredSize(FrameworkElement element)
2023-01-21 14:41:03 +01:00
{
2024-08-28 20:25:36 +02:00
var width = element.DesiredSize.Width;
var height = element.DesiredSize.Height;
2023-01-21 14:41:03 +01:00
2024-08-28 20:25:36 +02:00
if (width < 0d || width == double.PositiveInfinity)
2023-01-21 14:41:03 +01:00
{
2024-08-28 20:25:36 +02:00
width = 0d;
2023-01-21 14:41:03 +01:00
}
2024-08-28 20:25:36 +02:00
if (height < 0d || height == double.PositiveInfinity)
2023-01-21 14:41:03 +01:00
{
2024-08-28 20:25:36 +02:00
height = 0d;
2023-01-21 14:41:03 +01:00
}
return new Size(width, height);
}
2012-04-25 22:02:53 +02:00
}
}