Browse Source

Some lookup and id changes

Krzysztof Krysiński 1 month ago
parent
commit
dbb6bde565

+ 3 - 1
src/PixiEditor.ChangeableDocument/Changeables/Brushes/BrushData.cs

@@ -8,10 +8,12 @@ public struct BrushData
     public IReadOnlyNodeGraph BrushGraph { get; set; }
     public bool AntiAliasing { get; set; }
     public float StrokeWidth { get; set; }
+    public Guid TargetBrushNodeId { get; set; }
 
 
-    public BrushData(IReadOnlyNodeGraph brushGraph)
+    public BrushData(IReadOnlyNodeGraph brushGraph, Guid targetBrushNodeId)
     {
         BrushGraph = brushGraph;
+        TargetBrushNodeId = targetBrushNodeId;
     }
 }

+ 4 - 3
src/PixiEditor.ChangeableDocument/Changeables/Brushes/BrushEngine.cs

@@ -1,4 +1,5 @@
-using Drawie.Backend.Core;
+using System.Diagnostics;
+using Drawie.Backend.Core;
 using Drawie.Backend.Core.ColorsImpl.Paintables;
 using Drawie.Backend.Core.Numerics;
 using Drawie.Backend.Core.Surfaces;
@@ -76,7 +77,7 @@ public class BrushEngine : IDisposable
             return;
         }
 
-        if (brushData.BrushGraph.AllNodes.FirstOrDefault(x => x is BrushOutputNode) is not BrushOutputNode brushNode)
+        if (brushData.BrushGraph.LookupNode(brushData.TargetBrushNodeId) is not BrushOutputNode brushNode)
         {
             return;
         }
