From eeeef17c8d1e54cba7a85dfe3b983d22368e2ac8 Mon Sep 17 00:00:00 2001 From: Benjamin Popp Date: Mon, 28 Sep 2020 22:37:43 -0500 Subject: [PATCH] Add test to show that palette relocation works with the image editor This is implemented via the palette collection, so the image editor gets it for free. --- .../ViewModels/ImageEditorViewModel.cs | 8 ++++---- src/HexManiac.Tests/ImageEditorTests.cs | 17 +++++++++++++++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/HexManiac.Core/ViewModels/ImageEditorViewModel.cs b/src/HexManiac.Core/ViewModels/ImageEditorViewModel.cs index df1505a3..bc3f0a57 100644 --- a/src/HexManiac.Core/ViewModels/ImageEditorViewModel.cs +++ b/src/HexManiac.Core/ViewModels/ImageEditorViewModel.cs @@ -24,7 +24,6 @@ namespace HavenSoft.HexManiac.Core.ViewModels { public class ImageEditorViewModel : ViewModelCore, ITabContent, IPixelViewModel, IRaiseMessageTab { private readonly ChangeHistory history; private readonly IDataModel model; - private int palettePointerAddress; private int[,] pixels; private bool withinInteraction, withinDropperInteraction, withinPanInteraction; @@ -121,6 +120,7 @@ namespace HavenSoft.HexManiac.Core.ViewModels { public PaletteCollection Palette { get; } public int SpritePointer { get; } + public int PalettePointer { get; } private StubCommand setCursorSize; public ICommand SetCursorSize => StubCommand(ref setCursorSize, arg => CursorSize = int.Parse(arg)); @@ -138,7 +138,7 @@ namespace HavenSoft.HexManiac.Core.ViewModels { if (spriteRun == null) spriteRun = palRun.FindDependentSprites(model).First(); if (palRun == null) palRun = spriteRun.FindRelatedPalettes(model).First(); SpritePointer = spriteRun.PointerSources[0]; - palettePointerAddress = palRun.PointerSources[0]; + PalettePointer = palRun.PointerSources[0]; Palette = new PaletteCollection(this, model, history) { SourcePalette = palRun.Start }; Palette.Bind(nameof(Palette.HoverIndex), UpdateSelectionFromPaletteHover); Refresh(); @@ -259,7 +259,7 @@ namespace HavenSoft.HexManiac.Core.ViewModels { } private void RefreshPaletteColors() { - var paletteAddress = model.ReadPointer(palettePointerAddress); + var paletteAddress = model.ReadPointer(PalettePointer); var palRun = (IPaletteRun)model.GetNextRun(paletteAddress); Palette.SetContents(palRun.GetPalette(model, 0)); foreach (var e in Palette.Elements) { @@ -285,7 +285,7 @@ namespace HavenSoft.HexManiac.Core.ViewModels { private void Render() { var spriteAddress = model.ReadPointer(SpritePointer); - var paletteAddress = model.ReadPointer(palettePointerAddress); + var paletteAddress = model.ReadPointer(PalettePointer); var spriteRun = (ISpriteRun)model.GetNextRun(spriteAddress); var palRun = (IPaletteRun)model.GetNextRun(paletteAddress); diff --git a/src/HexManiac.Tests/ImageEditorTests.cs b/src/HexManiac.Tests/ImageEditorTests.cs index 362ac57c..b1c64201 100644 --- a/src/HexManiac.Tests/ImageEditorTests.cs +++ b/src/HexManiac.Tests/ImageEditorTests.cs @@ -58,6 +58,9 @@ namespace HavenSoft.HexManiac.Tests { var palette = new PaletteRun(0x40, new PaletteFormat(4, 1), new SortedSpan(0x88)); model.ObserveAnchorWritten(history.CurrentChange, "palette", palette); + model[0x20] = 0x23; // random data after the sprite, so expanding it causes a repoint + model[0x60] = 0x23; // random data after the palette, so expanding it causes a repoint + editor = new ImageEditorViewModel(history, model, 0); } @@ -247,17 +250,27 @@ namespace HavenSoft.HexManiac.Tests { [Fact] public void Image_Repoint_ToolStillWorks() { - var source = editor.SpritePointer; var destination = model.ReadPointer(editor.SpritePointer); var spriteRun = (ISpriteRun)model.GetNextRun(destination); - model.RelocateForExpansion(history.CurrentChange, spriteRun, spriteRun.Length + 1); + spriteRun = model.RelocateForExpansion(history.CurrentChange, spriteRun, spriteRun.Length + 1); // if this doesn't throw, we're happy editor.SelectedTool = ImageEditorTools.Draw; ToolMove(new Point()); } + [Fact] + public void Palette_Repoint_AdressUpdate() { + var destination = model.ReadPointer(editor.PalettePointer); + var palRun = (IPaletteRun)model.GetNextRun(destination); + + palRun = model.RelocateForExpansion(history.CurrentChange, palRun, palRun.Length + 1); + + editor.Palette.Elements[0].Color = Rgb(31, 31, 31); + Assert.Equal(Rgb(31, 31, 31), model.ReadMultiByteValue(palRun.Start, 2)); + } + [Fact] public void Zoom_Draw_ColorChanges() { editor.ZoomIn(-4, -4);