From 4f52c2bb1f76cdfb879a91e55e9a1c50a2ab0afb Mon Sep 17 00:00:00 2001 From: Paul Joannon Date: Sun, 6 Oct 2024 18:24:46 +0200 Subject: [PATCH] Implement [ExportToolButton] --- .../ExportDiagnosticsTests.cs | 27 ++++++++ .../ScriptPropertiesGeneratorTests.cs | 9 +++ ...stics_GD0108_ScriptProperties.generated.cs | 48 ++++++++++++++ ...stics_GD0109_ScriptProperties.generated.cs | 48 ++++++++++++++ ...stics_GD0110_ScriptProperties.generated.cs | 48 ++++++++++++++ ...dToolButtons_ScriptProperties.generated.cs | 48 ++++++++++++++ .../Sources/ExportDiagnostics_GD0108.cs | 8 +++ .../Sources/ExportDiagnostics_GD0109.cs | 9 +++ .../Sources/ExportDiagnostics_GD0110.cs | 9 +++ .../TestData/Sources/ExportedToolButtons.cs | 12 ++++ .../AnalyzerReleases.Unshipped.md | 3 + .../Godot.SourceGenerators/Common.cs | 30 +++++++++ .../ExtensionMethods.cs | 6 ++ .../Godot.SourceGenerators/GodotClasses.cs | 2 + .../Godot.SourceGenerators/GodotEnums.cs | 3 +- .../ScriptPropertiesGenerator.cs | 65 ++++++++++++++++++- .../Attributes/ExportToolButtonAttribute.cs | 33 ++++++++++ .../GodotSharp/GodotSharp/GodotSharp.csproj | 1 + 18 files changed, 406 insertions(+), 3 deletions(-) create mode 100644 modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/GeneratedSources/ExportDiagnostics_GD0108_ScriptProperties.generated.cs create mode 100644 modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/GeneratedSources/ExportDiagnostics_GD0109_ScriptProperties.generated.cs create mode 100644 modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/GeneratedSources/ExportDiagnostics_GD0110_ScriptProperties.generated.cs create mode 100644 modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/GeneratedSources/ExportedToolButtons_ScriptProperties.generated.cs create mode 100644 modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/Sources/ExportDiagnostics_GD0108.cs create mode 100644 modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/Sources/ExportDiagnostics_GD0109.cs create mode 100644 modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/Sources/ExportDiagnostics_GD0110.cs create mode 100644 modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/Sources/ExportedToolButtons.cs create mode 100644 modules/mono/glue/GodotSharp/GodotSharp/Core/Attributes/ExportToolButtonAttribute.cs diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/ExportDiagnosticsTests.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/ExportDiagnosticsTests.cs index 6425c723dda..fe511d67ef4 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/ExportDiagnosticsTests.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/ExportDiagnosticsTests.cs @@ -74,4 +74,31 @@ public class ExportDiagnosticsTests } ); } + + [Fact] + public async void ExportToolButtonInNonToolClass() + { + await CSharpSourceGeneratorVerifier.Verify( + new string[] { "ExportDiagnostics_GD0108.cs" }, + new string[] { "ExportDiagnostics_GD0108_ScriptProperties.generated.cs" } + ); + } + + [Fact] + public async void ExportAndExportToolButtonOnSameMember() + { + await CSharpSourceGeneratorVerifier.Verify( + new string[] { "ExportDiagnostics_GD0109.cs" }, + new string[] { "ExportDiagnostics_GD0109_ScriptProperties.generated.cs" } + ); + } + + [Fact] + public async void ExportToolButtonOnNonCallable() + { + await CSharpSourceGeneratorVerifier.Verify( + new string[] { "ExportDiagnostics_GD0110.cs" }, + new string[] { "ExportDiagnostics_GD0110_ScriptProperties.generated.cs" } + ); + } } diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/ScriptPropertiesGeneratorTests.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/ScriptPropertiesGeneratorTests.cs index 724fb164e02..9693cba9ce1 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/ScriptPropertiesGeneratorTests.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/ScriptPropertiesGeneratorTests.cs @@ -66,4 +66,13 @@ public class ScriptPropertiesGeneratorTests "AbstractGenericNode(Of T)_ScriptProperties.generated.cs" ); } + + [Fact] + public async void ExportedButtons() + { + await CSharpSourceGeneratorVerifier.Verify( + "ExportedToolButtons.cs", + "ExportedToolButtons_ScriptProperties.generated.cs" + ); + } } diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/GeneratedSources/ExportDiagnostics_GD0108_ScriptProperties.generated.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/GeneratedSources/ExportDiagnostics_GD0108_ScriptProperties.generated.cs new file mode 100644 index 00000000000..77114a7c934 --- /dev/null +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/GeneratedSources/ExportDiagnostics_GD0108_ScriptProperties.generated.cs @@ -0,0 +1,48 @@ +using Godot; +using Godot.NativeInterop; + +partial class ExportDiagnostics_GD0108 +{ +#pragma warning disable CS0109 // Disable warning about redundant 'new' keyword + /// + /// Cached StringNames for the properties and fields contained in this class, for fast lookup. + /// + public new class PropertyName : global::Godot.Node.PropertyName { + /// + /// Cached name for the 'MyButton' field. + /// + public new static readonly global::Godot.StringName @MyButton = "MyButton"; + } + /// + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + protected override bool SetGodotClassPropertyValue(in godot_string_name name, in godot_variant value) + { + if (name == PropertyName.@MyButton) { + this.@MyButton = global::Godot.NativeInterop.VariantUtils.ConvertTo(value); + return true; + } + return base.SetGodotClassPropertyValue(name, value); + } + /// + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + protected override bool GetGodotClassPropertyValue(in godot_string_name name, out godot_variant value) + { + if (name == PropertyName.@MyButton) { + value = global::Godot.NativeInterop.VariantUtils.CreateFrom(this.@MyButton); + return true; + } + return base.GetGodotClassPropertyValue(name, out value); + } + /// + /// Get the property information for all the properties declared in this class. + /// This method is used by Godot to register the available properties in the editor. + /// Do not call this method. + /// + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal new static global::System.Collections.Generic.List GetGodotPropertyList() + { + var properties = new global::System.Collections.Generic.List(); + return properties; + } +#pragma warning restore CS0109 +} diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/GeneratedSources/ExportDiagnostics_GD0109_ScriptProperties.generated.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/GeneratedSources/ExportDiagnostics_GD0109_ScriptProperties.generated.cs new file mode 100644 index 00000000000..fc4547f2c1f --- /dev/null +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/GeneratedSources/ExportDiagnostics_GD0109_ScriptProperties.generated.cs @@ -0,0 +1,48 @@ +using Godot; +using Godot.NativeInterop; + +partial class ExportDiagnostics_GD0109 +{ +#pragma warning disable CS0109 // Disable warning about redundant 'new' keyword + /// + /// Cached StringNames for the properties and fields contained in this class, for fast lookup. + /// + public new class PropertyName : global::Godot.Node.PropertyName { + /// + /// Cached name for the 'MyButton' field. + /// + public new static readonly global::Godot.StringName @MyButton = "MyButton"; + } + /// + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + protected override bool SetGodotClassPropertyValue(in godot_string_name name, in godot_variant value) + { + if (name == PropertyName.@MyButton) { + this.@MyButton = global::Godot.NativeInterop.VariantUtils.ConvertTo(value); + return true; + } + return base.SetGodotClassPropertyValue(name, value); + } + /// + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + protected override bool GetGodotClassPropertyValue(in godot_string_name name, out godot_variant value) + { + if (name == PropertyName.@MyButton) { + value = global::Godot.NativeInterop.VariantUtils.CreateFrom(this.@MyButton); + return true; + } + return base.GetGodotClassPropertyValue(name, out value); + } + /// + /// Get the property information for all the properties declared in this class. + /// This method is used by Godot to register the available properties in the editor. + /// Do not call this method. + /// + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal new static global::System.Collections.Generic.List GetGodotPropertyList() + { + var properties = new global::System.Collections.Generic.List(); + return properties; + } +#pragma warning restore CS0109 +} diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/GeneratedSources/ExportDiagnostics_GD0110_ScriptProperties.generated.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/GeneratedSources/ExportDiagnostics_GD0110_ScriptProperties.generated.cs new file mode 100644 index 00000000000..d1aac175571 --- /dev/null +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/GeneratedSources/ExportDiagnostics_GD0110_ScriptProperties.generated.cs @@ -0,0 +1,48 @@ +using Godot; +using Godot.NativeInterop; + +partial class ExportDiagnostics_GD0110 +{ +#pragma warning disable CS0109 // Disable warning about redundant 'new' keyword + /// + /// Cached StringNames for the properties and fields contained in this class, for fast lookup. + /// + public new class PropertyName : global::Godot.Node.PropertyName { + /// + /// Cached name for the 'MyButton' field. + /// + public new static readonly global::Godot.StringName @MyButton = "MyButton"; + } + /// + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + protected override bool SetGodotClassPropertyValue(in godot_string_name name, in godot_variant value) + { + if (name == PropertyName.@MyButton) { + this.@MyButton = global::Godot.NativeInterop.VariantUtils.ConvertTo(value); + return true; + } + return base.SetGodotClassPropertyValue(name, value); + } + /// + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + protected override bool GetGodotClassPropertyValue(in godot_string_name name, out godot_variant value) + { + if (name == PropertyName.@MyButton) { + value = global::Godot.NativeInterop.VariantUtils.CreateFrom(this.@MyButton); + return true; + } + return base.GetGodotClassPropertyValue(name, out value); + } + /// + /// Get the property information for all the properties declared in this class. + /// This method is used by Godot to register the available properties in the editor. + /// Do not call this method. + /// + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal new static global::System.Collections.Generic.List GetGodotPropertyList() + { + var properties = new global::System.Collections.Generic.List(); + return properties; + } +#pragma warning restore CS0109 +} diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/GeneratedSources/ExportedToolButtons_ScriptProperties.generated.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/GeneratedSources/ExportedToolButtons_ScriptProperties.generated.cs new file mode 100644 index 00000000000..6e4ad1f13ed --- /dev/null +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/GeneratedSources/ExportedToolButtons_ScriptProperties.generated.cs @@ -0,0 +1,48 @@ +using Godot; +using Godot.NativeInterop; + +partial class ExportedToolButtons +{ +#pragma warning disable CS0109 // Disable warning about redundant 'new' keyword + /// + /// Cached StringNames for the properties and fields contained in this class, for fast lookup. + /// + public new class PropertyName : global::Godot.GodotObject.PropertyName { + /// + /// Cached name for the 'MyButton1' property. + /// + public new static readonly global::Godot.StringName @MyButton1 = "MyButton1"; + /// + /// Cached name for the 'MyButton2' property. + /// + public new static readonly global::Godot.StringName @MyButton2 = "MyButton2"; + } + /// + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + protected override bool GetGodotClassPropertyValue(in godot_string_name name, out godot_variant value) + { + if (name == PropertyName.@MyButton1) { + value = global::Godot.NativeInterop.VariantUtils.CreateFrom(this.@MyButton1); + return true; + } + if (name == PropertyName.@MyButton2) { + value = global::Godot.NativeInterop.VariantUtils.CreateFrom(this.@MyButton2); + return true; + } + return base.GetGodotClassPropertyValue(name, out value); + } + /// + /// Get the property information for all the properties declared in this class. + /// This method is used by Godot to register the available properties in the editor. + /// Do not call this method. + /// + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal new static global::System.Collections.Generic.List GetGodotPropertyList() + { + var properties = new global::System.Collections.Generic.List(); + properties.Add(new(type: (global::Godot.Variant.Type)25, name: PropertyName.@MyButton1, hint: (global::Godot.PropertyHint)39, hintString: "Click me!", usage: (global::Godot.PropertyUsageFlags)4, exported: true)); + properties.Add(new(type: (global::Godot.Variant.Type)25, name: PropertyName.@MyButton2, hint: (global::Godot.PropertyHint)39, hintString: "Click me!,ColorRect", usage: (global::Godot.PropertyUsageFlags)4, exported: true)); + return properties; + } +#pragma warning restore CS0109 +} diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/Sources/ExportDiagnostics_GD0108.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/Sources/ExportDiagnostics_GD0108.cs new file mode 100644 index 00000000000..6fde2e5957b --- /dev/null +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/Sources/ExportDiagnostics_GD0108.cs @@ -0,0 +1,8 @@ +using Godot; +using Godot.Collections; + +public partial class ExportDiagnostics_GD0108 : Node +{ + [ExportToolButton("")] + public Callable {|GD0108:MyButton|}; +} diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/Sources/ExportDiagnostics_GD0109.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/Sources/ExportDiagnostics_GD0109.cs new file mode 100644 index 00000000000..80a04cd6413 --- /dev/null +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/Sources/ExportDiagnostics_GD0109.cs @@ -0,0 +1,9 @@ +using Godot; +using Godot.Collections; + +[Tool] +public partial class ExportDiagnostics_GD0109 : Node +{ + [Export, ExportToolButton("")] + public Callable {|GD0109:MyButton|}; +} diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/Sources/ExportDiagnostics_GD0110.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/Sources/ExportDiagnostics_GD0110.cs new file mode 100644 index 00000000000..586bf6c19ee --- /dev/null +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/Sources/ExportDiagnostics_GD0110.cs @@ -0,0 +1,9 @@ +using Godot; +using Godot.Collections; + +[Tool] +public partial class ExportDiagnostics_GD0110 : Node +{ + [ExportToolButton("")] + public string {|GD0110:MyButton|}; +} diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/Sources/ExportedToolButtons.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/Sources/ExportedToolButtons.cs new file mode 100644 index 00000000000..91c14387c5d --- /dev/null +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/Sources/ExportedToolButtons.cs @@ -0,0 +1,12 @@ +using Godot; +using System; + +[Tool] +public partial class ExportedToolButtons : GodotObject +{ + [ExportToolButton("Click me!")] + public Callable MyButton1 => Callable.From(() => { GD.Print("Clicked MyButton1!"); }); + + [ExportToolButton("Click me!", Icon = "ColorRect")] + public Callable MyButton2 => Callable.From(() => { GD.Print("Clicked MyButton2!"); }); +} diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/AnalyzerReleases.Unshipped.md b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/AnalyzerReleases.Unshipped.md index 7286853f7a7..90c84911770 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/AnalyzerReleases.Unshipped.md +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/AnalyzerReleases.Unshipped.md @@ -3,3 +3,6 @@ Rule ID | Category | Severity | Notes --------|----------|----------|-------------------- GD0003 | Usage | Error | ScriptPathAttributeGenerator, [Documentation](https://docs.godotengine.org/en/latest/tutorials/scripting/c_sharp/diagnostics/GD0003.html) +GD0108 | Usage | Error | ScriptPropertiesGenerator, [Documentation](https://docs.godotengine.org/en/latest/tutorials/scripting/c_sharp/diagnostics/GD0108.html) +GD0109 | Usage | Error | ScriptPropertiesGenerator, [Documentation](https://docs.godotengine.org/en/latest/tutorials/scripting/c_sharp/diagnostics/GD0109.html) +GD0110 | Usage | Error | ScriptPropertiesGenerator, [Documentation](https://docs.godotengine.org/en/latest/tutorials/scripting/c_sharp/diagnostics/GD0110.html) diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/Common.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/Common.cs index ad7962e7df1..8da0818e42e 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/Common.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/Common.cs @@ -107,6 +107,36 @@ namespace Godot.SourceGenerators "Types not derived from Node should not export Node members. Node export is only supported in Node-derived classes.", helpLinkUri: string.Format(_helpLinkFormat, "GD0107")); + public static readonly DiagnosticDescriptor OnlyToolClassesShouldUseExportToolButtonRule = + new DiagnosticDescriptor(id: "GD0108", + title: "The exported tool button is not in a tool class", + messageFormat: "The exported tool button '{0}' is not in a tool class", + category: "Usage", + DiagnosticSeverity.Error, + isEnabledByDefault: true, + "The exported tool button is not in a tool class. Annotate the class with the '[Tool]' attribute, or remove the '[ExportToolButton]' attribute.", + helpLinkUri: string.Format(_helpLinkFormat, "GD0108")); + + public static readonly DiagnosticDescriptor ExportToolButtonShouldNotBeUsedWithExportRule = + new DiagnosticDescriptor(id: "GD0109", + title: "The '[ExportToolButton]' attribute cannot be used with another '[Export]' attribute", + messageFormat: "The '[ExportToolButton]' attribute cannot be used with another '[Export]' attribute on '{0}'", + category: "Usage", + DiagnosticSeverity.Error, + isEnabledByDefault: true, + "The '[ExportToolButton]' attribute cannot be used with the '[Export]' attribute. Remove one of the attributes.", + helpLinkUri: string.Format(_helpLinkFormat, "GD0109")); + + public static readonly DiagnosticDescriptor ExportToolButtonIsNotCallableRule = + new DiagnosticDescriptor(id: "GD0110", + title: "The exported tool button is not a Callable", + messageFormat: "The exported tool button '{0}' is not a Callable", + category: "Usage", + DiagnosticSeverity.Error, + isEnabledByDefault: true, + "The exported tool button is not a Callable. The '[ExportToolButton]' attribute is only supported on members of type Callable.", + helpLinkUri: string.Format(_helpLinkFormat, "GD0110")); + public static readonly DiagnosticDescriptor SignalDelegateMissingSuffixRule = new DiagnosticDescriptor(id: "GD0201", title: "The name of the delegate must end with 'EventHandler'", diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ExtensionMethods.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ExtensionMethods.cs index 62fa7b0a367..46c446169a8 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ExtensionMethods.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ExtensionMethods.cs @@ -287,6 +287,12 @@ namespace Godot.SourceGenerators public static bool IsGodotGlobalClassAttribute(this INamedTypeSymbol symbol) => symbol.FullQualifiedNameOmitGlobal() == GodotClasses.GlobalClassAttr; + public static bool IsGodotExportToolButtonAttribute(this INamedTypeSymbol symbol) + => symbol.FullQualifiedNameOmitGlobal() == GodotClasses.ExportToolButtonAttr; + + public static bool IsGodotToolAttribute(this INamedTypeSymbol symbol) + => symbol.FullQualifiedNameOmitGlobal() == GodotClasses.ToolAttr; + public static bool IsSystemFlagsAttribute(this INamedTypeSymbol symbol) => symbol.FullQualifiedNameOmitGlobal() == GodotClasses.SystemFlagsAttr; diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/GodotClasses.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/GodotClasses.cs index af39a54b0b0..f6de09e25bf 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/GodotClasses.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/GodotClasses.cs @@ -9,10 +9,12 @@ namespace Godot.SourceGenerators public const string ExportCategoryAttr = "Godot.ExportCategoryAttribute"; public const string ExportGroupAttr = "Godot.ExportGroupAttribute"; public const string ExportSubgroupAttr = "Godot.ExportSubgroupAttribute"; + public const string ExportToolButtonAttr = "Godot.ExportToolButtonAttribute"; public const string SignalAttr = "Godot.SignalAttribute"; public const string MustBeVariantAttr = "Godot.MustBeVariantAttribute"; public const string GodotClassNameAttr = "Godot.GodotClassNameAttribute"; public const string GlobalClassAttr = "Godot.GlobalClassAttribute"; + public const string ToolAttr = "Godot.ToolAttribute"; public const string SystemFlagsAttr = "System.FlagsAttribute"; } } diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/GodotEnums.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/GodotEnums.cs index bb4c4824e7c..d77ebc3cec6 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/GodotEnums.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/GodotEnums.cs @@ -89,7 +89,8 @@ namespace Godot.SourceGenerators Password = 36, LayersAvoidance = 37, DictionaryType = 38, - Max = 39 + ToolButton = 39, + Max = 40 } [Flags] diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptPropertiesGenerator.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptPropertiesGenerator.cs index 0f86b3b91c5..eb9dc352559 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptPropertiesGenerator.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptPropertiesGenerator.cs @@ -69,6 +69,7 @@ namespace Godot.SourceGenerators bool hasNamespace = classNs.Length != 0; bool isInnerClass = symbol.ContainingType != null; + bool isToolClass = symbol.GetAttributes().Any(a => a.AttributeClass?.IsGodotToolAttribute() ?? false); string uniqueHint = symbol.FullQualifiedNameOmitGlobal().SanitizeQualifiedNameForUniqueHint() + "_ScriptProperties.generated"; @@ -277,6 +278,16 @@ namespace Godot.SourceGenerators if (propertyInfo == null) continue; + if (propertyInfo.Value.Hint == PropertyHint.ToolButton && !isToolClass) + { + context.ReportDiagnostic(Diagnostic.Create( + Common.OnlyToolClassesShouldUseExportToolButtonRule, + member.Symbol.Locations.FirstLocationWithSourceTreeOrDefault(), + member.Symbol.ToDisplayString() + )); + continue; + } + AppendPropertyInfo(source, propertyInfo.Value); } @@ -418,6 +429,19 @@ namespace Godot.SourceGenerators var exportAttr = memberSymbol.GetAttributes() .FirstOrDefault(a => a.AttributeClass?.IsGodotExportAttribute() ?? false); + var exportToolButtonAttr = memberSymbol.GetAttributes() + .FirstOrDefault(a => a.AttributeClass?.IsGodotExportToolButtonAttribute() ?? false); + + if (exportAttr != null && exportToolButtonAttr != null) + { + context.ReportDiagnostic(Diagnostic.Create( + Common.ExportToolButtonShouldNotBeUsedWithExportRule, + memberSymbol.Locations.FirstLocationWithSourceTreeOrDefault(), + memberSymbol.ToDisplayString() + )); + return null; + } + var propertySymbol = memberSymbol as IPropertySymbol; var fieldSymbol = memberSymbol as IFieldSymbol; @@ -446,19 +470,56 @@ namespace Godot.SourceGenerators } } + if (exportToolButtonAttr != null && propertySymbol != null && propertySymbol.GetMethod == null) + { + context.ReportDiagnostic(Diagnostic.Create( + Common.ExportedPropertyIsWriteOnlyRule, + propertySymbol.Locations.FirstLocationWithSourceTreeOrDefault(), + propertySymbol.ToDisplayString() + )); + return null; + } + var memberType = propertySymbol?.Type ?? fieldSymbol!.Type; var memberVariantType = MarshalUtils.ConvertMarshalTypeToVariantType(marshalType)!.Value; string memberName = memberSymbol.Name; + string? hintString = null; + + if (exportToolButtonAttr != null) + { + if (memberVariantType != VariantType.Callable) + { + context.ReportDiagnostic(Diagnostic.Create( + Common.ExportToolButtonIsNotCallableRule, + memberSymbol.Locations.FirstLocationWithSourceTreeOrDefault(), + memberSymbol.ToDisplayString() + )); + return null; + } + + hintString = exportToolButtonAttr.ConstructorArguments[0].Value?.ToString() ?? ""; + foreach (var namedArgument in exportToolButtonAttr.NamedArguments) + { + if (namedArgument is { Key: "Icon", Value.Value: string { Length: > 0 } }) + { + hintString += $",{namedArgument.Value.Value}"; + } + } + + return new PropertyInfo(memberVariantType, memberName, PropertyHint.ToolButton, + hintString: hintString, PropertyUsageFlags.Editor, exported: true); + } + if (exportAttr == null) { return new PropertyInfo(memberVariantType, memberName, PropertyHint.None, - hintString: null, PropertyUsageFlags.ScriptVariable, exported: false); + hintString: hintString, PropertyUsageFlags.ScriptVariable, exported: false); } if (!TryGetMemberExportHint(typeCache, memberType, exportAttr, memberVariantType, - isTypeArgument: false, out var hint, out var hintString)) + isTypeArgument: false, out var hint, out hintString)) { var constructorArguments = exportAttr.ConstructorArguments; diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Attributes/ExportToolButtonAttribute.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Attributes/ExportToolButtonAttribute.cs new file mode 100644 index 00000000000..87a9e628f90 --- /dev/null +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Attributes/ExportToolButtonAttribute.cs @@ -0,0 +1,33 @@ +using System; + +#nullable enable + +namespace Godot +{ + /// + /// Exports the annotated as a clickable button. + /// + [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)] + public sealed class ExportToolButtonAttribute : Attribute + { + /// + /// The label of the button. + /// + public string Text { get; } + + /// + /// If defined, used to fetch an icon for the button via , + /// from the EditorIcons theme type. + /// + public string? Icon { get; init; } + + /// + /// Exports the annotated as a clickable button. + /// + /// The label of the button. + public ExportToolButtonAttribute(string text) + { + Text = text; + } + } +} diff --git a/modules/mono/glue/GodotSharp/GodotSharp/GodotSharp.csproj b/modules/mono/glue/GodotSharp/GodotSharp/GodotSharp.csproj index 3a3134d1604..5aa68559d80 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/GodotSharp.csproj +++ b/modules/mono/glue/GodotSharp/GodotSharp/GodotSharp.csproj @@ -48,6 +48,7 @@ +