add option for defaults and make it easier to use

This commit is contained in:
OMGeeky
2023-11-03 21:32:55 +01:00
parent 04005d9cb6
commit 7d21f1de19
3 changed files with 70 additions and 19 deletions

5
.gitignore vendored Normal file
View File

@@ -0,0 +1,5 @@
/ExampleGenerator/obj
/TestConsole/obj/
/ExampleGenerator/bin
/TestConsole/bin

View 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>

View File

@@ -49,7 +49,7 @@ using System;
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, Inherited = true, AllowMultiple = false)]
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)]
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}
{{
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 )
{
source.AppendLine( GetAttributeDescription( fieldSymbol , uxmlTraitAttributeSymbol ) );
}
foreach ( ISymbol fieldSymbol in uxmlTraitFields )
{
source.AppendLine( GetAttributeDescription( fieldSymbol , uxmlTraitAttributeSymbol ) );
}
source.Append( $@"
source.Append( $@"
public override void Init(VisualElement ve , IUxmlAttributes bag , CreationContext cc )
{{
base.Init( ve , bag , cc );
@@ -141,14 +144,17 @@ public partial class {classSymbol.Name}
" );
foreach ( ISymbol fieldSymbol in uxmlTraitFields )
{
source.AppendLine( GetAttributeInitialization( fieldSymbol , uxmlTraitAttributeSymbol ) );
foreach ( ISymbol fieldSymbol in uxmlTraitFields )
{
source.AppendLine( GetAttributeInitialization( fieldSymbol , uxmlTraitAttributeSymbol ) );
}
source.Append( $@" }}
}}" );
}
source.Append( $@" }}
}}
public void QueryComponents()
source.Append( $@"
public void QueryElements()
{{
" );
@@ -178,12 +184,22 @@ public partial class {classSymbol.Name}
return null;
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}" );
}
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()
{
Name = name ,
@@ -268,6 +284,17 @@ public partial class {classSymbol.Name}
var attributeName = attr.Name;
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} }};";
}