Skip to content

Commit

Permalink
Lvup: Add views from LevelUp Editor
Browse files Browse the repository at this point in the history
  • Loading branch information
Rikux3 committed May 30, 2020
1 parent fa359ca commit 05fcd97
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 15 deletions.
98 changes: 98 additions & 0 deletions OpenKh.Tools.Kh2BattleEditor/ViewModels/LvupViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using OpenKh.Kh2;
using OpenKh.Kh2.Battle;
using OpenKh.Tools.Kh2BattleEditor.Extensions;
using OpenKh.Tools.Kh2BattleEditor.Interfaces;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Xe.Tools.Wpf.Models;

namespace OpenKh.Tools.Kh2BattleEditor.ViewModels
{
public class LvupViewModel : GenericListModel<LvupViewModel.CharacterViewModel>, IBattleGetChanges
{
private const int DefaultType = 2;
private const string entryName = "lvup";
private Lvup _lvup;
public string EntryName => entryName;

public LvupViewModel(IEnumerable<Bar.Entry> entries) :
this(Lvup.Read(entries.GetBattleStream(entryName)))
{
}

public LvupViewModel(Lvup Lvup) :
this(Lvup.Characters)
{
_lvup = Lvup;
}

public LvupViewModel(IEnumerable<Lvup.Character> characters) :
base(characters.Select((x, i) => new CharacterViewModel(x, i)))
{
}

public LvupViewModel() :
this(new Lvup
{
MagicCode = DefaultType,
Count = 14,
Unknown08 = new byte[0x30],
Characters = new List<Lvup.Character>()
})
{

}

public Stream CreateStream()
{
var stream = new MemoryStream();
new Lvup
{
MagicCode = _lvup.MagicCode,
Count = _lvup.Count,
Unknown08 = _lvup.Unknown08,
Characters = Items.Select(x => x.Character).ToList()
}.Write(stream);

return stream;
}

public class CharacterViewModel : GenericListModel<CharacterViewModel.LevelViewModel>
{
private int _index;
public Lvup.Character Character { get; set; }
public string Name => ((Lvup.PlayableCharacterType)_index).ToString();

public CharacterViewModel(Lvup.Character character, int index) :
base(character.Levels.Select((x, i) => new LevelViewModel(x, i)))
{
_index = index;
Character = character;
}

public class LevelViewModel
{
private Lvup.Character.Level _level;
private int _index;

public string Name => $"Level {_index + 1}";

public LevelViewModel(Lvup.Character.Level level, int index)
{
_level = level;
_index = index;
}

public int Exp { get => _level.Exp; set => _level.Exp = value; }
public byte Strength { get => _level.Strength; set => _level.Strength = value; }
public byte Magic { get => _level.Magic; set => _level.Magic = value; }
public byte Defense { get => _level.Defense; set => _level.Defense = value; }
public byte Ap { get => _level.Ap; set => _level.Ap = value; }
public short SwordAbility { get => _level.SwordAbility; set => _level.SwordAbility = value; }
public short ShieldAbility { get => _level.ShieldAbility; set => _level.ShieldAbility = value; }
public short StaffAbility { get => _level.StaffAbility; set => _level.StaffAbility = value; }
}
}
}
}
37 changes: 22 additions & 15 deletions OpenKh.Tools.Kh2BattleEditor/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using OpenKh.Kh2.Extensions;
using OpenKh.Tools.Common;
using OpenKh.Tools.Kh2BattleEditor.Interfaces;
using OpenKh.Tools.Kh2BattleEditor.Views;
using Xe.Tools;
using Xe.Tools.Wpf.Commands;
using Xe.Tools.Wpf.Dialogs;
Expand All @@ -20,12 +21,15 @@ public class MainViewModel : BaseNotifyPropertyChanged
{
private static string ApplicationName = Utilities.GetApplicationName();
private Window Window => Application.Current.Windows.OfType<Window>().FirstOrDefault(x => x.IsActive);
private static readonly List<FileDialogFilter> BattleFilter = FileDialogFilterComposer.Compose()
.AddExtensions("00battle", "bin", "bar").AddAllFiles();
private string _fileName;
private IEnumerable<Bar.Entry> _battleItems;
private EnmpViewModel _enmp;
private FmlvViewModel _fmlv;
private BonsViewModel _bons;
private PrztViewModel _przt;
private LvupViewModel _lvup;

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

Expand Down Expand Up @@ -69,22 +73,22 @@ public PrztViewModel Przt
private set { _przt = value; OnPropertyChanged(); }
}

public LvupViewModel Lvup
{
get => _lvup;
private set { _lvup = value; OnPropertyChanged(); }
}



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

