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

121 lines
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
2026-04-13 17:14:49 +02:00
namespace MapControl;
public partial class PushpinBorder
2022-02-05 15:52:17 +01:00
{
2026-04-13 17:14:49 +02:00
public Size ArrowSize
2022-02-05 15:52:17 +01:00
{
2026-04-13 17:14:49 +02:00
get => (Size)GetValue(ArrowSizeProperty);
set => SetValue(ArrowSizeProperty, value);
}
2022-02-05 15:52:17 +01:00
2026-04-13 17:14:49 +02:00
public double BorderWidth
{
get => (double)GetValue(BorderWidthProperty);
set => SetValue(BorderWidthProperty, value);
}
2022-02-05 15:52:17 +01:00
2026-04-13 17:14:49 +02:00
protected virtual Geometry BuildGeometry()
{
var width = Math.Floor(ActualWidth);
var height = Math.Floor(ActualHeight);
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;
2022-02-05 15:52:17 +01:00
2026-04-13 17:14:49 +02:00
var figure = new PathFigure
{
StartPoint = new Point(x1, y1 + r1),
IsClosed = true,
IsFilled = true
};
2022-02-05 15:52:17 +01:00
2026-04-13 17:14:49 +02: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
2026-04-13 17:14:49 +02:00
if (HorizontalAlignment == HorizontalAlignment.Right)
{
figure.LineTo(x2, y3);
figure.LineTo(x2 - aw, y2);
}
else
{
figure.LineTo(x2, y2 - r3);
figure.ArcTo(x2 - r3, y2, r3);
}
2022-02-05 15:52:17 +01:00
2026-04-13 17:14:49 +02:00
if (HorizontalAlignment == HorizontalAlignment.Center)
{
var c = width / 2d;
figure.LineTo(c + aw / 2d, y2);
figure.LineTo(c, y3);
figure.LineTo(c - aw / 2d, y2);
}
2022-02-05 15:52:17 +01:00
2026-04-13 17:14:49 +02:00
if (HorizontalAlignment == HorizontalAlignment.Left || HorizontalAlignment == HorizontalAlignment.Stretch)
{
figure.LineTo(x1 + aw, y2);
figure.LineTo(x1, y3);
}
else
{
figure.LineTo(x1 + r4, y2);
figure.ArcTo(x1, y2 - r4, r4);
}
2022-02-05 15:52:17 +01:00
2026-04-13 17:14:49 +02:00
var geometry = new PathGeometry();
geometry.Figures.Add(figure);
2022-02-05 15:52:17 +01:00
2026-04-13 17:14:49 +02:00
return geometry;
2025-11-05 16:25:19 +01:00
}
2026-04-13 17:14:49 +02:00
}
2022-02-05 15:52:17 +01:00
2026-04-13 17:14:49 +02:00
internal static class PathFigureExtensions
{
public static void LineTo(this PathFigure figure, double x, double y)
2025-11-05 16:25:19 +01:00
{
2026-04-13 17:14:49 +02:00
figure.Segments.Add(new LineSegment
2022-02-05 15:52:17 +01:00
{
2026-04-13 17:14:49 +02:00
Point = new Point(x, y)
});
}
2022-02-05 15:52:17 +01:00
2026-04-13 17:14:49 +02:00
public static void ArcTo(this PathFigure figure, double x, double y, double r)
{
if (r > 0d)
2022-02-05 15:52:17 +01:00
{
2026-04-13 17:14:49 +02:00
figure.Segments.Add(new ArcSegment
2022-02-05 15:52:17 +01:00
{
2026-04-13 17:14:49 +02:00
Point = new Point(x, y),
Size = new Size(r, r),
SweepDirection = SweepDirection.Clockwise
});
2022-02-05 15:52:17 +01:00
}
}
}