Browse Source

Fixed folder preview and crash

flabbet 1 year ago
parent
commit
b2d89d8eeb

+ 18 - 2
src/PixiEditor.AvaloniaUI/Models/Rendering/MemberPreviewUpdater.cs

@@ -551,8 +551,19 @@ internal class MemberPreviewUpdater
             var pos = chunk * ChunkResolution.Full.PixelSize();
             // drawing in full res here is kinda slow
             // we could switch to a lower resolution based on (canvas size / preview size) to make it run faster
-            OneOf<Chunk, EmptyChunk> rendered = doc.Renderer.RenderChunk(chunk, ChunkResolution.Full, folder,
-                doc.AnimationHandler.ActiveFrameBindable);
+            var contentNode = folder.Content.Connection?.Node;
+
+            OneOf<Chunk, EmptyChunk> rendered;
+
+            if (contentNode is null)
+            {
+                rendered = new EmptyChunk();
+            }
+            else
+            {
+                rendered = doc.Renderer.RenderChunk(chunk, ChunkResolution.Full, contentNode, doc.AnimationHandler.ActiveFrameBindable);
+            }
+            
             if (rendered.IsT0)
             {
                 memberVM.PreviewSurface.DrawingSurface.Canvas.DrawSurface(rendered.AsT0.Surface.DrawingSurface, pos,
@@ -697,6 +708,11 @@ internal class MemberPreviewUpdater
             }
 
             var nodeVm = doc.StructureHelper.FindNode<INodeHandler>(node.Id);
+            if (nodeVm == null)
+            {
+                return;
+            }
+            
             if (nodeVm.ResultPreview == null)
             {
                 nodeVm.ResultPreview =

+ 1 - 1
src/PixiEditor.ChangeableDocument/ChangeInfos/Structure/MoveStructureMember_ChangeInfo.cs

@@ -1,3 +1,3 @@
 namespace PixiEditor.ChangeableDocument.ChangeInfos.Structure;
 
-public record class MoveStructureMember_ChangeInfo(Guid Id, Guid ParentFromGuid, Guid ParentToGuid, int NewIndex) : IChangeInfo;
+public record class MoveStructureMember_ChangeInfo(Guid Id, Guid ParentFromGuid, Guid ParentToGuid) : IChangeInfo;

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

@@ -26,6 +26,7 @@ public interface IOutputProperty : INodeProperty
 {
     public void ConnectTo(IInputProperty property);
     public void DisconnectFrom(IInputProperty property);
+    IReadOnlyCollection<IInputProperty> Connections { get; }
 }
 
 public interface IInputProperty<T> : IInputProperty, INodeProperty<T>

+ 4 - 2
src/PixiEditor.ChangeableDocument/Changeables/Graph/Interfaces/IReadOnlyFolderNode.cs

@@ -1,6 +1,8 @@
-namespace PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
+using PixiEditor.Numerics;
+
+namespace PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
 
 public interface IReadOnlyFolderNode : IReadOnlyStructureNode
 {
-    
+    InputProperty<Surface?> Content { get; }
 }

+ 5 - 10
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/Node.cs

@@ -14,13 +14,10 @@ public abstract class Node : IReadOnlyNode, IDisposable
     private List<InputProperty> inputs = new();
     private List<OutputProperty> outputs = new();
 
-    private List<IReadOnlyNode> _connectedNodes = new();
-
     public Guid Id { get; internal set; } = Guid.NewGuid();
 
     public IReadOnlyCollection<InputProperty> InputProperties => inputs;
     public IReadOnlyCollection<OutputProperty> OutputProperties => outputs;
-    public IReadOnlyCollection<IReadOnlyNode> ConnectedOutputNodes => _connectedNodes;
     public Surface? CachedResult { get; private set; }
 
     public virtual string InternalName { get; }
@@ -156,9 +153,12 @@ public abstract class Node : IReadOnlyNode, IDisposable
 
             foreach (var outputProperty in node.OutputProperties)
             {
-                foreach (var outputNode in ConnectedOutputNodes)
+                foreach (var connection in outputProperty.Connections)
                 {
-                    queueNodes.Enqueue(outputNode);
+                    if (connection.Connection != null)
+                    {
+                        queueNodes.Enqueue(connection.Node);
+                    }
                 }
             }
         }
@@ -193,8 +193,6 @@ public abstract class Node : IReadOnlyNode, IDisposable
     {
         var property = new FieldOutputProperty<T>(this, propName, displayName, defaultFunc);
         outputs.Add(property);
-        property.Connected += (input, _) => _connectedNodes.Add(input.Node);
-        property.Disconnected += (input, _) => _connectedNodes.Remove(input.Node);
         return property;
     }
 
@@ -202,8 +200,6 @@ public abstract class Node : IReadOnlyNode, IDisposable
     {
         var property = new OutputProperty<T>(this, propName, displayName, defaultValue);
         outputs.Add(property);
-        property.Connected += (input, _) => _connectedNodes.Add(input.Node);
-        property.Disconnected += (input, _) => _connectedNodes.Remove(input.Node);
         return property;
     }
 
@@ -234,7 +230,6 @@ public abstract class Node : IReadOnlyNode, IDisposable
         clone.Id = Guid.NewGuid();
         clone.inputs = new List<InputProperty>();
         clone.outputs = new List<OutputProperty>();
-        clone._connectedNodes = new List<IReadOnlyNode>();
         foreach (var input in inputs)
         {
             var newInput = input.Clone(clone);

+ 11 - 0
src/PixiEditor.ChangeableDocument/Changes/Structure/MoveStructureMember_Change.cs

@@ -49,6 +49,8 @@ internal class MoveStructureMember_Change : Change
             return [];
 
         List<IChangeInfo> changes = new();
+        
+        Guid oldBackgroundId = sourceNode.Background.Node.Id;
 
         InputProperty<Surface?> inputProperty = backgroundInput.Background;
 
@@ -57,10 +59,14 @@ internal class MoveStructureMember_Change : Change
             inputProperty = folder.Content;
         }
 
+        MoveStructureMember_ChangeInfo changeInfo = new(sourceNodeGuid, oldBackgroundId, targetNodeGuid);
+        
         changes.AddRange(NodeOperations.DetachStructureNode(sourceNode));
         changes.AddRange(NodeOperations.AppendMember(inputProperty, sourceNode.Output,
             sourceNode.Background,
             sourceNode.Id));
+        
+        changes.Add(changeInfo);
 
         return changes;
     }
