瀏覽代碼

Expose variable wip

flabbet 1 月之前
父節點
當前提交
d3eb2dcc17

+ 0 - 10
src/ChunkyImageLib/ChunkyImage.cs

@@ -758,16 +758,6 @@ public class ChunkyImage : IReadOnlyChunkyImage, IDisposable, ICloneable, ICache
             EnqueueOperation(operation);
         }
     }
-    
-    public void EnqueueDrawPaintable(Action<Canvas> customDrawPaintable, RectD pathBounds)
-    {
-        lock (lockObject)
-        {
-            ThrowIfDisposed();
-            CustomDrawPaintableOperation operation = new(customDrawPaintable, pathBounds);
-            EnqueueOperation(operation);
-        }
-    }
 
     /// <exception cref="ObjectDisposedException">This image is disposed</exception>
     public void EnqueueDrawEllipse(RectD location, Paintable? strokeColor, Paintable? fillColor, float strokeWidth,

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

@@ -1,5 +1,6 @@
 using PixiEditor.ChangeableDocument.Rendering;
 using Drawie.Backend.Core;
+using PixiEditor.ChangeableDocument.Changeables.Graph.Nodes.Workspace;
 using PixiEditor.ChangeableDocument.ChangeInfos.NodeGraph;
 using PixiEditor.Common;
 
@@ -19,4 +20,5 @@ public interface IReadOnlyNodeGraph : ICacheable, IDisposable
     Queue<IReadOnlyNode> CalculateExecutionQueue(IReadOnlyNode endNode);
     public IReadOnlyNodeGraph Clone();
     public event Action<NodeOutputsChanged_ChangeInfo> NodeOutputsChanged;
+    public void Execute(IEnumerable<IReadOnlyNode> exposeVariableNodes, RenderContext context);
 }

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

@@ -184,6 +184,39 @@ public class NodeGraph : IReadOnlyNodeGraph
         Execute(OutputNode, context);
     }
 
