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

411 lines
12 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
2012-05-04 12:52:20 +02:00
// Licensed under the Microsoft Public License (Ms-PL)
2024-05-22 15:14:46 +02:00
using System;
using System.Diagnostics;
2024-05-22 11:25:32 +02:00
#if WPF
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<MapPanel, Point?>("ViewPosition");
private static readonly DependencyProperty ParentMapProperty =
2024-05-23 19:21:28 +02:00
DependencyPropertyHelper.RegisterAttached<MapPanel, MapBase>("ParentMap", 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
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)
{
2024-05-22 15:14:46 +02:00
try
{
2024-05-22 15:14:46 +02:00
ArrangeChildElement(element, finalSize);
2021-01-13 00:08:55 +01:00
}
2024-05-22 15:14:46 +02:00
catch (Exception ex)
2021-01-13 00:08:55 +01:00
{
2024-05-22 15:14:46 +02:00
Debug.WriteLine($"MapPanel.ArrangeOverride: {element}: {ex.Message}");
2022-02-25 20:58:48 +01:00
}
}
}
return finalSize;
}
2023-01-14 18:41:10 +01:00
protected Point? GetViewPosition(Location location)
2022-11-05 00:19:43 +01:00
{
var position = parentMap.LocationToView(location);
if (parentMap.MapProjection.Type <= MapProjectionType.NormalCylindrical &&
position.HasValue &&
IsOutsideViewport(position.Value))
2022-11-05 00:19:43 +01:00
{
2024-03-14 19:40:02 +01:00
position = parentMap.LocationToView(
new Location(location.Latitude, parentMap.CoerceLongitude(location.Longitude)));
2022-11-05 00:19:43 +01:00
}
return position;
}
protected ViewRect? GetViewRect(BoundingBox boundingBox)
2022-11-05 00:19:43 +01:00
{
var rect = parentMap.MapProjection.BoundingBoxToMap(boundingBox);
2022-12-08 18:53:59 +01:00
if (!rect.HasValue)
{
return null;
}
return GetViewRect(rect.Value);
2022-11-05 00:19:43 +01:00
}
protected ViewRect GetViewRect(Rect mapRect)
2022-11-05 00:19:43 +01:00
{
var rectCenter = new Point(mapRect.X + mapRect.Width / 2d, mapRect.Y + mapRect.Height / 2d);
var position = parentMap.ViewTransform.MapToView(rectCenter);
2024-05-20 23:24:34 +02:00
var projection = parentMap.MapProjection;
2022-11-05 00:19:43 +01:00
2024-05-20 23:24:34 +02:00
if (projection.Type <= MapProjectionType.NormalCylindrical && IsOutsideViewport(position))
2022-11-05 00:19:43 +01:00
{
2024-05-20 23:24:34 +02:00
var location = projection.MapToLocation(rectCenter);
2022-11-05 00:19:43 +01:00
if (location != null)
{
2024-04-11 13:21:38 +02:00
var pos = parentMap.LocationToView(
new Location(location.Latitude, parentMap.CoerceLongitude(location.Longitude)));
if (pos.HasValue)
{
position = pos.Value;
}
2022-11-05 00:19:43 +01: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;
return new ViewRect(x, y, width, height, parentMap.ViewTransform.Rotation);
}
2023-01-14 01:06:46 +01:00
private bool IsOutsideViewport(Point point)
2021-01-13 21:46:23 +01:00
{
return point.X < 0d || point.X > parentMap.ActualWidth
|| point.Y < 0d || point.Y > parentMap.ActualHeight;
2021-01-13 21:46:23 +01:00
}
2024-05-22 15:14:46 +02:00
private void ArrangeChildElement(FrameworkElement element, Size panelSize)
{
var location = GetLocation(element);
var position = location != null ? GetViewPosition(location) : null;
SetViewPosition(element, ref position);
if (GetAutoCollapse(element))
{
SetVisible(element, !(position.HasValue && IsOutsideViewport(position.Value)));
}
if (position.HasValue)
{
ArrangeElement(element, position.Value);
}
else
{
var boundingBox = GetBoundingBox(element);
if (boundingBox != null)
{
var viewRect = GetViewRect(boundingBox);
if (viewRect.HasValue)
{
ArrangeElement(element, viewRect.Value);
}
}
else
{
ArrangeElement(element, panelSize);
}
}
}
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
}
2023-01-13 18:42:08 +01:00
private static void ArrangeElement(FrameworkElement element, ViewRect rect)
2020-07-15 16:40:33 +02:00
{
element.Width = rect.Rect.Width;
element.Height = rect.Rect.Height;
2023-01-13 18:42:08 +01:00
element.Arrange(rect.Rect);
2023-01-13 18:42:08 +01:00
if (element.RenderTransform is RotateTransform rotateTransform)
2023-01-13 11:43:42 +01:00
{
2023-01-13 18:42:08 +01:00
rotateTransform.Angle = rect.Rotation;
2023-01-13 11:43:42 +01:00
}
2023-01-13 18:42:08 +01:00
else if (rect.Rotation != 0d)
2023-01-13 11:43:42 +01:00
{
2024-05-20 23:24:34 +02:00
SetRenderTransform(element, new RotateTransform { Angle = rect.Rotation }, 0.5, 0.5);
2023-01-13 11:43:42 +01:00
}
}
2021-01-13 00:08:55 +01:00
2024-05-20 23:24:34 +02:00
internal static Size GetDesiredSize(FrameworkElement element)
2023-01-21 14:41:03 +01:00
{
var width = 0d;
var height = 0d;
if (element.DesiredSize.Width >= 0d &&
element.DesiredSize.Width < double.PositiveInfinity)
{
width = element.DesiredSize.Width;
}
if (element.DesiredSize.Height >= 0d &&
element.DesiredSize.Height < double.PositiveInfinity)
{
height = element.DesiredSize.Height;
}
return new Size(width, height);
}
2012-04-25 22:02:53 +02:00
}
}