2017-06-25 23:05:48 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2024-02-03 21:01:53 +01:00
|
|
|
|
// Copyright © 2024 Clemens Fischer
|
2012-05-04 12:52:20 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
2024-05-22 15:14:46 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Diagnostics;
|
2024-05-22 11:25:32 +02:00
|
|
|
|
#if WPF
|
|
|
|
|
|
using System.Windows;
|
|
|
|
|
|
using System.Windows.Controls;
|
|
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
|
#elif UWP
|
|
|
|
|
|
using Windows.Foundation;
|
|
|
|
|
|
using Windows.UI.Xaml;
|
|
|
|
|
|
using Windows.UI.Xaml.Controls;
|
|
|
|
|
|
using Windows.UI.Xaml.Media;
|
|
|
|
|
|
#elif WINUI
|
|
|
|
|
|
using Windows.Foundation;
|
|
|
|
|
|
using Microsoft.UI.Xaml;
|
|
|
|
|
|
using Microsoft.UI.Xaml.Controls;
|
|
|
|
|
|
using Microsoft.UI.Xaml.Media;
|
|
|
|
|
|
#elif AVALONIA
|
2024-05-20 23:24:34 +02:00
|
|
|
|
using Avalonia.Controls;
|
|
|
|
|
|
using Avalonia.Media;
|
|
|
|
|
|
using DependencyProperty = Avalonia.AvaloniaProperty;
|
|
|
|
|
|
using FrameworkElement = Avalonia.Controls.Control;
|
|
|
|
|
|
using HorizontalAlignment = Avalonia.Layout.HorizontalAlignment;
|
|
|
|
|
|
using VerticalAlignment = Avalonia.Layout.VerticalAlignment;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
#endif
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
2024-05-20 23:24:34 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Arranges child elements on a Map at positions specified by the attached property Location,
|
|
|
|
|
|
/// or in rectangles specified by the attached property BoundingBox.
|
|
|
|
|
|
/// </summary>
|
2012-04-25 22:02:53 +02:00
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2017-09-05 20:57:17 +02:00
|
|
|
|
/// <summary>
|
2021-01-16 18:32:31 +01:00
|
|
|
|
/// Optional interface to hold the value of the attached property MapPanel.ParentMap.
|
2017-09-05 20:57:17 +02:00
|
|
|
|
/// </summary>
|
2017-06-25 23:05:48 +02:00
|
|
|
|
public interface IMapElement
|
|
|
|
|
|
{
|
|
|
|
|
|
MapBase ParentMap { get; set; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public partial class MapPanel : Panel, IMapElement
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2024-05-20 23:24:34 +02:00
|
|
|
|
public static readonly DependencyProperty AutoCollapseProperty =
|
|
|
|
|
|
DependencyPropertyHelper.RegisterAttached<MapPanel, bool>("AutoCollapse");
|
|
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty LocationProperty =
|
|
|
|
|
|
DependencyPropertyHelper.RegisterAttached<MapPanel, Location>("Location", null, false,
|
2024-05-22 11:47:57 +02:00
|
|
|
|
(element, oldValue, newValue) => (element.Parent as MapPanel)?.InvalidateArrange());
|
2024-05-20 23:24:34 +02:00
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty BoundingBoxProperty =
|
|
|
|
|
|
DependencyPropertyHelper.RegisterAttached<MapPanel, BoundingBox>("BoundingBox", null, false,
|
2024-05-22 11:47:57 +02:00
|
|
|
|
(element, oldValue, newValue) => (element.Parent as MapPanel)?.InvalidateArrange());
|
2024-05-20 23:24:34 +02:00
|
|
|
|
|
|
|
|
|
|
private static readonly DependencyProperty ViewPositionProperty =
|
|
|
|
|
|
DependencyPropertyHelper.RegisterAttached<MapPanel, Point?>("ViewPosition");
|
|
|
|
|
|
|
|
|
|
|
|
private static readonly DependencyProperty ParentMapProperty =
|
|
|
|
|
|
DependencyPropertyHelper.RegisterAttached<MapPanel, MapBase>("ParentMap", null, true,
|
2024-05-22 11:47:57 +02:00
|
|
|
|
(element, oldValue, newValue) =>
|
2024-05-20 23:24:34 +02:00
|
|
|
|
{
|
2024-05-22 11:47:57 +02:00
|
|
|
|
if (element is IMapElement mapElement)
|
2024-05-20 23:24:34 +02:00
|
|
|
|
{
|
|
|
|
|
|
mapElement.ParentMap = newValue;
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2020-07-14 17:03:36 +02:00
|
|
|
|
|
2017-09-05 20:57:17 +02:00
|
|
|
|
private MapBase parentMap;
|
|
|
|
|
|
|
2023-01-19 17:16:02 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Implements IMapElement.ParentMap.
|
|
|
|
|
|
/// </summary>
|
2020-07-14 17:03:36 +02:00
|
|
|
|
public MapBase ParentMap
|
|
|
|
|
|
{
|
2022-08-06 10:40:59 +02:00
|
|
|
|
get => parentMap;
|
|
|
|
|
|
set => SetParentMap(value);
|
2020-07-14 17:03:36 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-13 00:08:55 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets a value that controls whether an element's Visibility is automatically
|
|
|
|
|
|
/// set to Collapsed when it is located outside the visible viewport area.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static bool GetAutoCollapse(FrameworkElement element)
|
|
|
|
|
|
{
|
|
|
|
|
|
return (bool)element.GetValue(AutoCollapseProperty);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Sets the AutoCollapse property.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static void SetAutoCollapse(FrameworkElement element, bool value)
|
|
|
|
|
|
{
|
|
|
|
|
|
element.SetValue(AutoCollapseProperty, value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-04-16 19:55:59 +02:00
|
|
|
|
/// Gets the Location of an element.
|
2021-01-13 00:08:55 +01:00
|
|
|
|
/// </summary>
|
2018-04-30 23:13:50 +02:00
|
|
|
|
public static Location GetLocation(FrameworkElement 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
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-13 00:08:55 +01:00
|
|
|
|
/// <summary>
|
2024-04-16 19:55:59 +02:00
|
|
|
|
/// Sets the Location of an element.
|
2021-01-13 00:08:55 +01:00
|
|
|
|
/// </summary>
|
2018-04-30 23:13:50 +02:00
|
|
|
|
public static void SetLocation(FrameworkElement element, Location value)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
|
|
|
|
|
element.SetValue(LocationProperty, value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-13 00:08:55 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the BoundingBox of an element.
|
|
|
|
|
|
/// </summary>
|
2018-04-30 23:13:50 +02:00
|
|
|
|
public static BoundingBox GetBoundingBox(FrameworkElement element)
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
|
|
|
|
|
return (BoundingBox)element.GetValue(BoundingBoxProperty);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-13 00:08:55 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Sets the BoundingBox of an element.
|
|
|
|
|
|
/// </summary>
|
2018-04-30 23:13:50 +02:00
|
|
|
|
public static void SetBoundingBox(FrameworkElement element, BoundingBox value)
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
|
|
|
|
|
element.SetValue(BoundingBoxProperty, value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-13 00:08:55 +01:00
|
|
|
|
/// <summary>
|
2023-01-14 18:41:10 +01:00
|
|
|
|
/// Gets the view position of an element with Location.
|
2021-01-13 00:08:55 +01:00
|
|
|
|
/// </summary>
|
2020-03-28 21:53:38 +01:00
|
|
|
|
public static Point? GetViewPosition(FrameworkElement element)
|
2018-04-30 23:13:50 +02:00
|
|
|
|
{
|
2020-03-28 21:53:38 +01:00
|
|
|
|
return (Point?)element.GetValue(ViewPositionProperty);
|
2018-04-30 23:13:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-20 23:24:34 +02:00
|
|
|
|
/// <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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-01 02:06:22 +02:00
|
|
|
|
protected virtual void SetParentMap(MapBase map)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (parentMap != null && parentMap != this)
|
2018-04-30 23:13:50 +02:00
|
|
|
|
{
|
2018-05-01 02:06:22 +02:00
|
|
|
|
parentMap.ViewportChanged -= OnViewportChanged;
|
|
|
|
|
|
}
|
2018-04-30 23:13:50 +02:00
|
|
|
|
|
2018-05-01 02:06:22 +02:00
|
|
|
|
parentMap = map;
|
2018-04-30 23:13:50 +02:00
|
|
|
|
|
2018-05-01 02:06:22 +02:00
|
|
|
|
if (parentMap != null && parentMap != this)
|
|
|
|
|
|
{
|
|
|
|
|
|
parentMap.ViewportChanged += OnViewportChanged;
|
2018-04-30 23:13:50 +02:00
|
|
|
|
|
2018-05-01 02:06:22 +02:00
|
|
|
|
OnViewportChanged(new ViewportChangedEventArgs());
|
2018-04-30 23:13:50 +02:00
|
|
|
|
}
|
2014-07-22 20:02:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-01 12:45:19 +02:00
|
|
|
|
private void OnViewportChanged(object sender, ViewportChangedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
OnViewportChanged(e);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected virtual void OnViewportChanged(ViewportChangedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
InvalidateArrange();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-06-25 23:05:48 +02:00
|
|
|
|
protected override Size MeasureOverride(Size availableSize)
|
|
|
|
|
|
{
|
|
|
|
|
|
availableSize = new Size(double.PositiveInfinity, double.PositiveInfinity);
|
|
|
|
|
|
|
2024-05-20 23:24:34 +02:00
|
|
|
|
foreach (var element in ChildElements)
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
|
|
|
|
|
element.Measure(availableSize);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return new Size();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-04-25 22:02:53 +02:00
|
|
|
|
protected override Size ArrangeOverride(Size finalSize)
|
|
|
|
|
|
{
|
2019-07-08 23:00:57 +02:00
|
|
|
|
if (parentMap != null)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2024-05-20 23:24:34 +02:00
|
|
|
|
foreach (var element in ChildElements)
|
2014-06-11 23:03:13 +02:00
|
|
|
|
{
|
2024-05-22 15:14:46 +02:00
|
|
|
|
try
|
2019-07-08 23:00:57 +02:00
|
|
|
|
{
|
2024-05-22 15:14:46 +02:00
|
|
|
|
ArrangeChildElement(element, finalSize);
|
2021-01-13 00:08:55 +01:00
|
|
|
|
}
|
2024-05-22 15:14:46 +02:00
|
|
|
|
catch (Exception ex)
|
2021-01-13 00:08:55 +01:00
|
|
|
|
{
|
2024-05-22 15:14:46 +02:00
|
|
|
|
Debug.WriteLine($"MapPanel.ArrangeOverride: {element}: {ex.Message}");
|
2022-02-25 20:58:48 +01:00
|
|
|
|
}
|
2014-06-11 23:03:13 +02:00
|
|
|
|
}
|
2015-08-09 20:04:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-04-30 23:13:50 +02:00
|
|
|
|
return finalSize;
|
2015-08-09 20:04:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-14 18:41:10 +01:00
|
|
|
|
protected Point? GetViewPosition(Location location)
|
2022-11-05 00:19:43 +01:00
|
|
|
|
{
|
|
|
|
|
|
var position = parentMap.LocationToView(location);
|
|
|
|
|
|
|
|
|
|
|
|
if (parentMap.MapProjection.Type <= MapProjectionType.NormalCylindrical &&
|
2022-12-02 16:50:10 +01:00
|
|
|
|
position.HasValue &&
|
|
|
|
|
|
IsOutsideViewport(position.Value))
|
2022-11-05 00:19:43 +01:00
|
|
|
|
{
|
2024-03-14 19:40:02 +01:00
|
|
|
|
position = parentMap.LocationToView(
|
2024-05-21 17:39:03 +02:00
|
|
|
|
new Location(location.Latitude, parentMap.CoerceLongitude(location.Longitude)));
|
2022-11-05 00:19:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return position;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-19 17:24:18 +02:00
|
|
|
|
protected ViewRect? GetViewRect(BoundingBox boundingBox)
|
2022-11-05 00:19:43 +01:00
|
|
|
|
{
|
2024-05-19 17:24:18 +02:00
|
|
|
|
var rect = parentMap.MapProjection.BoundingBoxToMap(boundingBox);
|
2022-12-08 18:53:59 +01:00
|
|
|
|
|
2024-05-19 17:24:18 +02:00
|
|
|
|
if (!rect.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return GetViewRect(rect.Value);
|
2022-11-05 00:19:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-19 17:24:18 +02:00
|
|
|
|
protected ViewRect GetViewRect(Rect rect)
|
2022-11-05 00:19:43 +01:00
|
|
|
|
{
|
2024-05-19 17:24:18 +02:00
|
|
|
|
var rectCenter = new Point(rect.X + rect.Width / 2d, rect.Y + rect.Height / 2d);
|
|
|
|
|
|
var position = parentMap.ViewTransform.MapToView(rectCenter);
|
2024-05-20 23:24:34 +02:00
|
|
|
|
var projection = parentMap.MapProjection;
|
2022-11-05 00:19:43 +01:00
|
|
|
|
|
2024-05-20 23:24:34 +02:00
|
|
|
|
if (projection.Type <= MapProjectionType.NormalCylindrical && IsOutsideViewport(position))
|
2022-11-05 00:19:43 +01:00
|
|
|
|
{
|
2024-05-20 23:24:34 +02:00
|
|
|
|
var location = projection.MapToLocation(rectCenter);
|
2022-11-05 00:19:43 +01:00
|
|
|
|
|
|
|
|
|
|
if (location != null)
|
|
|
|
|
|
{
|
2024-04-11 13:21:38 +02:00
|
|
|
|
var pos = parentMap.LocationToView(
|
2024-05-21 17:39:03 +02:00
|
|
|
|
new Location(location.Latitude, parentMap.CoerceLongitude(location.Longitude)));
|
2022-12-02 16:50:10 +01:00
|
|
|
|
|
|
|
|
|
|
if (pos.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
position = pos.Value;
|
|
|
|
|
|
}
|
2022-11-05 00:19:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-19 17:24:18 +02:00
|
|
|
|
var width = rect.Width * parentMap.ViewTransform.Scale;
|
|
|
|
|
|
var height = rect.Height * parentMap.ViewTransform.Scale;
|
2022-11-05 00:19:43 +01:00
|
|
|
|
var x = position.X - width / 2d;
|
|
|
|
|
|
var y = position.Y - height / 2d;
|
|
|
|
|
|
|
|
|
|
|
|
return new ViewRect(x, y, width, height, parentMap.ViewTransform.Rotation);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-14 01:06:46 +01:00
|
|
|
|
private bool IsOutsideViewport(Point point)
|
2021-01-13 21:46:23 +01:00
|
|
|
|
{
|
2021-01-15 18:00:51 +01:00
|
|
|
|
return point.X < 0d || point.X > parentMap.RenderSize.Width
|
|
|
|
|
|
|| point.Y < 0d || point.Y > parentMap.RenderSize.Height;
|
2021-01-13 21:46:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-22 15:14:46 +02:00
|
|
|
|
private void ArrangeChildElement(FrameworkElement element, Size panelSize)
|
|
|
|
|
|
{
|
|
|
|
|
|
var location = GetLocation(element);
|
|
|
|
|
|
var position = location != null ? GetViewPosition(location) : null;
|
|
|
|
|
|
|
|
|
|
|
|
SetViewPosition(element, ref position);
|
|
|
|
|
|
|
|
|
|
|
|
if (GetAutoCollapse(element))
|
|
|
|
|
|
{
|
|
|
|
|
|
SetVisible(element, !(position.HasValue && IsOutsideViewport(position.Value)));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (position.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
ArrangeElement(element, position.Value);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
var boundingBox = GetBoundingBox(element);
|
|
|
|
|
|
|
|
|
|
|
|
if (boundingBox != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var viewRect = GetViewRect(boundingBox);
|
|
|
|
|
|
|
|
|
|
|
|
if (viewRect.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
ArrangeElement(element, viewRect.Value);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
ArrangeElement(element, panelSize);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-13 18:42:08 +01:00
|
|
|
|
private static void ArrangeElement(FrameworkElement element, Point position)
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
2023-01-21 16:08:23 +01:00
|
|
|
|
var size = GetDesiredSize(element);
|
2024-05-19 17:24:18 +02:00
|
|
|
|
var x = position.X;
|
|
|
|
|
|
var y = position.Y;
|
2012-08-09 23:41:47 +02:00
|
|
|
|
|
2018-04-30 23:13:50 +02:00
|
|
|
|
switch (element.HorizontalAlignment)
|
2012-08-09 23:41:47 +02:00
|
|
|
|
{
|
2018-04-30 23:13:50 +02:00
|
|
|
|
case HorizontalAlignment.Center:
|
2024-05-19 17:24:18 +02:00
|
|
|
|
x -= size.Width / 2d;
|
2018-04-30 23:13:50 +02:00
|
|
|
|
break;
|
2016-06-04 08:42:38 +02:00
|
|
|
|
|
2018-04-30 23:13:50 +02:00
|
|
|
|
case HorizontalAlignment.Right:
|
2024-05-19 17:24:18 +02:00
|
|
|
|
x -= size.Width;
|
2018-04-30 23:13:50 +02:00
|
|
|
|
break;
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
2018-04-30 23:13:50 +02:00
|
|
|
|
default:
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
2018-04-30 23:13:50 +02:00
|
|
|
|
switch (element.VerticalAlignment)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2018-04-30 23:13:50 +02:00
|
|
|
|
case VerticalAlignment.Center:
|
2024-05-19 17:24:18 +02:00
|
|
|
|
y -= size.Height / 2d;
|
2018-04-30 23:13:50 +02:00
|
|
|
|
break;
|
2012-11-26 19:17:12 +01:00
|
|
|
|
|
2018-04-30 23:13:50 +02:00
|
|
|
|
case VerticalAlignment.Bottom:
|
2024-05-19 17:24:18 +02:00
|
|
|
|
y -= size.Height;
|
2018-04-30 23:13:50 +02:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-22 10:16:22 +02:00
|
|
|
|
element.Arrange(new Rect(x, y, size.Width, size.Height));
|
2019-07-08 23:00:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-22 15:14:46 +02:00
|
|
|
|
private static void ArrangeElement(FrameworkElement element, Size panelSize)
|
2019-07-08 23:00:57 +02:00
|
|
|
|
{
|
2023-01-21 16:08:23 +01:00
|
|
|
|
var size = GetDesiredSize(element);
|
2024-05-19 17:24:18 +02:00
|
|
|
|
var x = 0d;
|
|
|
|
|
|
var y = 0d;
|
|
|
|
|
|
var width = size.Width;
|
|
|
|
|
|
var height = size.Height;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
2018-04-30 23:13:50 +02:00
|
|
|
|
switch (element.HorizontalAlignment)
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
2018-04-30 23:13:50 +02:00
|
|
|
|
case HorizontalAlignment.Center:
|
2024-05-22 15:14:46 +02:00
|
|
|
|
x = (panelSize.Width - size.Width) / 2d;
|
2018-04-30 23:13:50 +02:00
|
|
|
|
break;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
2018-04-30 23:13:50 +02:00
|
|
|
|
case HorizontalAlignment.Right:
|
2024-05-22 15:14:46 +02:00
|
|
|
|
x = panelSize.Width - size.Width;
|
2019-07-08 23:00:57 +02:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case HorizontalAlignment.Stretch:
|
2024-05-22 15:14:46 +02:00
|
|
|
|
width = panelSize.Width;
|
2018-04-30 23:13:50 +02:00
|
|
|
|
break;
|
2012-12-07 17:04:44 +01:00
|
|
|
|
|
2018-04-30 23:13:50 +02:00
|
|
|
|
default:
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2013-05-25 08:53:50 +02:00
|
|
|
|
|
2018-04-30 23:13:50 +02:00
|
|
|
|
switch (element.VerticalAlignment)
|
2012-12-07 17:04:44 +01:00
|
|
|
|
{
|
2018-04-30 23:13:50 +02:00
|
|
|
|
case VerticalAlignment.Center:
|
2024-05-22 15:14:46 +02:00
|
|
|
|
y = (panelSize.Height - size.Height) / 2d;
|
2018-04-30 23:13:50 +02:00
|
|
|
|
break;
|
2012-12-07 17:04:44 +01:00
|
|
|
|
|
2018-04-30 23:13:50 +02:00
|
|
|
|
case VerticalAlignment.Bottom:
|
2024-05-22 15:14:46 +02:00
|
|
|
|
y = panelSize.Height - size.Height;
|
2019-07-08 23:00:57 +02:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case VerticalAlignment.Stretch:
|
2024-05-22 15:14:46 +02:00
|
|
|
|
height = panelSize.Height;
|
2018-04-30 23:13:50 +02:00
|
|
|
|
break;
|
2012-12-07 17:04:44 +01:00
|
|
|
|
|
2018-04-30 23:13:50 +02:00
|
|
|
|
default:
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2012-12-07 17:04:44 +01:00
|
|
|
|
|
2024-05-22 10:16:22 +02:00
|
|
|
|
element.Arrange(new Rect(x, y, width, height));
|
2020-07-15 16:40:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-13 18:42:08 +01:00
|
|
|
|
private static void ArrangeElement(FrameworkElement element, ViewRect rect)
|
2020-07-15 16:40:33 +02:00
|
|
|
|
{
|
2024-05-19 17:24:18 +02:00
|
|
|
|
element.Width = rect.Rect.Width;
|
|
|
|
|
|
element.Height = rect.Rect.Height;
|
2023-01-13 18:42:08 +01:00
|
|
|
|
|
2024-05-22 10:16:22 +02:00
|
|
|
|
element.Arrange(rect.Rect);
|
2014-06-11 23:03:13 +02:00
|
|
|
|
|
2023-01-13 18:42:08 +01:00
|
|
|
|
if (element.RenderTransform is RotateTransform rotateTransform)
|
2023-01-13 11:43:42 +01:00
|
|
|
|
{
|
2023-01-13 18:42:08 +01:00
|
|
|
|
rotateTransform.Angle = rect.Rotation;
|
2023-01-13 11:43:42 +01:00
|
|
|
|
}
|
2023-01-13 18:42:08 +01:00
|
|
|
|
else if (rect.Rotation != 0d)
|
2023-01-13 11:43:42 +01:00
|
|
|
|
{
|
2024-05-20 23:24:34 +02:00
|
|
|
|
SetRenderTransform(element, new RotateTransform { Angle = rect.Rotation }, 0.5, 0.5);
|
2023-01-13 11:43:42 +01:00
|
|
|
|
}
|
2014-06-11 23:03:13 +02:00
|
|
|
|
}
|
2021-01-13 00:08:55 +01:00
|
|
|
|
|
2024-05-20 23:24:34 +02:00
|
|
|
|
internal static Size GetDesiredSize(FrameworkElement element)
|
2023-01-21 14:41:03 +01:00
|
|
|
|
{
|
|
|
|
|
|
var width = 0d;
|
|
|
|
|
|
var height = 0d;
|
|
|
|
|
|
|
|
|
|
|
|
if (element.DesiredSize.Width >= 0d &&
|
|
|
|
|
|
element.DesiredSize.Width < double.PositiveInfinity)
|
|
|
|
|
|
{
|
|
|
|
|
|
width = element.DesiredSize.Width;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (element.DesiredSize.Height >= 0d &&
|
|
|
|
|
|
element.DesiredSize.Height < double.PositiveInfinity)
|
|
|
|
|
|
{
|
|
|
|
|
|
height = element.DesiredSize.Height;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return new Size(width, height);
|
|
|
|
|
|
}
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|