Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template logic for creating new files inside VIV #29

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/App/Vivianne.Wpf/Views/VivMainView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
</RadioButton>
<mcart:StretchyWrapPanel Orientation="Horizontal" DockPanel.Dock="Bottom">
<Button Command="{Binding ImportFileCommand}">➕</Button>
<Button Command="{Binding NewFromTemplateCommand}">📄</Button>
</mcart:StretchyWrapPanel>
<ScrollViewer VerticalScrollBarVisibility="Auto">
<ItemsControl Margin="0,0,0,0" MinWidth="150" ItemsSource="{Binding State.Directory}">
Expand Down
32 changes: 9 additions & 23 deletions src/App/Vivianne.Wpf/Views/VivMainView.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,14 @@
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;
using System.Windows.Controls;

namespace TheXDS.Vivianne.Views
namespace TheXDS.Vivianne.Views;

/// <summary>
/// Business logic for VivMainView.xaml
/// </summary>
public partial class VivMainView : UserControl
{
/// <summary>
/// Lógica de interacción para VivMainView.xaml
/// </summary>
public partial class VivMainView : UserControl
public VivMainView()
{
public VivMainView()
{
InitializeComponent();
}
InitializeComponent();
}
}
42 changes: 41 additions & 1 deletion src/App/Vivianne/ViewModels/VivMainViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
using System;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Input;
using TheXDS.Ganymede.Helpers;
using TheXDS.Ganymede.Models;
using TheXDS.Ganymede.Types.Base;
using TheXDS.Ganymede.ViewModels;
using TheXDS.Vivianne.Extensions;
using TheXDS.Vivianne.Models;
using TheXDS.Vivianne.Resources;
using TheXDS.Vivianne.Serializers;
Expand Down Expand Up @@ -54,6 +58,25 @@ public class VivMainViewModel : HostViewModelBase, IStatefulViewModel<VivMainSta
{ ".swe", CreateFeDataPreviewViewModel },
};

private static readonly Dictionary<string, Func<byte[]>> VivTemplates = new()
{
{ "DASH.qfs", TemplateDashQfs}
};

private static byte[] TemplateDashQfs()
{
var cabinBlob = new FshBlob() { Magic = FshBlobFormat.Argb32, GaugeData = new() };
cabinBlob.ReplaceWith(new Image<Rgba32>(640, 480));
cabinBlob.Footer = Mappings.FshFooterWriter[FshBlobFooterType.CarDashboard].Invoke(cabinBlob);
var steerBlob = new FshBlob() { Magic = FshBlobFormat.Argb32, XRotation=128, YRotation=128, XPosition=192, YPosition=352 };
steerBlob.ReplaceWith(new Image<Rgba32>(256, 256));

var fsh = new FshFile();
fsh.Entries.Add("0000", cabinBlob);
fsh.Entries.Add("0001", steerBlob);
return QfsCodec.Compress(((ISerializer<FshFile>)new FshSerializer()).Serialize(fsh));
}

private static IViewModel CreateFeDataPreviewViewModel(byte[] data, Action<byte[]> saveCallback)
{
return new FeDataPreviewViewModel(data, saveCallback);
Expand Down Expand Up @@ -97,6 +120,7 @@ public VivMainViewModel()
ReplaceFileCommand = cb.BuildSimple(OnReplaceFile);
ExportFileCommand = cb.BuildSimple(OnExportFile);
RemoveFileCommand = cb.BuildSimple(OnRemoveFile);
NewFromTemplateCommand = cb.BuildSimple(OnNewFromTemplate);
}

/// <summary>
Expand All @@ -111,6 +135,12 @@ public VivMainViewModel()
/// </summary>
public ICommand ImportFileCommand { get; }

/// <summary>
/// Gets a reference to the command used to create a new file inside the
/// VIV directory based on a template.
/// </summary>
public ICommand NewFromTemplateCommand { get; }

/// <summary>
/// Gets a reference to the command used to export a selected file from the
/// VIV directory.
Expand Down Expand Up @@ -226,6 +256,16 @@ private async Task OnRemoveFile(object? parameter)
}
}

private async Task OnNewFromTemplate()
{
var r = await DialogService!.SelectOption("New from template", "Select a template to create a new file in the VIV directory.", VivTemplates.Keys.ToArray());
if (r.Success)
{
var template = VivTemplates.ToList()[r.Result];
State.Directory[template.Key] = template.Value.Invoke();
}
}

async Task IViewModel.OnNavigateBack(CancelFlag navigation)
{
if (State.UnsavedChanges)
Expand Down
2 changes: 1 addition & 1 deletion src/Lib/VivLib/QfsCodec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ namespace TheXDS.Vivianne;
/// <item>César Morgan (xds_xps_ivx@hotmail.com)</item>
/// </list>
/// </remarks>
public class QfsCodec
public static class QfsCodec
{
private const ushort QFS_Signature = 0xFB10;

Expand Down
10 changes: 10 additions & 0 deletions src/nuget.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<configuration>
<config>
<add key="dependencyVersion" value="Highest" />
<add key="globalPackagesFolder" value="E:\Nuget\repo" />
</config>
<packageSources>
<clear />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</packageSources>
</configuration>