|
@@ -1,4 +1,5 @@
|
|
-using PixiEditor.ChangeableDocument.Changeables.Interfaces;
|
|
|
|
|
|
+using System.Diagnostics.CodeAnalysis;
|
|
|
|
+using PixiEditor.ChangeableDocument.Changeables.Interfaces;
|
|
|
|
|
|
namespace PixiEditor.ChangeableDocument.Changeables;
|
|
namespace PixiEditor.ChangeableDocument.Changeables;
|
|
|
|
|
|
@@ -7,10 +8,14 @@ internal class Document : IChangeable, IReadOnlyDocument, IDisposable
|
|
IReadOnlyFolder IReadOnlyDocument.StructureRoot => StructureRoot;
|
|
IReadOnlyFolder IReadOnlyDocument.StructureRoot => StructureRoot;
|
|
IReadOnlySelection IReadOnlyDocument.Selection => Selection;
|
|
IReadOnlySelection IReadOnlyDocument.Selection => Selection;
|
|
IReadOnlyStructureMember? IReadOnlyDocument.FindMember(Guid guid) => FindMember(guid);
|
|
IReadOnlyStructureMember? IReadOnlyDocument.FindMember(Guid guid) => FindMember(guid);
|
|
|
|
+ bool IReadOnlyDocument.TryFindMember(Guid guid, [NotNullWhen(true)] out IReadOnlyStructureMember? member) => TryFindMember(guid, out member);
|
|
IReadOnlyList<IReadOnlyStructureMember> IReadOnlyDocument.FindMemberPath(Guid guid) => FindMemberPath(guid);
|
|
IReadOnlyList<IReadOnlyStructureMember> IReadOnlyDocument.FindMemberPath(Guid guid) => FindMemberPath(guid);
|
|
IReadOnlyStructureMember IReadOnlyDocument.FindMemberOrThrow(Guid guid) => FindMemberOrThrow(guid);
|
|
IReadOnlyStructureMember IReadOnlyDocument.FindMemberOrThrow(Guid guid) => FindMemberOrThrow(guid);
|
|
(IReadOnlyStructureMember, IReadOnlyFolder) IReadOnlyDocument.FindChildAndParentOrThrow(Guid guid) => FindChildAndParentOrThrow(guid);
|
|
(IReadOnlyStructureMember, IReadOnlyFolder) IReadOnlyDocument.FindChildAndParentOrThrow(Guid guid) => FindChildAndParentOrThrow(guid);
|
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// The default size for a new document
|
|
|
|
+ /// </summary>
|
|
public static VecI DefaultSize { get; } = new VecI(64, 64);
|
|
public static VecI DefaultSize { get; } = new VecI(64, 64);
|
|
internal Folder StructureRoot { get; } = new() { GuidValue = Guid.Empty };
|
|
internal Folder StructureRoot { get; } = new() { GuidValue = Guid.Empty };
|
|
internal Selection Selection { get; } = new();
|
|
internal Selection Selection { get; } = new();
|
|
@@ -25,8 +30,11 @@ internal class Document : IChangeable, IReadOnlyDocument, IDisposable
|
|
StructureRoot.Dispose();
|
|
StructureRoot.Dispose();
|
|
Selection.Dispose();
|
|
Selection.Dispose();
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
public void ForEveryReadonlyMember(Action<IReadOnlyStructureMember> action) => ForEveryReadonlyMember(StructureRoot, action);
|
|
public void ForEveryReadonlyMember(Action<IReadOnlyStructureMember> action) => ForEveryReadonlyMember(StructureRoot, action);
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// Performs the specified action on each member of the document
|
|
|
|
+ /// </summary>
|
|
public void ForEveryMember(Action<StructureMember> action) => ForEveryMember(StructureRoot, action);
|
|
public void ForEveryMember(Action<StructureMember> action) => ForEveryMember(StructureRoot, action);
|
|
|
|
|
|
private void ForEveryReadonlyMember(IReadOnlyFolder folder, Action<IReadOnlyStructureMember> action)
|
|
private void ForEveryReadonlyMember(IReadOnlyFolder folder, Action<IReadOnlyStructureMember> action)
|
|
@@ -49,14 +57,97 @@ internal class Document : IChangeable, IReadOnlyDocument, IDisposable
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- public StructureMember FindMemberOrThrow(Guid guid) => FindMember(guid) ?? throw new ArgumentException("Could not find member with guid " + guid.ToString());
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// Checks if a member with the <paramref name="guid"/> exists
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="guid">The <see cref="StructureMember.GuidValue"/> of the member</param>
|
|
|
|
+ /// <returns>True if the member can be found, otherwise false</returns>
|
|
|
|
+ public bool HasMember(Guid guid)
|
|
|
|
+ {
|
|
|
|
+ var list = FindMemberPath(guid);
|
|
|
|
+ return list.Count > 0;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// Checks if a member with the <paramref name="guid"/> exists and is of type <typeparamref name="T"/>
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="guid">The <see cref="StructureMember.GuidValue"/> of the member</param>
|
|
|
|
+ /// <returns>True if the member can be found and is of type <typeparamref name="T"/>, otherwise false</returns>
|
|
|
|
+ public bool HasMember<T>(Guid guid) where T : StructureMember
|
|
|
|
+ {
|
|
|
|
+ var list = FindMemberPath(guid);
|
|
|
|
+ return list.Count > 0 && list[0] is T;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// Finds the member with the <paramref name="guid"/> or throws a ArgumentException if not found
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="guid">The <see cref="StructureMember.GuidValue"/> of the member</param>
|
|
|
|
+ /// <exception cref="ArgumentException">Thrown if the member could not be found</exception>
|
|
|
|
+ public StructureMember FindMemberOrThrow(Guid guid) => FindMember(guid) ?? throw new ArgumentException($"Could not find member with guid '{guid}'");
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// Finds the member of type <typeparamref name="T"/> with the <paramref name="guid"/> or throws an exception
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="guid">The <see cref="StructureMember.GuidValue"/> of the member</param>
|
|
|
|
+ /// <exception cref="ArgumentException">Thrown if the member could not be found</exception>
|
|
|
|
+ /// <exception cref="InvalidCastException">Thrown if the member is not of type <typeparamref name="T"/></exception>
|
|
|
|
+ public T FindMemberOrThrow<T>(Guid guid) where T : StructureMember => (T)FindMember(guid)!;
|
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// Finds the member with the <paramref name="guid"/> or returns null if not found
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="guid">The <see cref="StructureMember.GuidValue"/> of the member</param>
|
|
public StructureMember? FindMember(Guid guid)
|
|
public StructureMember? FindMember(Guid guid)
|
|
{
|
|
{
|
|
var list = FindMemberPath(guid);
|
|
var list = FindMemberPath(guid);
|
|
return list.Count > 0 ? list[0] : null;
|
|
return list.Count > 0 ? list[0] : null;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// Tries finding the member with the <paramref name="guid"/> and returns true if it was found
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="guid">The <see cref="StructureMember.GuidValue"/> of the <paramref name="member"/></param>
|
|
|
|
+ /// <param name="member">The member</param>
|
|
|
|
+ /// <returns>True if the member could be found, otherwise false</returns>
|
|
|
|
+ public bool TryFindMember(Guid guid, [NotNullWhen(true)] out StructureMember? member)
|
|
|
|
+ {
|
|
|
|
+ var list = FindMemberPath(guid);
|
|
|
|
+ if (list.Count == 0)
|
|
|
|
+ {
|
|
|
|
+ member = null;
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ member = list[0];
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// Tries finding the member with the <paramref name="guid"/> of type <typeparamref name="T"/> and returns true if it was found
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="guid">The <see cref="StructureMember.GuidValue"/> of the <paramref name="member"/></param>
|
|
|
|
+ /// <param name="member">The member</param>
|
|
|
|
+ /// <typeparam name="T">The type of the <see cref="StructureMember"/></typeparam>
|
|
|
|
+ /// <returns>True if the member could be found and is of type <typeparamref name="T"/>, otherwise false</returns>
|
|
|
|
+ public bool TryFindMember<T>(Guid guid, [NotNullWhen(true)] out T? member) where T : IReadOnlyStructureMember
|
|
|
|
+ {
|
|
|
|
+ if (!TryFindMember(guid, out var structureMember) || structureMember is not T cast)
|
|
|
|
+ {
|
|
|
|
+ member = default;
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ member = cast;
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// Finds a member with the <paramref name="childGuid"/> and its parent, throws a ArgumentException if they can't be found
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="childGuid">The <see cref="StructureMember.GuidValue"/> of the member</param>
|
|
|
|
+ /// <returns>A value tuple consisting of child (<see cref="ValueTuple{T, T}.Item1"/>) and parent (<see cref="ValueTuple{T, T}.Item2"/>)</returns>
|
|
|
|
+ /// <exception cref="ArgumentException">Thrown if the member and parent could not be found</exception>
|
|
public (StructureMember, Folder) FindChildAndParentOrThrow(Guid childGuid)
|
|
public (StructureMember, Folder) FindChildAndParentOrThrow(Guid childGuid)
|
|
{
|
|
{
|
|
var path = FindMemberPath(childGuid);
|
|
var path = FindMemberPath(childGuid);
|
|
@@ -65,6 +156,11 @@ internal class Document : IChangeable, IReadOnlyDocument, IDisposable
|
|
return (path[0], (Folder)path[1]);
|
|
return (path[0], (Folder)path[1]);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// Finds a member with the <paramref name="childGuid"/> and its parent
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="childGuid">The <see cref="StructureMember.GuidValue"/> of the member</param>
|
|
|
|
+ /// <returns>A value tuple consisting of child (<see cref="ValueTuple{T, T}.Item1"/>) and parent (<see cref="ValueTuple{T, T}.Item2"/>)<para>Child and parent can be null if not found!</para></returns>
|
|
public (StructureMember?, Folder?) FindChildAndParent(Guid childGuid)
|
|
public (StructureMember?, Folder?) FindChildAndParent(Guid childGuid)
|
|
{
|
|
{
|
|
var path = FindMemberPath(childGuid);
|
|
var path = FindMemberPath(childGuid);
|
|
@@ -76,6 +172,10 @@ internal class Document : IChangeable, IReadOnlyDocument, IDisposable
|
|
};
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// Finds the path to the member with <paramref name="guid"/>, the first element will be the member
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="guid">The <see cref="StructureMember.GuidValue"/> of the member</param>
|
|
public List<StructureMember> FindMemberPath(Guid guid)
|
|
public List<StructureMember> FindMemberPath(Guid guid)
|
|
{
|
|
{
|
|
var list = new List<StructureMember>();
|
|
var list = new List<StructureMember>();
|