Added Core Functionality

Added core functionality that was from the old Godot.Sharp.Extra library.  Not all functions, only functions that were considered worth porting.
This commit is contained in:
Mario Steele 2025-09-26 20:12:37 -05:00
parent 44c3d49aad
commit b4a2ce40c1
6 changed files with 132 additions and 0 deletions

View file

@ -0,0 +1,6 @@
namespace Godot.Sharp.Extended.Attributes;
[System.AttributeUsage(System.AttributeTargets.Property | System.AttributeTargets.Field)]
public class NodeBindAttribute : Attribute
{
}

View file

@ -0,0 +1,14 @@
namespace Godot.Sharp.Extended.Attributes;
[System.AttributeUsage(System.AttributeTargets.Field)]
public class NodePropBindAttribute : System.Attribute
{
public string TargetNodeName { get; private set; }
public string GodotPropertyName { get; private set; }
public NodePropBindAttribute(string targetNodeName, string godotPropertyName)
{
TargetNodeName = targetNodeName;
GodotPropertyName = godotPropertyName;
}
}

View file

@ -0,0 +1,11 @@
namespace Godot.Sharp.Extended.Attributes;
[System.AttributeUsage(System.AttributeTargets.Property | System.AttributeTargets.Field)]
public class ResourcePathAttribute : System.Attribute
{
public string Path { get; private set; }
public ResourcePathAttribute(string path) {
Path = path;
}
}

View file

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PackageId>Godot.Sharp.Extended</PackageId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="GodotSharp" Version="4.4.0" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,22 @@
namespace Godot.Sharp.Extended.Tools;
public static partial class MathFunctionExtensions
{
public static bool InRange(this int @this, int min, int max) => @this >= min && @this <= max;
public static bool InRange(this float @this, float min, float max) => @this >= min && @this <= max;
public static bool InRange(this double @this, double min, double max) => @this >= min && @this <= max;
public static bool InRange(this Vector2 @this, Vector2 min, Vector2 max) =>
@this.X.InRange(min.X, max.X) && @this.Y.InRange(min.Y, max.Y);
public static bool InRange(this Vector2I @this, Vector2I min, Vector2I max) =>
@this.X.InRange(min.X, max.X) && @this.Y.InRange(min.Y, max.Y);
public static bool InRange(this Vector3 @this, Vector3 min, Vector3 max) =>
@this.X.InRange(min.X, max.X) && @this.Y.InRange(min.Y, max.Y) && @this.Z.InRange(min.Z, max.Z);
public static bool InRange(this Vector3I @this, Vector3I min, Vector3I max) =>
@this.X.InRange(min.X, max.X) && @this.Y.InRange(min.Y, max.Y) && @this.Z.InRange(min.Z, max.Z);
public static bool InRange(this Vector4 @this, Vector4 min, Vector4 max) =>
@this.X.InRange(min.X, max.X) && @this.Y.InRange(min.Y, max.Y) && @this.Z.InRange(min.Z, max.Z) && @this.W.InRange(min.W, max.W);
public static bool InRange(this Vector4I @this, Vector4I min, Vector4I max) =>
@this.X.InRange(min.X, max.X) && @this.Y.InRange(min.Y, max.Y) && @this.Z.InRange(min.Z, max.Z) && @this.W.InRange(min.W, max.W);
public static bool InRange(this Quaternion @this, Quaternion min, Quaternion max) =>
@this.X.InRange(min.X, max.X) && @this.Y.InRange(min.Y, max.Y) && @this.Z.InRange(min.Z, max.Z) && @this.W.InRange(min.W, max.W);
}

View file

@ -0,0 +1,65 @@
using Godot.Collections;
namespace Godot.Sharp.Extended.Tools;
public static class NodeExtensions
{
public static void AddToGroup(this Node node) => node.AddToGroup(node.GetType().Name);
public static T GetNode<T>(this Node node) where T : Node => node.GetNode<T>(typeof(T).Name);
public static Array<T> GetChildrenOfType<T>(this Node node) where T : Node
{
var children = node.GetChildren();
return new Array<T>(children.OfType<T>().ToArray());
}
public static T? GetFirstChildOfType<T>(this Node node) where T : Node => GetChildrenOfType<T>(node).FirstOrDefault();
public static void RemoveAllChildren(this Node node)
{
foreach (var child in node.GetChildren())
node.RemoveChild(child);
}
public static void RemoveAndQueueFreeChildren(this Node node)
{
foreach (var child in node.GetChildren())
{
node.RemoveChild(child);
child.QueueFree();
}
}
public static void QueueFreeChildren(this Node node)
{
foreach (var child in node.GetChildren())
node.QueueFree();
}
public static T? GetAncestor<T>(this Node node) where T : Node
{
Node current = node;
Node root = node.GetTree().Root;
while (current != root && !(current is T))
current = current.GetParent();
return current as T;
}
public static Node? GetLastChild(this Node node)
{
var count = node.GetChildCount();
if (count == 0) return null;
return node.GetChild(count - 1);
}
public static T? GetLastChild<T>(this Node node) where T : Node
{
Node? last = null;
foreach (var child in node.GetChildren())
{
if (child is T) last = child;
}
return last as T;
}
}