This commit is contained in:
2026-04-15 22:22:56 -04:00
commit 5906f248f4
90 changed files with 6345 additions and 0 deletions

View File

@@ -0,0 +1,77 @@
using System;
using System.Collections.Generic;
using Godot;
using Godot.Collections;
namespace SJK.GodotHelpers;
public static class VariantUtils
{
public static Variant SafeToVariant(object value)
{
return value switch
{
null => new Variant(),
bool b => Variant.From(b),
int i => Variant.From(i),
long l => Variant.From((int)l), // Godot Variant only supports 32-bit int
float f => Variant.From(f),
double d => Variant.From((float)d),
string s => Variant.From(s),
Vector2 v2 => Variant.From(v2),
Vector2I v2i => Variant.From(v2i),
Vector3 v3 => Variant.From(v3),
Vector3I v3i => Variant.From(v3i),
Vector4 v4 => Variant.From(v4),
Vector4I v4i => Variant.From(v4i),
Rect2 rect2 => Variant.From(rect2),
Rect2I rect2i => Variant.From(rect2i),
Quaternion q => Variant.From(q),
Basis basis => Variant.From(basis),
Transform2D t2d => Variant.From(t2d),
Transform3D t3d => Variant.From(t3d),
Color color => Variant.From(color),
Plane plane => Variant.From(plane),
Aabb aabb => Variant.From(aabb),
GodotObject go => Variant.From(go),
byte[] bytes => Variant.From(bytes),
StringName sn => Variant.From(sn),
NodePath np => Variant.From(np),
Callable call => Variant.From(call),
Signal sig => Variant.From(sig),
Dictionary dict => Variant.From(dict),
Godot.Collections.Array array => Variant.From(array),
_ => throw new InvalidCastException($"Unsupported type '{value?.GetType().FullName}' for Variant conversion.")
};
}
public static Type GetSystemType(this Variant.Type type) => type switch
{
Variant.Type.Nil => typeof(object),
Variant.Type.Bool => typeof(bool),
Variant.Type.Int => typeof(int),
Variant.Type.Float => typeof(float),
Variant.Type.String => typeof(string),
Variant.Type.Vector2 => typeof(Vector2),
Variant.Type.Vector2I => typeof(Vector2I),
Variant.Type.Rect2 => typeof(Rect2),
Variant.Type.Rect2I => typeof(Rect2I),
Variant.Type.Vector3 => typeof(Vector3),
Variant.Type.Vector3I => typeof(Vector3I),
Variant.Type.Vector4 => typeof(Vector4),
Variant.Type.Vector4I => typeof(Vector4I),
Variant.Type.Transform2D => typeof(Transform2D),
Variant.Type.Transform3D => typeof(Transform3D),
Variant.Type.Basis => typeof(Basis),
Variant.Type.Quaternion => typeof(Quaternion),
Variant.Type.Aabb => typeof(Aabb),
Variant.Type.Color => typeof(Color),
Variant.Type.Plane => typeof(Plane),
Variant.Type.StringName => typeof(StringName),
Variant.Type.NodePath => typeof(NodePath),
Variant.Type.Rid => typeof(Rid),
Variant.Type.Object => typeof(GodotObject),
Variant.Type.Callable => typeof(Callable),
Variant.Type.Signal => typeof(Signal),
Variant.Type.Dictionary => typeof(Godot.Collections.Dictionary),
Variant.Type.Array => typeof(Godot.Collections.Array),
_ => typeof(object)
};
}