Update MapScale.cs

This commit is contained in:
ClemensFischer 2024-05-26 15:55:36 +02:00
parent bbd4f6d6aa
commit 9980733c37

View file

@ -7,25 +7,21 @@ using System.Globalization;
#if WPF #if WPF
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Shapes; using System.Windows.Shapes;
#elif UWP #elif UWP
using Windows.Foundation; using Windows.Foundation;
using Windows.UI.Xaml; using Windows.UI.Xaml;
using Windows.UI.Xaml.Data;
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;
#elif WINUI #elif WINUI
using Windows.Foundation; using Windows.Foundation;
using Microsoft.UI.Xaml; using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Data;
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;
#elif AVALONIA #elif AVALONIA
using Avalonia.Data;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Controls.Shapes; using Avalonia.Controls.Shapes;
using Avalonia.Media; using Avalonia.Media;
@ -40,18 +36,32 @@ namespace MapControl
/// <summary> /// <summary>
/// Draws a map scale overlay. /// Draws a map scale overlay.
/// </summary> /// </summary>
public class MapScale : MapOverlay public class MapScale : MapPanel
{ {
public static readonly DependencyProperty PaddingProperty = public static readonly DependencyProperty PaddingProperty =
DependencyPropertyHelper.Register<MapScale, Thickness>(nameof(Padding), new Thickness(4)); DependencyPropertyHelper.Register<MapScale, Thickness>(nameof(Padding), new Thickness(4));
public static readonly DependencyProperty StrokeThicknessProperty =
DependencyPropertyHelper.Register<MapScale, double>(nameof(StrokeThickness), 1d);
public Thickness Padding
{
get => (Thickness)GetValue(PaddingProperty);
set => SetValue(PaddingProperty, value);
}
public double StrokeThickness
{
get => (double)GetValue(StrokeThicknessProperty);
set => SetValue(StrokeThicknessProperty, value);
}
private readonly Polyline line = new Polyline(); private readonly Polyline line = new Polyline();
private readonly TextBlock label = new TextBlock private readonly TextBlock label = new TextBlock
{ {
HorizontalAlignment = HorizontalAlignment.Left, HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Top, VerticalAlignment = VerticalAlignment.Center
TextAlignment = TextAlignment.Center
}; };
public MapScale() public MapScale()
@ -65,17 +75,12 @@ namespace MapControl
{ {
base.SetParentMap(map); base.SetParentMap(map);
line.SetBinding(Shape.StrokeProperty, this.CreateBinding(nameof(Stroke))); line.SetBinding(Shape.StrokeProperty, map.CreateBinding(nameof(Map.Foreground)));
line.SetBinding(Shape.StrokeThicknessProperty, this.CreateBinding(nameof(StrokeThickness))); line.SetBinding(Shape.StrokeThicknessProperty, this.CreateBinding(nameof(StrokeThickness)));
#if UWP || WINUI
label.SetBinding(TextBlock.ForegroundProperty, this.CreateBinding(nameof(Foreground)));
#endif
}
public Thickness Padding #if UWP || WINUI
{ label.SetBinding(TextBlock.ForegroundProperty, map.CreateBinding(nameof(Map.Foreground)));
get => (Thickness)GetValue(PaddingProperty); #endif
set => SetValue(PaddingProperty, value);
} }
protected override Size MeasureOverride(Size availableSize) protected override Size MeasureOverride(Size availableSize)
@ -96,7 +101,7 @@ namespace MapControl
var size = new Size( var size = new Size(
length * scale + StrokeThickness + Padding.Left + Padding.Right, length * scale + StrokeThickness + Padding.Left + Padding.Right,
1.25 * FontSize + StrokeThickness + Padding.Top + Padding.Bottom); 1.5 * label.FontSize + 2 * StrokeThickness + Padding.Top + Padding.Bottom);
var x1 = Padding.Left + StrokeThickness / 2d; var x1 = Padding.Left + StrokeThickness / 2d;
var x2 = size.Width - Padding.Right - StrokeThickness / 2d; var x2 = size.Width - Padding.Right - StrokeThickness / 2d;
@ -110,13 +115,12 @@ namespace MapControl
new Point(x2, y2), new Point(x2, y2),
new Point(x2, y1) new Point(x2, y1)
}; };
line.Measure(size);
label.Text = length >= 1000d label.Text = length >= 1000d
? string.Format(CultureInfo.InvariantCulture, "{0:F0} km", length / 1000d) ? string.Format(CultureInfo.InvariantCulture, "{0:F0} km", length / 1000d)
: string.Format(CultureInfo.InvariantCulture, "{0:F0} m", length); : string.Format(CultureInfo.InvariantCulture, "{0:F0} m", length);
label.Width = size.Width;
label.Height = size.Height; line.Measure(size);
label.Measure(size); label.Measure(size);
return size; return size;