Avalonia MapGraticule

This commit is contained in:
ClemensFischer 2024-05-26 20:32:29 +02:00
parent 9980733c37
commit 8bb7dc3eb3
13 changed files with 247 additions and 495 deletions

View file

@ -24,7 +24,7 @@ namespace MapControl
if (coerce != null) if (coerce != null)
{ {
// do not coerce default value // 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; var bindingMode = bindTwoWayByDefault ? Avalonia.Data.BindingMode.TwoWay : Avalonia.Data.BindingMode.OneWay;
@ -57,11 +57,17 @@ namespace MapControl
public static StyledProperty<TValue> AddOwner<TOwner, TValue>( public static StyledProperty<TValue> AddOwner<TOwner, TValue>(
StyledProperty<TValue> property, StyledProperty<TValue> property,
TValue defaultValue = default,
Action<TOwner, TValue, TValue> changed = null) Action<TOwner, TValue, TValue> changed = null)
where TOwner : AvaloniaObject where TOwner : AvaloniaObject
{ {
var newProperty = property.AddOwner<TOwner>(); var newProperty = property.AddOwner<TOwner>();
if (!Equals(defaultValue, newProperty.GetMetadata(typeof(TOwner)).DefaultValue))
{
newProperty.OverrideMetadata<TOwner>(new StyledPropertyMetadata<TValue>(defaultValue));
}
if (changed != null) if (changed != null)
{ {
newProperty.Changed.AddClassHandler<TOwner, TValue>((o, e) => changed(o, e.OldValue.Value, e.NewValue.Value)); newProperty.Changed.AddClassHandler<TOwner, TValue>((o, e) => changed(o, e.OldValue.Value, e.NewValue.Value));

View file

@ -25,7 +25,6 @@
<Compile Include="..\Shared\*.cs" /> <Compile Include="..\Shared\*.cs" />
<Compile Remove="..\Shared\BindingHelper.cs" /> <Compile Remove="..\Shared\BindingHelper.cs" />
<Compile Remove="..\Shared\GeoImage.cs" /> <Compile Remove="..\Shared\GeoImage.cs" />
<Compile Remove="..\Shared\MapGraticule.cs" />
<Compile Remove="..\Shared\MapItem.cs" /> <Compile Remove="..\Shared\MapItem.cs" />
<Compile Remove="..\Shared\MapItemsControl.cs" /> <Compile Remove="..\Shared\MapItemsControl.cs" />
<Compile Remove="..\Shared\ViewTransform.cs" /> <Compile Remove="..\Shared\ViewTransform.cs" />

View 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;
}
}
}

View file

@ -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)));
}
}
}
}

View file

@ -15,7 +15,7 @@ namespace MapControl
} }
public static readonly StyledProperty<Geometry> DataProperty = public static readonly StyledProperty<Geometry> DataProperty =
DependencyPropertyHelper.AddOwner<MapPath, Geometry>(Path.DataProperty, DependencyPropertyHelper.AddOwner<MapPath, Geometry>(Path.DataProperty, null,
(path, oldValue, newValue) => path.UpdateData()); (path, oldValue, newValue) => path.UpdateData());
public Geometry Data public Geometry Data

View file