+    public void Execute(IEnumerable<IReadOnlyNode> exposeVariableNodes, RenderContext context)
+    {
+        isExecuting = true;
+        if (!CanExecute()) return;
+
+        HashSet<IReadOnlyNode> executedNodes = new();
+        foreach (var exposeVariableNode in exposeVariableNodes)
+        {
+            var queue = CalculateExecutionQueueInternal(exposeVariableNode);
+
+            foreach (var node in queue)
+            {
+                if (!executedNodes.Add(node)) continue;
+
+                lock (node)
+                {
+                    if (node is Node typedNode)
+                    {
+                        if (typedNode.IsDisposed) continue;
+
+                        typedNode.ExecuteInternal(context);
+                    }
+                    else
+                    {
+                        node.Execute(context);
+                    }
+                }
+            }
+        }
+
+        isExecuting = false;
+    }
+
     public void Execute(IReadOnlyNode end, RenderContext context)
     {
         //if (isExecuting) return;

+ 61 - 23
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/NestedDocumentNode.cs

@@ -6,6 +6,7 @@ using Drawie.Numerics;
 using PixiEditor.ChangeableDocument.Changeables.Animations;
 using PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
 using PixiEditor.ChangeableDocument.Changeables.Graph.Nodes.Brushes;
+using PixiEditor.ChangeableDocument.Changeables.Graph.Nodes.Workspace;
 using PixiEditor.ChangeableDocument.Changeables.Interfaces;
 using PixiEditor.ChangeableDocument.ChangeInfos.NodeGraph;
 using PixiEditor.ChangeableDocument.Rendering;
@@ -20,7 +21,7 @@ public class NestedDocumentNode : LayerNode, IInputDependentOutputs, ITransforma
     public InputProperty<DocumentReference> NestedDocument { get; }
 
     public InputProperty<bool> BilinearSampling { get; }
-    
+
     public OutputProperty<IReadOnlyNodeGraph> Graph { get; }
 
     public Matrix3X3 TransformationMatrix { get; set; } = Matrix3X3.Identity;
@@ -34,6 +35,7 @@ public class NestedDocumentNode : LayerNode, IInputDependentOutputs, ITransforma
     private Texture? dummyTexture;
 
     private string[] builtInOutputs;
+    private string[] builtInInputs;
 
     public NestedDocumentNode()
     {
@@ -45,6 +47,7 @@ public class NestedDocumentNode : LayerNode, IInputDependentOutputs, ITransforma
         AllowHighDpiRendering = true;
 
         builtInOutputs = OutputProperties.Select(x => x.InternalPropertyName).ToArray();
+        builtInInputs = InputProperties.Select(x => x.InternalPropertyName).ToArray();
     }
 
     protected override int GetContentCacheHash()
@@ -69,20 +72,31 @@ public class NestedDocumentNode : LayerNode, IInputDependentOutputs, ITransforma
 
         var brushOutput = document.DocumentInstance.NodeGraph.AllNodes.OfType<BrushOutputNode>().FirstOrDefault();
 
-        if (brushOutput is null)
-            return;
 
-        foreach (var input in brushOutput.InputProperties)
+        if (brushOutput != null)
         {
-            if (input.InternalPropertyName == Output.InternalPropertyName)
-                continue;
+            foreach (var input in brushOutput.InputProperties)
+            {
+                if (input.InternalPropertyName == Output.InternalPropertyName)
+                    continue;
+
+                if (OutputProperties.Any(x =>
+                        x.InternalPropertyName == input.InternalPropertyName && x.ValueType == input.ValueType))
+                    continue;
+
+                AddOutputProperty(new OutputProperty(this, input.InternalPropertyName, input.DisplayName, input.Value,
+                    input.ValueType));
+            }
+        }
 
-            if (OutputProperties.Any(x =>
-                    x.InternalPropertyName == input.InternalPropertyName && x.ValueType == input.ValueType))
+        foreach (var variable in document.DocumentInstance.NodeGraph.Blackboard.Variables)
+        {
+            if (InputProperties.Any(x =>
+                    x.InternalPropertyName == variable.Key && x.ValueType == variable.Value.Type))
                 continue;
 
-            AddOutputProperty(new OutputProperty(this, input.InternalPropertyName, input.DisplayName, input.Value,
-                input.ValueType));
+            AddInputProperty(new InputProperty(this, variable.Key, variable.Key, variable.Value.Value,
+                variable.Value.Type));
         }
 
         for (int i = OutputProperties.Count - 1; i >= 0; i--)
@@ -91,11 +105,21 @@ public class NestedDocumentNode : LayerNode, IInputDependentOutputs, ITransforma
             if (builtInOutputs.Contains(output.InternalPropertyName))
                 continue;
 
-            var correspondingInput = brushOutput.InputProperties.FirstOrDefault(x =>
-                x.InternalPropertyName == output.InternalPropertyName && x.ValueType == output.ValueType);
+            bool shouldRemove = false;
+            if (brushOutput != null)
+            {
+                var correspondingInput = brushOutput.InputProperties.FirstOrDefault(x =>
+                    x.InternalPropertyName == output.InternalPropertyName && x.ValueType == output.ValueType);
+                shouldRemove = correspondingInput is null;
+            }
 
-            if (correspondingInput is null)
+            if (!shouldRemove)
             {
+                bool variableExists = document.DocumentInstance.NodeGraph.Blackboard.Variables
+                    .Any(x => x.Key == output.InternalPropertyName && x.Value.Type == output.ValueType);
+                if (variableExists)
+                    continue;
+
                 RemoveOutputProperty(output);
             }
         }
@@ -133,28 +157,42 @@ public class NestedDocumentNode : LayerNode, IInputDependentOutputs, ITransforma
             clonedContext.ProcessingColorSpace = Instance?.ProcessingColorSpace;
             clonedContext.VisibleDocumentRegion = null;
             clonedContext.RenderSurface =
