Skip to content

Commit

Permalink
feat: improve deck screenshot loading behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
azeier committed Nov 12, 2024
1 parent 209d321 commit f0ef6b8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Hearthstone_Deck_Tracker.Controls;
using Hearthstone_Deck_Tracker.Controls.Error;
using Hearthstone_Deck_Tracker.Hearthstone;
using Hearthstone_Deck_Tracker.Utility.Assets;
using Hearthstone_Deck_Tracker.Utility.Logging;

namespace Hearthstone_Deck_Tracker.FlyoutControls.DeckScreenshot
Expand All @@ -23,6 +25,26 @@ public static async Task<RenderTargetBitmap> Generate(Deck deck, bool cardsOnly)
var height = CardHeight * deck.GetSelectedDeckVersion().Cards.Count;
if(!cardsOnly)
height += InfoHeight;

// Wait for all images to be available
if(AssetDownloaders.cardTileDownloader != null)
{
var tasks = deck.Cards
.Select(c => AssetDownloaders.cardTileDownloader.GetAssetData(c))
// If the tiles are available on disk they will be loaded
// synchronously and the task should complete immediately.
.Where(x => !x.IsCompleted).ToList();

if(tasks.Any())
{
await Task.WhenAll(tasks);

// For good measure. For some reason some small number of
// images isn't always available immediately
await Task.Delay(250);
}
}

var control = new DeckView(deck, cardsOnly);
await control.Init();
control.Measure(new Size(ScreenshotWidth, height));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<UserControl x:Class="Hearthstone_Deck_Tracker.FlyoutControls.DeckScreenshot.DeckScreenshotView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:deckScreenshot="clr-namespace:Hearthstone_Deck_Tracker.FlyoutControls.DeckScreenshot"
xmlns:lex="http://wpflocalizeextension.codeplex.com"
xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="200">
<UserControl.DataContext>
Expand Down Expand Up @@ -49,14 +50,14 @@
<TextBlock Text="{Binding UploadButtonText}" Margin="4,0,0,0"/>
</StackPanel>
</Button>
<TextBlock Text="{lex:Loc DeckScreenshot_Text_UploadError}"
<TextBlock Text="{lex:Loc DeckScreenshot_Text_UploadError}"
TextWrapping="Wrap" DockPanel.Dock="Top" Foreground="Red" Visibility="{Binding UploadErrorVisibility}"/>
<TextBlock DockPanel.Dock="Top" Visibility="{Binding ImgurUrlVisibility}" Margin="5,0,0,0">
<Hyperlink NavigateUri="{Binding ImgurUrl, TargetNullValue=''}" RequestNavigate="Hyperlink_OnRequestNavigate">
<Run Text="{Binding ImgurUrl}"></Run>
</Hyperlink>
</TextBlock>
<Button Command="{Binding CopyToClipboardCommand}"
<Button Command="{Binding CopyToClipboardCommand}"
DockPanel.Dock="Top" Margin="0,5,0,0">
<StackPanel Orientation="Horizontal">
<Rectangle Width="12">
Expand All @@ -69,7 +70,10 @@
</Button>
<GroupBox Header="{lex:LocTextUpper DeckScreenshot_Preview_Header}" Margin="0,5">
<ScrollViewer VerticalScrollBarVisibility="Auto">
<Image DockPanel.Dock="Left" Width="219" Source="{Binding DeckImage}" RenderOptions.BitmapScalingMode="fant"/>
<Grid>
<mah:ProgressRing IsActive="True" Foreground="White" Width="20" Height="20" Visibility="{Binding ImageReady, Converter={StaticResource InverseBoolToVisibility}}"/>
<Image DockPanel.Dock="Left" Width="219" Source="{Binding DeckImage}" RenderOptions.BitmapScalingMode="fant" Visibility="{Binding ImageReady, Converter={StaticResource BoolToVisibility}}"/>
</Grid>
</ScrollViewer>
</GroupBox>
</DockPanel>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ public class DeckScreenshotViewModel : ViewModel
private string _uploadButtonText = LocUtil.Get(ImgurDefault, true);
private Visibility _uploadErrorVisibility = Visibility.Collapsed;

public bool ImageReady
{
get => GetProp(false);
set => SetProp(value);
}

public bool CardsOnly
{
get { return _cardsOnly; }
Expand Down Expand Up @@ -223,7 +229,9 @@ private async void UpdateImage()
{
if(_deck == null)
return;
ImageReady = false;
DeckImage = await DeckScreenshotHelper.Generate(_deck, CardsOnly);
ImageReady = true;
}
}
}

0 comments on commit f0ef6b8

Please sign in to comment.