Skip to content

Commit

Permalink
More FontEditor stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
thesupersonic16 committed Apr 24, 2024
1 parent 8db0482 commit 1fe568d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 17 deletions.
7 changes: 4 additions & 3 deletions DALTools/FontEditor/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@
</GroupBox>

<DockPanel Grid.ColumnSpan="2" Grid.Column="0" Grid.Row="0">
<Button Content="Load Font File" HorizontalAlignment="Left" Height="25" Margin="10,10,0,0" VerticalAlignment="Top" Width="128" Click="Button_Click" />
<Button Content="Load Font Texture" HorizontalAlignment="Left" Height="25" Margin="10,10,0,0" VerticalAlignment="Top" Width="128" Click="UI_LoadTextureButton_Click" />
<Button Content="Import Font Texture" HorizontalAlignment="Left" Height="25" Margin="10,10,0,0" VerticalAlignment="Top" Width="128" Click="UI_ImportTextureButton_Click" />
<Button Content="Load File" HorizontalAlignment="Left" Height="25" Margin="10,10,0,0" VerticalAlignment="Top" Width="128" Click="Button_Click" />
<Button Content="Load Texture" HorizontalAlignment="Left" Height="25" Margin="10,10,0,0" VerticalAlignment="Top" Width="128" Click="UI_LoadTextureButton_Click" />
<Button Content="Import Texture" HorizontalAlignment="Left" Height="25" Margin="10,10,0,0" VerticalAlignment="Top" Width="128" Click="UI_ImportTextureButton_Click" />
<Button x:Name="UI_ExportButton" Content="Export Texture" HorizontalAlignment="Left" Height="25" Margin="10,10,0,0" VerticalAlignment="Top" Width="128" IsEnabled="False" Click="UI_ExportTextureButton_Click" />
<Button x:Name="UI_SaveButton" Content="Save Font" HorizontalAlignment="Left" Height="25" Margin="10,10,0,0" VerticalAlignment="Top" Width="128" IsEnabled="False" Click="UI_SaveButton_Click" />
<CheckBox Content="Monospace Only" Width="114" VerticalContentAlignment="Center" Margin="0,10,0,0" IsChecked="{Binding MonospaceOnly}" Checked="CheckBox_Checked" Unchecked="CheckBox_Checked"/>
</DockPanel>
Expand Down
60 changes: 46 additions & 14 deletions DALTools/FontEditor/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public partial class MainWindow : Window
public TEXFile FontImageTexFile = new TEXFile();
public PCKFile PCKFontArchive = new PCKFile();
public FontFile FontCodeFile = new FontFile();
public FNTFile FontTableFile = null;
public List<Border> Borders = new List<Border>();

public string FilePath = null;
Expand Down Expand Up @@ -131,7 +132,11 @@ public void UpdatePreview()

public void SaveFont()
{
if (!string.IsNullOrEmpty(FilePath) && FilePath.EndsWith(".pck"))
if (!string.IsNullOrEmpty(FilePath) && FontTableFile != null)
{
FontTableFile.Save(FilePath);
}
else if (!string.IsNullOrEmpty(FilePath) && FilePath.EndsWith(".pck"))
{
// PCK Save
using (var stream = new MemoryStream())
Expand Down Expand Up @@ -172,6 +177,7 @@ public void LoadFontPCK(string path)
FontImageTexFile.Load(PCKFontArchive.GetFileStream(textureFilePath));
// Set Texture
UI_FontImage.Source = ImageTools.ConvertToSource(FontImageTexFile.CreateBitmap());
UI_ExportButton.IsEnabled = true;
}
else
MessageBox.Show("Failed to load Texture!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
Expand All @@ -189,11 +195,11 @@ public void LoadFontFNT(string path)
{
FilePath = path;

FNTFile file = new FNTFile();
file.Load(path);
FontTableFile = new FNTFile();
FontTableFile.Load(path);

FontCodeFile = file.FontCode;
FontImageTexFile = file.FontTexture;
FontCodeFile = FontTableFile.FontCode;
FontImageTexFile = FontTableFile.FontTexture;

UI_FontImage.Source = ImageTools.ConvertToSource(FontImageTexFile.CreateBitmap());

Expand All @@ -204,13 +210,16 @@ public void LoadFontFNT(string path)
ReloadUI();

UI_SaveButton.IsEnabled = true;
UI_ExportButton.IsEnabled = true;
}

public void LoadFile(string path)
{
if (path == null)
return;


FontTableFile = null;

// Check if its a PCK
if (path.ToLowerInvariant().EndsWith(".pck"))
{
Expand Down Expand Up @@ -447,6 +456,7 @@ private void UI_LoadTextureButton_Click(object sender, RoutedEventArgs e)
// Load Font TEX
FontImageTexFile.Load(ofd.FileName);
UI_FontImage.Source = ImageTools.ConvertToSource(FontImageTexFile.CreateBitmap());
UI_ExportButton.IsEnabled = true;
}
}

Expand All @@ -456,7 +466,7 @@ private void UI_ImportTextureButton_Click(object sender, RoutedEventArgs e)
ofd.Filter = "All Supported Files|*.tex;*.png|Texture|*.tex|Supported Image Files|*.png";
if (ofd.ShowDialog() == true)
{
if (ofd.FileName.ToLower(CultureInfo.GetCultureInfo("en-US")).EndsWith(".tex"))
if (ofd.FileName.ToLowerInvariant().EndsWith(".tex"))
{
var textureFilePath = PCKFontArchive.SearchForFile(".tex");
var newTexture = new TEXFile();
Expand All @@ -479,8 +489,11 @@ private void UI_ImportTextureButton_Click(object sender, RoutedEventArgs e)
return;
}

// Import Texture into PCK
PCKFontArchive.ReplaceFile(textureFilePath, File.ReadAllBytes(ofd.FileName));
// Copy texture to archive
if (FontTableFile != null)
FontTableFile.FontTexture = newTexture;
else
PCKFontArchive.ReplaceFile(textureFilePath, File.ReadAllBytes(ofd.FileName));

// Set Texture
UI_FontImage.Source = ImageTools.ConvertToSource((FontImageTexFile = newTexture).CreateBitmap());
Expand Down Expand Up @@ -508,19 +521,38 @@ private void UI_ImportTextureButton_Click(object sender, RoutedEventArgs e)
return;
}

// Save Texture to PCK
using (var stream = new MemoryStream())

if (FontTableFile != null)
FontTableFile.FontTexture = newTexture;
else
{
newTexture.Save(stream);
PCKFontArchive.ReplaceFile(textureFilePath, stream.ToArray());
// Save Texture to PCK
using (var stream = new MemoryStream())
{
newTexture.Save(stream);
PCKFontArchive.ReplaceFile(textureFilePath, stream.ToArray());
}
}

// Set Texture
UI_FontImage.Source = ImageTools.ConvertToSource((FontImageTexFile = newTexture).CreateBitmap());
}
}
}

private void UI_ExportTextureButton_Click(object sender, RoutedEventArgs e)
{
var sfd = new SaveFileDialog();
sfd.Filter = "Portable Network Graphics (*.png)|*.png|Texture|*.tex";
sfd.FileName = Path.ChangeExtension(Path.GetFileName(FilePath), "png");
if (sfd.ShowDialog() == true)
{
if (sfd.FilterIndex == 1)
FontImageTexFile.SaveSheetImage(sfd.FileName);
else
FontImageTexFile.Save(sfd.FileName);
}
}

private void UI_BorderDrag_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (UI_CharactersListBox.SelectedIndex == -1)
Expand Down

0 comments on commit 1fe568d

Please sign in to comment.