SaveCommand = new RelayCommand(x =>
Expand All @@ -101,12 +105,11 @@ public MainViewModel()

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

ExitCommand = new RelayCommand(x =>
Expand Down Expand Up @@ -161,6 +164,7 @@ private bool Is00battle(List<Bar.Entry> items) => items.Any(x => new[]
"lvpm",
"bons",
"przt",
"lvup",
}.Contains(x.Name));

private void CreateBattleItems()
Expand All @@ -170,6 +174,7 @@ private void CreateBattleItems()
Fmlv = GetDefaultBattleViewModelInstance<FmlvViewModel>();
Bons = GetDefaultBattleViewModelInstance<BonsViewModel>();
Przt = GetDefaultBattleViewModelInstance<PrztViewModel>();
Lvup = GetDefaultBattleViewModelInstance<LvupViewModel>();
}

private void LoadBattleItems(IEnumerable<Bar.Entry> entries)
Expand All @@ -179,6 +184,7 @@ private void LoadBattleItems(IEnumerable<Bar.Entry> entries)
Fmlv = GetBattleViewModelInstance<FmlvViewModel>(_battleItems);
Bons = GetBattleViewModelInstance<BonsViewModel>(_battleItems);
Przt = GetBattleViewModelInstance<PrztViewModel>(_battleItems);
Lvup = GetBattleViewModelInstance<LvupViewModel>(_battleItems);
}

private void SaveBattleItems()
Expand All @@ -187,6 +193,7 @@ private void SaveBattleItems()
_battleItems = SaveBattleItem(_battleItems, Fmlv);
_battleItems = SaveBattleItem(_battleItems, Bons);
_battleItems = SaveBattleItem(_battleItems, Przt);
_battleItems = SaveBattleItem(_battleItems, Lvup);
}

private IEnumerable<Bar.Entry> SaveBattleItem(IEnumerable<Bar.Entry> entries, IBattleGetChanges battleGetChanges) =>
Expand Down
62 changes: 62 additions & 0 deletions OpenKh.Tools.Kh2BattleEditor/Views/LvupView.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<UserControl x:Class="OpenKh.Tools.Kh2BattleEditor.Views.LvupView"
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:controls="clr-namespace:Xe.Tools.Wpf.Controls;assembly=Xe.Tools.Wpf"
xmlns:local="clr-namespace:OpenKh.Tools.Kh2BattleEditor.Views"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="10"/>
<RowDefinition Height="2*"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Grid.Row="0" VerticalAlignment="Center">
<TextBlock Text="Character" Margin="0 2 5 0"/>
<ComboBox Width="200"
ItemsSource="{Binding Items}"
SelectedIndex="{Binding SelectedIndex}"
SelectedItem="{Binding SelectedItem}"
DisplayMemberPath="Name"/>
</StackPanel>
<Grid Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ListBox Grid.Column="0"
ItemsSource="{Binding SelectedItem.Items}"
SelectedIndex="{Binding SelectedItem.SelectedIndex}"
SelectedItem="{Binding SelectedItem.SelectedItem}"
DisplayMemberPath="Name" />
<controls:TwoColumnsPanel Grid.Column="2" RowSpacing="3" DataContext="{Binding SelectedItem.SelectedItem}">
<TextBlock Text="EXP for Level Up" />
<TextBox Text="{Binding Exp, UpdateSourceTrigger=PropertyChanged}" />

<TextBlock Text="Strength" />
<TextBox Text="{Binding Strength, UpdateSourceTrigger=PropertyChanged}" />

<TextBlock Text="Magic" />
<TextBox Text="{Binding Magic, UpdateSourceTrigger=PropertyChanged}" />

<TextBlock Text="Defense" />
<TextBox Text="{Binding Defense, UpdateSourceTrigger=PropertyChanged}" />

<TextBlock Text="AP" />
<TextBox Text="{Binding Ap, UpdateSourceTrigger=PropertyChanged}" />

<TextBlock Text="Sword ability" />
<TextBox Text="{Binding SwordAbility, UpdateSourceTrigger=PropertyChanged}" />

<TextBlock Text="Shield ability" />
<TextBox Text="{Binding ShieldAbility, UpdateSourceTrigger=PropertyChanged}" />

<TextBlock Text="Staff ability" />
<TextBox Text="{Binding StaffAbility, UpdateSourceTrigger=PropertyChanged}" />
</controls:TwoColumnsPanel>
</Grid>
</Grid>
</UserControl>
28 changes: 28 additions & 0 deletions OpenKh.Tools.Kh2BattleEditor/Views/LvupView.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace OpenKh.Tools.Kh2BattleEditor.Views
{
/// <summary>
/// Interaction logic for LvupView.xaml
/// </summary>
public partial class LvupView : UserControl
{
public LvupView()
{
InitializeComponent();
}
}
}
3 changes: 3 additions & 0 deletions OpenKh.Tools.Kh2BattleEditor/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
<TabItem Header="Przt">
<local:PrztView DataContext="{Binding Przt}"/>
</TabItem>
<TabItem Header="Lvup">
<local:LvupView DataContext="{Binding Lvup}" />
</TabItem>
</TabControl>
</Grid>
</Window>

0 comments on commit 05fcd97

Please sign in to comment.