XAML-Map-Control/MapControl/MapPanel.cs

268 lines
8.4 KiB
C#
Raw Normal View History

// XAML Map Control - http://xamlmapcontrol.codeplex.com/
2012-05-04 12:52:20 +02:00
// Copyright © 2012 Clemens Fischer
// 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
{
void ParentMapChanged(MapBase oldParentMap, MapBase newParentMap);
}
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 into a viewport position by the MapBase.LocationToViewportPoint
/// method and then applied to the 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);
}
2012-04-25 22:02:53 +02:00
protected override Size MeasureOverride(Size availableSize)
{
foreach (UIElement element in Children)
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)
{
var parentMap = GetParentMap(this);
foreach (UIElement element in Children)
2012-04-25 22:02:53 +02:00
{
var rect = new Rect(0d, 0d, element.DesiredSize.Width, element.DesiredSize.Height);
2012-12-07 17:04:44 +01:00
var location = GetLocation(element);
2012-12-07 17:04:44 +01:00
if (element is FrameworkElement)
{
2012-12-07 17:04:44 +01:00
if (location != null)
{
2012-12-07 17:04:44 +01:00
AlignElementWithLocation((FrameworkElement)element, ref rect);
}
2012-12-07 17:04:44 +01:00
else
{
2012-12-07 17:04:44 +01:00
AlignElementWithoutLocation((FrameworkElement)element, finalSize, ref rect);
}
2012-04-25 22:02:53 +02:00
}
element.Arrange(rect);
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;
}
2012-08-08 20:42:06 +02:00
protected virtual void OnViewportChanged()
2012-04-25 22:02:53 +02:00
{
var parentMap = GetParentMap(this);
foreach (UIElement element in Children)
2012-04-25 22:02:53 +02:00
{
var location = GetLocation(element);
2012-04-25 22:02:53 +02:00
2012-05-04 12:52:20 +02:00
if (location != null)
2012-04-25 22:02:53 +02:00
{
SetViewportPosition(element, parentMap, location);
2012-04-25 22:02:53 +02:00
}
}
}
private void OnViewportChanged(object sender, EventArgs e)
{
OnViewportChanged();
}
void IMapElement.ParentMapChanged(MapBase oldParentMap, MapBase newParentMap)
2012-04-25 22:02:53 +02:00
{
if (oldParentMap != null && oldParentMap != this)
2012-04-25 22:02:53 +02:00
{
oldParentMap.ViewportChanged -= OnViewportChanged;
}
2012-04-25 22:02:53 +02:00
if (newParentMap != null && newParentMap != this)
{
newParentMap.ViewportChanged += OnViewportChanged;
OnViewportChanged();
2012-04-25 22:02:53 +02:00
}
}
private static void ParentMapPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
var element = obj as IMapElement;
if (element != null)
{
element.ParentMapChanged(e.OldValue as MapBase, 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
{
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
{
Point viewportPosition = new Point();
if (parentMap != null && location != null)
{
viewportPosition = parentMap.LocationToViewportPoint(location);
element.SetValue(ViewportPositionProperty, viewportPosition);
}
else
{
element.ClearValue(ViewportPositionProperty);
}
2012-04-25 22:02:53 +02:00
TranslateTransform translateTransform;
var transformGroup = element.RenderTransform as TransformGroup;
2012-04-25 22:02:53 +02:00
if (transformGroup != null)
2012-04-25 22:02:53 +02:00
{
var last = transformGroup.Children.Count - 1;
if (last >= 0 && transformGroup.Children[last] is TranslateTransform)
2012-04-25 22:02:53 +02:00
{
translateTransform = (TranslateTransform)transformGroup.Children[last];
}
else
2012-04-25 22:02:53 +02:00
{
translateTransform = new TranslateTransform();
transformGroup.Children.Add(translateTransform);
2012-04-25 22:02:53 +02:00
}
}
else
{
if (element.RenderTransform is TranslateTransform)
{
translateTransform = (TranslateTransform)element.RenderTransform;
}
else
{
translateTransform = new TranslateTransform();
element.RenderTransform = 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 AlignElementWithLocation(FrameworkElement element, ref Rect arrangeRect)
{
switch (element.HorizontalAlignment)
{
case HorizontalAlignment.Center:
arrangeRect.X = -arrangeRect.Width / 2d;
break;
case HorizontalAlignment.Right:
arrangeRect.X = -arrangeRect.Width;
break;
default:
break;
}
switch (element.VerticalAlignment)
{
case VerticalAlignment.Center:
arrangeRect.Y = -arrangeRect.Height / 2d;
break;
case VerticalAlignment.Bottom:
arrangeRect.Y = -arrangeRect.Height;
break;
default:
break;
}
}
private static void AlignElementWithoutLocation(FrameworkElement element, Size panelSize, ref Rect arrangeRect)
{
switch (element.HorizontalAlignment)
{
case HorizontalAlignment.Center:
arrangeRect.X = (panelSize.Width - arrangeRect.Width) / 2d;
break;
case HorizontalAlignment.Right:
arrangeRect.X = panelSize.Width - arrangeRect.Width;
break;
case HorizontalAlignment.Stretch:
arrangeRect.Width = panelSize.Width;
break;
default:
break;
}
switch (element.VerticalAlignment)
{
case VerticalAlignment.Center:
arrangeRect.Y = (panelSize.Height - arrangeRect.Height) / 2d;
break;
case VerticalAlignment.Bottom:
arrangeRect.Y = panelSize.Height - arrangeRect.Height;
break;
case VerticalAlignment.Stretch:
arrangeRect.Height = panelSize.Height;
break;
default:
break;
}
}
2012-04-25 22:02:53 +02:00
}
}