Add MapBorderPanel

This commit is contained in:
ClemensFischer 2023-01-14 18:41:10 +01:00
parent be8c1d2c4b
commit 07710f0e40
5 changed files with 112 additions and 6 deletions

View file

@ -0,0 +1,94 @@
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// Copyright © 2023 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
#if WINUI
using Microsoft.UI.Xaml;
#elif UWP
using Windows.UI.Xaml;
#else
using System.Windows;
#endif
namespace MapControl
{
/// <summary>
/// A MapPanel that adjusts the ViewPosition property of its child elements so that
/// elements that would be outside the current viewport are arranged on a border area.
/// Such elements are arranged at a distance of BorderWidth/2 from the edges of the
/// MapBorderPanel in direction of their original azimuth from the map center.
/// </summary>
public class MapBorderPanel : MapPanel
{
public static readonly DependencyProperty BorderWidthProperty = DependencyProperty.Register(
nameof(BorderWidth), typeof(double), typeof(MapBorderPanel), null);
public static readonly DependencyProperty OnBorderProperty = DependencyProperty.Register(
"OnBorder", typeof(bool), typeof(MapBorderPanel), null);
public double BorderWidth
{
get => (double)GetValue(BorderWidthProperty);
set => SetValue(BorderWidthProperty, value);
}
public static bool GetOnBorder(FrameworkElement element)
{
return (bool)element.GetValue(OnBorderProperty);
}
protected override void SetViewPosition(FrameworkElement element, ref Point? position)
{
var onBorder = false;
var w = ParentMap.RenderSize.Width;
var h = ParentMap.RenderSize.Height;
var minX = BorderWidth / 2d;
var minY = BorderWidth / 2d;
var maxX = w - BorderWidth / 2d;
var maxY = h - BorderWidth / 2d;
if (position.HasValue &&
(position.Value.X < minX || position.Value.X > maxX ||
position.Value.Y < minY || position.Value.Y > maxY))
{
var dx = position.Value.X - w / 2d;
var dy = position.Value.Y - h / 2d;
var cx = (maxX - minX) / 2d;
var cy = (maxY - minY) / 2d;
double x, y;
if (dx < 0d)
{
x = minX;
y = minY + cy - cx * dy / dx;
}
else
{
x = maxX;
y = minY + cy + cx * dy / dx;
}
if (y < minY || y > maxY)
{
if (dy < 0d)
{
x = minX + cx - cy * dx / dy;
y = minY;
}
else
{
x = minX + cx + cy * dx / dy;
y = maxY;
}
}
position = new Point(x, y);
onBorder = true;
}
element.SetValue(OnBorderProperty, onBorder);
base.SetViewPosition(element, ref position);
}
}
}

View file

@ -105,8 +105,7 @@ namespace MapControl
}
/// <summary>
/// Gets the view position of an element with Location
/// or null when the element has no Location.
/// Gets the view position of an element with Location.
/// </summary>
public static Point? GetViewPosition(FrameworkElement element)
{
@ -161,7 +160,7 @@ namespace MapControl
var location = GetLocation(element);
var position = location != null ? GetViewPosition(location) : null;
SetViewPosition(element, position);
SetViewPosition(element, ref position);
if (GetAutoCollapse(element))
{
@ -203,7 +202,7 @@ namespace MapControl
return finalSize;
}
protected virtual Point? GetViewPosition(Location location)
protected Point? GetViewPosition(Location location)
{
var position = parentMap.LocationToView(location);

View file

@ -104,6 +104,9 @@
<Compile Include="..\Shared\MapBase.cs">
<Link>MapBase.cs</Link>
</Compile>
<Compile Include="..\Shared\MapBorderPanel.cs">
<Link>MapBorderPanel.cs</Link>
</Compile>
<Compile Include="..\Shared\MapGraticule.cs">
<Link>MapGraticule.cs</Link>
</Compile>

View file

@ -39,7 +39,12 @@ namespace MapControl
return (MapBase)element.GetValue(ParentMapProperty);
}
private static void SetViewPosition(FrameworkElement element, Point? position)
/// <summary>
/// Sets the attached ViewPosition property of an element. The method is called during
/// ArrangeOverride and may be overridden to modify the actual view position value.
/// An overridden method should call this method to set the attached property.
/// </summary>
protected virtual void SetViewPosition(FrameworkElement element, ref Point? position)
{
element.SetValue(ViewPositionPropertyKey, position);
}

View file

@ -67,7 +67,12 @@ namespace MapControl
return parentMap;
}
private static void SetViewPosition(FrameworkElement element, Point? position)
/// <summary>
/// Sets the attached ViewPosition property of an element. The method is called during
/// ArrangeOverride and may be overridden to modify the actual view position value.
/// An overridden method should call this method to set the attached property.
/// </summary>
protected virtual void SetViewPosition(FrameworkElement element, ref Point? position)
{
element.SetValue(ViewPositionProperty, position);
}