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

387 lines
12 KiB
C#
Raw Normal View History

// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2023-01-03 15:12:53 +01:00
// Copyright © 2023 Clemens Fischer
2012-05-04 12:52:20 +02:00
// Licensed under the Microsoft Public License (Ms-PL)
using System;
2022-02-25 20:58:48 +01:00
using System.Diagnostics;
using System.Linq;
2021-06-14 21:41:37 +02:00
#if WINUI
using Windows.Foundation;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
2021-11-17 23:17:11 +01:00
#elif UWP
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
#else
2012-04-25 22:02:53 +02:00
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
#endif
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; }
}
2012-05-04 12:52:20 +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.
2012-05-04 12:52:20 +02:00
/// </summary>
public partial class MapPanel : Panel, IMapElement
2012-04-25 22:02:53 +02:00
{
2021-01-13 00:08:55 +01:00
public static readonly DependencyProperty AutoCollapseProperty = DependencyProperty.RegisterAttached(
"AutoCollapse", typeof(bool), typeof(MapPanel), new PropertyMetadata(false));
2020-07-14 17:03:36 +02:00
2017-09-05 20:57:17 +02:00
private MapBase parentMap;
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>
/// Gets the geodetic Location of an element.
/// </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>
/// Sets the geodetic Location of an element.
/// </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>
2022-11-06 15:27:39 +01:00
/// Gets the view position of an element with Location
2022-11-05 17:32:29 +01:00
/// or null when the element has no Location.
2021-01-13 00:08:55 +01:00
/// </summary>
public static Point? GetViewPosition(FrameworkElement element)
{
return (Point?)element.GetValue(ViewPositionProperty);
}
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);
foreach (var element in Children.OfType<FrameworkElement>())
{
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
{
foreach (var element in Children.OfType<FrameworkElement>())
{
2022-11-04 23:46:54 +01:00
var location = GetLocation(element);
2022-11-05 00:19:43 +01:00
Point? position = null;
2022-11-05 17:32:29 +01:00
2022-11-04 23:46:54 +01:00
if (location != null)
{
position = GetViewPosition(location);
}
2021-01-13 00:08:55 +01:00
SetViewPosition(element, position);
if (GetAutoCollapse(element))
{
2021-01-15 18:00:51 +01:00
if (position.HasValue && IsOutsideViewport(position.Value))
2021-01-13 00:08:55 +01:00
{
element.SetValue(VisibilityProperty, Visibility.Collapsed);
}
else
{
element.ClearValue(VisibilityProperty);
}
}
2023-01-13 11:43:42 +01:00
if (position.HasValue)
2021-01-13 00:08:55 +01:00
{
2023-01-13 11:43:42 +01:00
ArrangeElement(element, position.Value);
}
else
{
var boundingBox = GetBoundingBox(element);
2022-02-25 20:58:48 +01:00
2023-01-13 11:43:42 +01:00
if (boundingBox != null)
{
var viewRect = GetViewRect(boundingBox);
2022-12-09 19:17:08 +01:00
2023-01-13 11:43:42 +01:00
if (viewRect != null)
2022-02-25 20:58:48 +01:00
{
2023-01-13 11:43:42 +01:00
ArrangeElement(element, viewRect);
2022-02-25 20:58:48 +01:00
}
}
2023-01-13 11:43:42 +01:00
else
{
ArrangeElement(element, finalSize);
}
2022-02-25 20:58:48 +01:00
}
}
}
return finalSize;
}
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
{
location = new Location(location.Latitude, parentMap.ConstrainedLongitude(location.Longitude));
position = parentMap.LocationToView(location);
}
return position;
}
protected ViewRect GetViewRect(BoundingBox boundingBox)
{
2022-12-08 18:53:59 +01:00
var mapRect = parentMap.MapProjection.BoundingBoxToMapRect(boundingBox);
return mapRect != null ? GetViewRect(mapRect) : null;
2022-11-05 00:19:43 +01:00
}
2022-12-07 17:00:25 +01:00
protected ViewRect GetViewRect(MapRect mapRect)
2022-11-05 00:19:43 +01:00
{
2022-12-07 17:00:25 +01:00
var position = parentMap.ViewTransform.MapToView(mapRect.Center);
2022-11-05 00:19:43 +01:00
if (parentMap.MapProjection.Type <= MapProjectionType.NormalCylindrical &&
IsOutsideViewport(position))
{
2022-12-07 17:00:25 +01:00
var location = parentMap.MapProjection.MapToLocation(mapRect.Center);
2022-11-05 00:19:43 +01:00
if (location != null)
{
location.Longitude = parentMap.ConstrainedLongitude(location.Longitude);
var pos = parentMap.LocationToView(location);
if (pos.HasValue)
{
position = pos.Value;
}
2022-11-05 00:19:43 +01:00
}
}
2022-12-07 17:00:25 +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);
}
2021-01-15 18:00:51 +01:00
private bool IsOutsideViewport(Point point)
2021-01-13 21:46:23 +01:00
{
2021-01-15 18:00:51 +01:00
return point.X < 0d || point.X > parentMap.RenderSize.Width
|| point.Y < 0d || point.Y > parentMap.RenderSize.Height;
2021-01-13 21:46:23 +01:00
}
2020-07-14 17:03:36 +02:00
private static void ArrangeElement(FrameworkElement element, ViewRect rect)
{
2020-07-14 17:03:36 +02:00
element.Width = rect.Width;
element.Height = rect.Height;
2020-07-15 16:40:33 +02:00
ArrangeElement(element, new Rect(rect.X, rect.Y, rect.Width, rect.Height));
2020-07-14 17:03:36 +02:00
if (element.RenderTransform is RotateTransform rotateTransform)
{
2020-07-14 17:03:36 +02:00
rotateTransform.Angle = rect.Rotation;
}
2020-07-14 17:03:36 +02:00
else if (rect.Rotation != 0d)
{
rotateTransform = new RotateTransform { Angle = rect.Rotation };
element.RenderTransform = rotateTransform;
element.RenderTransformOrigin = new Point(0.5, 0.5);
}
}
2020-07-14 17:03:36 +02:00
private static void ArrangeElement(FrameworkElement element, Point position)
{
var rect = new Rect(position, element.DesiredSize);
switch (element.HorizontalAlignment)
{
case HorizontalAlignment.Center:
rect.X -= rect.Width / 2d;
break;
case HorizontalAlignment.Right:
rect.X -= rect.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:
rect.Y -= rect.Height / 2d;
break;
case VerticalAlignment.Bottom:
rect.Y -= rect.Height;
break;
default:
break;
}
2020-07-15 16:40:33 +02:00
ArrangeElement(element, rect);
}
2020-07-14 17:03:36 +02:00
private static void ArrangeElement(FrameworkElement element, Size parentSize)
{
var rect = new Rect(new Point(), element.DesiredSize);
switch (element.HorizontalAlignment)
{
case HorizontalAlignment.Center:
rect.X = (parentSize.Width - rect.Width) / 2d;
break;
case HorizontalAlignment.Right:
rect.X = parentSize.Width - rect.Width;
break;
case HorizontalAlignment.Stretch:
rect.Width = parentSize.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:
rect.Y = (parentSize.Height - rect.Height) / 2d;
break;
2012-12-07 17:04:44 +01:00
case VerticalAlignment.Bottom:
rect.Y = parentSize.Height - rect.Height;
break;
case VerticalAlignment.Stretch:
rect.Height = parentSize.Height;
break;
2012-12-07 17:04:44 +01:00
default:
break;
}
2012-12-07 17:04:44 +01:00
2020-07-15 16:40:33 +02:00
ArrangeElement(element, rect);
}
private static void ArrangeElement(FrameworkElement element, Rect rect)
{
if (element.UseLayoutRounding)
{
rect.X = Math.Round(rect.X);
rect.Y = Math.Round(rect.Y);
2020-07-14 17:03:36 +02:00
rect.Width = Math.Round(rect.Width);
rect.Height = Math.Round(rect.Height);
}
2023-01-13 11:43:42 +01:00
try
{
element.Arrange(rect);
}
catch (Exception ex)
{
Debug.WriteLine($"MapPanel.ArrangeElement: {ex.Message}");
}
}
2021-01-13 00:08:55 +01:00
private static void ParentMapPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
if (obj is IMapElement mapElement)
{
mapElement.ParentMap = e.NewValue as MapBase;
}
}
2012-04-25 22:02:53 +02:00
}
}