-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Item editor to Avalonia branch (#448)
- Loading branch information
Showing
7 changed files
with
148 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using System; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using System.Windows.Input; | ||
using Avalonia.Platform.Storage; | ||
using HaruhiChokuretsuLib.Archive.Graphics; | ||
using HaruhiChokuretsuLib.Util; | ||
using ReactiveUI; | ||
using SerialLoops.Assets; | ||
using SerialLoops.Controls; | ||
using SerialLoops.Lib.Items; | ||
using SerialLoops.Utility; | ||
using SerialLoops.ViewModels.Dialogs; | ||
using SerialLoops.Views.Dialogs; | ||
using SkiaSharp; | ||
|
||
namespace SerialLoops.ViewModels.Editors; | ||
|
||
public class ItemEditorViewModel : EditorViewModel | ||
{ | ||
private ItemItem _item; | ||
public SKBitmap ItemBitmap => _item.GetImage(); | ||
|
||
public ICommand ExportCommand { get; } | ||
public ICommand ImportCommand { get; } | ||
|
||
public ItemEditorViewModel(ItemItem item, MainWindowViewModel window, ILogger log) : base(item, window, log) | ||
{ | ||
_item = item; | ||
ExportCommand = ReactiveCommand.CreateFromTask(Export); | ||
ImportCommand = ReactiveCommand.CreateFromTask(Import); | ||
} | ||
|
||
private async Task Export() | ||
{ | ||
string exportPath = (await Window.Window.ShowSaveFilePickerAsync(Strings.Export_Item_Image, | ||
[new(Strings.PNG_Image) { Patterns = ["*.png"] }]))?.TryGetLocalPath(); | ||
if (!string.IsNullOrEmpty(exportPath)) | ||
{ | ||
await using FileStream fs = File.Create(exportPath); | ||
ItemBitmap.Encode(fs, SKEncodedImageFormat.Png, GraphicsFile.PNG_QUALITY); | ||
} | ||
} | ||
|
||
private async Task Import() | ||
{ | ||
string importPath = (await Window.Window.ShowOpenFilePickerAsync(Strings.Import_Item_Image, | ||
[new(Strings.Supported_Images) { Patterns = Shared.SupportedImageFiletypes }]))?.TryGetLocalPath(); | ||
if (!string.IsNullOrEmpty(importPath)) | ||
{ | ||
SKBitmap original = _item.GetImage(); | ||
ImageCropResizeDialogViewModel cropResizeDialogViewModel = new(importPath, original.Width, original.Height, _log); | ||
SKBitmap finalImage = await new ImageCropResizeDialog | ||
{ | ||
DataContext = cropResizeDialogViewModel, | ||
}.ShowDialog<SKBitmap>(Window.Window); | ||
if (finalImage is not null) | ||
{ | ||
try | ||
{ | ||
LoopyProgressTracker tracker = new(); | ||
await new ProgressDialog(() => _item.SetImage(finalImage, tracker, _log), | ||
() => { }, tracker, string.Format(Strings.Replacing__0____, _item.DisplayName)).ShowDialog(Window.Window); | ||
this.RaisePropertyChanged(nameof(ItemBitmap)); | ||
Description.UnsavedChanges = true; | ||
} | ||
catch (Exception ex) | ||
{ | ||
_log.LogException(string.Format(Strings.Failed_to_replace_background__0__with_file__1_, _item.DisplayName, importPath), ex); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<UserControl xmlns="https://github.com/avaloniaui" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:vm="using:SerialLoops.ViewModels.Editors" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
xmlns:assets="using:SerialLoops.Assets" | ||
xmlns:utility="using:SerialLoops.Utility" | ||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" | ||
x:DataType="vm:ItemEditorViewModel" | ||
x:Class="SerialLoops.Views.Editors.ItemEditorView"> | ||
<Grid RowDefinitions="Auto,Auto" ColumnDefinitions="Auto"> | ||
<Image Grid.Row="0" Source="{Binding ItemBitmap, Converter={x:Static utility:SLConverters.SKBitmapToAvaloniaConverter}}"/> | ||
|
||
<StackPanel Grid.Row="1" Orientation="Horizontal" Spacing="3"> | ||
<Button Content="{x:Static assets:Strings.Export}" Command="{Binding ExportCommand}"/> | ||
<Button Content="{x:Static assets:Strings.Import}" Command="{Binding ImportCommand}"/> | ||
</StackPanel> | ||
</Grid> | ||
</UserControl> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Avalonia.Controls; | ||
|
||
namespace SerialLoops.Views.Editors; | ||
|
||
public partial class ItemEditorView : UserControl | ||
{ | ||
public ItemEditorView() | ||
{ | ||
InitializeComponent(); | ||
} | ||
} | ||
|