Browse Source

Fixed Half4 -> Float1 crash

flabbet 11 months ago
parent
commit
908f137f75

+ 11 - 1
src/PixiEditor.ChangeableDocument/Changeables/Graph/FuncInputProperty.cs

@@ -57,7 +57,7 @@ public class FuncInputProperty<T> : InputProperty<Func<FuncContext, T>>, IFuncIn
                 }
                 else if (sourceObj is Expression expression)
                 {
-                    ((ShaderExpressionVariable)toReturn).OverrideExpression = expression;
+                    ((ShaderExpressionVariable)toReturn).OverrideExpression = Adjust(expression, toReturn);
                 }
 
                 return (T)toReturn;
@@ -67,6 +67,16 @@ public class FuncInputProperty<T> : InputProperty<Func<FuncContext, T>>, IFuncIn
         };
         return func;
     }
+    
+    private Expression Adjust(Expression expression, object toReturn)
+    {
+        if (expression is IMultiValueVariable multiVal && toReturn is not IMultiValueVariable)
+        {
+            return multiVal.GetValueAt(0);
+        }
+
+        return expression;
+    }
 
     object? IFuncInputProperty.GetFuncConstantValue() => constantNonOverrideValue;
 

+ 12 - 2
src/PixiEditor.DrawingApi.Core/Shaders/Generation/Expressions/Float2.cs

@@ -1,8 +1,9 @@
-using PixiEditor.Numerics;
+using System;
+using PixiEditor.Numerics;
 
 namespace PixiEditor.DrawingApi.Core.Shaders.Generation.Expressions;
 
-public class Float2(string name) : ShaderExpressionVariable<VecD>(name)
+public class Float2(string name) : ShaderExpressionVariable<VecD>(name), IMultiValueVariable
 {
     private Expression? _overrideExpression;
     public override string ConstantValueString
@@ -44,4 +45,13 @@ public class Float2(string name) : ShaderExpressionVariable<VecD>(name)
     
     public static implicit operator Float2(VecD value) => new Float2("") { ConstantValue = value };
     public static explicit operator VecD(Float2 value) => value.ConstantValue;
+    public ShaderExpressionVariable GetValueAt(int index)
+    {
+        return index switch
+        {
+            0 => X,
+            1 => Y,
+            _ => throw new IndexOutOfRangeException()
+        };
+    }
 }

+ 15 - 2
src/PixiEditor.DrawingApi.Core/Shaders/Generation/Expressions/Half4.cs

@@ -1,8 +1,9 @@
-using PixiEditor.DrawingApi.Core.ColorsImpl;
+using System;
+using PixiEditor.DrawingApi.Core.ColorsImpl;
 
 namespace PixiEditor.DrawingApi.Core.Shaders.Generation.Expressions;
 
-public class Half4(string name) : ShaderExpressionVariable<Color>(name)
+public class Half4(string name) : ShaderExpressionVariable<Color>(name), IMultiValueVariable
 {
     private Expression? _overrideExpression;
     public override string ConstantValueString => $"half4({ConstantValue.R}, {ConstantValue.G}, {ConstantValue.B}, {ConstantValue.A})";
@@ -23,4 +24,16 @@ public class Half4(string name) : ShaderExpressionVariable<Color>(name)
             _overrideExpression = value;
         }
     }
+
+    public ShaderExpressionVariable GetValueAt(int index)
+    {
+        return index switch
+        {
+            0 => R,
+            1 => G,
+            2 => B,
+            3 => A,
+            _ => throw new IndexOutOfRangeException()
+        };
+    }
 }

+ 13 - 2
src/PixiEditor.DrawingApi.Core/Shaders/Generation/Expressions/Int2.cs

@@ -1,8 +1,9 @@
-using PixiEditor.Numerics;
+using System;
+using PixiEditor.Numerics;
 
 namespace PixiEditor.DrawingApi.Core.Shaders.Generation.Expressions;
 
-public class Int2(string name) : ShaderExpressionVariable<VecI>(name)
+public class Int2(string name) : ShaderExpressionVariable<VecI>(name), IMultiValueVariable
 {
     private Expression? _overrideExpression;
     public override string ConstantValueString => $"int2({ConstantValue.X}, {ConstantValue.Y})";
@@ -30,4 +31,14 @@ public class Int2(string name) : ShaderExpressionVariable<VecI>(name)
             Y.OverrideExpression = value;
         }
     }
+
+    public ShaderExpressionVariable GetValueAt(int index)
+    {
+        return index switch
+        {
+            0 => X,
+            1 => Y,
+            _ => throw new IndexOutOfRangeException()
+        };
+    }
 }

+ 8 - 0
src/PixiEditor.DrawingApi.Core/Shaders/Generation/IMultiValueVariable.cs

@@ -0,0 +1,8 @@
+using PixiEditor.DrawingApi.Core.Shaders.Generation.Expressions;
+
+namespace PixiEditor.DrawingApi.Core.Shaders.Generation;
+
+public interface IMultiValueVariable
+{
+    public ShaderExpressionVariable GetValueAt(int index);
+}