Skip to content

Commit

Permalink
Merge pull request #29 from TheXDS/feature/file-templates
Browse files Browse the repository at this point in the history
Template logic for creating new files inside VIV
  • Loading branch information
TheXDS authored Apr 2, 2024
2 parents fd97096 + 94c9231 commit 0bcab9f
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 25 deletions.
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>

0 comments on commit 0bcab9f

Please sign in to comment.