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

208 lines
6.5 KiB
C#
Raw Normal View History

2025-02-27 18:46:32 +01:00
using System;
2020-03-20 18:12:56 +01:00
using System.Collections.Generic;
2024-05-22 11:25:32 +02:00
#if WPF
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
#elif UWP
using Windows.UI.Composition;
2024-05-22 11:25:32 +02:00
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Hosting;
2024-05-22 11:25:32 +02:00
using Windows.UI.Xaml.Media;
2024-05-20 23:24:34 +02:00
#elif WINUI
using Microsoft.UI.Composition;
2021-06-14 21:41:37 +02:00
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Hosting;
2021-06-14 21:41:37 +02:00
using Microsoft.UI.Xaml.Media;
2025-08-19 19:43:02 +02:00
#elif AVALONIA
using Avalonia.Controls;
2025-11-13 13:36:28 +01:00
using Brush = Avalonia.Media.IBrush;
2020-03-20 18:12:56 +01:00
#endif
2026-04-13 17:14:49 +02:00
namespace MapControl;
public abstract class TilePyramidLayer : Panel, IMapLayer
2020-03-20 18:12:56 +01:00
{
2026-04-13 17:14:49 +02:00
public static readonly DependencyProperty SourceNameProperty =
DependencyPropertyHelper.Register<TilePyramidLayer, string>(nameof(SourceName));
2020-03-20 18:12:56 +01:00
2026-04-13 17:14:49 +02:00
public static readonly DependencyProperty DescriptionProperty =
DependencyPropertyHelper.Register<TilePyramidLayer, string>(nameof(Description));
2020-03-20 18:12:56 +01:00
2026-04-13 17:14:49 +02:00
public static readonly DependencyProperty MaxBackgroundLevelsProperty =
DependencyPropertyHelper.Register<TilePyramidLayer, int>(nameof(MaxBackgroundLevels), 5);
2020-03-20 18:12:56 +01:00
2026-04-13 17:14:49 +02:00
public static readonly DependencyProperty UpdateIntervalProperty =
DependencyPropertyHelper.Register<TilePyramidLayer, TimeSpan>(nameof(UpdateInterval), TimeSpan.FromSeconds(0.2),
(layer, oldValue, newValue) => layer.updateTimer.Interval = newValue);
2020-03-20 18:12:56 +01:00
2026-04-13 17:14:49 +02:00
public static readonly DependencyProperty UpdateWhileViewportChangingProperty =
DependencyPropertyHelper.Register<TilePyramidLayer, bool>(nameof(UpdateWhileViewportChanging));
2020-03-20 18:12:56 +01:00
2026-04-13 17:14:49 +02:00
public static readonly DependencyProperty MapBackgroundProperty =
DependencyPropertyHelper.Register<TilePyramidLayer, Brush>(nameof(MapBackground));
2020-03-20 18:12:56 +01:00
2026-04-13 17:14:49 +02:00
public static readonly DependencyProperty MapForegroundProperty =
DependencyPropertyHelper.Register<TilePyramidLayer, Brush>(nameof(MapForeground));
2020-03-20 18:12:56 +01:00
2026-04-13 17:14:49 +02:00
public static readonly DependencyProperty LoadingProgressProperty =
DependencyPropertyHelper.Register<TilePyramidLayer, double>(nameof(LoadingProgress), 1d);
2026-04-13 17:14:49 +02:00
private readonly Progress<double> loadingProgress;
private readonly UpdateTimer updateTimer;
2020-03-20 18:12:56 +01:00
2026-04-13 17:14:49 +02:00
protected TilePyramidLayer()
{
IsHitTestVisible = false;
2026-04-13 17:14:49 +02:00
loadingProgress = new Progress<double>(p => SetValue(LoadingProgressProperty, p));
2022-01-12 23:56:05 +01:00
2026-04-13 17:14:49 +02:00
updateTimer = new UpdateTimer { Interval = UpdateInterval };
updateTimer.Tick += (_, _) => UpdateTiles();
#if WPF
2026-04-13 17:14:49 +02:00
RenderOptions.SetEdgeMode(this, EdgeMode.Aliased);
#elif UWP || WINUI
2026-04-13 17:14:49 +02:00
ElementCompositionPreview.GetElementVisual(this).BorderMode = CompositionBorderMode.Hard;
MapPanel.InitMapElement(this);
2021-01-17 00:31:30 +01:00
#endif
2026-04-13 17:14:49 +02:00
}
2020-03-20 18:12:56 +01:00
2026-04-13 17:14:49 +02:00
public ITileImageLoader TileImageLoader
{
get => field ??= new TileImageLoader();
set;
}
2020-03-20 18:12:56 +01:00
2026-04-13 17:14:49 +02:00
/// <summary>
/// Name of the tile source that is used as component of a tile cache key.
/// Tile images are not cached when SourceName is null or empty.
/// </summary>
public string SourceName
{
get => (string)GetValue(SourceNameProperty);
set => SetValue(SourceNameProperty, value);
}
2020-03-20 18:12:56 +01:00
2026-04-13 17:14:49 +02:00
/// <summary>
/// Description of the layer. Used to display copyright information on top of the map.
/// </summary>
public string Description
{
get => (string)GetValue(DescriptionProperty);
set => SetValue(DescriptionProperty, value);
}
2020-03-20 18:12:56 +01:00
2026-04-13 17:14:49 +02:00
/// <summary>
/// Maximum number of background tile levels. Default value is 5.
/// Only effective in a MapTileLayer or WmtsTileLayer that is the MapLayer of its ParentMap.
/// </summary>
public int MaxBackgroundLevels
{
get => (int)GetValue(MaxBackgroundLevelsProperty);
set => SetValue(MaxBackgroundLevelsProperty, value);
}
2020-03-20 18:12:56 +01:00
2026-04-13 17:14:49 +02:00
/// <summary>
/// Minimum time interval between tile updates.
/// </summary>
public TimeSpan UpdateInterval
{
get => (TimeSpan)GetValue(UpdateIntervalProperty);
set => SetValue(UpdateIntervalProperty, value);
}
2020-03-20 18:12:56 +01:00
2026-04-13 17:14:49 +02:00
/// <summary>
/// Controls if tiles are updated while the viewport is still changing.
/// </summary>
public bool UpdateWhileViewportChanging
{
get => (bool)GetValue(UpdateWhileViewportChangingProperty);
set => SetValue(UpdateWhileViewportChangingProperty, value);
}
2020-03-20 18:12:56 +01:00
2026-04-13 17:14:49 +02:00
/// <summary>
/// Optional background brush. Sets MapBase.Background if not null and this layer is the base map layer.
/// </summary>
public Brush MapBackground
{
get => (Brush)GetValue(MapBackgroundProperty);
set => SetValue(MapBackgroundProperty, value);
}
2020-03-20 18:12:56 +01:00
2026-04-13 17:14:49 +02:00
/// <summary>
/// Optional foreground brush. Sets MapBase.Foreground if not null and this layer is the base map layer.
/// </summary>
public Brush MapForeground
{
get => (Brush)GetValue(MapForegroundProperty);
set => SetValue(MapForegroundProperty, value);
}
2020-03-20 18:12:56 +01:00
2026-04-13 17:14:49 +02:00
/// <summary>
/// Gets the progress of the TileImageLoader as a double value between 0 and 1.
/// </summary>
public double LoadingProgress => (double)GetValue(LoadingProgressProperty);
2026-04-13 17:14:49 +02:00
/// <summary>
/// Implements IMapElement.ParentMap.
/// </summary>
public MapBase ParentMap
{
get;
set
2020-03-20 18:12:56 +01:00
{
2026-04-13 17:14:49 +02:00
if (field != null)
2020-03-20 18:12:56 +01:00
{
2026-04-13 17:14:49 +02:00
field.ViewportChanged -= OnViewportChanged;
}
2020-03-20 18:12:56 +01:00
2026-04-13 17:14:49 +02:00
field = value;
2020-03-20 18:12:56 +01:00
2026-04-13 17:14:49 +02:00
if (field != null)
{
field.ViewportChanged += OnViewportChanged;
2020-03-20 18:12:56 +01:00
}
2026-04-13 17:14:49 +02:00
updateTimer.Run();
2020-03-20 18:12:56 +01:00
}
2026-04-13 17:14:49 +02:00
}
2020-03-20 18:12:56 +01:00
2026-04-13 17:14:49 +02:00
public bool IsBaseMapLayer => ParentMap != null && ParentMap.Children.Count > 0 && ParentMap.Children[0] == this;
2025-09-19 18:49:12 +02:00
2026-04-13 17:14:49 +02:00
protected void BeginLoadTiles(IEnumerable<Tile> tiles, TileSource tileSource, string cacheName)
{
TileImageLoader.BeginLoadTiles(tiles, tileSource, cacheName, loadingProgress);
}
2021-11-14 19:29:35 +01:00
2026-04-13 17:14:49 +02:00
protected void CancelLoadTiles()
{
TileImageLoader.CancelLoadTiles();
ClearValue(LoadingProgressProperty);
}
2025-08-19 23:20:11 +02:00
2026-04-13 17:14:49 +02:00
protected abstract void UpdateRenderTransform();
2025-01-28 17:52:40 +01:00
2026-04-13 17:14:49 +02:00
protected abstract void UpdateTileCollection();
2025-01-28 17:52:40 +01:00
2026-04-13 17:14:49 +02:00
private void UpdateTiles()
{
updateTimer.Stop();
UpdateTileCollection();
}
private void OnViewportChanged(object sender, ViewportChangedEventArgs e)
{
if (e.TransformCenterChanged || e.ProjectionChanged || Children.Count == 0)
2021-11-22 23:53:01 +01:00
{
2026-04-13 17:14:49 +02:00
UpdateTiles(); // update immediately
2021-11-22 23:53:01 +01:00
}
2026-04-13 17:14:49 +02:00
else
2020-03-20 18:12:56 +01:00
{
2026-04-13 17:14:49 +02:00
UpdateRenderTransform();
updateTimer.Run(!UpdateWhileViewportChanging);
2020-03-20 18:12:56 +01:00
}
}
}