Browse Source

Added a sample image node

CPKreuz 1 year ago
parent
commit
4882b42dc4

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

@@ -5,4 +5,6 @@ namespace PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
 public interface IFieldContext
 {
     public VecD Position { get; }
+    
+    public VecI Size { get; }
 }

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

@@ -8,31 +8,32 @@ namespace PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
 
 public class CreateImageNode : Node
 {
-    public InputProperty<int> Width { get; }
-    public InputProperty<int> Height { get; }
+    public InputProperty<VecI> Size { get; }
+    
     public FieldInputProperty<Color> Color { get; }
+    
     public OutputProperty<ChunkyImage> Output { get; }
     
+    
     public CreateImageNode() 
     {
-        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);
+        Size = CreateInput(nameof(Size), "SIZE", new VecI(32, 32));
+        Color = CreateFieldInput(nameof(Color), "COLOR", _ => new Color(0, 0, 0, 255));
+        Output = CreateOutput<ChunkyImage>(nameof(Output), "OUTPUT", null);
     }
 
     protected override ChunkyImage? OnExecute(KeyFrameTime frameTime)
     {
-        var width = Width.Value;
-        var height = Height.Value;
-        
-        Output.Value = new ChunkyImage(new VecI(Width.Value, Height.Value));
+        var width = Size.Value.X;
+        var height = Size.Value.Y;
+
+        Output.Value = new ChunkyImage(Size.Value);
 
         for (int y = 0; y < width; y++)
         {
             for (int x = 0; x < height; x++)
             {
-                var context = new CreateImageContext(new VecD((double)x / width, (double)y / width));
+                var context = new CreateImageContext(new VecD((double)x / width, (double)y / width), new VecI(width, height));
                 var color = Color.Value(context);
 
                 Output.Value.EnqueueDrawPixel(new VecI(x, y), color, BlendMode.Src);
@@ -44,14 +45,11 @@ public class CreateImageNode : Node
         return Output.Value;
     }
 
-    public override bool Validate()
-    {
-        return Width.Value > 0 && Height.Value > 0;
-    }
+    public override bool Validate() => true;
 
     public override Node CreateCopy() => new CreateImageNode();
 
-    record CreateImageContext(VecD Position) : IFieldContext
+    record CreateImageContext(VecD Position, VecI Size) : IFieldContext
     {
     }
 }

+ 28 - 0
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/ImageSizeNode.cs

@@ -0,0 +1,28 @@
+using PixiEditor.ChangeableDocument.Changeables.Animations;
+using PixiEditor.Numerics;
+
+namespace PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
+
+public class ImageSizeNode : Node
+{
+    public InputProperty<ChunkyImage?> Image { get; }
+    
+    public OutputProperty<VecI> Size { get; }
+    
+    public ImageSizeNode()
+    {
+        Image = CreateInput<ChunkyImage>(nameof(Image), "IMAGE", null);
+        Size = CreateOutput(nameof(Size), "SIZE", new VecI());
+    }
+    
+    protected override ChunkyImage? OnExecute(KeyFrameTime frameTime)
+    {
+        Size.Value = Image.Value?.CommittedSize ?? new VecI();
+
+        return null;
+    }
+
+    public override bool Validate() => Image.Value != null;
+
+    public override Node CreateCopy() => new ImageSizeNode();
+}

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

@@ -7,10 +7,13 @@ namespace PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
 public class ImageSpaceNode : Node
 {
     public FieldOutputProperty<VecD> Position { get; }
+    
+    public FieldOutputProperty<VecI> Size { get; }
 
     public ImageSpaceNode()
     {
         Position = CreateFieldOutput(nameof(Position), "PIXEL_COORDINATE", ctx => ctx.Position);
+        Size = CreateFieldOutput(nameof(Size), "SIZE", ctx => ctx.Size);
     }
     
     protected override ChunkyImage? OnExecute(KeyFrameTime frameTime)

+ 45 - 0
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/SampleImageNode.cs

@@ -0,0 +1,45 @@
+using PixiEditor.ChangeableDocument.Changeables.Animations;
+using PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
+using PixiEditor.DrawingApi.Core.ColorsImpl;
+using PixiEditor.Numerics;
+
+namespace PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
+
+public class SampleImageNode : Node
+{
+    public InputProperty<ChunkyImage?> Image { get; }
+    
+    public FieldInputProperty<VecD> Coordinate { get; }
+    
+    public FieldOutputProperty<Color> Color { get; }
+    
+    public SampleImageNode()
+    {
+        Image = CreateInput<ChunkyImage>(nameof(Image), "IMAGE", null);
+        Coordinate = CreateFieldInput(nameof(Coordinate), "COORDINATE", ctx => ctx.Position);
+        Color = CreateFieldOutput(nameof(Color), "COLOR", GetColor);
+    }
+
+    private Color GetColor(IFieldContext context)
+    {
+        if (Image.Value is not { } image)
+        {
+            return new Color();
+        }
+        
+        var pos = new VecI(
+            (int)(context.Position.X * context.Size.X),
+            (int)(context.Position.Y * context.Size.Y));
+        
+        return image.GetCommittedPixel(pos);
+    }
+
+    protected override ChunkyImage? OnExecute(KeyFrameTime frameTime)
+    {
+        return Image.Value;
+    }
+
+    public override bool Validate() => Image.Value != null;
+
+    public override Node CreateCopy() => new SampleImageNode();
+}

+ 36 - 0
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/SeparateColorNode.cs

@@ -0,0 +1,36 @@
+using PixiEditor.ChangeableDocument.Changeables.Animations;
+using PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
+using PixiEditor.DrawingApi.Core.ColorsImpl;
+
+namespace PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
+
+public class SeparateColorNode : Node
+{
+    public FieldInputProperty<Color> Color { get; }
+    
+    public FieldOutputProperty<double> R { get; }
+    
+    public FieldOutputProperty<double> G { get; }
+    
+    public FieldOutputProperty<double> B { get; }
+    
+    public FieldOutputProperty<double> A { get; }
+
+    public SeparateColorNode()
+    {
+        Color = CreateFieldInput(nameof(Color), "COLOR", _ => new Color());
+        R = CreateFieldOutput(nameof(R), "R", ctx => Color.Value(ctx).R / 255d);
+        G = CreateFieldOutput(nameof(G), "G", ctx => Color.Value(ctx).G / 255d);
+        B = CreateFieldOutput(nameof(B), "B", ctx => Color.Value(ctx).B / 255d);
+        A = CreateFieldOutput(nameof(A), "A", ctx => Color.Value(ctx).A / 255d);
+    }
+
+    protected override ChunkyImage? OnExecute(KeyFrameTime frameTime)
+    {
+        return null;
+    }
+
+    public override bool Validate() => true;
+
+    public override Node CreateCopy() => new SeparateColorNode();
+}