Skip to content

Commit

Permalink
Add open and save editor functionalities for 00battle.bin
Browse files Browse the repository at this point in the history
  • Loading branch information
Xeeynamo authored and Rikux3 committed May 31, 2020
1 parent 87e5f95 commit f2c03b1
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<ExtrasEnableWpfProjectSetup>true</ExtrasEnableWpfProjectSetup>
<AssemblyTitle>KH2 Battle Editor</AssemblyTitle>
<Product>KH2 Battle Editor - OpenKh</Product>
<LangVersion>7</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
112 changes: 111 additions & 1 deletion OpenKh.Tools.Kh2BattleEditor/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows;
using OpenKh.Common;
using OpenKh.Kh2;
using OpenKh.Tools.Common;
using Xe.Tools;
using Xe.Tools.Wpf.Commands;
using Xe.Tools.Wpf.Dialogs;

namespace OpenKh.Tools.Kh2BattleEditor.ViewModels
{
Expand All @@ -10,6 +17,7 @@ public class MainViewModel : BaseNotifyPropertyChanged
private static string ApplicationName = Utilities.GetApplicationName();
private Window Window => Application.Current.Windows.OfType<Window>().FirstOrDefault(x => x.IsActive);
private string _fileName;
private IEnumerable<Bar.Entry> _battleItems;

public string Title => $"{FileName ?? "untitled"} | {ApplicationName}";

Expand All @@ -22,5 +30,107 @@ private string FileName
OnPropertyChanged(nameof(Title));
}
}

public RelayCommand OpenCommand { get; }
public RelayCommand SaveCommand { get; }
public RelayCommand SaveAsCommand { get; }
public RelayCommand ExitCommand { get; }
public RelayCommand AboutCommand { get; }

public MainViewModel()
{
OpenCommand = new RelayCommand(x =>
{
var fd = FileDialog.Factory(Window, FileDialog.Behavior.Open, new[]
{
("00battle.bin", "bin"),
("BAR file", "bar"),
("All files", "*")
});
if (fd.ShowDialog() == true)
{
OpenFile(fd.FileName);
}
}, x => true);

SaveCommand = new RelayCommand(x =>
{
if (!string.IsNullOrEmpty(FileName))
{
SaveFile(FileName, FileName);
}
else
{
SaveAsCommand.Execute(x);
}
}, x => true);

SaveAsCommand = new RelayCommand(x =>
{
var fd = FileDialog.Factory(Window, FileDialog.Behavior.Save);
if (fd.ShowDialog() == true)
{
SaveFile(FileName, fd.FileName);
FileName = fd.FileName;
}
}, x => true);

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

AboutCommand = new RelayCommand(x =>
{
new AboutDialog(Assembly.GetExecutingAssembly()).ShowDialog();
}, x => true);
}

public bool OpenFile(string fileName) => File.OpenRead(fileName).Using(stream =>
{
if (!Bar.IsValid(stream))
{
MessageBox.Show(Window, $"{Path.GetFileName(fileName)} is not a valid BAR file.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return false;
}
var items = Bar.Read(stream);
if (!Is00battle(items))
{
MessageBox.Show(Window, $"{Path.GetFileName(fileName)} does not appear to be a valid 00battle.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return false;
}
LoadBattleItems(items);
FileName = fileName;
return true;
});

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

private bool Is00battle(IEnumerable<Bar.Entry> entries)
{
return true;
}

private void LoadBattleItems(IEnumerable<Bar.Entry> entries)
{
_battleItems = entries;
}

private void SaveBattleItems()
{

}
}
}
23 changes: 22 additions & 1 deletion OpenKh.Tools.Kh2BattleEditor/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,27 @@
mc:Ignorable="d"
Title="{Binding Title}" Height="450" Width="800">
<Grid>

<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>

<Menu Grid.Row="0">
<MenuItem Header="_File">
<MenuItem Header="_Open 00battle"
Command="{Binding OpenCommand}"/>
<MenuItem Header="_Save 00battle"
Command="{Binding SaveCommand}"/>
<MenuItem Header="Save 00battle _as..."
Command="{Binding SaveAsCommand}"/>
<Separator/>
<MenuItem Header="E_xit"
Command="{Binding ExitCommand}"/>
</MenuItem>
<MenuItem Header="_Help">
<MenuItem Header="_About"
Command="{Binding AboutCommand}"/>
</MenuItem>
</Menu>
</Grid>
</Window>

0 comments on commit f2c03b1

Please sign in to comment.