XAML-Map-Control/MapControl/MapPanel.cs

217 lines
6.9 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 WINRT
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));
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);
}
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 location = GetLocation(element);
2012-04-25 22:02:53 +02:00
if (location != null)
2012-04-25 22:02:53 +02:00
{
SetViewportPosition(element, parentMap, location);
}
var rect = new Rect(0d, 0d, element.DesiredSize.Width, element.DesiredSize.Height);
var frameworkElement = element as FrameworkElement;
if (frameworkElement != null)
{
switch (frameworkElement.HorizontalAlignment)
{
case HorizontalAlignment.Center:
rect.X = ((location == null ? finalSize.Width : 0) - rect.Width) / 2d;
break;
case HorizontalAlignment.Right:
rect.X = (location == null ? finalSize.Width : 0) - rect.Width;
break;
case HorizontalAlignment.Stretch:
if (location == null)
{
rect.Width = finalSize.Width;
}
break;
default:
break;
}
switch (frameworkElement.VerticalAlignment)
{
case VerticalAlignment.Center:
rect.Y = ((location == null ? finalSize.Height : 0) - rect.Height) / 2d;
break;
case VerticalAlignment.Bottom:
rect.Y = (location == null ? finalSize.Height : 0) - rect.Height;
break;
case VerticalAlignment.Stretch:
if (location == null)
{
rect.Height = finalSize.Height;
}
break;
default:
break;
}
2012-04-25 22:02:53 +02:00
}
element.Arrange(rect);
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
{
Transform transform = null;
if (parentMap != null && location != null)
{
Point position = parentMap.LocationToViewportPoint(location);
transform = new TranslateTransform { X = position.X, Y = position.Y };
}
2012-04-25 22:02:53 +02:00
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
{
if (transform == null)
2012-04-25 22:02:53 +02:00
{
transform = new TranslateTransform();
2012-04-25 22:02:53 +02:00
}
var transformIndex = transformGroup.Children.Count - 1;
if (transformIndex >= 0 &&
transformGroup.Children[transformIndex] is TranslateTransform)
{
transformGroup.Children[transformIndex] = transform;
}
else
2012-04-25 22:02:53 +02:00
{
transformGroup.Children.Add(transform);
2012-04-25 22:02:53 +02:00
}
}
else if (transform != null)
{
element.RenderTransform = transform;
}
else
{
element.ClearValue(UIElement.RenderTransformProperty);
}
2012-04-25 22:02:53 +02:00
}
}
}