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

395 lines
14 KiB
C#
Raw Normal View History

// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// © 2017 Clemens Fischer
2012-05-04 12:52:20 +02:00
// Licensed under the Microsoft Public License (Ms-PL)
using System;
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
{
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.
/// An element's viewport position is assigned as TranslateTransform to its RenderTransform property,
/// either directly or as last child of a TransformGroup.
2012-05-04 12:52:20 +02:00
/// </summary>
public partial class MapPanel : Panel, IMapElement
2012-04-25 22:02:53 +02:00
{
public static readonly DependencyProperty LocationProperty = DependencyProperty.RegisterAttached(
"Location", typeof(Location), typeof(MapPanel), new PropertyMetadata(null, LocationPropertyChanged));
public static readonly DependencyProperty BoundingBoxProperty = DependencyProperty.RegisterAttached(
"BoundingBox", typeof(BoundingBox), typeof(MapPanel), new PropertyMetadata(null, BoundingBoxPropertyChanged));
2012-05-04 12:52:20 +02:00
public static Location GetLocation(UIElement 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
}
2012-05-04 12:52:20 +02:00
public static void SetLocation(UIElement element, Location value)
2012-04-25 22:02:53 +02:00
{
element.SetValue(LocationProperty, value);
}
public static BoundingBox GetBoundingBox(UIElement element)
{
return (BoundingBox)element.GetValue(BoundingBoxProperty);
}
public static void SetBoundingBox(UIElement element, BoundingBox value)
{
element.SetValue(BoundingBoxProperty, value);
}
private MapBase parentMap;
public MapBase ParentMap
{
get { return parentMap; }
set { SetParentMapOverride(value); }
}
protected override Size MeasureOverride(Size availableSize)
{
availableSize = new Size(double.PositiveInfinity, double.PositiveInfinity);
foreach (UIElement element in Children)
{
element.Measure(availableSize);
}
return new Size();
}
2012-04-25 22:02:53 +02:00
protected override Size ArrangeOverride(Size finalSize)
{
foreach (UIElement element in Children)
2012-04-25 22:02:53 +02:00
{
BoundingBox boundingBox;
Location location;
if ((boundingBox = GetBoundingBox(element)) != null)
{
ArrangeElementWithBoundingBox(element);
SetBoundingBoxRect(element, parentMap, boundingBox);
}
else if ((location = GetLocation(element)) != null)
2012-12-07 17:04:44 +01:00
{
ArrangeElementWithLocation(element);
2012-12-07 17:04:44 +01:00
SetViewportPosition(element, parentMap, location);
}
else
{
ArrangeElement(element, finalSize);
}
2012-04-25 22:02:53 +02:00
}
return finalSize;
}
protected virtual void SetParentMapOverride(MapBase map)
{
if (parentMap != null && parentMap != this)
{
parentMap.ViewportChanged -= OnViewportChanged;
}
parentMap = map;
if (parentMap != null && parentMap != this)
{
parentMap.ViewportChanged += OnViewportChanged;
OnViewportChanged(new ViewportChangedEventArgs());
}
}
protected virtual void OnViewportChanged(ViewportChangedEventArgs e)
{
foreach (UIElement element in Children)
{
BoundingBox boundingBox;
Location location;
if ((boundingBox = GetBoundingBox(element)) != null)
{
SetBoundingBoxRect(element, parentMap, boundingBox);
}
else if ((location = GetLocation(element)) != null)
{
SetViewportPosition(element, parentMap, location);
}
}
}
private void OnViewportChanged(object sender, ViewportChangedEventArgs e)
{
OnViewportChanged(e);
}
private static void ParentMapPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
var mapElement = obj as IMapElement;
if (mapElement != null)
{
mapElement.ParentMap = e.NewValue as MapBase;
}
}
private static void LocationPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
2012-04-25 22:02:53 +02:00
{
var element = (UIElement)obj;
var map = GetParentMap(element);
var location = (Location)e.NewValue;
2012-04-25 22:02:53 +02:00
if (location == null)
2012-04-25 22:02:53 +02:00
{
ArrangeElement(element, map?.RenderSize ?? new Size());
}
else if (e.OldValue == null)
{
ArrangeElementWithLocation(element); // arrange once when Location was null before
2012-04-25 22:02:53 +02:00
}
SetViewportPosition(element, map, location);
}
private static void BoundingBoxPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
var element = (FrameworkElement)obj;
var map = GetParentMap(element);
var boundingBox = (BoundingBox)e.NewValue;
if (boundingBox == null)
{
ArrangeElement(element, map?.RenderSize ?? new Size());
}
else if (e.OldValue == null)
{
ArrangeElementWithBoundingBox(element); // arrange once when BoundingBox was null before
}
SetBoundingBoxRect(element, map, boundingBox);
2012-04-25 22:02:53 +02:00
}
private static void SetViewportPosition(UIElement element, MapBase parentMap, Location location)
2012-04-25 22:02:53 +02:00
{
var viewportPosition = new Point();
if (parentMap != null && location != null)
{
viewportPosition = parentMap.MapProjection.LocationToViewportPoint(location);
if (viewportPosition.X < 0d || viewportPosition.X > parentMap.RenderSize.Width ||
viewportPosition.Y < 0d || viewportPosition.Y > parentMap.RenderSize.Height)
{
viewportPosition = parentMap.MapProjection.LocationToViewportPoint(new Location(
location.Latitude,
Location.NearestLongitude(location.Longitude, parentMap.Center.Longitude)));
}
if ((bool)element.GetValue(UseLayoutRoundingProperty))
{
viewportPosition.X = Math.Round(viewportPosition.X);
viewportPosition.Y = Math.Round(viewportPosition.Y);
}
}
2012-04-25 22:02:53 +02:00
var translateTransform = element.RenderTransform as TranslateTransform;
2012-04-25 22:02:53 +02:00
if (translateTransform == null)
2012-04-25 22:02:53 +02:00
{
var transformGroup = element.RenderTransform as TransformGroup;
if (transformGroup == null)
2012-04-25 22:02:53 +02:00
{
translateTransform = new TranslateTransform();
element.RenderTransform = translateTransform;
}
else
{
if (transformGroup.Children.Count > 0)
{
translateTransform = transformGroup.Children[transformGroup.Children.Count - 1] as TranslateTransform;
}
if (translateTransform == null)
{
translateTransform = new TranslateTransform();
transformGroup.Children.Add(translateTransform);
}
}
}
translateTransform.X = viewportPosition.X;
translateTransform.Y = viewportPosition.Y;
}
private static void SetBoundingBoxRect(UIElement element, MapBase parentMap, BoundingBox boundingBox)
{
var rotation = 0d;
var viewportPosition = new Point();
if (parentMap != null && boundingBox != null)
{
var projection = parentMap.MapProjection;
var rect = projection.BoundingBoxToRect(boundingBox);
var center = new Point(rect.X + rect.Width / 2d, rect.Y + rect.Height / 2d);
rotation = parentMap.Heading;
viewportPosition = projection.ViewportTransform.Transform(center);
if (viewportPosition.X < 0d || viewportPosition.X > parentMap.RenderSize.Width ||
viewportPosition.Y < 0d || viewportPosition.Y > parentMap.RenderSize.Height)
{
var location = projection.PointToLocation(center);
location.Longitude = Location.NearestLongitude(location.Longitude, parentMap.Center.Longitude);
viewportPosition = projection.LocationToViewportPoint(location);
}
var width = rect.Width * projection.ViewportScale;
var height = rect.Height * projection.ViewportScale;
if (element is FrameworkElement)
{
((FrameworkElement)element).Width = width;
((FrameworkElement)element).Height = height;
}
else
{
element.Arrange(new Rect(-width / 2d, -height / 2d, width, height)); // ???
}
}
var transformGroup = element.RenderTransform as TransformGroup;
RotateTransform rotateTransform;
TranslateTransform translateTransform;
if (transformGroup == null ||
transformGroup.Children.Count != 2 ||
(rotateTransform = transformGroup.Children[0] as RotateTransform) == null ||
(translateTransform = transformGroup.Children[1] as TranslateTransform) == null)
{
transformGroup = new TransformGroup();
rotateTransform = new RotateTransform();
translateTransform = new TranslateTransform();
transformGroup.Children.Add(rotateTransform);
transformGroup.Children.Add(translateTransform);
element.RenderTransform = transformGroup;
element.RenderTransformOrigin = new Point(0.5, 0.5);
}
rotateTransform.Angle = rotation;
translateTransform.X = viewportPosition.X;
translateTransform.Y = viewportPosition.Y;
}
private static void ArrangeElementWithBoundingBox(UIElement element)
{
var size = element.DesiredSize;
element.Arrange(new Rect(-size.Width / 2d, -size.Height / 2d, size.Width, size.Height));
2012-04-25 22:02:53 +02:00
}
2012-12-07 17:04:44 +01:00
private static void ArrangeElementWithLocation(UIElement element)
2012-12-07 17:04:44 +01:00
{
var rect = new Rect(new Point(), element.DesiredSize);
if (element is FrameworkElement)
2012-12-07 17:04:44 +01:00
{
switch (((FrameworkElement)element).HorizontalAlignment)
{
case HorizontalAlignment.Center:
rect.X = -rect.Width / 2d;
break;
2012-12-07 17:04:44 +01:00
case HorizontalAlignment.Right:
rect.X = -rect.Width;
break;
2012-12-07 17:04:44 +01:00
default:
break;
}
2012-12-07 17:04:44 +01:00
switch (((FrameworkElement)element).VerticalAlignment)
{
case VerticalAlignment.Center:
rect.Y = -rect.Height / 2d;
break;
2012-12-07 17:04:44 +01:00
case VerticalAlignment.Bottom:
rect.Y = -rect.Height;
break;
2012-12-07 17:04:44 +01:00
default:
break;
}
}
element.Arrange(rect);
}
private static void ArrangeElement(UIElement element, Size parentSize)
{
var rect = new Rect(new Point(), element.DesiredSize);
if (element is FrameworkElement)
{
switch (((FrameworkElement)element).HorizontalAlignment)
{
case HorizontalAlignment.Center:
rect.X = (parentSize.Width - rect.Width) / 2d;
break;
2012-12-07 17:04:44 +01:00
case HorizontalAlignment.Right:
rect.X = parentSize.Width - rect.Width;
break;
2012-12-07 17:04:44 +01:00
case HorizontalAlignment.Stretch:
rect.Width = parentSize.Width;
break;
2012-12-07 17:04:44 +01:00
default:
break;
}
2012-12-07 17:04:44 +01:00
switch (((FrameworkElement)element).VerticalAlignment)
{
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;
2012-12-07 17:04:44 +01:00
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
}
element.Arrange(rect);
2012-12-07 17:04:44 +01:00
}
2012-04-25 22:02:53 +02:00
}
}