From 21d7938d33a4b39e4f5a3c927c3567bd70280556 Mon Sep 17 00:00:00 2001 From: ClemensFischer Date: Fri, 5 Dec 2025 15:07:44 +0100 Subject: [PATCH] WPF specific file names --- MapControl/Shared/ImageTileList.cs | 2 +- ...xLayer.cs => BitmapTileMatrixLayer.WPF.cs} | 0 MapControl/WPF/DrawingTile.WPF.cs | 90 +++++++++++++++++++ ...Layer.cs => DrawingTileMatrixLayer.WPF.cs} | 58 +----------- 4 files changed, 95 insertions(+), 55 deletions(-) rename MapControl/WPF/{BitmapTileMatrixLayer.cs => BitmapTileMatrixLayer.WPF.cs} (100%) create mode 100644 MapControl/WPF/DrawingTile.WPF.cs rename MapControl/WPF/{DrawingTileMatrixLayer.cs => DrawingTileMatrixLayer.WPF.cs} (67%) diff --git a/MapControl/Shared/ImageTileList.cs b/MapControl/Shared/ImageTileList.cs index 12342531..6ec65733 100644 --- a/MapControl/Shared/ImageTileList.cs +++ b/MapControl/Shared/ImageTileList.cs @@ -36,7 +36,7 @@ namespace MapControl if (equivalentTile != null) { tile.IsPending = false; - tile.Image.Source = equivalentTile.Image.Source; // no opacity animation + tile.Image.Source = equivalentTile.Image.Source; // no Opacity animation } } diff --git a/MapControl/WPF/BitmapTileMatrixLayer.cs b/MapControl/WPF/BitmapTileMatrixLayer.WPF.cs similarity index 100% rename from MapControl/WPF/BitmapTileMatrixLayer.cs rename to MapControl/WPF/BitmapTileMatrixLayer.WPF.cs diff --git a/MapControl/WPF/DrawingTile.WPF.cs b/MapControl/WPF/DrawingTile.WPF.cs new file mode 100644 index 00000000..6bc26947 --- /dev/null +++ b/MapControl/WPF/DrawingTile.WPF.cs @@ -0,0 +1,90 @@ +using System; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Media; +using System.Windows.Media.Animation; +using System.Windows.Media.Imaging; + +namespace MapControl +{ + public class DrawingTile : Tile + { + private readonly ImageDrawing imageDrawing = new ImageDrawing(); + + public DrawingTile(int zoomLevel, int x, int y, int columnCount) + : base(zoomLevel, x, y, columnCount) + { + Drawing.Children.Add(imageDrawing); + } + + public DrawingGroup Drawing { get; } = new DrawingGroup(); + + public ImageSource ImageSource + { + get => imageDrawing.ImageSource; + set => imageDrawing.ImageSource = value; + } + + public void SetRect(int xMin, int yMin, int tileWidth, int tileHeight) + { + imageDrawing.Rect = new Rect(tileWidth * (X - xMin), tileHeight * (Y - yMin), tileWidth, tileHeight); + } + + public override async Task LoadImageAsync(Func> loadImageFunc) + { + var image = await loadImageFunc().ConfigureAwait(false); + + void SetImageSource() + { + imageDrawing.ImageSource = image; + + if (image != null && MapBase.ImageFadeDuration > TimeSpan.Zero) + { + if (image is BitmapSource bitmap && !bitmap.IsFrozen && bitmap.IsDownloading) + { + bitmap.DownloadCompleted += BitmapDownloadCompleted; + bitmap.DownloadFailed += BitmapDownloadFailed; + } + else + { + BeginFadeInAnimation(); + } + } + } + + await Drawing.Dispatcher.InvokeAsync(SetImageSource); + } + + private void BeginFadeInAnimation() + { + var fadeInAnimation = new DoubleAnimation + { + From = 0d, + Duration = MapBase.ImageFadeDuration, + FillBehavior = FillBehavior.Stop + }; + + Drawing.BeginAnimation(DrawingGroup.OpacityProperty, fadeInAnimation); + } + + private void BitmapDownloadCompleted(object sender, EventArgs e) + { + var bitmap = (BitmapSource)sender; + + bitmap.DownloadCompleted -= BitmapDownloadCompleted; + bitmap.DownloadFailed -= BitmapDownloadFailed; + + BeginFadeInAnimation(); + } + + private void BitmapDownloadFailed(object sender, ExceptionEventArgs e) + { + var bitmap = (BitmapSource)sender; + + bitmap.DownloadCompleted -= BitmapDownloadCompleted; + bitmap.DownloadFailed -= BitmapDownloadFailed; + + imageDrawing.ImageSource = null; + } + } +} diff --git a/MapControl/WPF/DrawingTileMatrixLayer.cs b/MapControl/WPF/DrawingTileMatrixLayer.WPF.cs similarity index 67% rename from MapControl/WPF/DrawingTileMatrixLayer.cs rename to MapControl/WPF/DrawingTileMatrixLayer.WPF.cs index a0f12b94..94a6dffb 100644 --- a/MapControl/WPF/DrawingTileMatrixLayer.cs +++ b/MapControl/WPF/DrawingTileMatrixLayer.WPF.cs @@ -1,68 +1,18 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Threading.Tasks; using System.Windows; using System.Windows.Media; -using System.Windows.Media.Animation; namespace MapControl { - public class ImageDrawingTile : Tile - { - private readonly ImageDrawing imageDrawing = new ImageDrawing(); - - public ImageDrawingTile(int zoomLevel, int x, int y, int columnCount) - : base(zoomLevel, x, y, columnCount) - { - Drawing.Children.Add(imageDrawing); - } - - public DrawingGroup Drawing { get; } = new DrawingGroup(); - - public ImageSource ImageSource - { - get => imageDrawing.ImageSource; - set => imageDrawing.ImageSource = value; - } - - public void SetRect(int xMin, int yMin, int tileWidth, int tileHeight) - { - imageDrawing.Rect = new Rect(tileWidth * (X - xMin), tileHeight * (Y - yMin), tileWidth, tileHeight); - } - - public override async Task LoadImageAsync(Func> loadImageFunc) - { - var image = await loadImageFunc().ConfigureAwait(false); - - void SetImageSource() - { - imageDrawing.ImageSource = image; - - if (image != null && MapBase.ImageFadeDuration > TimeSpan.Zero) - { - var fadeInAnimation = new DoubleAnimation - { - From = 0d, - Duration = MapBase.ImageFadeDuration, - FillBehavior = FillBehavior.Stop - }; - - Drawing.BeginAnimation(DrawingGroup.OpacityProperty, fadeInAnimation); - } - } - - await Drawing.Dispatcher.InvokeAsync(SetImageSource); - } - } - public class DrawingTileMatrixLayer(WmtsTileMatrix wmtsTileMatrix, int zoomLevel) : UIElement { public WmtsTileMatrix WmtsTileMatrix => wmtsTileMatrix; public TileMatrix TileMatrix { get; private set; } = new TileMatrix(zoomLevel, 1, 1, 0, 0); - public IEnumerable Tiles { get; private set; } = []; + public IEnumerable Tiles { get; private set; } = []; public DrawingGroup Drawing { get; } = new DrawingGroup { Transform = new MatrixTransform() }; @@ -125,7 +75,7 @@ namespace MapControl private void CreateTiles() { var tileCount = TileMatrix.Width * TileMatrix.Height; - var tiles = new List(tileCount); + var tiles = new List(tileCount); var drawings = new DrawingCollection(tileCount); for (var y = TileMatrix.YMin; y <= TileMatrix.YMax; y++) @@ -136,14 +86,14 @@ namespace MapControl if (tile == null) { - tile = new ImageDrawingTile(TileMatrix.ZoomLevel, x, y, WmtsTileMatrix.MatrixWidth); + tile = new DrawingTile(TileMatrix.ZoomLevel, x, y, WmtsTileMatrix.MatrixWidth); var equivalentTile = Tiles.FirstOrDefault(t => t.ImageSource != null && t.Column == tile.Column && t.Row == tile.Row); if (equivalentTile != null) { tile.IsPending = false; - tile.ImageSource = equivalentTile.ImageSource; + tile.ImageSource = equivalentTile.ImageSource; // no Opacity animation } }