Skip to content

Commit

Permalink
Add load of a font archive file
Browse files Browse the repository at this point in the history
  • Loading branch information
Xeeynamo committed Feb 28, 2020
1 parent 39ee914 commit 7fcb1e0
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 26 deletions.
10 changes: 10 additions & 0 deletions OpenKh.Tools.CtdEditor/ViewModels/CtdViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class CtdViewModel : GenericListModel<MessageViewModel>
{
private string _searchTerm;
private readonly Ctd _ctd;
private FontsArc _fonts;

public Ctd Ctd
{
Expand All @@ -21,6 +22,15 @@ public Ctd Ctd
}
}

public FontsArc Fonts
{
get => _fonts;
set
{
_fonts = value;
}
}

public string SearchTerm
{
get => _searchTerm;
Expand Down
62 changes: 36 additions & 26 deletions OpenKh.Tools.CtdEditor/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using OpenKh.Bbs;
using OpenKh.Common;
using OpenKh.Tools.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
Expand All @@ -14,6 +16,13 @@ namespace OpenKh.Tools.CtdEditor.ViewModels
public class MainViewModel : BaseNotifyPropertyChanged
{
private static string ApplicationName = Utilities.GetApplicationName();
private static readonly IEnumerable<FileDialogFilter> CtdFilter = FileDialogFilterComposer.Compose()
.AddExtensions("CTD message", "ctd")
.AddAllFiles();
private static readonly IEnumerable<FileDialogFilter> FontArcFilter = FileDialogFilterComposer.Compose()
.AddExtensions("Font archive", "arc")
.AddAllFiles();

private Window Window => Application.Current.Windows.OfType<Window>().FirstOrDefault(x => x.IsActive);
private string _fileName;
private CtdViewModel _ctdViewModel;
Expand All @@ -36,6 +45,8 @@ private string FileName
public RelayCommand ExitCommand { get; }
public RelayCommand AboutCommand { get; }

public RelayCommand OpenFontCommand { get; }

public CtdViewModel CtdViewModel
{
get => _ctdViewModel;
Expand All @@ -48,21 +59,16 @@ public Ctd Ctd
set => CtdViewModel = new CtdViewModel(value);
}

public FontsArc Fonts
{
get => CtdViewModel.Fonts;
set => CtdViewModel.Fonts = value;
}

public MainViewModel()
{
OpenCommand = new RelayCommand(x =>
{
var fd = FileDialog.Factory(Window, FileDialog.Behavior.Open, new[]
{
("CTD text file", "ctd"),
("All files", "*")
});
if (fd.ShowDialog() == true)
{
OpenFile(fd.FileName);
}
}, x => true);
FileDialog.OnOpen(fileName => OpenFile(fileName), CtdFilter), x => true);

SaveCommand = new RelayCommand(x =>
{
Expand All @@ -77,24 +83,17 @@ public MainViewModel()
}, x => true);

SaveAsCommand = new RelayCommand(x =>
{
var fd = FileDialog.Factory(Window, FileDialog.Behavior.Save, new[]
{
("CTD text file", "ctd"),
("All files", "*")
});
if (fd.ShowDialog() == true)
{
SaveFile(FileName, fd.FileName);
FileName = fd.FileName;
}
}, x => true);
FileDialog.OnSave(fileName => SaveFile(FileName, fileName), CtdFilter), x => true);

ExitCommand = new RelayCommand(x =>
{
Window.Close();
}, x => true);

OpenFontCommand = new RelayCommand(x =>
FileDialog.OnOpen(fileName => OpenFontFile(fileName), FontArcFilter),
x => CtdViewModel != null);

AboutCommand = new RelayCommand(x =>
{
new AboutDialog(Assembly.GetExecutingAssembly()).ShowDialog();
Expand All @@ -103,7 +102,7 @@ public MainViewModel()
CtdViewModel = new CtdViewModel();
}

public bool OpenFile(string fileName) => File.OpenRead(fileName).Using(stream =>
private bool OpenFile(string fileName) => File.OpenRead(fileName).Using(stream =>
{
if (!Ctd.IsValid(stream))
{
Expand All @@ -116,12 +115,23 @@ public bool OpenFile(string fileName) => File.OpenRead(fileName).Using(stream =>
return true;
});

public void SaveFile(string previousFileName, string fileName)
private void SaveFile(string previousFileName, string fileName)
{
File.Create(fileName).Using(stream =>
{
Ctd.Write(stream);
});
}

private void OpenFontFile(string fileName)
{
Fonts = File.OpenRead(fileName).Using(stream =>
{
if (!Arc.IsValid(stream))
throw new Exception("Not a valid ARC file");
return FontsArc.Read(stream);
});
}
}
}
4 changes: 4 additions & 0 deletions OpenKh.Tools.CtdEditor/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
<MenuItem Header="E_xit"
Command="{Binding ExitCommand}"/>
</MenuItem>
<MenuItem Header="F_ont">
<MenuItem Header="_Open font archive"
Command="{Binding OpenFontCommand}"/>
</MenuItem>
<MenuItem Header="_Help">
<MenuItem Header="_About"
Command="{Binding AboutCommand}"/>
Expand Down

0 comments on commit 7fcb1e0

Please sign in to comment.