2012-11-22 21:42:29 +01:00
|
|
|
|
// 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)
|
|
|
|
|
|
|
2012-10-12 19:22:49 +02:00
|
|
|
|
using System;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
#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;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
|
#endif
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2012-11-22 21:42:29 +01:00
|
|
|
|
internal interface IMapElement
|
|
|
|
|
|
{
|
|
|
|
|
|
void ParentMapChanged(MapBase oldParentMap, MapBase newParentMap);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-05-04 12:52:20 +02:00
|
|
|
|
/// <summary>
|
2012-11-22 21:42:29 +01:00
|
|
|
|
/// 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>
|
2012-11-23 16:16:09 +01:00
|
|
|
|
public partial class MapPanel : Panel, IMapElement
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-08-10 09:41:59 +02:00
|
|
|
|
public static readonly DependencyProperty LocationProperty = DependencyProperty.RegisterAttached(
|
2012-11-22 21:42:29 +01:00
|
|
|
|
"Location", typeof(Location), typeof(MapPanel), new PropertyMetadata(null, LocationPropertyChanged));
|
2012-08-10 09:41:59 +02:00
|
|
|
|
|
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)
|
|
|
|
|
|
{
|
2012-11-22 21:42:29 +01:00
|
|
|
|
foreach (UIElement element in Children)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-11-22 21:42:29 +01:00
|
|
|
|
element.Measure(availableSize);
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return new Size();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override Size ArrangeOverride(Size finalSize)
|
|
|
|
|
|
{
|
2012-11-22 21:42:29 +01:00
|
|
|
|
var parentMap = GetParentMap(this);
|
|
|
|
|
|
|
|
|
|
|
|
foreach (UIElement element in Children)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-11-22 21:42:29 +01:00
|
|
|
|
var location = GetLocation(element);
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
2012-11-22 21:42:29 +01:00
|
|
|
|
if (location != null)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-11-22 21:42:29 +01:00
|
|
|
|
SetViewportPosition(element, parentMap, location);
|
2012-08-26 21:54:00 +02:00
|
|
|
|
}
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
|
|
|
|
|
var rect = new Rect(0d, 0d, element.DesiredSize.Width, element.DesiredSize.Height);
|
2012-11-23 16:16:09 +01:00
|
|
|
|
var frameworkElement = element as FrameworkElement;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
|
|
|
|
|
if (frameworkElement != null)
|
2012-08-26 21:54:00 +02:00
|
|
|
|
{
|
2012-11-22 21:42:29 +01:00
|
|
|
|
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
|
|
|
|
}
|
2012-11-22 21:42:29 +01: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
|
|
|
|
{
|
2012-11-22 21:42:29 +01:00
|
|
|
|
var parentMap = GetParentMap(this);
|
|
|
|
|
|
|
|
|
|
|
|
foreach (UIElement element in Children)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-11-22 21:42:29 +01: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
|
|
|
|
{
|
2012-11-22 21:42:29 +01:00
|
|
|
|
SetViewportPosition(element, parentMap, location);
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-10-12 19:22:49 +02:00
|
|
|
|
private void OnViewportChanged(object sender, EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
OnViewportChanged();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-11-22 21:42:29 +01:00
|
|
|
|
void IMapElement.ParentMapChanged(MapBase oldParentMap, MapBase newParentMap)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-11-22 21:42:29 +01:00
|
|
|
|
if (oldParentMap != null && oldParentMap != this)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-11-22 21:42:29 +01:00
|
|
|
|
oldParentMap.ViewportChanged -= OnViewportChanged;
|
|
|
|
|
|
}
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
2012-11-22 21:42:29 +01:00
|
|
|
|
if (newParentMap != null && newParentMap != this)
|
|
|
|
|
|
{
|
|
|
|
|
|
newParentMap.ViewportChanged += OnViewportChanged;
|
|
|
|
|
|
OnViewportChanged();
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-11-22 21:42:29 +01:00
|
|
|
|
private static void ParentMapPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
|
2012-08-10 09:41:59 +02:00
|
|
|
|
{
|
2012-11-22 21:42:29 +01:00
|
|
|
|
var element = obj as IMapElement;
|
2012-08-10 09:41:59 +02:00
|
|
|
|
|
|
|
|
|
|
if (element != null)
|
|
|
|
|
|
{
|
2012-11-22 21:42:29 +01:00
|
|
|
|
element.ParentMapChanged(e.OldValue as MapBase, e.NewValue as MapBase);
|
2012-08-10 09:41:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-22 20:21:31 +02:00
|
|
|
|
private static void LocationPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-11-22 21:42:29 +01:00
|
|
|
|
var element = obj as UIElement;
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
2012-08-09 23:41:47 +02:00
|
|
|
|
if (element != null)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-08-22 20:21:31 +02:00
|
|
|
|
SetViewportPosition(element, GetParentMap(element), (Location)e.NewValue);
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-10-12 19:22:49 +02:00
|
|
|
|
private static void SetViewportPosition(UIElement element, MapBase parentMap, Location location)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-11-22 21:42:29 +01:00
|
|
|
|
Transform transform = null;
|
2012-08-09 23:41:47 +02:00
|
|
|
|
|
|
|
|
|
|
if (parentMap != null && location != null)
|
|
|
|
|
|
{
|
2012-11-22 21:42:29 +01:00
|
|
|
|
Point position = parentMap.LocationToViewportPoint(location);
|
|
|
|
|
|
transform = new TranslateTransform { X = position.X, Y = position.Y };
|
2012-08-09 23:41:47 +02:00
|
|
|
|
}
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
2012-11-22 21:42:29 +01:00
|
|
|
|
var transformGroup = element.RenderTransform as TransformGroup;
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
2012-11-22 21:42:29 +01:00
|
|
|
|
if (transformGroup != null)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-11-22 21:42:29 +01:00
|
|
|
|
if (transform == null)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-11-22 21:42:29 +01:00
|
|
|
|
transform = new TranslateTransform();
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-11-22 21:42:29 +01: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
|
|
|
|
{
|
2012-11-22 21:42:29 +01:00
|
|
|
|
transformGroup.Children.Add(transform);
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2012-11-22 21:42:29 +01:00
|
|
|
|
else if (transform != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
element.RenderTransform = transform;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
element.ClearValue(UIElement.RenderTransformProperty);
|
|
|
|
|
|
}
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|