diff --git a/GlitchyEngine/src/Math/VectorAttribute.bf b/GlitchyEngine/src/Math/VectorAttribute.bf index 7c0503d..b7a0b62 100644 --- a/GlitchyEngine/src/Math/VectorAttribute.bf +++ b/GlitchyEngine/src/Math/VectorAttribute.bf @@ -20,6 +20,8 @@ struct VectorAttribute : Attribute, IComptimeTypeApply where GenerateEqualityOperators(type); GenerateArrayAccess(type); + + GenerateToString(type); } [Comptime] @@ -307,10 +309,36 @@ struct VectorAttribute : Attribute, IComptimeTypeApply where (&X)[index] = value; }} }} + """; Compiler.EmitTypeBody(type, arrayAccess); } + + [Comptime] + private static void GenerateToString(Type type) + { + String toString = scope .(); + + for (int i < ComponentCount) + { + toString.AppendF($""" + \toutString.Append(\"{(i == 0 ? "(" : ", ")}{ComponentNames[i]}: \"); + \t{ComponentNames[i]}.ToString(outString); + + """); + } + + String func = scope $""" + public void ToString(String outString) + {{ + {toString} + outString.Append(')'); + }} + + """; + Compiler.EmitTypeBody(type, func); + } } [AttributeUsage(.Struct | .Class)] @@ -438,3 +466,114 @@ struct VectorMathAttribute : Attribute, IComptimeTypeApply wh EmitOperatorOverload(componentTypeName, typeName, resultArguments); } } + +[AttributeUsage(.Struct | .Class)] +struct VectorConditionalsAttribute : Attribute, IComptimeTypeApply where ComponentCount : const int +{ + public const String[4] ComponentNames = .("X", "Y", "Z", "W"); + + [Comptime] + public void ApplyToType(Type type) + { + GenerateUnaryOperatorOverloads(type); + + GenerateOperatorOverloads(type, "&&"); + GenerateOperatorOverloads(type, "||"); + } + + [Comptime] + public static void GenerateUnaryOperatorOverloads(Type type) + { + String typeName = type.GetName(.. scope String()); + /* + + String unaryAdd = scope $""" + public static {typeName} operator+({typeName} value) => value; + + """; + + Compiler.EmitTypeBody(type, unaryAdd);*/ + + + + String resultArguments = scope .(); + + for (int i < ComponentCount) + { + if (i != 0) + resultArguments.Append(", "); + + resultArguments.AppendF($"!value.{ComponentNames[i]}"); + } + + String unaryMinus = scope $""" + public static {typeName} operator!({typeName} value) + {{ + return {typeName}({resultArguments}); + }} + + """; + + Compiler.EmitTypeBody(type, unaryMinus); + } + + [Comptime] + public static void GenerateOperatorOverloads(Type type, String op) + { + String typeName = type.GetName(.. scope String()); + String componentTypeName = typeof(T).GetName(.. scope String()); + + [Comptime] + void EmitOperatorOverload(String leftType, String rightType, String resultArguments) + { + String func = scope $""" + public static {typeName} operator{op}({leftType} left, {rightType} right) + {{ + return {typeName}({resultArguments}); + }} + + """; + + Compiler.EmitTypeBody(type, func); + } + + // Vector + Vector + String resultArguments = scope .(); + + for (int i < ComponentCount) + { + if (i != 0) + resultArguments.Append(", "); + + resultArguments.AppendF($"left.{ComponentNames[i]} {op} right.{ComponentNames[i]}"); + } + + EmitOperatorOverload(typeName, typeName, resultArguments); + + // Vector + Scalar + resultArguments.Clear(); + + for (int i < ComponentCount) + { + if (i != 0) + resultArguments.Append(", "); + + resultArguments.AppendF($"left.{ComponentNames[i]} {op} right"); + } + + EmitOperatorOverload(typeName, componentTypeName, resultArguments); + + // Scalar + Vector + resultArguments.Clear(); + + for (int i < ComponentCount) + { + if (i != 0) + resultArguments.Append(", "); + + resultArguments.AppendF($"left {op} right.{ComponentNames[i]}"); + } + + EmitOperatorOverload(componentTypeName, typeName, resultArguments); + } +} diff --git a/GlitchyEngine/src/Math/boolVector.bf b/GlitchyEngine/src/Math/boolVector.bf index 54e49f0..54f07f8 100644 --- a/GlitchyEngine/src/Math/boolVector.bf +++ b/GlitchyEngine/src/Math/boolVector.bf @@ -14,6 +14,7 @@ struct bool2 [BonTarget, CRepr] [Vector] [SwizzleVector(3, "GlitchyEngine.Math.bool")] +[VectorConditionals] struct bool3 { @@ -22,6 +23,7 @@ struct bool3 [BonTarget, CRepr] [Vector] [SwizzleVector(4, "GlitchyEngine.Math.bool")] +[VectorConditionals] struct bool4 {