XAML-Map-Control/MapControl/Shared/PushpinBorder.cs

122 lines
3.3 KiB
C#
Raw Normal View History

2025-02-27 18:46:32 +01:00
using System;
2024-05-22 11:25:32 +02:00
#if WPF
using System.Windows;
using System.Windows.Media;
2022-02-05 15:52:17 +01:00
#elif UWP
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
2024-05-22 11:25:32 +02:00
#elif WINUI
using Windows.Foundation;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
2025-08-19 19:43:02 +02:00
#elif AVALONIA
using Avalonia;
using Avalonia.Layout;
using Avalonia.Media;
2022-02-05 15:52:17 +01:00
#endif
namespace MapControl
{
public partial class PushpinBorder
{
public Size ArrowSize
{
2022-08-06 10:40:59 +02:00
get => (Size)GetValue(ArrowSizeProperty);
set => SetValue(ArrowSizeProperty, value);
2022-02-05 15:52:17 +01:00
}
public double BorderWidth
{
2022-08-06 10:40:59 +02:00
get => (double)GetValue(BorderWidthProperty);
set => SetValue(BorderWidthProperty, value);
2022-02-05 15:52:17 +01:00
}
protected virtual Geometry BuildGeometry()
{
var width = Math.Floor(ActualWidth);
var height = Math.Floor(ActualHeight);
2022-02-05 15:52:17 +01:00
var x1 = BorderWidth / 2d;
var y1 = BorderWidth / 2d;
var x2 = width - x1;
var y3 = height - y1;
var y2 = y3 - ArrowSize.Height;
var aw = ArrowSize.Width;
var r1 = CornerRadius.TopLeft;
var r2 = CornerRadius.TopRight;
var r3 = CornerRadius.BottomRight;
var r4 = CornerRadius.BottomLeft;
var figure = new PathFigure
{
StartPoint = new Point(x1, y1 + r1),
IsClosed = true,
IsFilled = true
};
2025-11-05 16:25:19 +01:00
figure.ArcTo(x1 + r1, y1, r1);
figure.LineTo(x2 - r2, y1);
figure.ArcTo(x2, y1 + r2, r2);
2022-02-05 15:52:17 +01:00
if (HorizontalAlignment == HorizontalAlignment.Right)
{
2025-11-05 16:25:19 +01:00
figure.LineTo(x2, y3);
figure.LineTo(x2 - aw, y2);
2022-02-05 15:52:17 +01:00
}
else
{
2025-11-05 16:25:19 +01:00
figure.LineTo(x2, y2 - r3);
figure.ArcTo(x2 - r3, y2, r3);
2022-02-05 15:52:17 +01:00
}
if (HorizontalAlignment == HorizontalAlignment.Center)
2022-02-05 15:52:17 +01:00
{
var c = width / 2d;
2025-11-05 16:25:19 +01:00
figure.LineTo(c + aw / 2d, y2);
figure.LineTo(c, y3);
figure.LineTo(c - aw / 2d, y2);
2022-02-05 15:52:17 +01:00
}
if (HorizontalAlignment == HorizontalAlignment.Left || HorizontalAlignment == HorizontalAlignment.Stretch)
2022-02-05 15:52:17 +01:00
{
2025-11-05 16:25:19 +01:00
figure.LineTo(x1 + aw, y2);
figure.LineTo(x1, y3);
2022-02-05 15:52:17 +01:00
}
else
{
2025-11-05 16:25:19 +01:00
figure.LineTo(x1 + r4, y2);
figure.ArcTo(x1, y2 - r4, r4);
2022-02-05 15:52:17 +01:00
}
var geometry = new PathGeometry();
geometry.Figures.Add(figure);
return geometry;
}
2025-11-05 16:25:19 +01:00
}
2022-02-05 15:52:17 +01:00
2025-11-05 16:25:19 +01:00
internal static class PathFigureExtensions
{
public static void LineTo(this PathFigure figure, double x, double y)
2022-02-05 15:52:17 +01:00
{
2025-11-05 16:25:19 +01:00
figure.Segments.Add(new LineSegment
2022-02-05 15:52:17 +01:00
{
Point = new Point(x, y)
2025-11-05 16:25:19 +01:00
});
2022-02-05 15:52:17 +01:00
}
2025-11-05 16:25:19 +01:00
public static void ArcTo(this PathFigure figure, double x, double y, double r)
2022-02-05 15:52:17 +01:00
{
2025-11-05 16:25:19 +01:00
if (r > 0d)
2022-02-05 15:52:17 +01:00
{
2025-11-05 16:25:19 +01:00
figure.Segments.Add(new ArcSegment
{
Point = new Point(x, y),
Size = new Size(r, r),
SweepDirection = SweepDirection.Clockwise
});
}
2022-02-05 15:52:17 +01:00
}
}
}