2022-07-08 20:47:11 +02:00
|
|
|
using Avalonia.Controls;
|
|
|
|
using Avalonia.Interactivity;
|
2022-07-24 19:38:38 +02:00
|
|
|
using Avalonia.VisualTree;
|
|
|
|
using FluentAvalonia.UI.Controls;
|
|
|
|
using FluentAvalonia.UI.Navigation;
|
2022-07-08 20:47:11 +02:00
|
|
|
using Ryujinx.Ava.Common.Locale;
|
2023-01-11 06:20:19 +01:00
|
|
|
using Ryujinx.Ava.UI.Controls;
|
2022-12-29 15:24:05 +01:00
|
|
|
using Ryujinx.Ava.UI.Models;
|
2023-01-11 06:20:19 +01:00
|
|
|
using Ryujinx.Ava.UI.ViewModels;
|
2022-07-08 20:47:11 +02:00
|
|
|
using Ryujinx.HLE.FileSystem;
|
|
|
|
using SixLabors.ImageSharp;
|
|
|
|
using SixLabors.ImageSharp.Processing;
|
|
|
|
using System.IO;
|
|
|
|
using Image = SixLabors.ImageSharp.Image;
|
|
|
|
|
2023-01-11 06:20:19 +01:00
|
|
|
namespace Ryujinx.Ava.UI.Views.User
|
2022-07-08 20:47:11 +02:00
|
|
|
{
|
2023-01-11 06:20:19 +01:00
|
|
|
public partial class UserProfileImageSelectorView : UserControl
|
2022-07-08 20:47:11 +02:00
|
|
|
{
|
2022-07-24 19:38:38 +02:00
|
|
|
private ContentManager _contentManager;
|
|
|
|
private NavigationDialogHost _parent;
|
|
|
|
private TempProfile _profile;
|
2022-07-08 20:47:11 +02:00
|
|
|
|
2023-01-11 06:20:19 +01:00
|
|
|
internal UserProfileImageSelectorViewModel ViewModel { get; private set; }
|
2022-07-08 20:47:11 +02:00
|
|
|
|
2023-01-11 06:20:19 +01:00
|
|
|
public UserProfileImageSelectorView()
|
2022-07-08 20:47:11 +02:00
|
|
|
{
|
|
|
|
InitializeComponent();
|
2022-07-24 19:38:38 +02:00
|
|
|
AddHandler(Frame.NavigatedToEvent, (s, e) =>
|
|
|
|
{
|
|
|
|
NavigatedTo(e);
|
|
|
|
}, RoutingStrategies.Direct);
|
2022-07-08 20:47:11 +02:00
|
|
|
}
|
|
|
|
|
2022-07-24 19:38:38 +02:00
|
|
|
private void NavigatedTo(NavigationEventArgs arg)
|
2022-07-08 20:47:11 +02:00
|
|
|
{
|
2022-07-24 19:38:38 +02:00
|
|
|
if (Program.PreviewerDetached)
|
|
|
|
{
|
|
|
|
switch (arg.NavigationMode)
|
|
|
|
{
|
|
|
|
case NavigationMode.New:
|
|
|
|
(_parent, _profile) = ((NavigationDialogHost, TempProfile))arg.Parameter;
|
|
|
|
_contentManager = _parent.ContentManager;
|
2023-01-11 06:20:19 +01:00
|
|
|
|
|
|
|
((ContentDialog)_parent.Parent).Title = $"{LocaleManager.Instance[LocaleKeys.UserProfileWindowTitle]} - {LocaleManager.Instance[LocaleKeys.ProfileImageSelectionHeader]}";
|
|
|
|
|
|
|
|
if (Program.PreviewerDetached)
|
|
|
|
{
|
|
|
|
DataContext = ViewModel = new UserProfileImageSelectorViewModel();
|
|
|
|
ViewModel.FirmwareFound = _contentManager.GetCurrentFirmwareVersion() != null;
|
|
|
|
}
|
|
|
|
|
2022-07-24 19:38:38 +02:00
|
|
|
break;
|
|
|
|
case NavigationMode.Back:
|
2023-01-11 06:20:19 +01:00
|
|
|
if (_profile.Image != null)
|
|
|
|
{
|
|
|
|
_parent.GoBack();
|
|
|
|
}
|
2022-07-24 19:38:38 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-07-08 20:47:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private async void Import_OnClick(object sender, RoutedEventArgs e)
|
|
|
|
{
|
|
|
|
OpenFileDialog dialog = new();
|
|
|
|
dialog.Filters.Add(new FileDialogFilter
|
|
|
|
{
|
2023-01-03 19:45:08 +01:00
|
|
|
Name = LocaleManager.Instance[LocaleKeys.AllSupportedFormats],
|
2022-07-08 20:47:11 +02:00
|
|
|
Extensions = { "jpg", "jpeg", "png", "bmp" }
|
|
|
|
});
|
|
|
|
dialog.Filters.Add(new FileDialogFilter { Name = "JPEG", Extensions = { "jpg", "jpeg" } });
|
|
|
|
dialog.Filters.Add(new FileDialogFilter { Name = "PNG", Extensions = { "png" } });
|
|
|
|
dialog.Filters.Add(new FileDialogFilter { Name = "BMP", Extensions = { "bmp" } });
|
|
|
|
|
|
|
|
dialog.AllowMultiple = false;
|
|
|
|
|
2022-07-24 19:38:38 +02:00
|
|
|
string[] image = await dialog.ShowAsync(((TopLevel)_parent.GetVisualRoot()) as Window);
|
2022-07-08 20:47:11 +02:00
|
|
|
|
|
|
|
if (image != null)
|
|
|
|
{
|
|
|
|
if (image.Length > 0)
|
|
|
|
{
|
|
|
|
string imageFile = image[0];
|
|
|
|
|
2022-07-24 19:38:38 +02:00
|
|
|
_profile.Image = ProcessProfileImage(File.ReadAllBytes(imageFile));
|
2022-07-08 20:47:11 +02:00
|
|
|
|
2023-01-11 06:20:19 +01:00
|
|
|
if (_profile.Image != null)
|
|
|
|
{
|
|
|
|
_parent.GoBack();
|
|
|
|
}
|
|
|
|
}
|
2022-07-08 20:47:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 06:20:19 +01:00
|
|
|
private void GoBack(object sender, RoutedEventArgs e)
|
|
|
|
{
|
|
|
|
_parent.GoBack();
|
|
|
|
}
|
|
|
|
|
2022-07-24 19:38:38 +02:00
|
|
|
private void SelectFirmwareImage_OnClick(object sender, RoutedEventArgs e)
|
2022-07-08 20:47:11 +02:00
|
|
|
{
|
2023-01-11 06:20:19 +01:00
|
|
|
if (ViewModel.FirmwareFound)
|
2022-07-08 20:47:11 +02:00
|
|
|
{
|
2023-01-11 06:20:19 +01:00
|
|
|
_parent.Navigate(typeof(UserFirmwareAvatarSelectorView), (_parent, _profile));
|
2022-07-08 20:47:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-24 19:38:38 +02:00
|
|
|
private static byte[] ProcessProfileImage(byte[] buffer)
|
2022-07-08 20:47:11 +02:00
|
|
|
{
|
|
|
|
using (Image image = Image.Load(buffer))
|
|
|
|
{
|
|
|
|
image.Mutate(x => x.Resize(256, 256));
|
|
|
|
|
|
|
|
using (MemoryStream streamJpg = new())
|
|
|
|
{
|
|
|
|
image.SaveAsJpeg(streamJpg);
|
|
|
|
|
2022-07-24 19:38:38 +02:00
|
|
|
return streamJpg.ToArray();
|
2022-07-08 20:47:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|