Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
stevemonaco committed Oct 14, 2020
2 parents 1be5537 + 85dcc5f commit ff9c3d4
Show file tree
Hide file tree
Showing 71 changed files with 2,697 additions and 2,019 deletions.
66 changes: 66 additions & 0 deletions ImageMagitek.Services/ProjectService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ public interface IProjectService
void CloseProject(ProjectTree projectTree);
void CloseProjects();

MagitekResult<ResourceNode> AddResource(ResourceNode parentNode, IProjectResource resource, bool saveProject);
MagitekResult<ResourceNode> CreateNewFolder(ResourceNode parentNode, string name, bool saveProject);

ProjectTree GetContainingProject(ResourceNode node);
ProjectTree GetContainingProject(IProjectResource resource);
bool AreResourcesInSameProject(IProjectResource a, IProjectResource b);
}

public class ProjectService : IProjectService
Expand Down Expand Up @@ -181,6 +185,60 @@ public void CloseProjects()
Projects.Clear();
}

public MagitekResult<ResourceNode> AddResource(ResourceNode parentNode, IProjectResource resource, bool saveProject)
{
var projectTree = Projects.FirstOrDefault(x => x.ContainsNode(parentNode));

if (projectTree is null)
return new MagitekResult<ResourceNode>.Failed($"{parentNode.Value.Name} is not contained within any loaded project");


var addResult = projectTree.AddResource(parentNode, resource);

return addResult.Match(
addSuccess =>
{
if (saveProject)
{
var saveResult = SaveProject(projectTree);
return saveResult.Match(
saveSuccess => addResult,
saveFailed => new MagitekResult<ResourceNode>.Failed(saveFailed.Reason));
}
else
return addResult;
},
addFailed => addResult);
}

public MagitekResult<ResourceNode> CreateNewFolder(ResourceNode parentNode, string name, bool saveProject)
{
var projectTree = Projects.FirstOrDefault(x => x.ContainsNode(parentNode));

if (projectTree is null)
return new MagitekResult<ResourceNode>.Failed($"{parentNode.Value.Name} is not contained within any loaded project");


var addResult = projectTree.CreateNewFolder(parentNode, name);

return addResult.Match(
addSuccess =>
{
if (saveProject)
{
var saveResult = SaveProject(projectTree);
return saveResult.Match(
saveSuccess => addResult,
saveFailed => new MagitekResult<ResourceNode>.Failed(saveFailed.Reason));
}
else
return addResult;
},
addFailed => addResult);
}

public ProjectTree GetContainingProject(ResourceNode node)
{
return Projects.FirstOrDefault(x => x.ContainsNode(node)) ??
Expand All @@ -192,5 +250,13 @@ public ProjectTree GetContainingProject(IProjectResource resource)
return Projects.FirstOrDefault(x => x.ContainsResource(resource)) ??
throw new ArgumentException($"{nameof(GetContainingProject)} could not locate the resource '{resource.Name}'");
}

public bool AreResourcesInSameProject(IProjectResource a, IProjectResource b)
{
var projectA = Projects.FirstOrDefault(x => x.ContainsResource(a));
var projectB = Projects.FirstOrDefault(x => x.ContainsResource(b));

return ReferenceEquals(projectA, projectB);
}
}
}
38 changes: 20 additions & 18 deletions ImageMagitek.UnitTests/ElementCopierTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,39 @@ namespace ImageMagitek.UnitTests
[TestFixture]
public class ElementCopierTests
{
ScatteredArranger sourceIndexed;
DataFile df;
ScatteredArranger _sourceIndexed;
DataFile _df;

[OneTimeSetUp]
public void Setup()
{
df = new DataFile("sourceDataFile", new MemoryStream());
sourceIndexed = new ScatteredArranger("source", PixelColorType.Indexed, ArrangerLayout.Tiled, 6, 6, 8, 8);
_df = new DataFile("sourceDataFile", new MemoryStream());
_sourceIndexed = new ScatteredArranger("source", PixelColorType.Indexed, ArrangerLayout.Tiled, 6, 6, 8, 8);

for (int y = 0; y < sourceIndexed.ArrangerElementSize.Height; y++)
for (int y = 0; y < _sourceIndexed.ArrangerElementSize.Height; y++)
{
for (int x = 0; x < sourceIndexed.ArrangerElementSize.Width; x++)
for (int x = 0; x < _sourceIndexed.ArrangerElementSize.Width; x++)
{
var el = sourceIndexed.GetElement(x, y);
el = el.WithTarget(df, new FileBitAddress(x * y), new Snes3bppCodec(8, 8), null);
sourceIndexed.SetElement(el, x, y);
if (_sourceIndexed.GetElement(x, y) is ArrangerElement element)
{
element = element.WithTarget(_df, new FileBitAddress(x * y), new Snes3bppCodec(8, 8), null);
_sourceIndexed.SetElement(element, x, y);
}
}
}
}

[Test]
public void CopyElements_ValidIndexedToIndexed_ReturnsTrue()
{
ScatteredArranger dest = new ScatteredArranger("dest", PixelColorType.Indexed, ArrangerLayout.Tiled, 4, 4, 8, 8);
//[Test]
//public void CopyElements_ValidIndexedToIndexed_ReturnsTrue()
//{
// ScatteredArranger dest = new ScatteredArranger("dest", PixelColorType.Indexed, ArrangerLayout.Tiled, 4, 4, 8, 8);

ElementCopier.CopyElements(sourceIndexed, dest, new Point(2, 2), new Point(0, 0), 4, 4);
var sourceItems = sourceIndexed.EnumerateElements(2, 2, 4, 4).ToList();
var destItems = dest.EnumerateElements().ToList();
// ElementCopier.CopyElements(sourceIndexed, dest, new Point(2, 2), new Point(0, 0), 4, 4);
// var sourceItems = sourceIndexed.EnumerateElements(2, 2, 4, 4).ToList();
// var destItems = dest.EnumerateElements().ToList();

CollectionAssert.AreEqual(sourceItems, destItems, new ElementWithoutLocationComparer());
}
// CollectionAssert.AreEqual(sourceItems, destItems, new ElementWithoutLocationComparer());
//}

//public void CanCopyElements_IndexedToDirect_ReturnsFalse()
//{
Expand Down
17 changes: 17 additions & 0 deletions ImageMagitek.UnitTests/TestFactories/ArrangerTestFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using ImageMagitek.Codec;
using System;
using System.Collections.Generic;
using System.Text;

namespace ImageMagitek.UnitTests.TestFactories
{
public enum ArrangerFonts { Bubbles }

public static class ArrangerTestFactory
{
//public static ScatteredArranger CreateArrangerFromImage(string imageFile, IGraphicsCodec codec)
//{
//
//}
}
}
4 changes: 4 additions & 0 deletions ImageMagitek.UnitTests/TestImages/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Attributions

## bubbles_font_2bpp.bmp
- Spoony Bard - [RHDN Link](http://www.romhacking.net/fonts/99/)
Binary file not shown.
Loading

0 comments on commit ff9c3d4

Please sign in to comment.