XAML-Map-Control/MapControl/MapPanel.cs

279 lines
9 KiB
C#
Raw Normal View History

// XAML Map Control - http://xamlmapcontrol.codeplex.com/
2016-02-23 20:07:30 +01:00
// © 2016 Clemens Fischer
2012-05-04 12:52:20 +02:00
// Licensed under the Microsoft Public License (Ms-PL)
using System;
#if NETFX_CORE
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
#else
2012-04-25 22:02:53 +02:00
using System.Windows;
using System.Windows.Media;
#endif
2012-04-25 22:02:53 +02:00
namespace MapControl
{
2012-05-04 12:52:20 +02:00
/// <summary>
/// Arranges child elements on a Map at positions specified by the attached property Location.
/// The Location value is transformed to a viewport position and assigned as TranslateTransform
/// to the RenderTransform of the element, either directly or as last child of a TransformGroup.
2012-05-04 12:52:20 +02:00
/// </summary>
public partial class MapPanel : PanelBase, 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));
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);
}
private MapBase parentMap;
public MapBase ParentMap
{
get { return parentMap; }
set { SetParentMapOverride(value); }
}
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
{
Location location;
if (!(element is IMapShape) && (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
{
ArrangeElementWithoutLocation(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();
}
}
protected virtual void OnViewportChanged()
{
foreach (UIElement element in Children)
{
Location location;
if (!(element is IMapShape) && (location = GetLocation(element)) != null)
{
SetViewportPosition(element, parentMap, location);
}
}
}
private void OnViewportChanged(object sender, EventArgs e)
{
OnViewportChanged();
}
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 mapShape = element as IMapShape;
2012-04-25 22:02:53 +02:00
if (mapShape != null)
2012-04-25 22:02:53 +02:00
{
mapShape.LocationChanged((Location)e.OldValue, (Location)e.NewValue);
}
else
{
if (e.NewValue == null)
{
ArrangeElementWithoutLocation(element, Size.Empty);
}
else if (e.OldValue == null)
{
ArrangeElementWithLocation(element);
}
SetViewportPosition(element, GetParentMap(element), (Location)e.NewValue);
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.LocationToViewportPoint(location);
if (viewportPosition.X < 0d || viewportPosition.X > parentMap.RenderSize.Width ||
viewportPosition.Y < 0d || viewportPosition.Y > parentMap.RenderSize.Height)
{
viewportPosition = parentMap.LocationToViewportPoint(new Location(
location.Latitude,
Location.NearestLongitude(location.Longitude, parentMap.Center.Longitude)));
}
if ((bool)element.GetValue(FrameworkElement.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;
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);
var frameworkElement = element as FrameworkElement;
if (frameworkElement != null)
2012-12-07 17:04:44 +01:00
{
switch (frameworkElement.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.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 ArrangeElementWithoutLocation(UIElement element, Size parentSize)
{
var rect = new Rect(new Point(), element.DesiredSize);
var frameworkElement = element as FrameworkElement;
if (frameworkElement != null)
{
if (parentSize.IsEmpty)
{
var parent = frameworkElement.Parent as UIElement;
parentSize = parent != null ? parent.RenderSize : new Size();
}
2012-12-07 17:04:44 +01:00
switch (frameworkElement.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.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
}
}