@ -56,7 +56,7 @@ namespace MapControl
MapToViewMatrix MapToViewMatrix
= Matrix.CreateTranslation(-mapCenter.X, -mapCenter.Y) = Matrix.CreateTranslation(-mapCenter.X, -mapCenter.Y)
* Matrix.CreateScale(scale, -scale) * Matrix.CreateScale(scale, -scale)
* Matrix.CreateRotation(Rotation * Math.PI / 180d) * Matrix.CreateRotation(Matrix.ToRadians(Rotation))
* Matrix.CreateTranslation(viewCenter.X, viewCenter.Y); * Matrix.CreateTranslation(viewCenter.X, viewCenter.Y);
ViewToMapMatrix = MapToViewMatrix.Invert(); ViewToMapMatrix = MapToViewMatrix.Invert();
@ -95,7 +95,7 @@ namespace MapControl
var scale = GetMapScale(relativeScale); var scale = GetMapScale(relativeScale);
return Matrix.CreateScale(scale.X, scale.Y) return Matrix.CreateScale(scale.X, scale.Y)
* Matrix.CreateRotation(Rotation * Math.PI / 180d); * Matrix.CreateRotation(Matrix.ToRadians(Rotation));
} }
/// <summary> /// <summary>
@ -116,7 +116,7 @@ namespace MapControl
var transformScale = Scale / tileMatrixScale; var transformScale = Scale / tileMatrixScale;
return Matrix.CreateScale(transformScale, transformScale) return Matrix.CreateScale(transformScale, transformScale)
* Matrix.CreateRotation(Rotation * Math.PI / 180d) * Matrix.CreateRotation(Matrix.ToRadians(Rotation))
* Matrix.CreateTranslation(viewOrigin.X, viewOrigin.Y); * Matrix.CreateTranslation(viewOrigin.X, viewOrigin.Y);
} }
@ -133,7 +133,7 @@ namespace MapControl
var transform var transform
= Matrix.CreateScale(transformScale, transformScale) = Matrix.CreateScale(transformScale, transformScale)
* Matrix.CreateRotation(-Rotation * Math.PI / 180d); * Matrix.CreateRotation(Matrix.ToRadians(-Rotation));
// Translate origin to tile matrix origin in pixels. // Translate origin to tile matrix origin in pixels.
// //
@ -152,7 +152,7 @@ namespace MapControl
double translation2X, double translation2Y) double translation2X, double translation2Y)
{ {
return Matrix.CreateTranslation(translation1X, translation1Y) return Matrix.CreateTranslation(translation1X, translation1Y)
* Matrix.CreateRotation(rotation * Math.PI / 180d) * Matrix.CreateRotation(Matrix.ToRadians(rotation))
* Matrix.CreateTranslation(translation2X, translation2Y); * Matrix.CreateTranslation(translation2X, translation2Y);
} }
} }

View file

