Edit stream array elements to support plm runs

I previously thought I'd need a different type of run for each type of stream, so I made a text specific one. But I realized that the ViewModel's for both PCSRuns and PLMRuns would have very similar interfaces. Mostly, they need to expose a function to push data to the model, or pull data to a text string. So I added an interface that does that, and now they can both use the same ViewModel.
This commit is contained in:
Benjamin Popp
2019-06-04 01:47:29 -05:00
parent 44951fde72
commit 26d7581368
4 changed files with 11 additions and 15 deletions

View File

@@ -220,7 +220,7 @@ namespace HavenSoft.HexManiac.Core.ViewModels.Tools {
}
}
public class TextStreamArrayElementViewModel : ViewModelCore, IArrayElementViewModel {
public class StreamArrayElementViewModel : ViewModelCore, IArrayElementViewModel {
private readonly ChangeHistory<ModelDelta> history;
private readonly FieldArrayElementViewModel matchingField;
private readonly IDataModel model;
@@ -238,23 +238,19 @@ namespace HavenSoft.HexManiac.Core.ViewModels.Tools {
set {
if (TryUpdate(ref content, value)) {
var destination = model.ReadPointer(start);
var run = model.GetNextRun(destination);
var data = PCSString.Convert(content);
var newRun = model.RelocateForExpansion(history.CurrentChange, run, data.Count);
var run = (IStreamRun)model.GetNextRun(destination);
var newRun = run.DeserializeRun(content, history.CurrentChange);
if (run.Start != newRun.Start) {
DataMoved?.Invoke(this, (run.Start, newRun.Start));
matchingField.RefreshControlFromModelChange();
}
run = newRun;
for (int i = 0; i < data.Count; i++) history.CurrentChange.ChangeData(model, run.Start + i, data[i]);
for (int i = data.Count; i < run.Length; i++) history.CurrentChange.ChangeData(model, run.Start + i, 0xFF);
DataChanged?.Invoke(this, EventArgs.Empty);
model.ObserveRunWritten(history.CurrentChange, new PCSRun(model, run.Start, data.Count));
}
}
}
public TextStreamArrayElementViewModel(ChangeHistory<ModelDelta> history, FieldArrayElementViewModel matchingField, IDataModel model, string name, int start) {
public StreamArrayElementViewModel(ChangeHistory<ModelDelta> history, FieldArrayElementViewModel matchingField, IDataModel model, string name, int start) {
this.history = history;
this.matchingField = matchingField;
this.model = model;
@@ -265,8 +261,8 @@ namespace HavenSoft.HexManiac.Core.ViewModels.Tools {
// by the time we get this far, we're guaranteed that this will be a PCSRun.
// if it's not a PCSRun, we shouldn't have asked to construct this object.
var run = (PCSRun)model.GetNextRun(destination);
content = PCSString.Convert(model, run.Start, run.Length);
var run = (IStreamRun)model.GetNextRun(destination);
content = run.SerializeRun();
}
}
}

View File

@@ -164,8 +164,8 @@ namespace HavenSoft.HexManiac.Core.ViewModels.Tools {
if (item is ArrayRunPointerSegment pointerSegment) {
var destination = model.ReadPointer(itemAddress);
if (destination != Pointer.NULL && pointerSegment.DestinationDataMatchesPointerFormat(model, history.CurrentChange, destination)) {
if (pointerSegment.InnerFormat == $"{PCSRun.StringDelimeter}{PCSRun.StringDelimeter}") {
var streamElement = new TextStreamArrayElementViewModel(history, (FieldArrayElementViewModel)viewModel, model, item.Name, itemAddress);
if (pointerSegment.InnerFormat == PCSRun.SharedFormatString || pointerSegment.InnerFormat == PLMRun.SharedFormatString) {
var streamElement = new StreamArrayElementViewModel(history, (FieldArrayElementViewModel)viewModel, model, item.Name, itemAddress);
streamElement.DataChanged += ForwardModelChanged;
streamElement.DataMoved += ForwardModelDataMoved;
Children.Add(streamElement);

View File

@@ -139,7 +139,7 @@ namespace HavenSoft.HexManiac.Tests {
viewPort.FollowLink(0, 0);
Assert.Equal(2, viewPort.Tools.TableTool.Children.Count);
Assert.IsType<TextStreamArrayElementViewModel>(viewPort.Tools.TableTool.Children[1]);
Assert.IsType<StreamArrayElementViewModel>(viewPort.Tools.TableTool.Children[1]);
}
[Fact]
@@ -151,7 +151,7 @@ namespace HavenSoft.HexManiac.Tests {
viewPort.SelectionStart = new Point(0, 4);
viewPort.Edit("^table[description<\"\">]4 <000000>"); // note that this auto-scrolls, since a table was created
viewPort.SelectionStart = new Point(0, 0);
var textViewModel = (TextStreamArrayElementViewModel)viewPort.Tools.TableTool.Children[1];
var textViewModel = (StreamArrayElementViewModel)viewPort.Tools.TableTool.Children[1];
// act: use the tool to change the content, forcing a repoint
messages.Clear();

View File

@@ -129,7 +129,7 @@
<ComboBox SelectedIndex="{Binding SelectedIndex}" ItemsSource="{Binding Options}" Grid.Column="1"/>
</Grid>
</DataTemplate>
<DataTemplate DataType="{x:Type hsg3hvmtr:TextStreamArrayElementViewModel}">
<DataTemplate DataType="{x:Type hsg3hvmtr:StreamArrayElementViewModel}">
<TextBox Text="{Binding Content, UpdateSourceTrigger=PropertyChanged}" Margin="20,2,2,2" AcceptsReturn="True"/>
</DataTemplate>
</ItemsControl.Resources>