2025-08-19 19:43:02 +02:00
|
|
|
|
using Avalonia;
|
|
|
|
|
|
using Avalonia.Controls;
|
|
|
|
|
|
using Avalonia.Controls.Documents;
|
2026-02-08 17:18:32 +01:00
|
|
|
|
using Avalonia.Layout;
|
2025-08-19 19:43:02 +02:00
|
|
|
|
using Avalonia.Media;
|
2026-02-07 22:10:06 +01:00
|
|
|
|
using System;
|
2025-08-19 19:43:02 +02:00
|
|
|
|
using System.Collections.Generic;
|
2024-05-26 20:32:29 +02:00
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2026-02-07 22:10:06 +01:00
|
|
|
|
public partial class MapGrid : Control, IMapElement
|
2024-05-26 20:32:29 +02:00
|
|
|
|
{
|
2026-02-07 22:10:06 +01:00
|
|
|
|
static MapGrid()
|
2024-05-26 20:32:29 +02:00
|
|
|
|
{
|
2026-02-07 22:10:06 +01:00
|
|
|
|
AffectsRender<MapGrid>(ForegroundProperty);
|
2025-06-10 09:27:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static readonly StyledProperty<IBrush> ForegroundProperty =
|
2026-02-07 22:10:06 +01:00
|
|
|
|
DependencyPropertyHelper.AddOwner<MapGrid, IBrush>(TextElement.ForegroundProperty);
|
2025-06-10 09:27:10 +02:00
|
|
|
|
|
|
|
|
|
|
public static readonly StyledProperty<FontFamily> FontFamilyProperty =
|
2026-02-07 22:10:06 +01:00
|
|
|
|
DependencyPropertyHelper.AddOwner<MapGrid, FontFamily>(TextElement.FontFamilyProperty);
|
2025-06-10 09:27:10 +02:00
|
|
|
|
|
|
|
|
|
|
public static readonly StyledProperty<double> FontSizeProperty =
|
2026-02-07 22:10:06 +01:00
|
|
|
|
DependencyPropertyHelper.AddOwner<MapGrid, double>(TextElement.FontSizeProperty, 12d);
|
2024-05-26 20:32:29 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Implements IMapElement.ParentMap.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public MapBase ParentMap
|
|
|
|
|
|
{
|
2025-12-27 21:24:01 +01:00
|
|
|
|
get;
|
2024-05-26 20:32:29 +02:00
|
|
|
|
set
|
|
|
|
|
|
{
|
2025-12-27 21:24:01 +01:00
|
|
|
|
if (field != null)
|
2024-05-26 20:32:29 +02:00
|
|
|
|
{
|
2025-12-27 21:24:01 +01:00
|
|
|
|
field.ViewportChanged -= OnViewportChanged;
|
2024-05-26 20:32:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-27 21:24:01 +01:00
|
|
|
|
field = value;
|
2024-05-26 20:32:29 +02:00
|
|
|
|
|
2025-12-27 21:24:01 +01:00
|
|
|
|
if (field != null)
|
2024-05-26 20:32:29 +02:00
|
|
|
|
{
|
2025-12-27 21:24:01 +01:00
|
|
|
|
field.ViewportChanged += OnViewportChanged;
|
2024-05-26 20:32:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnViewportChanged(object sender, ViewportChangedEventArgs e)
|
2026-02-07 22:10:06 +01:00
|
|
|
|
{
|
|
|
|
|
|
OnViewportChanged(e);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected virtual void OnViewportChanged(ViewportChangedEventArgs e)
|
2024-05-26 20:32:29 +02:00
|
|
|
|
{
|
|
|
|
|
|
InvalidateVisual();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void Render(DrawingContext drawingContext)
|
|
|
|
|
|
{
|
2025-12-27 21:24:01 +01:00
|
|
|
|
if (ParentMap != null)
|
2024-05-26 20:32:29 +02:00
|
|
|
|
{
|
|
|
|
|
|
var pathGeometry = new PathGeometry();
|
2026-02-07 00:01:04 +01:00
|
|
|
|
var labels = new List<Label>();
|
2024-05-26 20:32:29 +02:00
|
|
|
|
var pen = new Pen
|
|
|
|
|
|
{
|
|
|
|
|
|
Brush = Foreground,
|
|
|
|
|
|
Thickness = StrokeThickness,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-02-07 22:10:06 +01:00
|
|
|
|
DrawGrid(pathGeometry.Figures, labels);
|
2026-02-07 00:01:04 +01:00
|
|
|
|
|
2024-05-26 20:32:29 +02:00
|
|
|
|
drawingContext.DrawGeometry(null, pen, pathGeometry);
|
|
|
|
|
|
|
|
|
|
|
|
if (labels.Count > 0)
|
|
|
|
|
|
{
|
2025-06-10 09:27:10 +02:00
|
|
|
|
var typeface = new Typeface(FontFamily, FontStyle.Normal, FontWeight.Normal, FontStretch.Normal);
|
2024-05-26 20:32:29 +02:00
|
|
|
|
|
|
|
|
|
|
foreach (var label in labels)
|
|
|
|
|
|
{
|
2026-02-07 22:10:06 +01:00
|
|
|
|
var text = new FormattedText(label.Text,
|
2024-05-26 20:32:29 +02:00
|
|
|
|
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground);
|
2026-02-08 17:18:32 +01:00
|
|
|
|
var x = label.X +
|
|
|
|
|
|
label.HorizontalAlignment switch
|
|
|
|
|
|
{
|
|
|
|
|
|
HorizontalAlignment.Left => 2d,
|
|
|
|
|
|
HorizontalAlignment.Right => -text.Width - 2d,
|
|
|
|
|
|
_ => -text.Width / 2d
|
|
|
|
|
|
};
|
|
|
|
|
|
var y = label.Y +
|
|
|
|
|
|
label.VerticalAlignment switch
|
|
|
|
|
|
{
|
|
|
|
|
|
VerticalAlignment.Top => 0,
|
|
|
|
|
|
VerticalAlignment.Bottom => -text.Height,
|
|
|
|
|
|
_ => -text.Height / 2d
|
|
|
|
|
|
};
|
2026-02-07 22:10:06 +01:00
|
|
|
|
|
|
|
|
|
|
if (label.Rotation != 0d)
|
|
|
|
|
|
{
|
|
|
|
|
|
var transform = Avalonia.Matrix.CreateRotation(
|
|
|
|
|
|
label.Rotation * Math.PI / 180d, new Point(label.X, label.Y));
|
|
|
|
|
|
|
|
|
|
|
|
using var pushedState = drawingContext.PushTransform(transform);
|
|
|
|
|
|
|
|
|
|
|
|
drawingContext.DrawText(text, new Point(x, y));
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
drawingContext.DrawText(text, new Point(x, y));
|
|
|
|
|
|
}
|
2024-05-26 20:32:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-01 01:42:24 +01:00
|
|
|
|
private static PolyLineSegment CreatePolyLineSegment(IEnumerable<Point> points)
|
2024-05-26 20:32:29 +02:00
|
|
|
|
{
|
2026-02-01 01:42:24 +01:00
|
|
|
|
return new PolyLineSegment(points);
|
2024-05-26 20:32:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|