@ -15,6 +15,10 @@ using Windows.UI.Xaml.Media;
#elif WINUI #elif WINUI
using Microsoft.UI.Xaml; using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media; using Microsoft.UI.Xaml.Media;
#elif AVALONIA
using Avalonia.Media;
using DependencyProperty = Avalonia.AvaloniaProperty;
using PathFigureCollection = Avalonia.Media.PathFigures;
#endif #endif
namespace MapControl namespace MapControl
@ -22,7 +26,7 @@ namespace MapControl
/// <summary> /// <summary>
/// Draws a graticule overlay. /// Draws a graticule overlay.
/// </summary> /// </summary>
public partial class MapGraticule : MapOverlay public partial class MapGraticule
{ {
private class Label private class Label
{ {

View file

@ -248,9 +248,6 @@
<Compile Include="..\WinUI\MapItemsControl.WinUI.cs"> <Compile Include="..\WinUI\MapItemsControl.WinUI.cs">
<Link>MapItemsControl.WinUI.cs</Link> <Link>MapItemsControl.WinUI.cs</Link>
</Compile> </Compile>
<Compile Include="..\WinUI\MapOverlay.WinUI.cs">
<Link>MapOverlay.WinUI.cs</Link>
</Compile>
<Compile Include="..\WinUI\MapPanel.WinUI.cs"> <Compile Include="..\WinUI\MapPanel.WinUI.cs">
<Link>MapPanel.WinUI.cs</Link> <Link>MapPanel.WinUI.cs</Link>
</Compile> </Compile>

View file

@ -6,31 +6,66 @@ using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
using System.Windows; using System.Windows;
using System.Windows.Controls;
using System.Windows.Media; using System.Windows.Media;
namespace MapControl namespace MapControl
{ {
public partial class MapGraticule public partial class MapGraticule : Control, IMapElement
{ {
static MapGraticule() public static readonly DependencyProperty StrokeThicknessProperty =
DependencyPropertyHelper.Register<MapGraticule, double>(nameof(StrokeThickness), 0.5);
public double StrokeThickness
{ {
StrokeThicknessProperty.OverrideMetadata(typeof(MapGraticule), new FrameworkPropertyMetadata(0.5)); get => (double)GetValue(StrokeThicknessProperty);
set => SetValue(StrokeThicknessProperty, value);
} }
protected override void OnViewportChanged(ViewportChangedEventArgs e) 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(); InvalidateVisual();
} }
protected override void OnRender(DrawingContext drawingContext) protected override void OnRender(DrawingContext drawingContext)
{ {
if (ParentMap != null) if (parentMap != null)
{ {
var pathGeometry = new PathGeometry(); var pathGeometry = new PathGeometry();
var labels = DrawGraticule(pathGeometry.Figures); var labels = DrawGraticule(pathGeometry.Figures);
drawingContext.DrawGeometry(null, CreatePen(), pathGeometry); var pen = new Pen
{
Brush = Foreground,
Thickness = StrokeThickness,
};
drawingContext.DrawGeometry(null, pen, pathGeometry);
if (labels.Count > 0) if (labels.Count > 0)
{ {

View file

@ -1,157 +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 System;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Shapes;
namespace MapControl
{
public class MapOverlay : MapPanel
{
public static readonly DependencyProperty FontFamilyProperty =
DependencyPropertyHelper.AddOwner<MapOverlay, FontFamily>(TextElement.FontFamilyProperty);
public static readonly DependencyProperty FontSizeProperty =
DependencyPropertyHelper.AddOwner<MapOverlay, double>(TextElement.FontSizeProperty);
public static readonly DependencyProperty FontStyleProperty =
DependencyPropertyHelper.AddOwner<MapOverlay, FontStyle>(TextElement.FontStyleProperty);
public static readonly DependencyProperty FontStretchProperty =
DependencyPropertyHelper.AddOwner<MapOverlay, FontStretch>(TextElement.FontStretchProperty);
public static readonly DependencyProperty FontWeightProperty =
DependencyPropertyHelper.AddOwner<MapOverlay, FontWeight>(TextElement.FontWeightProperty);
public static readonly DependencyProperty ForegroundProperty =
DependencyPropertyHelper.AddOwner<MapOverlay, Brush>(TextElement.ForegroundProperty);
public static readonly DependencyProperty StrokeProperty =
DependencyPropertyHelper.AddOwner<MapOverlay, Brush>(Shape.StrokeProperty);
public static readonly DependencyProperty StrokeThicknessProperty =
DependencyPropertyHelper.AddOwner<MapOverlay, double>(Shape.StrokeThicknessProperty);
public static readonly DependencyProperty StrokeDashArrayProperty =
DependencyPropertyHelper.AddOwner<MapOverlay, DoubleCollection>(Shape.StrokeDashArrayProperty);
public static readonly DependencyProperty StrokeDashOffsetProperty =
DependencyPropertyHelper.AddOwner<MapOverlay, double>(Shape.StrokeDashOffsetProperty);
public static readonly DependencyProperty StrokeLineCapProperty =
DependencyPropertyHelper.AddOwner<MapOverlay, PenLineCap>(Shape.StrokeDashCapProperty);
public static readonly DependencyProperty StrokeLineJoinProperty =
DependencyPropertyHelper.AddOwner<MapOverlay, PenLineJoin>(Shape.StrokeLineJoinProperty);
public static readonly DependencyProperty StrokeMiterLimitProperty =
DependencyPropertyHelper.AddOwner<MapOverlay, double>(Shape.StrokeMiterLimitProperty);
public FontFamily FontFamily
{
get => (FontFamily)GetValue(FontFamilyProperty);
set => SetValue(FontFamilyProperty, value);
}
public double FontSize
{
get => (double)GetValue(FontSizeProperty);
set => SetValue(FontSizeProperty, value);
}
public FontStyle FontStyle
{
get => (FontStyle)GetValue(FontStyleProperty);
set => SetValue(FontStyleProperty, value);
}
public FontStretch FontStretch
{
get => (FontStretch)GetValue(FontStretchProperty);
set => SetValue(FontStretchProperty, value);
}
public FontWeight FontWeight
{
get => (FontWeight)GetValue(FontWeightProperty);
set => SetValue(FontWeightProperty, value);
}
public Brush Foreground
{
get => (Brush)GetValue(ForegroundProperty);
set => SetValue(ForegroundProperty, value);
}
public Brush Stroke
{
get => (Brush)GetValue(StrokeProperty);
set => SetValue(StrokeProperty, value);
}
public double StrokeThickness
{
get => (double)GetValue(StrokeThicknessProperty);
set => SetValue(StrokeThicknessProperty, value);
}
public DoubleCollection StrokeDashArray
{
get => (DoubleCollection)GetValue(StrokeDashArrayProperty);
set => SetValue(StrokeDashArrayProperty, value);
}
public double StrokeDashOffset
{
get => (double)GetValue(StrokeDashOffsetProperty);
set => SetValue(StrokeDashOffsetProperty, value);
}
public PenLineCap StrokeLineCap
{
get => (PenLineCap)GetValue(StrokeLineCapProperty);
set => SetValue(StrokeLineCapProperty, value);
}
public PenLineJoin StrokeLineJoin
{
get => (PenLineJoin)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,
StartLineCap = StrokeLineCap,
EndLineCap = StrokeLineCap,
DashCap = StrokeLineCap,
DashStyle = new DashStyle(StrokeDashArray, StrokeDashOffset)
};
}
protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
if (Stroke == null)
{
SetBinding(StrokeProperty, this.CreateBinding(nameof(Foreground)));
}
}
}
}

View file

@ -6,10 +6,12 @@ using Windows.Foundation;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
#if UWP #if UWP
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes; using Windows.UI.Xaml.Shapes;
#else #else
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media; using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Shapes; using Microsoft.UI.Xaml.Shapes;
@ -17,13 +19,54 @@ using Microsoft.UI.Xaml.Shapes;
namespace MapControl namespace MapControl
{ {
public partial class MapGraticule public partial class MapGraticule : MapPanel
{ {
public static readonly DependencyProperty ForegroundProperty =
DependencyPropertyHelper.Register<MapGraticule, Brush>(nameof(Foreground));
public static readonly DependencyProperty FontFamilyProperty =
DependencyPropertyHelper.Register<MapGraticule, FontFamily>(nameof(FontFamily));
public static readonly DependencyProperty FontSizeProperty =
DependencyPropertyHelper.Register<MapGraticule, double>(nameof(FontSize), 12d);
public static readonly DependencyProperty StrokeThicknessProperty =
DependencyPropertyHelper.Register<MapGraticule, double>(nameof(StrokeThickness), 0.5);
private readonly Path path = new Path { Data = new PathGeometry() }; private readonly Path path = new Path { Data = new PathGeometry() };
public MapGraticule() public Brush Foreground
{ {
StrokeThickness = 0.5; get => (Brush)GetValue(ForegroundProperty);
set => SetValue(ForegroundProperty, value);
}
public FontFamily FontFamily
{
get => (FontFamily)GetValue(FontFamilyProperty);
set => SetValue(FontFamilyProperty, value);
}
public double FontSize
{
get => (double)GetValue(FontSizeProperty);
set => SetValue(FontSizeProperty, value);
}
public double StrokeThickness
{
get => (double)GetValue(StrokeThicknessProperty);
set => SetValue(StrokeThicknessProperty, value);
}
protected override void SetParentMap(MapBase map)
{
if (map != null && Foreground == null)
{
SetBinding(ForegroundProperty, map.CreateBinding(nameof(Foreground)));
}
base.SetParentMap(map);
} }
protected override void OnViewportChanged(ViewportChangedEventArgs e) protected override void OnViewportChanged(ViewportChangedEventArgs e)
@ -32,13 +75,8 @@ namespace MapControl
if (Children.Count == 0) if (Children.Count == 0)
{ {
path.SetBinding(Shape.StrokeProperty, this.CreateBinding(nameof(Stroke))); path.SetBinding(Shape.StrokeProperty, this.CreateBinding(nameof(Foreground)));
path.SetBinding(Shape.StrokeThicknessProperty, this.CreateBinding(nameof(StrokeThickness))); path.SetBinding(Shape.StrokeThicknessProperty, this.CreateBinding(nameof(StrokeThickness)));
path.SetBinding(Shape.StrokeStartLineCapProperty, this.CreateBinding(nameof(StrokeLineCap)));
path.SetBinding(Shape.StrokeEndLineCapProperty, this.CreateBinding(nameof(StrokeLineCap)));
path.SetBinding(Shape.StrokeDashCapProperty, this.CreateBinding(nameof(StrokeLineCap)));
path.SetBinding(Shape.StrokeDashArrayProperty, this.CreateBinding(nameof(StrokeDashArray)));
path.SetBinding(Shape.StrokeDashOffsetProperty, this.CreateBinding(nameof(StrokeDashOffset)));
Children.Add(path); Children.Add(path);
} }
@ -57,9 +95,6 @@ namespace MapControl
{ {
textBlock = new TextBlock { RenderTransform = new MatrixTransform() }; textBlock = new TextBlock { RenderTransform = new MatrixTransform() };
textBlock.SetBinding(TextBlock.FontSizeProperty, this.CreateBinding(nameof(FontSize))); textBlock.SetBinding(TextBlock.FontSizeProperty, this.CreateBinding(nameof(FontSize)));
textBlock.SetBinding(TextBlock.FontStyleProperty, this.CreateBinding(nameof(FontStyle)));
textBlock.SetBinding(TextBlock.FontStretchProperty, this.CreateBinding(nameof(FontStretch)));
textBlock.SetBinding(TextBlock.FontWeightProperty, this.CreateBinding(nameof(FontWeight)));
textBlock.SetBinding(TextBlock.ForegroundProperty, this.CreateBinding(nameof(Foreground))); textBlock.SetBinding(TextBlock.ForegroundProperty, this.CreateBinding(nameof(Foreground)));
if (FontFamily != null) if (FontFamily != null)

View file

@ -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 Windows.UI.Text;
#if UWP
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
#else
using Microsoft.UI.Text;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
#endif
namespace MapControl
{
public class MapOverlay : MapPanel
{
public static readonly DependencyProperty FontFamilyProperty =
DependencyPropertyHelper.Register<MapOverlay, FontFamily>(nameof(FontFamily));
public static readonly DependencyProperty FontSizeProperty =
DependencyPropertyHelper.Register<MapOverlay, double>(nameof(FontSize), 12d);
public static readonly DependencyProperty FontStyleProperty =
DependencyPropertyHelper.Register<MapOverlay, FontStyle>(nameof(FontStyle), FontStyle.Normal);
public static readonly DependencyProperty FontStretchProperty =
DependencyPropertyHelper.Register<MapOverlay, FontStretch>(nameof(FontStretch), FontStretch.Normal);
public static readonly DependencyProperty FontWeightProperty =
DependencyPropertyHelper.Register<MapOverlay, FontWeight>(nameof(FontWeight), FontWeights.Normal);
public static readonly DependencyProperty ForegroundProperty =
DependencyPropertyHelper.Register<MapOverlay, Brush>(nameof(Foreground));
public static readonly DependencyProperty StrokeProperty =
DependencyPropertyHelper.Register<MapOverlay, Brush>(nameof(Stroke));
public static readonly DependencyProperty StrokeThicknessProperty =
DependencyPropertyHelper.Register<MapOverlay, double>(nameof(StrokeThickness), 1d);
public static readonly DependencyProperty StrokeDashArrayProperty =
DependencyPropertyHelper.Register<MapOverlay, DoubleCollection>(nameof(StrokeDashArray));
public static readonly DependencyProperty StrokeDashOffsetProperty =
DependencyPropertyHelper.Register<MapOverlay, double>(nameof(StrokeDashOffset));
public static readonly DependencyProperty StrokeLineCapProperty =
DependencyPropertyHelper.Register<MapOverlay, PenLineCap>(nameof(StrokeLineCap), PenLineCap.Flat);
public static readonly DependencyProperty StrokeLineJoinProperty =
DependencyPropertyHelper.Register<MapOverlay, PenLineJoin>(nameof(StrokeLineJoin), PenLineJoin.Miter);
public static readonly DependencyProperty StrokeMiterLimitProperty =
DependencyPropertyHelper.Register<MapOverlay, double>(nameof(StrokeMiterLimit), 1d);
public FontFamily FontFamily
{
get => (FontFamily)GetValue(FontFamilyProperty);
set => SetValue(FontFamilyProperty, value);
}
public double FontSize
{
get => (double)GetValue(FontSizeProperty);
set => SetValue(FontSizeProperty, value);
}
public FontStyle FontStyle
{
get => (FontStyle)GetValue(FontStyleProperty);
set => SetValue(FontStyleProperty, value);
}
public FontStretch FontStretch
{
get => (FontStretch)GetValue(FontStretchProperty);
set => SetValue(FontStretchProperty, value);
}
public FontWeight FontWeight
{
get => (FontWeight)GetValue(FontWeightProperty);
set => SetValue(FontWeightProperty, value);
}
public Brush Foreground
{
get => (Brush)GetValue(ForegroundProperty);
set => SetValue(ForegroundProperty, value);
}
public Brush Stroke
{
get => (Brush)GetValue(StrokeProperty);
set => SetValue(StrokeProperty, value);
}
public double StrokeThickness
{
get => (double)GetValue(StrokeThicknessProperty);
set => SetValue(StrokeThicknessProperty, value);
}
public DoubleCollection StrokeDashArray
{
get => (DoubleCollection)GetValue(StrokeDashArrayProperty);
set => SetValue(StrokeDashArrayProperty, value);
}
public double StrokeDashOffset
{
get => (double)GetValue(StrokeDashOffsetProperty);
set => SetValue(StrokeDashOffsetProperty, value);
}
public PenLineCap StrokeLineCap
{
get => (PenLineCap)GetValue(StrokeLineCapProperty);
set => SetValue(StrokeLineCapProperty, value);
}
public PenLineJoin StrokeLineJoin
{
get => (PenLineJoin)GetValue(StrokeLineJoinProperty);
set => SetValue(StrokeLineJoinProperty, value);
}
public double StrokeMiterLimit
{
get => (double)GetValue(StrokeMiterLimitProperty);
set => SetValue(StrokeMiterLimitProperty, value);
}
protected override void SetParentMap(MapBase map)
{
if (map != null)
{
if (Foreground == null)
{
SetBinding(ForegroundProperty, map.CreateBinding(nameof(Foreground)));
}
if (Stroke == null)
{
SetBinding(StrokeProperty, this.CreateBinding(nameof(Foreground)));
}
}
base.SetParentMap(map);
}
}
}

View file

@ -102,6 +102,9 @@
Description="© [terrestris GmbH &amp; Co. KG](http://ows.terrestris.de/) © [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)"/> Description="© [terrestris GmbH &amp; Co. KG](http://ows.terrestris.de/) © [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)"/>
</tools:MapLayerItem> </tools:MapLayerItem>
<tools:MapLayersMenuButton.MapOverlays> <tools:MapLayersMenuButton.MapOverlays>
<tools:MapLayerItem Text="Graticule">
<map:MapGraticule Opacity="0.7" FontSize="10"/>
</tools:MapLayerItem>
<tools:MapLayerItem Text="Scale"> <tools:MapLayerItem Text="Scale">
<map:MapScale HorizontalAlignment="Center" VerticalAlignment="Bottom"/> <map:MapScale HorizontalAlignment="Center" VerticalAlignment="Bottom"/>
</tools:MapLayerItem> </tools:MapLayerItem>