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

306 lines
9.2 KiB
C#
Raw Normal View History

// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// © 2020 Clemens Fischer
2012-05-04 12:52:20 +02:00
// Licensed under the Microsoft Public License (Ms-PL)
using System;
using System.Linq;
2017-08-04 21:38:58 +02:00
#if WINDOWS_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>
/// Optional interface to hold the value of the MapPanel.ParentMap attached property.
/// </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
{
2017-09-05 20:57:17 +02:00
private MapBase parentMap;
public MapPanel()
{
InitMapElement(this);
}
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
}
public static void SetLocation(FrameworkElement element, Location value)
2012-04-25 22:02:53 +02:00
{
element.SetValue(LocationProperty, value);
}
public static BoundingBox GetBoundingBox(FrameworkElement element)
{
return (BoundingBox)element.GetValue(BoundingBoxProperty);
}
public static void SetBoundingBox(FrameworkElement element, BoundingBox value)
{
element.SetValue(BoundingBoxProperty, value);
}
public static Point? GetViewPosition(FrameworkElement element)
{
return (Point?)element.GetValue(ViewPositionProperty);
}
public MapBase ParentMap
{
get { return parentMap; }
set { SetParentMap(value); }
}
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>())
{
var location = GetLocation(element);
if (location != null)
{
var viewPosition = ArrangeElement(element, location);
SetViewPosition(element, viewPosition);
}
else
{
var boundingBox = GetBoundingBox(element);
if (boundingBox != null)
{
ArrangeElement(element, boundingBox);
}
else
{
ArrangeElement(element, finalSize);
}
SetViewPosition(element, null);
}
}
}
return finalSize;
}
private Point ArrangeElement(FrameworkElement element, Location location)
{
var pos = parentMap.LocationToView(location);
if (parentMap.MapProjection.IsNormalCylindrical &&
(pos.X < 0d || pos.X > parentMap.RenderSize.Width ||
pos.Y < 0d || pos.Y > parentMap.RenderSize.Height))
{
pos = parentMap.LocationToView(new Location(
location.Latitude,
Location.NearestLongitude(location.Longitude, parentMap.Center.Longitude)));
}
var rect = new Rect(pos, 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;
}
if (element.UseLayoutRounding)
{
rect.X = Math.Round(rect.X);
rect.Y = Math.Round(rect.Y);
}
element.Arrange(rect);
return pos;
}
private void ArrangeElement(FrameworkElement element, BoundingBox boundingBox)
{
var projection = parentMap.MapProjection;
var rect = projection.BoundingBoxToRect(boundingBox);
var center = new Point(rect.X + rect.Width / 2d, rect.Y + rect.Height / 2d);
var pos = parentMap.ViewTransform.MapToView(center);
if (projection.IsNormalCylindrical &&
(pos.X < 0d || pos.X > parentMap.RenderSize.Width ||
pos.Y < 0d || pos.Y > parentMap.RenderSize.Height))
{
var location = projection.MapToLocation(center);
location.Longitude = Location.NearestLongitude(location.Longitude, parentMap.Center.Longitude);
pos = parentMap.LocationToView(location);
}
rect.Width *= parentMap.ViewTransform.Scale;
rect.Height *= parentMap.ViewTransform.Scale;
rect.X = pos.X - rect.Width / 2d;
rect.Y = pos.Y - rect.Height / 2d;
if (element.UseLayoutRounding)
{
rect.X = Math.Round(rect.X);
rect.Y = Math.Round(rect.Y);
}
element.Width = rect.Width;
element.Height = rect.Height;
element.Arrange(rect);
var rotateTransform = element.RenderTransform as RotateTransform;
var rotation = parentMap.ViewTransform.Rotation;
if (rotateTransform != null)
{
rotateTransform.Angle = rotation;
}
else if (rotation != 0d)
{
rotateTransform = new RotateTransform { Angle = rotation };
element.RenderTransform = rotateTransform;
element.RenderTransformOrigin = new Point(0.5, 0.5);
}
}
private 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
if (element.UseLayoutRounding)
{
rect.X = Math.Round(rect.X);
rect.Y = Math.Round(rect.Y);
}
element.Arrange(rect);
}
private static void ParentMapPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
var mapElement = obj as IMapElement;
2012-12-07 17:04:44 +01:00
if (mapElement != null)
2018-12-02 20:22:59 +01:00
{
mapElement.ParentMap = e.NewValue as MapBase;
2012-12-07 17:04:44 +01:00
}
}
2012-04-25 22:02:53 +02:00
}
}