Skip to content

Commit

Permalink
Boxarts updater
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuuzera committed Sep 18, 2024
1 parent 37221df commit 54ea42d
Show file tree
Hide file tree
Showing 19 changed files with 574 additions and 74 deletions.
16 changes: 15 additions & 1 deletion TrimuiSmartHub.Application/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,21 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TrimuiSmartHub.Application"
StartupUri="MainWindow.xaml">
StartupUri="MainWindow.xaml"
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml">

<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<!-- Theme setting -->
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Themes/Light.Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/LoadingIndicators.WPF;component/Styles/LoadingDoubleBounce.xaml"/>
<ResourceDictionary Source="pack://application:,,,/LoadingIndicators.WPF;component/Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>

<Style x:Key="TransparentButton" TargetType="{x:Type Button}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Template">
Expand Down Expand Up @@ -181,5 +194,6 @@
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</Application.Resources>
</Application>
47 changes: 47 additions & 0 deletions TrimuiSmartHub.Application/Components/CustomDialog.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<UserControl x:Class="TrimuiSmartHub.Application.Components.CustomDialog"
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:local="clr-namespace:TrimuiSmartHub.Application.Components"
mc:Ignorable="d"
d:DesignHeight="650" d:DesignWidth="800"
>
<Grid Background="#80000000">

<Border
HorizontalAlignment="Stretch"
>
<Border.Background>
<LinearGradientBrush StartPoint="0.5, 0" EndPoint="0.5,1">
<GradientStop Color="#2C3E50" />
<GradientStop Color="#1b2a3d" Offset="0.5" />

</LinearGradientBrush>
</Border.Background>
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center" >
<TextBlock TextWrapping="WrapWithOverflow"
Text="To connect your device, please plug in the USB cable, navigate to the App menu, and select the 'USB Storage' option."
Foreground="#AAB8C2"
Margin="15"
FontSize="16"
MaxWidth="550"
FontWeight="SemiBold"/>

<Image Source="/Resources/Images/Ui/trimui-usb-example.png" Width="500" RenderOptions.BitmapScalingMode="HighQuality"/>

<StackPanel x:Name="ButtonPanel"
Orientation="Horizontal"
HorizontalAlignment="Center"
VerticalAlignment="Bottom"
Margin="10" >
<Button Content="Agreed"
Width="155"
Foreground="#AAB8C2"
Margin="5"
Click="OkButton_Click"/>
</StackPanel>
</StackPanel>
</Border>
</Grid>
</UserControl>
36 changes: 36 additions & 0 deletions TrimuiSmartHub.Application/Components/CustomDialog.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TrimuiSmartHub.Application.Components
{
public partial class CustomDialog : UserControl
{
public CustomDialog()
{
InitializeComponent();
}

// Método para esconder o modal
public void Hide()
{
this.Visibility = Visibility.Collapsed;
}

private void OkButton_Click(object sender, RoutedEventArgs e)
{
Hide();
}
}
}
25 changes: 20 additions & 5 deletions TrimuiSmartHub.Application/Frames/GameImageScrapper.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,28 @@
xmlns:local="clr-namespace:TrimuiSmartHub.Application.Frames"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
xmlns:li="clr-namespace:LoadingIndicators.WPF;assembly=LoadingIndicators.WPF"
Title="GameImageScrapper">

<Grid>
<ScrollViewer VerticalScrollBarVisibility="Auto" Style="{StaticResource CustomScrollViewer}" >
<WrapPanel Orientation="Horizontal" Margin="10" x:Name="Container">
<StackPanel x:Name="LoadingIndicator" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,0,25,100">
<TextBlock FontSize="18" FontWeight="SemiBold" Foreground="#AAB8C2" Text="Loading Emulators" Margin="25"/>
<li:LoadingIndicator SpeedRatio="1" IsActive="True" Style="{DynamicResource LoadingIndicatorThreeDotsStyle}" Foreground="#2d607f" RenderTransformOrigin="0.5,0.5" >
<li:LoadingIndicator.RenderTransform>
<ScaleTransform ScaleX="2" ScaleY="2" />
</li:LoadingIndicator.RenderTransform>
</li:LoadingIndicator>
</StackPanel>
<StackPanel x:Name="Panel" Visibility="Collapsed" Orientation="Vertical" Margin="0,25">
<TextBlock Text="Select a console to download missing game box arts." Foreground="#AAB8C2" FontSize="16" FontWeight="SemiBold"/>
<Separator Width="Auto" Margin="5, 20, 20,20" Background="#AAB8C2"/>

