XAML-Map-Control/MapControl/MapPanel.cs

295 lines
9.7 KiB
C#
Raw Normal View History

// XAML Map Control - http://xamlmapcontrol.codeplex.com/
// Copyright © Clemens Fischer 2012-2013
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.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
{
internal interface IMapElement
{
MapBase ParentMap { get; set; }
}
2012-05-04 12:52:20 +02:00
/// <summary>
/// Positions child elements on a Map, at a position specified by the attached property Location.
/// The Location is transformed to a viewport position by ParentMap.MapTransform and ParentMap.ViewportTransform
/// and applied to a child element's RenderTransform as an appropriate TranslateTransform.
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 ViewportPositionProperty = DependencyProperty.RegisterAttached(
"ViewportPosition", typeof(Point?), typeof(MapPanel), null);
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 Point? GetViewportPosition(UIElement element)
{
return (Point?)element.GetValue(ViewportPositionProperty);
}
private MapBase parentMap;
public MapBase ParentMap
{
get { return parentMap; }
set
{
if (parentMap != null && parentMap != this)
{
parentMap.ViewportChanged -= OnViewportChanged;
}
parentMap = value;
if (parentMap != null && parentMap != this)
{
parentMap.ViewportChanged += OnViewportChanged;
OnViewportChanged();
}
}
}
2012-04-25 22:02:53 +02:00
protected override Size MeasureOverride(Size availableSize)
{
foreach (UIElement element in InternalChildren)
2012-04-25 22:02:53 +02:00
{
element.Measure(availableSize);
2012-04-25 22:02:53 +02:00
}
return new Size();
}
protected override Size ArrangeOverride(Size finalSize)
{
foreach (UIElement element in InternalChildren)
2012-04-25 22:02:53 +02:00
{
2012-12-07 17:04:44 +01:00
var location = GetLocation(element);
ArrangeElement(element, finalSize, location != null);
2012-12-07 17:04:44 +01:00
if (location != null)
{
SetViewportPosition(element, parentMap, location);
}
2012-04-25 22:02:53 +02:00
}
return finalSize;
}
protected virtual void OnViewportChanged()
{
foreach (UIElement element in InternalChildren)
{
var location = GetLocation(element);
if (location != 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 = obj as UIElement;
2012-04-25 22:02:53 +02:00
if (element != null)
2012-04-25 22:02:53 +02:00
{
var mapElement = element as IMapElement;
var parentMap = mapElement != null ? mapElement.ParentMap : GetParentMap(element);
var location = e.NewValue as Location;
if ((location != null) != (e.OldValue != null))
{
ArrangeElement(element, null, location != null);
}
SetViewportPosition(element, parentMap, location);
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
{
Point viewportPosition;
if (parentMap != null && location != null)
{
// transform location before checking longitude to keep location.TransformedLatitude
var mapPosition = parentMap.MapTransform.Transform(location);
mapPosition.X = Location.NormalizeLongitude(mapPosition.X);
var centerOffset = mapPosition.X - parentMap.Center.Longitude; // keep viewport position near map center
if (centerOffset > 180d)
{
mapPosition.X -= 360d;
}
else if (centerOffset < -180d)
{
mapPosition.X += 360d;
}
viewportPosition = parentMap.ViewportTransform.Transform(mapPosition);
element.SetValue(ViewportPositionProperty, viewportPosition);
}
else
{
viewportPosition = new Point();
element.ClearValue(ViewportPositionProperty);
}
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 ArrangeElement(UIElement element, Size? panelSize, bool hasLocation)
2012-12-07 17:04:44 +01:00
{
var rect = new Rect(0d, 0d, element.DesiredSize.Width, element.DesiredSize.Height);
var frameworkElement = element as FrameworkElement;
if (frameworkElement != null)
2012-12-07 17:04:44 +01:00
{
if (hasLocation)
{
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;
}
}
else
{
if (!panelSize.HasValue)
{
var panel = frameworkElement.Parent as Panel;
panelSize = panel != null ? panel.RenderSize : new Size();
}
2012-12-07 17:04:44 +01:00
switch (frameworkElement.HorizontalAlignment)
{
case HorizontalAlignment.Center:
rect.X = (panelSize.Value.Width - rect.Width) / 2d;
break;
2012-12-07 17:04:44 +01:00
case HorizontalAlignment.Right:
rect.X = panelSize.Value.Width - rect.Width;
break;
2012-12-07 17:04:44 +01:00
case HorizontalAlignment.Stretch:
rect.Width = panelSize.Value.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 = (panelSize.Value.Height - rect.Height) / 2d;
break;
2012-12-07 17:04:44 +01:00
case VerticalAlignment.Bottom:
rect.Y = panelSize.Value.Height - rect.Height;
break;
2012-12-07 17:04:44 +01:00
case VerticalAlignment.Stretch:
rect.Height = panelSize.Value.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
}
}