Skip to content

Commit

Permalink
Add skeleton for 00battle editing support
Browse files Browse the repository at this point in the history
  • Loading branch information
Xeeynamo authored and Rikux3 committed May 31, 2020
1 parent f2c03b1 commit 0d10673
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 4 deletions.
14 changes: 14 additions & 0 deletions OpenKh.Tools.Kh2BattleEditor/Extensions/BarExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using OpenKh.Kh2;

namespace OpenKh.Tools.Kh2BattleEditor.Extensions
{
public static class BarExtensions
{
public static Stream GetBattleStream(this IEnumerable<Bar.Entry> entries, string name) =>
// TODO throws exception if that entry is not found
entries.First(x => x.Name == name && x.Index == 0 && x.Type == Bar.EntryType.Binary).Stream;
}
}
11 changes: 11 additions & 0 deletions OpenKh.Tools.Kh2BattleEditor/Interfaces/IBattleGetChanges.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.IO;

namespace OpenKh.Tools.Kh2BattleEditor.Interfaces
{
public interface IBattleGetChanges
{
string EntryName { get; }

Stream CreateStream();
}
}
40 changes: 40 additions & 0 deletions OpenKh.Tools.Kh2BattleEditor/ViewModels/EnmpViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Collections.Generic;
using System.IO;
using OpenKh.Kh2;
using OpenKh.Kh2.Battle;
using OpenKh.Tools.Kh2BattleEditor.Extensions;
using OpenKh.Tools.Kh2BattleEditor.Interfaces;
using Xe.Tools;

namespace OpenKh.Tools.Kh2BattleEditor.ViewModels
{
public class EnmpViewModel : BaseNotifyPropertyChanged, IBattleGetChanges
{
private readonly BaseBattle<Enmp> _enmp;

public EnmpViewModel(IEnumerable<Bar.Entry> entries)
{
_enmp = Enmp.Read(entries.GetBattleStream(EntryName));
}

public EnmpViewModel()
{
_enmp = new BaseBattle<Enmp>
{
Items = new Enmp[0]
};
}

public string EntryName => "enmp";

public Stream CreateStream()
{
var stream = new MemoryStream();
_enmp.Write(stream);

return stream;
}

public string Foo { get; set; } = "Hello world!";
}
}
40 changes: 36 additions & 4 deletions OpenKh.Tools.Kh2BattleEditor/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows;
using OpenKh.Common;
using OpenKh.Kh2;
using OpenKh.Kh2.Battle;
using OpenKh.Kh2.Extensions;
using OpenKh.Tools.Common;
using OpenKh.Tools.Kh2BattleEditor.Interfaces;
using Xe.Tools;
using Xe.Tools.Wpf.Commands;
using Xe.Tools.Wpf.Dialogs;
Expand All @@ -18,6 +22,7 @@ public class MainViewModel : BaseNotifyPropertyChanged
private Window Window => Application.Current.Windows.OfType<Window>().FirstOrDefault(x => x.IsActive);
private string _fileName;
private IEnumerable<Bar.Entry> _battleItems;
private EnmpViewModel _enmp;

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

Expand All @@ -37,6 +42,12 @@ private string FileName
public RelayCommand ExitCommand { get; }
public RelayCommand AboutCommand { get; }

public EnmpViewModel Enmp
{
get => _enmp;
private set { _enmp = value; OnPropertyChanged();}
}

public MainViewModel()
{
OpenCommand = new RelayCommand(x =>
Expand Down Expand Up @@ -85,6 +96,8 @@ public MainViewModel()
{
new AboutDialog(Assembly.GetExecutingAssembly()).ShowDialog();
}, x => true);

CreateBattleItems();
}

public bool OpenFile(string fileName) => File.OpenRead(fileName).Using(stream =>
Expand Down Expand Up @@ -118,19 +131,38 @@ public void SaveFile(string previousFileName, string fileName)
});
}

private bool Is00battle(IEnumerable<Bar.Entry> entries)
private bool Is00battle(List<Bar.Entry> items) => items.Any(x => new[]
{
return true;
"atkp",
"enmp",
"fmlv",
"lvpm",
}.Contains(x.Name));

private void CreateBattleItems()
{
_battleItems = new Bar.Entry[0];
Enmp = GetDefaultBattleViewModelInstance<EnmpViewModel>();
}

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

private void SaveBattleItems()
{

_battleItems = SaveBattleItem(_battleItems, Enmp);
}

private IEnumerable<Bar.Entry> SaveBattleItem(IEnumerable<Bar.Entry> entries, IBattleGetChanges battleGetChanges) =>
entries.ForEntry(Bar.EntryType.Binary, battleGetChanges.EntryName, 0, entry => entry.Stream = battleGetChanges.CreateStream());

private T GetBattleViewModelInstance<T>(IEnumerable<Bar.Entry> entries)
where T : IBattleGetChanges => (T)Activator.CreateInstance(typeof(T), entries);

private T GetDefaultBattleViewModelInstance<T>()
where T : IBattleGetChanges => Activator.CreateInstance<T>();
}
}
12 changes: 12 additions & 0 deletions OpenKh.Tools.Kh2BattleEditor/Views/EnmpView.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<UserControl x:Class="OpenKh.Tools.Kh2BattleEditor.Views.EnmpView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:OpenKh.Tools.Kh2BattleEditor.Views"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<TextBlock Text="{Binding Foo}"/>
</Grid>
</UserControl>
15 changes: 15 additions & 0 deletions OpenKh.Tools.Kh2BattleEditor/Views/EnmpView.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Windows.Controls;

namespace OpenKh.Tools.Kh2BattleEditor.Views
{
/// <summary>
/// Interaction logic for EnmpView.xaml
/// </summary>
public partial class EnmpView : UserControl
{
public EnmpView()
{
InitializeComponent();
}
}
}
6 changes: 6 additions & 0 deletions OpenKh.Tools.Kh2BattleEditor/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,11 @@
Command="{Binding AboutCommand}"/>
</MenuItem>
</Menu>

<TabControl Grid.Row="1">
<TabItem Header="Enmp">
<local:EnmpView DataContext="{Binding Enmp}"/>
</TabItem>
</TabControl>
</Grid>
</Window>

0 comments on commit 0d10673

Please sign in to comment.