Browse Source

Added field properties

CPKreuz 1 year ago
parent
commit
404f1646fb

+ 11 - 0
src/PixiEditor.ChangeableDocument/Changeables/Graph/FieldInputProperty.cs

@@ -0,0 +1,11 @@
+using PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
+using PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
+
+namespace PixiEditor.ChangeableDocument.Changeables.Graph;
+
+public class FieldInputProperty<T> : InputProperty<Func<IFieldContext, T>>
+{
+    internal FieldInputProperty(Node node, string internalName, string displayName, Func<IFieldContext, T> defaultValue) : base(node, internalName, displayName, defaultValue)
+    {
+    }
+}

+ 11 - 0
src/PixiEditor.ChangeableDocument/Changeables/Graph/FieldOutputProperty.cs

@@ -0,0 +1,11 @@
+using PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
+using PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
+
+namespace PixiEditor.ChangeableDocument.Changeables.Graph;
+
+public class FieldOutputProperty<T> : OutputProperty<Func<IFieldContext, T>>
+{
+    internal FieldOutputProperty(Node node, string internalName, string displayName, Func<IFieldContext, T> defaultValue) : base(node, internalName, displayName, defaultValue)
+    {
+    }
+}

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

@@ -6,24 +6,24 @@ namespace PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
 
 public class CombineColor : Node
 {
-    public OutputProperty<Func<IFieldContext, Color>> Color { get; }
+    public FieldOutputProperty<Color> Color { get; }
     
-    public InputProperty<Func<IFieldContext, double>> R { get; }
+    public FieldInputProperty<double> R { get; }
     
-    public InputProperty<Func<IFieldContext, double>> G { get; }
+    public FieldInputProperty<double> G { get; }
     
-    public InputProperty<Func<IFieldContext, double>> B { get; }
+    public FieldInputProperty<double> B { get; }
     
-    public InputProperty<Func<IFieldContext, double>> A { get; }
+    public FieldInputProperty<double> A { get; }
 
     public CombineColor()
     {
-        Color = CreateOutput<Func<IFieldContext, Color>>(nameof(Color), "COLOR", GetColor);
+        Color = CreateFieldOutput(nameof(Color), "COLOR", GetColor);
         
-        R = CreateInput<Func<IFieldContext, double>>("R", "R", _ => 0);
-        G = CreateInput<Func<IFieldContext, double>>("G", "G", _ => 0);
-        B = CreateInput<Func<IFieldContext, double>>("B", "B", _ => 0);
-        A = CreateInput<Func<IFieldContext, double>>("A", "A", _ => 1);
+        R = CreateFieldInput("R", "R", _ => 0d);
+        G = CreateFieldInput("G", "G", _ => 0d);
+        B = CreateFieldInput("B", "B", _ => 0d);
+        A = CreateFieldInput("A", "A", _ => 1d);
     }
 
     private Color GetColor(IFieldContext ctx)

+ 4 - 4
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/CreateImageNode.cs

@@ -10,14 +10,14 @@ public class CreateImageNode : Node
 {
     public InputProperty<int> Width { get; }
     public InputProperty<int> Height { get; }
-    public InputProperty<Func<IFieldContext, Color>> Color { get; }
+    public FieldInputProperty<Color> Color { get; }
     public OutputProperty<ChunkyImage> Output { get; }
     
     public CreateImageNode() 
     {
-        Width = CreateInput<int>("Width", "WIDTH", 32);
-        Height = CreateInput<int>("Height", "HEIGHT", 32);
-        Color = CreateInput<Func<IFieldContext, Color>>("Color", "COLOR", _ => new Color(0, 0, 0, 255));
+        Width = CreateInput("Width", "WIDTH", 32);
+        Height = CreateInput("Height", "HEIGHT", 32);
+        Color = CreateFieldInput("Color", "COLOR", _ => new Color(0, 0, 0, 255));
         Output = CreateOutput<ChunkyImage>("Output", "OUTPUT", null);
     }
 

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

@@ -6,11 +6,11 @@ namespace PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
 
 public class ImageSpaceNode : Node
 {
-    public OutputProperty<Func<IFieldContext, VecD>> Position { get; }
+    public FieldOutputProperty<VecD> Position { get; }
 
     public ImageSpaceNode()
     {
-        Position = CreateOutput<Func<IFieldContext, VecD>>(nameof(Position), "PIXEL_COORDINATE", ctx => ctx.Position);
+        Position = CreateFieldOutput(nameof(Position), "PIXEL_COORDINATE", ctx => ctx.Position);
     }
     
     protected override ChunkyImage? OnExecute(KeyFrameTime frameTime)

+ 23 - 0
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/Node.cs

@@ -108,6 +108,19 @@ public abstract class Node : IReadOnlyNode, IDisposable
         }
     }
 
+    protected FieldInputProperty<T> CreateFieldInput<T>(string propName, string displayName,
+        Func<IFieldContext, T> defaultFunc)
+    {
+        var property = new FieldInputProperty<T>(this, propName, displayName, defaultFunc);
+        if (InputProperties.Any(x => x.InternalPropertyName == propName))
+        {
+            throw new InvalidOperationException($"Input with name {propName} already exists.");
+        }
+
+        inputs.Add(property);
+        return property;
+    }
+
     protected InputProperty<T> CreateInput<T>(string propName, string displayName, T defaultValue)
     {
         var property = new InputProperty<T>(this, propName, displayName, defaultValue);
@@ -120,6 +133,16 @@ public abstract class Node : IReadOnlyNode, IDisposable
         return property;
     }
 
+    protected FieldOutputProperty<T> CreateFieldOutput<T>(string propName, string displayName,
+        Func<IFieldContext, T> defaultFunc)
+    {
+        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;
+    }
+
     protected OutputProperty<T> CreateOutput<T>(string propName, string displayName, T defaultValue)
     {
         var property = new OutputProperty<T>(this, propName, displayName, defaultValue);

+ 7 - 7
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/SeparateXY.cs

@@ -6,19 +6,19 @@ namespace PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
 
 public class SeparateVecI : Node
 {
-    public InputProperty<Func<IFieldContext, VecD>> Vector { get; }
+    public FieldInputProperty<VecD> Vector { get; }
     
-    public OutputProperty<Func<IFieldContext, double>> X { get; }
+    public FieldOutputProperty<double> X { get; }
     
-    public OutputProperty<Func<IFieldContext, double>> Y { get; }
+    public FieldOutputProperty<double> Y { get; }
 
     public SeparateVecI()
     {
-        X = CreateOutput<Func<IFieldContext, double>>("X", "X", ctx => Vector.Value(ctx).X);
-        Y = CreateOutput<Func<IFieldContext, double>>("Y", "Y", ctx => Vector.Value(ctx).Y);
-        Vector = CreateInput<Func<IFieldContext, VecD>>("Vector", "VECTOR", _ => new VecD(0, 0));
+        X = CreateFieldOutput("X", "X", ctx => Vector.Value(ctx).X);
+        Y = CreateFieldOutput("Y", "Y", ctx => Vector.Value(ctx).Y);
+        Vector = CreateFieldInput("Vector", "VECTOR", _ => new VecD(0, 0));
     }
-    
+
     protected override ChunkyImage? OnExecute(KeyFrameTime frameTime)
     {
         return null;