mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2026-04-04 14:08:32 +00:00
Avalonia MapGraticule
This commit is contained in:
parent
9980733c37
commit
8bb7dc3eb3
13 changed files with 247 additions and 495 deletions
|
|
@ -24,7 +24,7 @@ namespace MapControl
|
|||
if (coerce != null)
|
||||
{
|
||||
// do not coerce default value
|
||||
coerceFunc = (obj, value) => value.Equals(defaultValue) ? value : coerce((TOwner)obj, value);
|
||||
coerceFunc = (obj, value) => Equals(value, defaultValue) ? value : coerce((TOwner)obj, value);
|
||||
}
|
||||
|
||||
var bindingMode = bindTwoWayByDefault ? Avalonia.Data.BindingMode.TwoWay : Avalonia.Data.BindingMode.OneWay;
|
||||
|
|
@ -57,11 +57,17 @@ namespace MapControl
|
|||
|
||||
public static StyledProperty<TValue> AddOwner<TOwner, TValue>(
|
||||
StyledProperty<TValue> property,
|
||||
TValue defaultValue = default,
|
||||
Action<TOwner, TValue, TValue> changed = null)
|
||||
where TOwner : AvaloniaObject
|
||||
{
|
||||
var newProperty = property.AddOwner<TOwner>();
|
||||
|
||||
if (!Equals(defaultValue, newProperty.GetMetadata(typeof(TOwner)).DefaultValue))
|
||||
{
|
||||
newProperty.OverrideMetadata<TOwner>(new StyledPropertyMetadata<TValue>(defaultValue));
|
||||
}
|
||||
|
||||
if (changed != null)
|
||||
{
|
||||
newProperty.Changed.AddClassHandler<TOwner, TValue>((o, e) => changed(o, e.OldValue.Value, e.NewValue.Value));
|
||||
|
|
|
|||
|
|
@ -25,7 +25,6 @@
|
|||
<Compile Include="..\Shared\*.cs" />
|
||||
<Compile Remove="..\Shared\BindingHelper.cs" />
|
||||
<Compile Remove="..\Shared\GeoImage.cs" />
|
||||
<Compile Remove="..\Shared\MapGraticule.cs" />
|
||||
<Compile Remove="..\Shared\MapItem.cs" />
|
||||
<Compile Remove="..\Shared\MapItemsControl.cs" />
|
||||
<Compile Remove="..\Shared\ViewTransform.cs" />
|
||||
|
|
|
|||
138
MapControl/Avalonia/MapGraticule.Avalonia.cs
Normal file
138
MapControl/Avalonia/MapGraticule.Avalonia.cs
Normal file
|
|
@ -0,0 +1,138 @@
|
|||
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
||||
// Copyright © 2024 Clemens Fischer
|
||||
// Licensed under the Microsoft Public License (Ms-PL)
|
||||
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Documents;
|
||||
using Avalonia.Controls.Shapes;
|
||||
using Avalonia.Media;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
|
||||
namespace MapControl
|
||||
{
|
||||
public partial class MapGraticule : Control, IMapElement
|
||||
{
|
||||
public static readonly StyledProperty<IBrush> ForegroundProperty =
|
||||
DependencyPropertyHelper.AddOwner<MapGraticule, IBrush>(TextElement.ForegroundProperty, null,
|
||||
(graticule, oldValue, newValue) => graticule.InvalidateVisual());
|
||||
|
||||
public static readonly StyledProperty<FontFamily> FontFamilyProperty =
|
||||
DependencyPropertyHelper.AddOwner<MapGraticule, FontFamily>(TextElement.FontFamilyProperty);
|
||||
|
||||
public static readonly StyledProperty<double> FontSizeProperty =
|
||||
DependencyPropertyHelper.AddOwner<MapGraticule, double>(TextElement.FontSizeProperty);
|
||||
|
||||
public static readonly StyledProperty<double> StrokeThicknessProperty =
|
||||
DependencyPropertyHelper.AddOwner<MapGraticule, double>(Shape.StrokeThicknessProperty, 0.5);
|
||||
|
||||
public IBrush Foreground
|
||||
{
|
||||
get => GetValue(ForegroundProperty);
|
||||
set => SetValue(ForegroundProperty, value);
|
||||
}
|
||||
|
||||
public FontFamily FontFamily
|
||||
{
|
||||
get => GetValue(FontFamilyProperty);
|
||||
set => SetValue(FontFamilyProperty, value);
|
||||
}
|
||||
|
||||
public double FontSize
|
||||
{
|
||||
get => GetValue(FontSizeProperty);
|
||||
set => SetValue(FontSizeProperty, value);
|
||||
}
|
||||
|
||||
public double StrokeThickness
|
||||
{
|
||||
get => GetValue(StrokeThicknessProperty);
|
||||
set => SetValue(StrokeThicknessProperty, value);
|
||||
}
|
||||
|
||||
private MapBase parentMap;
|
||||
|
||||
/// <summary>
|
||||
/// Implements IMapElement.ParentMap.
|
||||
/// </summary>
|
||||
public MapBase ParentMap
|
||||
{
|
||||
get => parentMap;
|
||||
set
|
||||
{
|
||||
if (parentMap != null)
|
||||
{
|
||||
parentMap.ViewportChanged -= OnViewportChanged;
|
||||
}
|
||||
|
||||
parentMap = value;
|
||||
|
||||
if (parentMap != null)
|
||||
{
|
||||
parentMap.ViewportChanged += OnViewportChanged;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnViewportChanged(object sender, ViewportChangedEventArgs e)
|
||||
{
|
||||
InvalidateVisual();
|
||||
}
|
||||
|
||||
public override void Render(DrawingContext drawingContext)
|
||||
{
|
||||
if (parentMap != null)
|
||||
{
|
||||
var pathGeometry = new PathGeometry();
|
||||
|
||||
var labels = DrawGraticule(pathGeometry.Figures);
|
||||
|
||||
var pen = new Pen
|
||||
{
|
||||
Brush = Foreground,
|
||||
Thickness = StrokeThickness,
|
||||
};
|
||||
|
||||
drawingContext.DrawGeometry(null, pen, pathGeometry);
|
||||
|
||||
if (labels.Count > 0)
|
||||
{
|
||||
var typeface = new Typeface(FontFamily, FontStyle.Normal, FontWeight.Normal, FontStretch.Normal);
|
||||
|
||||
foreach (var label in labels)
|
||||
{
|
||||
var latText = new FormattedText(label.LatitudeText,
|
||||
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground);
|
||||
|
||||
var lonText = new FormattedText(label.LongitudeText,
|
||||
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground);
|
||||
|
||||
var x = StrokeThickness / 2d + 2d;
|
||||
var y1 = -StrokeThickness / 2d - latText.Height;
|
||||
var y2 = StrokeThickness / 2d;
|
||||
|
||||
using var pushState = drawingContext.PushTransform(
|
||||
Matrix.CreateRotation(Matrix.ToRadians(label.Rotation)) *
|
||||
Matrix.CreateTranslation(label.X, label.Y));
|
||||
|
||||
drawingContext.DrawText(latText, new Point(x, y1));
|
||||
drawingContext.DrawText(lonText, new Point(x, y2));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static PathFigure CreatePolylineFigure(IEnumerable<Point> points)
|
||||
{
|
||||
var figure = new PathFigure
|
||||
{
|
||||
StartPoint = points.First(),
|
||||
IsFilled = false
|
||||
};
|
||||
|
||||
figure.Segments.Add(new PolyLineSegment(points.Skip(1)));
|
||||
return figure;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,154 +0,0 @@
|
|||
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
||||
// Copyright © 2024 Clemens Fischer
|
||||
// Licensed under the Microsoft Public License (Ms-PL)
|
||||
|
||||
using Avalonia.Collections;
|
||||
using Avalonia.Controls.Documents;
|
||||
using Avalonia.Controls.Shapes;
|
||||
using Avalonia.Media;
|
||||
|
||||
namespace MapControl
|
||||
{
|
||||
public class MapOverlay : MapPanel
|
||||
{
|
||||
public static readonly StyledProperty<FontFamily> FontFamilyProperty =
|
||||
DependencyPropertyHelper.AddOwner<MapOverlay, FontFamily>(TextElement.FontFamilyProperty);
|
||||
|
||||
public static readonly StyledProperty<double> FontSizeProperty =
|
||||
DependencyPropertyHelper.AddOwner<MapOverlay, double>(TextElement.FontSizeProperty);
|
||||
|
||||
public static readonly StyledProperty<FontStyle> FontStyleProperty =
|
||||
DependencyPropertyHelper.AddOwner<MapOverlay, FontStyle>(TextElement.FontStyleProperty);
|
||||
|
||||
public static readonly StyledProperty<FontStretch> FontStretchProperty =
|
||||
DependencyPropertyHelper.AddOwner<MapOverlay, FontStretch>(TextElement.FontStretchProperty);
|
||||
|
||||
public static readonly StyledProperty<FontWeight> FontWeightProperty =
|
||||
DependencyPropertyHelper.AddOwner<MapOverlay, FontWeight>(TextElement.FontWeightProperty);
|
||||
|
||||
public static readonly StyledProperty<IBrush> ForegroundProperty =
|
||||
DependencyPropertyHelper.AddOwner<MapOverlay, IBrush>(TextElement.ForegroundProperty);
|
||||
|
||||
public static readonly StyledProperty<IBrush> StrokeProperty =
|
||||
DependencyPropertyHelper.AddOwner<MapOverlay, IBrush>(Shape.StrokeProperty);
|
||||
|
||||
public static readonly StyledProperty<double> StrokeThicknessProperty =
|
||||
DependencyPropertyHelper.Register<MapOverlay, double>(nameof(StrokeThickness), 1d);
|
||||
|
||||
public static readonly StyledProperty<AvaloniaList<double>> StrokeDashArrayProperty =
|
||||
DependencyPropertyHelper.AddOwner<MapOverlay, AvaloniaList<double>>(Shape.StrokeDashArrayProperty);
|
||||
|
||||
public static readonly StyledProperty<double> StrokeDashOffsetProperty =
|
||||
DependencyPropertyHelper.AddOwner<MapOverlay, double>(Shape.StrokeDashOffsetProperty);
|
||||
|
||||
public static readonly StyledProperty<PenLineCap> StrokeLineCapProperty =
|
||||
DependencyPropertyHelper.AddOwner<MapOverlay, PenLineCap>(Shape.StrokeLineCapProperty);
|
||||
|
||||
public static readonly StyledProperty<PenLineJoin> StrokeLineJoinProperty =
|
||||
DependencyPropertyHelper.AddOwner<MapOverlay, PenLineJoin>(Shape.StrokeJoinProperty);
|
||||
|
||||
public static readonly StyledProperty<double> StrokeMiterLimitProperty =
|
||||
DependencyPropertyHelper.Register<MapOverlay, double>(nameof(StrokeMiterLimit));
|
||||
|
||||
public FontFamily FontFamily
|
||||
{
|
||||
get => GetValue(FontFamilyProperty);
|
||||
set => SetValue(FontFamilyProperty, value);
|
||||
}
|
||||
|
||||
public double FontSize
|
||||
{
|
||||
get => GetValue(FontSizeProperty);
|
||||
set => SetValue(FontSizeProperty, value);
|
||||
}
|
||||
|
||||
public FontStyle FontStyle
|
||||
{
|
||||
get => GetValue(FontStyleProperty);
|
||||
set => SetValue(FontStyleProperty, value);
|
||||
}
|
||||
|
||||
public FontStretch FontStretch
|
||||
{
|
||||
get => GetValue(FontStretchProperty);
|
||||
set => SetValue(FontStretchProperty, value);
|
||||
}
|
||||
|
||||
public FontWeight FontWeight
|
||||
{
|
||||
get => GetValue(FontWeightProperty);
|
||||
set => SetValue(FontWeightProperty, value);
|
||||
}
|
||||
|
||||
public IBrush Foreground
|
||||
{
|
||||
get => GetValue(ForegroundProperty);
|
||||
set => SetValue(ForegroundProperty, value);
|
||||
}
|
||||
|
||||
public IBrush Stroke
|
||||
{
|
||||
get => GetValue(StrokeProperty);
|
||||
set => SetValue(StrokeProperty, value);
|
||||
}
|
||||
|
||||
public double StrokeThickness
|
||||
{
|
||||
get => GetValue(StrokeThicknessProperty);
|
||||
set => SetValue(StrokeThicknessProperty, value);
|
||||
}
|
||||
|
||||
public AvaloniaList<double> StrokeDashArray
|
||||
{
|
||||
get => GetValue(StrokeDashArrayProperty);
|
||||
set => SetValue(StrokeDashArrayProperty, value);
|
||||
}
|
||||
|
||||
public double StrokeDashOffset
|
||||
{
|
||||
get => GetValue(StrokeDashOffsetProperty);
|
||||
set => SetValue(StrokeDashOffsetProperty, value);
|
||||
}
|
||||
|
||||
public PenLineCap StrokeLineCap
|
||||
{
|
||||
get => GetValue(StrokeLineCapProperty);
|
||||
set => SetValue(StrokeLineCapProperty, value);
|
||||
}
|
||||
|
||||
public PenLineJoin StrokeLineJoin
|
||||
{
|
||||
get => GetValue(StrokeLineJoinProperty);
|
||||
set => SetValue(StrokeLineJoinProperty, value);
|
||||
}
|
||||
|
||||
public double StrokeMiterLimit
|
||||
{
|
||||
get => (double)GetValue(StrokeMiterLimitProperty);
|
||||
set => SetValue(StrokeMiterLimitProperty, value);
|
||||
}
|
||||
|
||||
public Pen CreatePen()
|
||||
{
|
||||
return new Pen
|
||||
{
|
||||
Brush = Stroke,
|
||||
Thickness = StrokeThickness,
|
||||
LineJoin = StrokeLineJoin,
|
||||
MiterLimit = StrokeMiterLimit,
|
||||
LineCap = StrokeLineCap,
|
||||
DashStyle = new DashStyle(StrokeDashArray, StrokeDashOffset)
|
||||
};
|
||||
}
|
||||
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
base.OnInitialized();
|
||||
|
||||
if (Stroke == null)
|
||||
{
|
||||
this.SetBinding(StrokeProperty, this.CreateBinding(nameof(Foreground)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -15,7 +15,7 @@ namespace MapControl
|
|||
}
|
||||
|
||||
public static readonly StyledProperty<Geometry> DataProperty =
|
||||
DependencyPropertyHelper.AddOwner<MapPath, Geometry>(Path.DataProperty,
|
||||
DependencyPropertyHelper.AddOwner<MapPath, Geometry>(Path.DataProperty, null,
|
||||
(path, oldValue, newValue) => path.UpdateData());
|
||||
|
||||
public Geometry Data
|
||||
|
|
|
|||
|
|
@ -56,7 +56,7 @@ namespace MapControl
|
|||
MapToViewMatrix
|
||||
= Matrix.CreateTranslation(-mapCenter.X, -mapCenter.Y)
|
||||
* Matrix.CreateScale(scale, -scale)
|
||||
* Matrix.CreateRotation(Rotation * Math.PI / 180d)
|
||||
* Matrix.CreateRotation(Matrix.ToRadians(Rotation))
|
||||
* Matrix.CreateTranslation(viewCenter.X, viewCenter.Y);
|
||||
|
||||
ViewToMapMatrix = MapToViewMatrix.Invert();
|
||||
|
|
@ -95,7 +95,7 @@ namespace MapControl
|
|||
var scale = GetMapScale(relativeScale);
|
||||
|
||||
return Matrix.CreateScale(scale.X, scale.Y)
|
||||
* Matrix.CreateRotation(Rotation * Math.PI / 180d);
|
||||
* Matrix.CreateRotation(Matrix.ToRadians(Rotation));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -116,7 +116,7 @@ namespace MapControl
|
|||
var transformScale = Scale / tileMatrixScale;
|
||||
|
||||
return Matrix.CreateScale(transformScale, transformScale)
|
||||
* Matrix.CreateRotation(Rotation * Math.PI / 180d)
|
||||
* Matrix.CreateRotation(Matrix.ToRadians(Rotation))
|
||||
* Matrix.CreateTranslation(viewOrigin.X, viewOrigin.Y);
|
||||
}
|
||||
|
||||
|
|
@ -133,7 +133,7 @@ namespace MapControl
|
|||
|
||||
var transform
|
||||
= Matrix.CreateScale(transformScale, transformScale)
|
||||
* Matrix.CreateRotation(-Rotation * Math.PI / 180d);
|
||||
* Matrix.CreateRotation(Matrix.ToRadians(-Rotation));
|
||||
|
||||
// Translate origin to tile matrix origin in pixels.
|
||||
//
|
||||
|
|
@ -152,7 +152,7 @@ namespace MapControl
|
|||
double translation2X, double translation2Y)
|
||||
{
|
||||
return Matrix.CreateTranslation(translation1X, translation1Y)
|
||||
* Matrix.CreateRotation(rotation * Math.PI / 180d)
|
||||
* Matrix.CreateRotation(Matrix.ToRadians(rotation))
|
||||
* Matrix.CreateTranslation(translation2X, translation2Y);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue