diff --git a/src/Gen3Hex.Core/Gen3Hex.Core.csproj b/src/Gen3Hex.Core/Gen3Hex.Core.csproj index dd5f8fdd..584d77ce 100644 --- a/src/Gen3Hex.Core/Gen3Hex.Core.csproj +++ b/src/Gen3Hex.Core/Gen3Hex.Core.csproj @@ -60,7 +60,6 @@ - diff --git a/src/Gen3Hex.Core/ViewModels/IToolViewModel.cs b/src/Gen3Hex.Core/ViewModels/IToolViewModel.cs index f631a6ac..0e841012 100644 --- a/src/Gen3Hex.Core/ViewModels/IToolViewModel.cs +++ b/src/Gen3Hex.Core/ViewModels/IToolViewModel.cs @@ -1,8 +1,87 @@ -using System.Collections.Generic; +using HavenSoft.Gen3Hex.Core.Models; +using HavenSoft.Gen3Hex.Core.ViewModels.DataFormats; +using System.Collections; +using System.Collections.Generic; using System.ComponentModel; +using System.Linq; +using System.Windows.Input; namespace HavenSoft.Gen3Hex.Core.ViewModels { public interface IToolTrayViewModel : IReadOnlyList, INotifyPropertyChanged { int SelectedIndex { get; set; } + + PCSTool StringTool { get; } + } + + public class ToolTray : ViewModelCore, IToolTrayViewModel { + private readonly IList tools; + private readonly StubCommand stringToolCommand; + + private int selectedIndex; + public int SelectedIndex { + get => selectedIndex; + set => TryUpdate(ref selectedIndex, value); + } + + public int Count => tools.Count; + public IToolViewModel this[int index] => tools[index]; + public PCSTool StringTool => (PCSTool)tools[0]; + public ICommand StringToolCommand => stringToolCommand; + + public ToolTray(IModel model) { + tools = new[] { + new PCSTool(model), + }; + + stringToolCommand = new StubCommand { + CanExecute = ICommandExtensions.CanAlwaysExecute, + Execute = arg => SelectedIndex = selectedIndex == 0 ? -1 : 0, + }; + + foreach (var tool in tools) tool.PropertyChanged += (sender, e) => SelectedIndex = tools.IndexOf((IToolViewModel)sender); + SelectedIndex = -1; + } + + public IEnumerator GetEnumerator() => tools.GetEnumerator(); + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + } + + public interface IToolViewModel : INotifyPropertyChanged { + string Name { get; } + } + + public class PCSTool : ViewModelCore, IToolViewModel { + private readonly IModel model; + public string Name => "String"; + + private string content; + public string Content { + get => content; + set { + if (TryUpdate(ref content, value)) { + var run = model.GetNextRun(address) as PCSRun; + if (run == null) return; + var bytes = PCSString.Convert(content); + var newRun = model.RelocateForExpansion(run, bytes.Count); + for (int i = 0; i < bytes.Count; i++) model[newRun.Start + i] = bytes[i]; + model.ObserveRunWritten(new PCSRun(newRun.Start, bytes.Count, newRun.PointerSources)); + } + } + } + + private int address = Pointer.NULL; + public int Address { + get => address; + set { + if (TryUpdate(ref address, value)) { + var run = model.GetNextRun(address) as PCSRun; + if (run == null) return; + TryUpdate(ref content, PCSString.Convert(model, run.Start, run.Length), nameof(Content)); + } + } + } + + public PCSTool(IModel model) => this.model = model; } } diff --git a/src/Gen3Hex.Core/ViewModels/IToolViewModel1.cs b/src/Gen3Hex.Core/ViewModels/IToolViewModel1.cs deleted file mode 100644 index 16395009..00000000 --- a/src/Gen3Hex.Core/ViewModels/IToolViewModel1.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System.ComponentModel; - -namespace HavenSoft.Gen3Hex.Core.ViewModels { - public interface IToolViewModel : INotifyPropertyChanged { - string Name { get; } - } - - public class PCSTool : ViewModelCore, IToolViewModel { - public string Name => "String"; - } -} diff --git a/src/Gen3Hex.Core/ViewModels/ViewPort.cs b/src/Gen3Hex.Core/ViewModels/ViewPort.cs index f14a189c..a0bcaf77 100644 --- a/src/Gen3Hex.Core/ViewModels/ViewPort.cs +++ b/src/Gen3Hex.Core/ViewModels/ViewPort.cs @@ -225,6 +225,7 @@ namespace HavenSoft.Gen3Hex.Core.ViewModels { public ViewPort(LoadedFile file, IModel model = null) { Model = model ?? new BasicModel(file.Contents); FileName = file.Name; + Tools = new ToolTray(Model); scroll = new ScrollRegion { DataLength = Model.Count }; scroll.PropertyChanged += ScrollPropertyChanged; @@ -442,6 +443,9 @@ namespace HavenSoft.Gen3Hex.Core.ViewModels { OnError(this, $"Pointer destination {pointer.DestinationName} not found."); } } + if (format is PCS pcs) { + Tools.StringTool.Address = pcs.Source; + } } public void ConsiderReload(IFileSystem fileSystem) {