Browse Source

Field <-> Global connections

CPKreuz 1 year ago
parent
commit
487de83d26

+ 18 - 0
src/PixiEditor.ChangeableDocument/Changeables/Graph/Context/FieldContext.cs

@@ -0,0 +1,18 @@
+using PixiEditor.Numerics;
+
+namespace PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
+
+public record struct FieldContext(VecD Position, VecI Size, bool HasContext)
+{
+    public FieldContext(VecD position, VecI size) : this(position, size, true) { }
+
+    public static FieldContext NoContext => new(VecD.Zero, VecI.Zero, false);
+
+    public void ThrowOnMissingContext()
+    {
+        if (!HasContext)
+        {
+            throw new NoNodeFieldContextException();
+        }
+    }
+}

+ 7 - 0
src/PixiEditor.ChangeableDocument/Changeables/Graph/Context/NoNodeFieldContextException.cs

@@ -0,0 +1,7 @@
+namespace PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
+
+public class NoNodeFieldContextException : Exception
+{
+    public NoNodeFieldContextException() : base("The node field requires context")
+    { }
+}

+ 22 - 1
src/PixiEditor.ChangeableDocument/Changeables/Graph/InputProperty.cs

@@ -12,7 +12,28 @@ public class InputProperty : IInputProperty
 
 
     public object Value
     public object Value
     {
     {
-        get => Connection != null ? Connection.Value : _internalValue;
+        get
+        {
+            if (Connection == null)
+            {
+                return _internalValue;
+            }
+
+            var connectionValue = Connection.Value;
+            
+            if (!ValueType.IsAssignableTo(typeof(Delegate)) && connectionValue is Delegate connectionField)
+            {
+                return connectionField.DynamicInvoke(FieldContext.NoContext);
+            }
+
+            if (ValueType.IsAssignableTo(typeof(Delegate)) && connectionValue is not Delegate)
+            {
+                Func<FieldContext, object> field = _ => connectionValue;
+                return field;
+            }
+
+            return connectionValue;
+        }
     }
     }
     
     
     public object NonOverridenValue
     public object NonOverridenValue

+ 0 - 5
src/PixiEditor.ChangeableDocument/Changeables/Graph/Interfaces/FieldContext.cs

@@ -1,5 +0,0 @@
-using PixiEditor.Numerics;
-
-namespace PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
-
-public record struct FieldContext(VecD Position, VecI Size);

+ 2 - 0
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/ModifyImageLeftNode.cs

@@ -27,6 +27,8 @@ public class ModifyImageLeftNode : Node
 
 
     private Color GetColor(FieldContext context)
     private Color GetColor(FieldContext context)
     {
     {
+        context.ThrowOnMissingContext();
+        
         if (pixmap == null)
         if (pixmap == null)
             return new Color();
             return new Color();