adding the rest of the model for the tool for String editing.

This commit is contained in:
Benjamin Popp
2018-12-19 21:40:07 -06:00
parent 0d9fa6d6b6
commit ee77d42bed
4 changed files with 84 additions and 13 deletions

View File

@@ -60,7 +60,6 @@
<Compile Include="ViewModels\IChildViewPort.cs" />
<Compile Include="ViewModels\ITabContent.cs" />
<Compile Include="ViewModels\IToolViewModel.cs" />
<Compile Include="ViewModels\IToolViewModel1.cs" />
<Compile Include="ViewModels\IViewPort.cs" />
<Compile Include="ViewModels\ScrollRegion.cs" />
<Compile Include="ViewModels\SearchResultsViewPort.cs" />

View File

@@ -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<IToolViewModel>, INotifyPropertyChanged {
int SelectedIndex { get; set; }
PCSTool StringTool { get; }
}
public class ToolTray : ViewModelCore, IToolTrayViewModel {
private readonly IList<IToolViewModel> 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<IToolViewModel> 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;
}
}

View File

@@ -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";
}
}

View File

@@ -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) {