mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 21:01:52 +00:00
Generate ToString for vectors, generate Boolean-Opeartors for boolVectors
This commit is contained in:
@@ -20,6 +20,8 @@ struct VectorAttribute<T, ComponentCount> : Attribute, IComptimeTypeApply where
|
|||||||
GenerateEqualityOperators(type);
|
GenerateEqualityOperators(type);
|
||||||
|
|
||||||
GenerateArrayAccess(type);
|
GenerateArrayAccess(type);
|
||||||
|
|
||||||
|
GenerateToString(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Comptime]
|
[Comptime]
|
||||||
@@ -307,10 +309,36 @@ struct VectorAttribute<T, ComponentCount> : Attribute, IComptimeTypeApply where
|
|||||||
(&X)[index] = value;
|
(&X)[index] = value;
|
||||||
}}
|
}}
|
||||||
}}
|
}}
|
||||||
|
|
||||||
""";
|
""";
|
||||||
|
|
||||||
Compiler.EmitTypeBody(type, arrayAccess);
|
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)]
|
[AttributeUsage(.Struct | .Class)]
|
||||||
@@ -438,3 +466,114 @@ struct VectorMathAttribute<T, ComponentCount> : Attribute, IComptimeTypeApply wh
|
|||||||
EmitOperatorOverload(componentTypeName, typeName, resultArguments);
|
EmitOperatorOverload(componentTypeName, typeName, resultArguments);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[AttributeUsage(.Struct | .Class)]
|
||||||
|
struct VectorConditionalsAttribute<T, ComponentCount> : 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ struct bool2
|
|||||||
[BonTarget, CRepr]
|
[BonTarget, CRepr]
|
||||||
[Vector<bool, 3>]
|
[Vector<bool, 3>]
|
||||||
[SwizzleVector(3, "GlitchyEngine.Math.bool")]
|
[SwizzleVector(3, "GlitchyEngine.Math.bool")]
|
||||||
|
[VectorConditionals<bool, 3>]
|
||||||
struct bool3
|
struct bool3
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -22,6 +23,7 @@ struct bool3
|
|||||||
[BonTarget, CRepr]
|
[BonTarget, CRepr]
|
||||||
[Vector<bool, 4>]
|
[Vector<bool, 4>]
|
||||||
[SwizzleVector(4, "GlitchyEngine.Math.bool")]
|
[SwizzleVector(4, "GlitchyEngine.Math.bool")]
|
||||||
|
[VectorConditionals<bool, 4>]
|
||||||
struct bool4
|
struct bool4
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user