@@ -79,9 +85,14 @@ internal class MoveStructureMember_Change : Change
 
         List<IChangeInfo> changes = new List<IChangeInfo>();
         
+        MoveStructureMember_ChangeInfo changeInfo = new(memberGuid, targetNodeGuid, originalFolderGuid);
+        
         changes.AddRange(NodeOperations.DetachStructureNode(member));
         changes.AddRange(NodeOperations.ConnectStructureNodeProperties(originalOutputConnections,
             originalInputConnections, member));
+        
+        changes.Add(changeInfo);
+        
         return changes;
     }
 }

+ 6 - 1
src/PixiEditor.ChangeableDocument/Rendering/DocumentEvaluator.cs

@@ -78,12 +78,17 @@ public class DocumentEvaluator
             chunk.Surface.DrawingSurface.Canvas.Save();
             chunk.Surface.DrawingSurface.Canvas.Clear();
 
+            int x = 0;
+            int y = 0;
+            
             if (transformedClippingRect is not null)
             {
                 chunk.Surface.DrawingSurface.Canvas.ClipRect((RectD)transformedClippingRect);
+                x = transformedClippingRect.Value.X;
+                y = transformedClippingRect.Value.Y;
             }
             
-            chunk.Surface.DrawingSurface.Canvas.DrawSurface(evaluated.DrawingSurface, transformedClippingRect.Value.X, transformedClippingRect.Value.Y, context.ReplacingPaintWithOpacity);
+            chunk.Surface.DrawingSurface.Canvas.DrawSurface(evaluated.DrawingSurface, x, y, context.ReplacingPaintWithOpacity);
 
             chunk.Surface.DrawingSurface.Canvas.Restore();