Selaa lähdekoodia

Added TryFindMember methods tho IReadOnlyDocument.cs and added more documentation to Document.cs and IReadOnlyDocument.cs

CPKreuz 3 vuotta sitten
vanhempi
commit
804508054c

+ 27 - 5
src/PixiEditor.ChangeableDocument/Changeables/Document.cs

@@ -8,10 +8,14 @@ internal class Document : IChangeable, IReadOnlyDocument, IDisposable
     IReadOnlyFolder IReadOnlyDocument.StructureRoot => StructureRoot;
     IReadOnlySelection IReadOnlyDocument.Selection => Selection;
     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);
     IReadOnlyStructureMember IReadOnlyDocument.FindMemberOrThrow(Guid guid) => FindMemberOrThrow(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);
     internal Folder StructureRoot { get; } = new() { GuidValue = Guid.Empty };
     internal Selection Selection { get; } = new();
@@ -26,8 +30,11 @@ internal class Document : IChangeable, IReadOnlyDocument, IDisposable
         StructureRoot.Dispose();
         Selection.Dispose();
     }
-
+    
     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);
 
     private void ForEveryReadonlyMember(IReadOnlyFolder folder, Action<IReadOnlyStructureMember> action)
@@ -98,7 +105,7 @@ internal class Document : IChangeable, IReadOnlyDocument, IDisposable
     }
 
     /// <summary>
-    /// Tries finding the member with the <paramref name="guid"/> and returns if it was found
+    /// 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>
@@ -117,17 +124,17 @@ internal class Document : IChangeable, IReadOnlyDocument, IDisposable
     }
 
     /// <summary>
-    /// Tries finding the member with the <paramref name="guid"/> and returns if it was found
+    /// 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 : StructureMember
+    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 = null;
+            member = default;
             return false;
         }
 
@@ -135,6 +142,12 @@ internal class Document : IChangeable, IReadOnlyDocument, IDisposable
         return false;
     }
 
+    /// <summary>
+    /// Find's a member with the <paramref name="childGuid"/>  and it's 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)
     {
         var path = FindMemberPath(childGuid);
@@ -143,6 +156,11 @@ internal class Document : IChangeable, IReadOnlyDocument, IDisposable
         return (path[0], (Folder)path[1]);
     }
 
+    /// <summary>
+    /// Find's a member with the <paramref name="childGuid"/> and it's 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)
     {
         var path = FindMemberPath(childGuid);
@@ -154,6 +172,10 @@ internal class Document : IChangeable, IReadOnlyDocument, IDisposable
         };
     }
 
+    /// <summary>
+    /// Find's 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)
     {
         var list = new List<StructureMember>();

+ 44 - 1
src/PixiEditor.ChangeableDocument/Changeables/Interfaces/IReadOnlyDocument.cs

@@ -1,16 +1,59 @@
-namespace PixiEditor.ChangeableDocument.Changeables.Interfaces;
+using System.Diagnostics.CodeAnalysis;
+
+namespace PixiEditor.ChangeableDocument.Changeables.Interfaces;
 
 public interface IReadOnlyDocument
 {
     IReadOnlyFolder StructureRoot { get; }
+    /// <summary>
+    /// The selection of the document
+    /// </summary>
     IReadOnlySelection Selection { get; }
     VecI Size { get; }
+    /// <summary>
+    /// Is the horizontal symmetry axis enabled (Mirrors top and bottom)
+    /// </summary>
     bool HorizontalSymmetryAxisEnabled { get; }
+    /// <summary>
+    /// Is the vertical symmetry axis enabled (Mirrors left and right)
+    /// </summary>
     bool VerticalSymmetryAxisEnabled { get; }
+    /// <summary>
+    /// The position of the horizontal symmetry axis (Mirrors top and bottom)
+    /// </summary>
     int HorizontalSymmetryAxisY { get; }
+    /// <summary>
+    /// The position of the vertical symmetry axis (Mirrors top and bottom)
+    /// </summary>
     int VerticalSymmetryAxisX { get; }
+    /// <summary>
+    /// Performs the specified action on each readonly member of the document
+    /// </summary>
     void ForEveryReadonlyMember(Action<IReadOnlyStructureMember> action);
+    /// <summary>
+    /// Find's the member with the <paramref name="guid"/> or returns null if not found
+    /// </summary>
+    /// <param name="guid">The <see cref="IReadOnlyStructureMember.GuidValue"/> of the member</param>
     IReadOnlyStructureMember? FindMember(Guid guid);
+    /// <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>
+    /// <returns>True if the member could be found, otherwise false</returns>
+    bool TryFindMember<T>(Guid guid, [NotNullWhen(true)] out T? member) where T : IReadOnlyStructureMember;
+    /// <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>
+    bool TryFindMember(Guid guid, [NotNullWhen(true)] out IReadOnlyStructureMember? member);
+    /// <summary>
+    /// Find's 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>
     IReadOnlyStructureMember FindMemberOrThrow(Guid guid);
     (IReadOnlyStructureMember, IReadOnlyFolder) FindChildAndParentOrThrow(Guid guid);
     IReadOnlyList<IReadOnlyStructureMember> FindMemberPath(Guid guid);

+ 1 - 2
src/PixiEditorPrototype/Models/Rendering/AffectedChunkGatherer.cs

@@ -128,8 +128,7 @@ internal class AffectedChunkGatherer
 
     private void AddAllToMaskPreview(Guid memberGuid)
     {
-        var member = tracker.Document.FindMember(memberGuid);
-        if (member is null)
+        if (!tracker.Document.TryFindMember(memberGuid, out var member))
             return;
         if (member.Mask is not null)
         {