mirror of
https://github.com/OMGeeky/UnityCodeGenerators.git
synced 2026-01-18 17:34:41 +01:00
add option for defaults and make it easier to use
This commit is contained in:
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
/ExampleGenerator/obj
|
||||||
|
/TestConsole/obj/
|
||||||
|
|
||||||
|
/ExampleGenerator/bin
|
||||||
|
/TestConsole/bin
|
||||||
19
ExampleGenerator/ExampleGenerator.csproj
Normal file
19
ExampleGenerator/ExampleGenerator.csproj
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
|
<PublishSingleFile>true</PublishSingleFile>
|
||||||
|
<SelfContained>true</SelfContained>
|
||||||
|
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.CodeAnalysis" Version="3.8.0" />
|
||||||
|
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="3.9.0-5" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Compile Remove="Generators.cs" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -49,7 +49,7 @@ using System;
|
|||||||
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, Inherited = true, AllowMultiple = false)]
|
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, Inherited = true, AllowMultiple = false)]
|
||||||
internal class {Helpers.UxmlTraitAttribute} : Attribute
|
internal class {Helpers.UxmlTraitAttribute} : Attribute
|
||||||
{{
|
{{
|
||||||
public {Helpers.UxmlTraitAttribute}(string name, object defaultValue) {{ }}
|
public {Helpers.UxmlTraitAttribute}(string name, object defaultValue=null) {{ }}
|
||||||
}}
|
}}
|
||||||
";
|
";
|
||||||
|
|
||||||
@@ -59,7 +59,7 @@ using System;
|
|||||||
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, Inherited = true, AllowMultiple = false)]
|
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, Inherited = true, AllowMultiple = false)]
|
||||||
internal class {Helpers.UiElementAttribute} : Attribute
|
internal class {Helpers.UiElementAttribute} : Attribute
|
||||||
{{
|
{{
|
||||||
public {Helpers.UiElementAttribute}(string name) {{ }}
|
public {Helpers.UiElementAttribute}(string name=null) {{ }}
|
||||||
}}
|
}}
|
||||||
";
|
";
|
||||||
|
|
||||||
@@ -121,19 +121,22 @@ using UnityEngine.UIElements;
|
|||||||
namespace {classSymbol.ContainingNamespace}
|
namespace {classSymbol.ContainingNamespace}
|
||||||
{{
|
{{
|
||||||
public partial class {classSymbol.Name}
|
public partial class {classSymbol.Name}
|
||||||
{{
|
{{" );
|
||||||
public new class UxmlFactory : UxmlFactory<{classSymbol.Name}, UxmlTraits> {{ }}
|
|
||||||
public new class UxmlTraits : VisualElement.UxmlTraits
|
if ( uxmlTraitFields.Any() )
|
||||||
|
{
|
||||||
|
source.Append( $@"
|
||||||
|
public new partial class UxmlFactory : UxmlFactory<{classSymbol.Name}, UxmlTraits> {{ }}
|
||||||
|
public new partial class UxmlTraits : VisualElement.UxmlTraits
|
||||||
{{
|
{{
|
||||||
" );
|
" );
|
||||||
|
|
||||||
// throw new NotImplementedException( $"elements: {elementFields.Count} uxmlTraits: {uxmlTraitFields.Count}" );
|
foreach ( ISymbol fieldSymbol in uxmlTraitFields )
|
||||||
foreach ( ISymbol fieldSymbol in uxmlTraitFields )
|
{
|
||||||
{
|
source.AppendLine( GetAttributeDescription( fieldSymbol , uxmlTraitAttributeSymbol ) );
|
||||||
source.AppendLine( GetAttributeDescription( fieldSymbol , uxmlTraitAttributeSymbol ) );
|
}
|
||||||
}
|
|
||||||
|
|
||||||
source.Append( $@"
|
source.Append( $@"
|
||||||
public override void Init(VisualElement ve , IUxmlAttributes bag , CreationContext cc )
|
public override void Init(VisualElement ve , IUxmlAttributes bag , CreationContext cc )
|
||||||
{{
|
{{
|
||||||
base.Init( ve , bag , cc );
|
base.Init( ve , bag , cc );
|
||||||
@@ -141,14 +144,17 @@ public partial class {classSymbol.Name}
|
|||||||
|
|
||||||
" );
|
" );
|
||||||
|
|
||||||
foreach ( ISymbol fieldSymbol in uxmlTraitFields )
|
foreach ( ISymbol fieldSymbol in uxmlTraitFields )
|
||||||
{
|
{
|
||||||
source.AppendLine( GetAttributeInitialization( fieldSymbol , uxmlTraitAttributeSymbol ) );
|
source.AppendLine( GetAttributeInitialization( fieldSymbol , uxmlTraitAttributeSymbol ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
source.Append( $@" }}
|
||||||
|
}}" );
|
||||||
}
|
}
|
||||||
|
|
||||||
source.Append( $@" }}
|
source.Append( $@"
|
||||||
}}
|
public void QueryElements()
|
||||||
public void QueryComponents()
|
|
||||||
{{
|
{{
|
||||||
" );
|
" );
|
||||||
|
|
||||||
@@ -178,12 +184,22 @@ public partial class {classSymbol.Name}
|
|||||||
return null;
|
return null;
|
||||||
|
|
||||||
var args = attr.ConstructorArguments.ToList();
|
var args = attr.ConstructorArguments.ToList();
|
||||||
if ( args.Count != 1 )
|
if ( args.Count > 1 )
|
||||||
{
|
{
|
||||||
throw new NotImplementedException( $"Attribute did not have enough parameters: expected 1 got {args.Count} {attr}: args: {args}" );
|
throw new NotImplementedException( $"Attribute did not have enough parameters: expected 1 got {args.Count} {attr}: args: {args}" );
|
||||||
}
|
}
|
||||||
|
|
||||||
var name = args[0].Value as string;
|
string name = null;
|
||||||
|
if ( args.Count == 1 )
|
||||||
|
{
|
||||||
|
name = args[0].Value as string;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( name is null )
|
||||||
|
{
|
||||||
|
name = fieldSymbol.Name;
|
||||||
|
}
|
||||||
|
|
||||||
return new UiElementAttributeData()
|
return new UiElementAttributeData()
|
||||||
{
|
{
|
||||||
Name = name ,
|
Name = name ,
|
||||||
@@ -268,6 +284,17 @@ public partial class {classSymbol.Name}
|
|||||||
|
|
||||||
var attributeName = attr.Name;
|
var attributeName = attr.Name;
|
||||||
var defaultValue = attr.defaultValue;
|
var defaultValue = attr.defaultValue;
|
||||||
|
|
||||||
|
if ( GetTypeName( attr.Type ) == "string" )
|
||||||
|
{
|
||||||
|
defaultValue = $"\"{defaultValue}\"";
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( defaultValue is null )
|
||||||
|
{
|
||||||
|
defaultValue = "default";
|
||||||
|
}
|
||||||
|
|
||||||
return $" private {type} {name} = new() {{ name = \"{attributeName}\" , defaultValue = {defaultValue} }};";
|
return $" private {type} {name} = new() {{ name = \"{attributeName}\" , defaultValue = {defaultValue} }};";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user