Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Godot.Bindings/Core/Attributes/BindPropertyAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ public sealed class BindPropertyAttribute : Attribute
/// </summary>
public string? Name { get; init; }

/// <summary>
/// Specifies the hint that will be used to register the property.
/// If unspecified it will use the hint calculated from the property type.
/// </summary>
public PropertyHint Hint { get; init; } = PropertyHint.None;

/// <summary>
/// Specifies the hint string that will be used to register the property.
/// If unspecified it will use the hint string calculated from the property type.
/// </summary>
public string? HintString { get; init; }

/// <summary>
/// Specifies the marshaller type that will be used to marshal this property.
/// If unspecified the default marshaller for the annotated property's type
Expand Down
8 changes: 4 additions & 4 deletions src/Godot.SourceGenerators/BindMethodsWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -524,13 +524,13 @@ private static void AppendPropertyInfoObjectInitializer(IndentedStringBuilder sb

List<string> lines = [];

if (marshalInfo.Hint != PropertyHint.None)
if (property.HintOverride != PropertyHint.None || marshalInfo.Hint != PropertyHint.None)
{
lines.Add($"Hint = {marshalInfo.Hint.FullNameWithGlobal()},");
lines.Add($"Hint = {(property.HintOverride == PropertyHint.None ? marshalInfo.Hint : property.HintOverride).FullNameWithGlobal()},");
}
if (!string.IsNullOrEmpty(marshalInfo.HintString))
if (!string.IsNullOrEmpty(property.HintStringOverride) || !string.IsNullOrEmpty(marshalInfo.HintString))
{
lines.Add($"""HintString = "{marshalInfo.HintString}",""");
lines.Add($"""HintString = "{property.HintStringOverride ?? marshalInfo.HintString}",""");
}
if (marshalInfo.Usage != PropertyUsageFlags.None)
{
Expand Down
13 changes: 13 additions & 0 deletions src/Godot.SourceGenerators/SpecCollectors/PropertySpecCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ public static GodotPropertySpec Collect(Compilation compilation, ITypeSymbol ret
private static GodotPropertySpec CollectCore(Compilation compilation, string symbolName, ITypeSymbol typeSymbol, AttributeData? attribute, CancellationToken cancellationToken = default)
{
string? nameOverride = null;
PropertyHint hintOverride = PropertyHint.None;
string? hintStringOverride = null;

if (attribute is not null)
{
Expand All @@ -98,6 +100,15 @@ private static GodotPropertySpec CollectCore(Compilation compilation, string sym
case "Name":
nameOverride = constant.Value as string;
break;
case "Hint":
if (constant.Value is long longValue)
{
hintOverride = (PropertyHint)longValue;
}
break;
case "HintString":
hintStringOverride = constant.Value as string;
break;
}
}
}
Expand All @@ -110,6 +121,8 @@ private static GodotPropertySpec CollectCore(Compilation compilation, string sym
FullyQualifiedTypeName = typeSymbol.FullNameWithGlobal(),
MarshalInfo = marshalInfo,
NameOverride = nameOverride,
HintOverride = hintOverride,
HintStringOverride = hintStringOverride
};
}
}
14 changes: 14 additions & 0 deletions src/Godot.SourceGenerators/Specs/GodotPropertySpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ namespace Godot.SourceGenerators;
/// </summary>
public string? NameOverride { get; init; }

/// <summary>
/// Hint specified in the <c>[BindProperty]</c> attribute for this property,
/// or PropertyType.None if a hint was not specified.
/// If unspecified the hint of the property will be calculated from the <see cref="MarshalInfo"/>.
/// </summary>
public PropertyHint HintOverride { get; init; }

/// <summary>
/// Hint string specified in the <c>[BindProperty]</c> attribute for this property,
/// or <see langword="null"/> if a hint string was not specified.
/// If unspecified the hint string of the property will be calculated from the <see cref="MarshalInfo"/>.
/// </summary>
public string? HintStringOverride { get; init; }

/// <summary>
/// Group information specified in the <c>[PropertyGroup]</c> attribute,
/// if the property is annotated to define a group from this property.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ partial class NodeWithProperties
public static global::Godot.StringName @myField { get; } = global::Godot.StringName.CreateStaticFromAscii("myField"u8);
public static global::Godot.StringName @myNamedField { get; } = global::Godot.StringName.CreateStaticFromAscii("my_named_field"u8);
public static global::Godot.StringName @myFieldWithDefaultValue { get; } = global::Godot.StringName.CreateStaticFromAscii("myFieldWithDefaultValue"u8);
public static global::Godot.StringName @myFieldWithHintOverride { get; } = global::Godot.StringName.CreateStaticFromAscii("myFieldWithHintOverride"u8);
public static global::Godot.StringName @myFieldWithHintStringOverride { get; } = global::Godot.StringName.CreateStaticFromAscii("myFieldWithHintStringOverride"u8);
}
public new partial class SignalName : global::Godot.Node.SignalName
{
Expand Down Expand Up @@ -99,5 +101,31 @@ internal static void BindMethods(global::Godot.Bridge.ClassRegistrationContext c
{
__instance.@myFieldWithDefaultValue = value;
});
context.BindProperty(new global::Godot.Bridge.PropertyInfo(PropertyName.@myFieldWithHintOverride, global::Godot.VariantType.Int, global::Godot.Bridge.VariantTypeMetadata.Int32)
{
Hint = global::Godot.PropertyHint.Layers3DPhysics,
Usage = global::Godot.PropertyUsageFlags.Default,
},
static (NodeWithProperties __instance) =>
{
return __instance.@myFieldWithHintOverride;
},
static (NodeWithProperties __instance, int value) =>
{
__instance.@myFieldWithHintOverride = value;
});
context.BindProperty(new global::Godot.Bridge.PropertyInfo(PropertyName.@myFieldWithHintStringOverride, global::Godot.VariantType.Int, global::Godot.Bridge.VariantTypeMetadata.Int32)
{
HintString = "my_hint_string",
Usage = global::Godot.PropertyUsageFlags.Default,
},
static (NodeWithProperties __instance) =>
{
return __instance.@myFieldWithHintStringOverride;
},
static (NodeWithProperties __instance, int value) =>
{
__instance.@myFieldWithHintStringOverride = value;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,10 @@ public partial class NodeWithProperties : Node

[BindProperty]
public int myFieldWithDefaultValue = 42;

[BindProperty(Hint = PropertyHint.Layers3DPhysics)]
public int myFieldWithHintOverride;

[BindProperty(HintString = "my_hint_string")]
public int myFieldWithHintStringOverride;
}