@@ -209,7 +210,7 @@ public class BrushEngine : IDisposable
         var previous = brushNode.Previous.Value;
         while (previous != null)
         {
-            var data = new BrushData(previous)
+            var data = new BrushData(previous, brushData.TargetBrushNodeId)
             {
                 AntiAliasing = brushData.AntiAliasing, StrokeWidth = brushData.StrokeWidth,
             };

+ 1 - 0
src/PixiEditor.ChangeableDocument/Changeables/Graph/Interfaces/IReadOnlyNodeGraph.cs

@@ -10,6 +10,7 @@ public interface IReadOnlyNodeGraph : ICacheable, IDisposable
 {
     public IReadOnlyBlackboard Blackboard { get; }
     public IReadOnlyCollection<IReadOnlyNode> AllNodes { get; }
+    public IReadOnlyNode LookupNode(Guid guid);
     public IReadOnlyNode OutputNode { get; }
     public void AddNode(IReadOnlyNode node);
     public void RemoveNode(IReadOnlyNode node);

+ 6 - 0
src/PixiEditor.ChangeableDocument/Changeables/Graph/NodeGraph.cs

@@ -14,6 +14,7 @@ public class NodeGraph : IReadOnlyNodeGraph
     private readonly List<Node> _nodes = new();
     public IReadOnlyCollection<Node> Nodes => _nodes;
     public IReadOnlyDictionary<Guid, Node> NodeLookup => nodeLookup;
+
     public Node? OutputNode => CustomOutputNode ?? Nodes.OfType<OutputNode>().FirstOrDefault();
     public Node? CustomOutputNode { get; set; }
 
@@ -29,6 +30,11 @@ public class NodeGraph : IReadOnlyNodeGraph
 
     bool isExecuting = false;
 
+    public IReadOnlyNode LookupNode(Guid guid)
+    {
+        return nodeLookup[guid];
+    }
+
     public void AddNode(Node node)
     {
         if (Nodes.Contains(node))

+ 2 - 2
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/Brushes/BrushOutputNode.cs

@@ -154,7 +154,7 @@ public class BrushOutputNode : Node
             pos = new VecI(x, maxSize);
 
             previewEngine.ExecuteBrush(previewChunkyImage,
-                new BrushData(context.Graph) { StrokeWidth = size, AntiAliasing = true },
+                new BrushData(context.Graph, Id) { StrokeWidth = size, AntiAliasing = true },
                 (VecI)pos, context.FrameTime, context.ProcessingColorSpace, context.DesiredSamplingOptions,
                 new PointerInfo(pos, 1, 0, VecD.Zero, new VecD(0, 1)),
                 new KeyboardInfo(),
@@ -169,7 +169,7 @@ public class BrushOutputNode : Node
             pos = new VecD(pos.X, pos.Y + maxSize / 2f);
 
             previewEngine.ExecuteBrush(previewChunkyImage,
-                new BrushData(context.Graph) { StrokeWidth = maxSize, AntiAliasing = true },
+                new BrushData(context.Graph, Id) { StrokeWidth = maxSize, AntiAliasing = true },
                 [(VecI)pos], context.FrameTime, context.ProcessingColorSpace, context.DesiredSamplingOptions,
                 new PointerInfo(pos, pressure, 0, VecD.Zero, vec4D.ZW),
                 new KeyboardInfo(),

+ 3 - 2
src/PixiEditor.ChangeableDocument/Changes/Drawing/LineBasedPen_UpdateableChange.cs

@@ -1,4 +1,5 @@
-using System.Reflection;
+using System.Diagnostics;
+using System.Reflection;
 using ChunkyImageLib.Operations;
 using Drawie.Backend.Core;
 using Drawie.Backend.Core.ColorsImpl;
@@ -97,7 +98,7 @@ internal class LineBasedPen_UpdateableChange : UpdateableChange
     {
         if (brushOutputNode != null)
         {
-            brushData = new BrushData(brushData.BrushGraph) { StrokeWidth = strokeWidth, AntiAliasing = antiAliasing };
+            brushData = new BrushData(brushData.BrushGraph, brushData.TargetBrushNodeId) { StrokeWidth = strokeWidth, AntiAliasing = antiAliasing };
         }
     }
 

+ 8 - 1
src/PixiEditor/Models/BrushEngine/Brush.cs

@@ -4,6 +4,7 @@ using PixiEditor.ChangeableDocument.Changeables.Graph.Nodes.Brushes;
 using PixiEditor.ChangeableDocument.Changeables.Interfaces;
 using PixiEditor.Models.Handlers;
 using PixiEditor.Models.IO;
+using PixiEditor.ViewModels.Document.Nodes.Brushes;
 
 namespace PixiEditor.Models.BrushEngine;
 
@@ -13,7 +14,7 @@ internal class Brush : IBrush
     IReadOnlyDocument IBrush.Document => Document.AccessInternalReadOnlyDocument();
     public string Name { get; set; }
     public string? FilePath { get; }
-    public Guid Id { get; } = Guid.NewGuid();
+    public Guid Id { get; }
 
     public Brush(Uri uri)
     {
@@ -39,6 +40,11 @@ internal class Brush : IBrush
         if (outputNode != null)
         {
             name = outputNode.BrushName.Value;
+            Id = outputNode.Id;
+        }
+        else
+        {
+            Id = Guid.NewGuid();
         }
 
         Name = name;
@@ -53,6 +59,7 @@ internal class Brush : IBrush
         Name = name;
         Document = brushDocument;
         FilePath = brushDocument.FullFilePath;
+        Id = brushDocument.NodeGraphHandler.AllNodes.OfType<BrushOutputNodeViewModel>().FirstOrDefault()?.Id ?? Guid.NewGuid();
     }
 
     public Brush(string name, IDocument brushDocument, Guid id)

+ 1 - 0
src/PixiEditor/Models/DocumentModels/UpdateableChangeExecutors/BrushBasedExecutor.cs

@@ -101,6 +101,7 @@ internal class BrushBasedExecutor : UpdateableChangeExecutor
     protected virtual void EnqueueDrawActions()
     {
         var point = GetStabilizedPoint();
+
         if (handler != null)
         {
             handler.LastAppliedPoint = point;

+ 2 - 1
src/PixiEditor/Models/Serialization/Factories/BrushSerializationFactory.cs

@@ -2,6 +2,7 @@
 using PixiEditor.Models.BrushEngine;
 using PixiEditor.Parser;
 using PixiEditor.ViewModels.Document;
+using PixiEditor.ViewModels.Document.Nodes.Brushes;
 
 namespace PixiEditor.Models.Serialization.Factories;
 
@@ -29,7 +30,7 @@ internal class BrushSerializationFactory : SerializationFactory<byte[], Brush>
             int docLength = extractor.GetInt();
             byte[] docBytes = extractor.GetByteSpan(docLength).ToArray();
             var doc = PixiParser.V5.Deserialize(docBytes).ToDocument();
-            original = new Brush(name, doc, doc.Id);
+            original = new Brush(name, doc, doc.NodeGraph.AllNodes.OfType<BrushOutputNodeViewModel>().FirstOrDefault()?.Id ?? Guid.NewGuid());
 
             return true;
         }

+ 1 - 1
src/PixiEditor/ViewModels/Tools/ToolSettings/Toolbars/BrushToolbar.cs

@@ -49,7 +49,7 @@ internal class BrushToolbar : Toolbar, IBrushToolbar
         }
 
         var pipe = Brush.Document.ShareGraph();
-        var data = new BrushData(pipe.TryAccessData()) { AntiAliasing = AntiAliasing, StrokeWidth = (float)ToolSize };
+        var data = new BrushData(pipe.TryAccessData(), Brush.Id) { AntiAliasing = AntiAliasing, StrokeWidth = (float)ToolSize };
 
         pipe.Dispose();
         return data;