Skip to content

Commit

Permalink
Settings saving, colour wheel
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinslane committed Aug 6, 2024
1 parent 3d33f47 commit d0b0af9
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 7 deletions.
14 changes: 13 additions & 1 deletion TextureMagic/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TextureMagic"
xmlns:colorPicker="clr-namespace:ColorPicker;assembly=ColorPicker"
mc:Ignorable="d"
Title="MainWindow" SizeToContent="WidthAndHeight" MinWidth="400" MinHeight="400" >
<Grid HorizontalAlignment="Stretch">
Expand All @@ -15,7 +16,18 @@
<TextBlock Name="SelectedPath" Margin="0,0,0,8" >C:\Program Files</TextBlock>
<TextBlock x:Name="FilesFound" Margin="0,0,0,8" >0 Files Found</TextBlock>
<CheckBox x:Name="ExportDdsFromYtd" IsChecked="False" Visibility="Collapsed" HorizontalAlignment="Left" Content="Export DDS" Margin="0,0,0,8" Checked="ExportDds_Checked" Unchecked="ExportDds_Unchecked" />
<CheckBox x:Name="FillBackgroundCheckBox" IsChecked="True" HorizontalAlignment="Left" Content="Fill background with color" Margin="0,0,0,8" Checked="FillBackgroundCheckBox_Checked" Unchecked="FillBackgroundCheckBox_Unchecked" />

<StackPanel Orientation="Horizontal" >
<CheckBox x:Name="FillBackgroundCheckBox" IsChecked="True" HorizontalAlignment="Left"
Content="Fill background with color" Margin="0,0,8,8"
Checked="FillBackgroundCheckBox_Checked" Unchecked="FillBackgroundCheckBox_Unchecked" />

<colorPicker:PortableColorPicker x:Name="backgroundColorPicker"
Width="24" Margin="0,-2,0,2"
ColorChanged="PickerControlBase_OnColorChanged"/>
</StackPanel>


<TextBlock Name="TextureBorderText" Margin="0,0,0,0" >Texture Border:</TextBlock>
<ComboBox x:Name="TextureBorderComboBox" SelectedIndex="0" HorizontalAlignment="Stretch" Margin="0,0,0,8" SelectionChanged="TextureBorderComboBox_SelectionChanged" Background="#FF202020">
<ComboBoxItem Content="0 Pixels" x:Name="x0"></ComboBoxItem>
Expand Down
38 changes: 32 additions & 6 deletions TextureMagic/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Windows.Navigation;
using CodeWalker.GameFiles;
using CodeWalker.Utils;
using ColorPicker.Models;
using ImageMagick;
using Microsoft.Win32;
using Path = System.IO.Path;
Expand All @@ -35,7 +36,6 @@ public partial class MainWindow : Window
private int _currentProgress = 0;
private string _lastPath;
private bool _geometryInitialized = false;
private static readonly MagickColor _backgroundColor = new MagickColor(0x1f, 0x20, 0x20, 0xff);
private bool _squareTexture = true;
private MagickGeometry[] _geometry;
private List<IConnectedComponent<byte>> _connectedComponents = new ();
Expand All @@ -47,6 +47,8 @@ public partial class MainWindow : Window
private int _textureBorder = 0;
private bool ExportYtdToDds = false;

internal static SettingsStorage Settings;

struct FileEntry
{
public int Index { get; set; }
Expand All @@ -62,12 +64,22 @@ enum FileType
public MainWindow()
{
this.InitializeComponent();
this.Title = "Texture Magic by Dustin Slane ( v 0.7.0 )";
this.Title = "Texture Magic by Dustin Slane ( v 0.8.0 )";
Progress.Value = 0;
_cancellationTokenSource = new CancellationTokenSource();
_worker.DoWork += WorkerOnDoWork;
_worker.RunWorkerCompleted += RunWorkerCompleted;
_worker.WorkerSupportsCancellation = true;

Task.Run(async () =>
{
Settings = await SettingsStorage.Load();

backgroundColorPicker.Dispatcher.Invoke(() =>
{
backgroundColorPicker.SelectedColor = Settings.BackgroundColor;
});
});
}

private void RunWorkerCompleted(object? sender, RunWorkerCompletedEventArgs e)
Expand Down Expand Up @@ -369,7 +381,7 @@ private IMagickImage ProcessImage(MagickImage img, string name)
MagickColor color;
if (_fillBackGround)
{
color = new MagickColor(0x20, 0x20, 0x20, 0xff);
color = Settings.BackgroundColorMagick;
}
else
{
Expand Down Expand Up @@ -416,7 +428,7 @@ private MagickImage NormalOptimization(MagickImage img, string name)
// Trim the image to it's extents
img.Trim();
// Add a little border around the image of 16 px
img.BorderColor = _backgroundColor;
img.BorderColor = Settings.BackgroundColorMagick;
img.Border(_textureBorder, _textureBorder);
// Scale the image down
img.Scale(res);
Expand All @@ -439,7 +451,7 @@ private MagickImage NormalOptimization(MagickImage img, string name)

if (_fillBackGround)
{
img.BackgroundColor = _backgroundColor;
img.BackgroundColor = Settings.BackgroundColorMagick;
}
return img;
}
Expand Down Expand Up @@ -521,7 +533,7 @@ private MagickImage CutImageIntoPieces(MagickImage img, string imageName)
var montageSettings = new MontageSettings
{
Geometry = new MagickGeometry(5, 5, 0, 0),
BackgroundColor = _fillBackGround ? new MagickColor(0x20, 0x20, 0x20, 0xff) : MagickColors.Transparent
BackgroundColor = _fillBackGround ? Settings.BackgroundColorMagick : MagickColors.Transparent
};

using var montage = masked.Montage(montageSettings);
Expand Down Expand Up @@ -657,5 +669,19 @@ private void ExportDds_Unchecked(object sender, RoutedEventArgs e)
{
ExportYtdToDds = false;
}

private void PickerControlBase_OnColorChanged(object sender, RoutedEventArgs routedEventArgs)
{
var currentColor = backgroundColorPicker.SelectedColor;

Settings.BackgroundColor = currentColor;
}

protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);

Task.Run(() => SettingsStorage.Save(Settings));
}
}
}
40 changes: 40 additions & 0 deletions TextureMagic/SettingsStorage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using System.Windows.Media;
using ImageMagick;

namespace TextureMagic;

public class SettingsStorage
{

// Generate magick colour
[JsonIgnore]
public MagickColor BackgroundColorMagick => new(BackgroundColor.R, BackgroundColor.G, BackgroundColor.B);


public Color BackgroundColor { get; set; }

public static async Task Save(SettingsStorage settingsStorage)
{
await File.WriteAllTextAsync("settings.json", JsonSerializer.Serialize(settingsStorage));
}

public static async Task<SettingsStorage> Load()
{
if (!File.Exists("settings.json"))
{
var settings = new SettingsStorage();

await Save(settings);

return settings;
}

var data = await File.ReadAllTextAsync("settings.json");

return JsonSerializer.Deserialize<SettingsStorage>(data) ?? new SettingsStorage();
}
}
1 change: 1 addition & 0 deletions TextureMagic/TextureMagic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

<ItemGroup>
<PackageReference Include="Magick.NET-Q8-AnyCPU" Version="13.4.0" />
<PackageReference Include="PixiEditor.ColorPicker" Version="3.4.1" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit d0b0af9

Please sign in to comment.