Skip to content

Commit

Permalink
Fix filename selector, zip path separators, wait cursors
Browse files Browse the repository at this point in the history
  • Loading branch information
katycat5e committed Sep 9, 2023
1 parent 1bbc01a commit 1fca26b
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 28 deletions.
5 changes: 3 additions & 2 deletions SkinConfigurator/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<ItemContainerTemplate DataType="model:PackItemModel">
<ItemContainerTemplate DataType="model:SkinFileModel">
<Grid ContextMenu="{StaticResource SkinFileContextMenu}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
Expand All @@ -192,7 +192,8 @@

<Image Width="64px" Height="64px" Source="{Binding Preview}"/>
<UniformGrid Rows="2" Grid.Column="1">
<ComboBox IsEditable="True" Text="{Binding FileName}" ItemsSource="{Binding Path=DataContext.DefaultTextureNames, RelativeSource={RelativeSource AncestorType=ListView}, Mode=OneWay}"/>
<local:SpecialCombo IsEditable="True" Text="{Binding FileName, UpdateSourceTrigger=LostFocus}"
ItemsSource="{Binding Path=DataContext.DefaultTextureNames, RelativeSource={RelativeSource AncestorType=ListView}, Mode=OneWay}"/>
<Button Grid.Row="1" Content="Upgrade Filename" Click="UpgradeFileNameButton_Click"
Visibility="{Binding CanUpgradeFileName, Mode=OneWay, Converter={StaticResource boolToCollapseConverter}}"
Background="PaleGoldenrod"/>
Expand Down
63 changes: 40 additions & 23 deletions SkinConfigurator/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
Expand Down Expand Up @@ -70,11 +71,6 @@ private void CreatePackButton_Click(object sender, RoutedEventArgs e)
Model.Reset();
}

private void OpenPackButton_Click(object sender, RoutedEventArgs e)
{

}

private void ImportPackButton_Click(object sender, RoutedEventArgs e)
{
using var folderDialog = new System.Windows.Forms.FolderBrowserDialog()
Expand All @@ -94,7 +90,10 @@ private void ImportPackButton_Click(object sender, RoutedEventArgs e)
return;
}

Model.SkinPack = PackImporter.ImportFromFolder(folderDialog.SelectedPath);
using (new WaitCursor())
{
Model.SkinPack = PackImporter.ImportFromFolder(folderDialog.SelectedPath);
}
}
}

Expand All @@ -118,7 +117,10 @@ private void ImportZipButton_Click(object sender, RoutedEventArgs e)
return;
}

Model.SkinPack = PackImporter.ImportFromArchive(dialog.FileName);
using (new WaitCursor())
{
Model.SkinPack = PackImporter.ImportFromArchive(dialog.FileName);
}
}
}

Expand Down Expand Up @@ -180,14 +182,17 @@ private void AddManySkinButton_Click(object sender, RoutedEventArgs e)

string? carId = PromptForCarType();

foreach (string subDir in Directory.GetDirectories(folderDialog.SelectedPath))
using (new WaitCursor())
{
bool hasTextures = Directory.EnumerateFiles(subDir)
.Any(f => Constants.IsSupportedExtension(Path.GetExtension(f)));

if (hasTextures)
foreach (string subDir in Directory.GetDirectories(folderDialog.SelectedPath))
{
Model.AddComponent(carId, subDir);
bool hasTextures = Directory.EnumerateFiles(subDir)
.Any(f => Constants.IsSupportedExtension(Path.GetExtension(f)));

if (hasTextures)
{
Model.AddComponent(carId, subDir);
}
}
}
}
Expand Down Expand Up @@ -386,21 +391,33 @@ private void TestPackButton_Click(object sender, RoutedEventArgs e)

private void RunPackaging<T>(string destination, string viewPath) where T : SkinPackager
{
try
using (new WaitCursor())
{
SkinPackager.Package<T>(destination, Model.SkinPack);
var openDest = MessageBox.Show("Skins packaged successfully. Did you want to open the destination folder?",
"Success!", MessageBoxButton.YesNo, MessageBoxImage.Information);
try
{
SkinPackager.Package<T>(destination, Model.SkinPack);

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
var openDest = MessageBox.Show("Skins packaged successfully. Did you want to open the destination folder?",
"Success!", MessageBoxButton.YesNo, MessageBoxImage.Information);

if (openDest == MessageBoxResult.Yes)
if (openDest == MessageBoxResult.Yes)
{
Process.Start("explorer.exe", viewPath);
}
}
else
{
MessageBox.Show("Skins packaged successfully.",
"Success!", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
catch (SkinPackageException ex)
{
Process.Start("explorer.exe", viewPath);
MessageBox.Show(ex.Message, "Error Packaging Skins", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
catch (SkinPackageException ex)
{
MessageBox.Show(ex.Message, "Error Packaging Skins", MessageBoxButton.OK, MessageBoxImage.Error);
}
}

#endregion
Expand Down
43 changes: 43 additions & 0 deletions SkinConfigurator/SpecialCombo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections.Specialized;
using System.Reflection;
using System.Windows.Controls;
using System.Windows.Data;

namespace SkinConfigurator
{
public class SpecialCombo : ComboBox
{
private bool _ignoreSelectionChanged = false;

private static readonly MethodInfo _selectedItemUpdated =
typeof(ComboBox).GetMethod("SelectedItemUpdated", BindingFlags.NonPublic | BindingFlags.Instance)!;

protected override void OnSelectionChanged(SelectionChangedEventArgs e)
{
if (!_ignoreSelectionChanged)
{
base.OnSelectionChanged(e);

var textBinding = BindingOperations.GetBindingExpression(this, TextProperty);
if (textBinding == null) return;

_selectedItemUpdated.Invoke(this, Array.Empty<object>());
textBinding.UpdateSource();
}
}

protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
{
_ignoreSelectionChanged = true;
try
{
base.OnItemsChanged(e);
}
finally
{
_ignoreSelectionChanged = false;
}
}
}
}
25 changes: 25 additions & 0 deletions SkinConfigurator/WaitCursor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace SkinConfigurator
{
public class WaitCursor : IDisposable
{
private Cursor _previousCursor;

public WaitCursor()
{
_previousCursor = Mouse.OverrideCursor;
Mouse.OverrideCursor = Cursors.Wait;
}

public void Dispose()
{
Mouse.OverrideCursor = _previousCursor;
}
}
}
6 changes: 3 additions & 3 deletions SkinConfigurator/ZipPackager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected override void WriteSkin(PackComponentModel skin)
// skin.json
string folderName = GetSkinFolderName(skin.Name!, skin.CarId!);
string jsonFileName = skin.Type == PackComponentType.Skin ? Constants.SKIN_CONFIG_FILE : Constants.SKIN_RESOURCE_FILE;
var jsonEntry = _archive.CreateEntry(Path.Combine(folderName, jsonFileName));
var jsonEntry = _archive.CreateEntry($"{folderName}/{jsonFileName}");
using var jsonStream = jsonEntry.Open();

var json = skin.JsonModel();
Expand All @@ -54,8 +54,8 @@ protected override void WriteSkin(PackComponentModel skin)
// textures & whatever else
foreach (var sourceFile in skin.Items)
{
string relativePath = Path.Combine(folderName, sourceFile.FileName);
_archive.CreateEntryFromFile(sourceFile.TempPath, relativePath);
string entryPath = $"{folderName}/{sourceFile.FileName}";
_archive.CreateEntryFromFile(sourceFile.TempPath, entryPath);
}
}
}
Expand Down

0 comments on commit 1fca26b

Please sign in to comment.