-                (dummyTexture ??= Texture.ForProcessing(new VecI(1, 1), context.ProcessingColorSpace)).DrawingSurface.Canvas;
-
-            var outputNode = Instance?.NodeGraph.AllNodes.OfType<BrushOutputNode>().FirstOrDefault() ??
-                             Instance?.NodeGraph.OutputNode;
+                (dummyTexture ??= Texture.ForProcessing(new VecI(1, 1), context.ProcessingColorSpace)).DrawingSurface
+                .Canvas;
 
-            Instance?.NodeGraph.Execute(outputNode, clonedContext);
+            var exposeVariableNodes = Instance?.NodeGraph.AllNodes
+                .OfType<ExposeVariableNode>().ToArray();
+            
+            Instance?.NodeGraph.Execute(exposeVariableNodes, clonedContext);
 
             foreach (var output in OutputProperties)
             {
                 if (output.InternalPropertyName == Output.InternalPropertyName)
                     continue;
 
-                var correspondingInput = outputNode.InputProperties.FirstOrDefault(x =>
-                    x.InternalPropertyName == output.InternalPropertyName && x.ValueType == output.ValueType);
+                var correspondingExposeNode = exposeVariableNodes?
+                    .FirstOrDefault(x => x.Name.Value == output.InternalPropertyName &&
+                                         x.Value.ValueType == output.ValueType);
 
-                if (correspondingInput is null)
+                if (correspondingExposeNode is null)
                     continue;
 
-                output.Value = correspondingInput.Value;
+                output.Value = correspondingExposeNode.Value;
             }
         }
         
+        foreach (var blackboardVariable in Instance?.NodeGraph.Blackboard.Variables)
+        {
+            var input = InputProperties.FirstOrDefault(x =>
+                x.InternalPropertyName == blackboardVariable.Key &&
+                x.ValueType == blackboardVariable.Value.Type);
+                
+            if (input is null || blackboardVariable.Value is not Variable variable)
+                continue;
+                
+            variable.Value = input.Value;
+        }
+
         Graph.Value = Instance.NodeGraph;
     }
 
@@ -340,7 +378,7 @@ public class NestedDocumentNode : LayerNode, IInputDependentOutputs, ITransforma
     {
         foreach (var output in OutputProperties)
         {
-            if (output.InternalPropertyName == Output.InternalPropertyName)
+            if (builtInOutputs.Contains(output.InternalPropertyName))
                 continue;
 
             if (output.Connections.Count > 0)

+ 26 - 0
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/Workspace/ExposeVariableNode.cs

@@ -0,0 +1,26 @@
+using PixiEditor.ChangeableDocument.Rendering;
+
+namespace PixiEditor.ChangeableDocument.Changeables.Graph.Nodes.Workspace;
+
+[NodeInfo("ExposeVariable")]
+public class ExposeVariableNode : Node
+{
+    public InputProperty<string> Name { get; }
+    public InputProperty<object?> Value { get; }
+
+    public ExposeVariableNode()
+    {
+        Name = CreateInput<string>("Name", "NAME", "");
+        Value = CreateInput<object?>("Input", "INPUT", null);
+    }
+
+    protected override void OnExecute(RenderContext context)
+    {
+        // no op
+    }
+
+    public override Node CreateCopy()
+    {
+        return new ExposeVariableNode();
+    }
+}

+ 10 - 0
src/PixiEditor/ViewModels/Document/Nodes/Workspace/ExposeVariableNodeViewModel.cs

@@ -0,0 +1,10 @@
+using PixiEditor.ChangeableDocument.Changeables.Graph.Nodes.Workspace;
+using PixiEditor.ViewModels.Nodes;
+
+namespace PixiEditor.ViewModels.Document.Nodes.Workspace;
+
+[NodeViewModel("EXPOSE_VARIABLE_NODE", "WORKSPACE", null)]
+internal class ExposeVariableNodeViewModel : NodeViewModel<ExposeVariableNode>
+{
+    
+}