<ScrollViewer VerticalScrollBarVisibility="Auto" Style="{StaticResource CustomScrollViewer}" >
<WrapPanel Orientation="Horizontal" Margin="10" x:Name="Container">


</WrapPanel>
</ScrollViewer>
</WrapPanel>
</ScrollViewer>

</StackPanel>
</Grid>
</Page>
184 changes: 162 additions & 22 deletions TrimuiSmartHub.Application/Frames/GameImageScrapper.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
using System.Windows;
using System.Windows.Controls;
using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;
using TrimuiSmartHub.Application.Services.Trimui;
using System.IO;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using TrimuiSmartHub.Application.Repository;
using CsQuery.ExtensionMethods.Internal;
using TrimuiSmartHub.Application.Services.LibRetro;
using TrimuiSmartHub.Application.Helpers;
using System.Windows.Media.Effects;

namespace TrimuiSmartHub.Application.Frames
{
public partial class GameImageScrapper : Page
{
private ProgressDialogController Controller { get; set; }
public GameImageScrapper()
{
InitializeComponent();
Expand All @@ -22,18 +27,31 @@ public GameImageScrapper()

private void PopulateEmulators()
{
var emulatorList = TrimuiService.New().GetEmulators();

foreach (var emulator in emulatorList)
Task.Run(() =>
{
var emulatorRoms = TrimuiService.New().GetRomsByEmulator(emulator);
var emulatorList = TrimuiService.New().GetEmulators();

var emulatorCard = CreateGameComponent(emulator, emulatorRoms);
foreach (var emulator in emulatorList)
{
var emulatorRoms = TrimuiService.New().GetRomsByEmulator(emulator);

if (emulatorCard == null) continue;
Dispatcher.Invoke(() =>
{
var emulatorCard = CreateGameComponent(emulator, emulatorRoms);

Container.Children.Add(emulatorCard);
}
if (emulatorCard != null)
{
Container.Children.Add(emulatorCard);
}
});
}

Dispatcher.Invoke(() =>
{
Panel.Visibility = Visibility.Visible;
LoadingIndicator.Visibility = Visibility.Collapsed;
});
});
}
private Button? CreateGameComponent(string emulator, List<string> romsList)
{
Expand All @@ -45,11 +63,22 @@ private void PopulateEmulators()

Border border = new Border
{
Background = new SolidColorBrush(Color.FromArgb(110, 51, 51, 51)),
Background = new LinearGradientBrush
{
StartPoint = new Point(0.5, 0),
EndPoint = new Point(0.5, 1),
GradientStops = new GradientStopCollection
{
new GradientStop(Color.FromArgb(35, 25, 84, 112), 1),
new GradientStop(Color.FromArgb(75, 45, 84, 112), 1)
}
},
CornerRadius = new CornerRadius(10),
Width = 120,
Height = 120,
Margin = new Thickness(5)
Margin = new Thickness(5),
BorderThickness = new Thickness(1),
BorderBrush = new SolidColorBrush(Color.FromArgb(75, 45, 84, 112)),
};

StackPanel stackPanel = new StackPanel
Expand Down Expand Up @@ -98,31 +127,142 @@ private void PopulateEmulators()
Content = border,
Style = (Style)System.Windows.Application.Current.FindResource("GameButtonStyle"),
Background = Brushes.Transparent,
BorderThickness = new Thickness(0)
BorderThickness = new Thickness(0),
};

button.Click += (sender, e) =>
button.Click += async (sender, e) =>
{
var imgFolder = TrimuiService.New().GetImageFolder(emulator);
var metroWindow = (MetroWindow)System.Windows.Application.Current.MainWindow;

var imgFolder = TrimuiService.New().GetImageFolder(emulator);
var count = 0;
foreach (var rom in romsList)
var totalRoms = romsList.Count;

int processedRoms = 0;

var libRetro = LibretroService.New();

await Loading(true, $"Downloading games box art...");

await Task.Run(async () =>
{
if (File.Exists(Path.Combine(imgFolder, $"{rom}.png"))) continue;
foreach (var romFile in romsList)
{
var rom = romFile.Split('.').First();

if (File.Exists(Path.Combine(imgFolder, $"{rom}.png")))
{
Interlocked.Increment(ref processedRoms);

var boxImage = LibretroService.New().SearchThumbnail(emulatorDescription, rom);
continue;
}

if (boxImage == null) continue;
var boxImage = await libRetro.SearchThumbnail(emulatorDescription, romFile);

if (boxImage == null)
{
continue;
}

try
{
File.WriteAllBytes(Path.Combine(imgFolder, $"{rom}.png"), boxImage);
Interlocked.Increment(ref count);
}
catch (Exception ex)
{
//ignore
}
}

File.WriteAllBytes($@"{imgFolder}\{rom}.png", boxImage);

count++;
}
});


if (count > 0) MessageBox.Show($"{count} new Game Images find!");
await Loading(false);

if (count > 0) await ShowMessageAsync("Download Completed!", $"{count} Files was updated!");
};

return button;
}
private async Task<MessageDialogResult> ShowMessageAsync(string title, string content, MetroDialogSettings dialogSettings = null)
{
if (dialogSettings == null)
{
dialogSettings = new MetroDialogSettings
{
AffirmativeButtonText = "OK",
NegativeButtonText = "Cancel",
AnimateShow = true,
AnimateHide = true,
ColorScheme = MetroDialogColorScheme.Inverted,
DialogTitleFontSize = 30,
DialogMessageFontSize = 20,
DefaultButtonFocus = MessageDialogResult.Affirmative,
};
}

var metroWindow = (MetroWindow)System.Windows.Application.Current.MainWindow;

return await metroWindow.ShowMessageAsync(title, content, MessageDialogStyle.Affirmative, dialogSettings);
}
private async Task Loading(bool isLoading, string controllerTitle = null, Action cancelAction = null)
{
try
{
await CloseController();

if (!isLoading) return;

var metroDialogSettings = new MetroDialogSettings
{
DialogTitleFontSize = 30,
DialogMessageFontSize = 20,
AnimateShow = true,
AnimateHide = true,
ColorScheme = MetroDialogColorScheme.Inverted
};

var metroWindow = (MetroWindow)System.Windows.Application.Current.MainWindow;

controllerTitle = controllerTitle.IsNullOrEmpty() ? "Downloading box arts..." : controllerTitle;

Controller = await metroWindow.ShowProgressAsync(controllerTitle, "Please don't disconnect the device.", cancelAction != null, metroDialogSettings);

Controller.SetIndeterminate();

Controller.Canceled += (sender, args) =>
{
cancelAction?.Invoke();
};
}
catch (Exception)
{
await CloseController();
}
}

private async Task CloseController()
{
try
{
if (Controller != null)
{
if (Controller.IsOpen) await Controller.CloseAsync();

Controller = null;
}
}
catch (Exception)
{
// ignored
}
}

private async Task UpdateProgressAsync(int processedRoms, int totalRoms)
{
double progress = (double)processedRoms / totalRoms;
await Dispatcher.InvokeAsync(() => Controller.SetProgress(progress));
}
}
}
Loading

0 comments on commit 54ea42d

Please sign in to comment.