Skip to content

Commit

Permalink
CS Generation Fixes
Browse files Browse the repository at this point in the history
- Column support
- Template Support
  • Loading branch information
ErnSur committed Sep 25, 2022
1 parent edcce40 commit 594fb47
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ namespace QuickEye.UIToolkit.Editor
internal static class UxmlClassGenerator
{
private const string GenerateCsMenuItemName = "CONTEXT/VisualTreeAsset/Generate C# Class";


private static readonly string[] IgnoredTagFullnames =
{
"UnityEngine.UIElements.Template",
"Style"
};

private const string ColumnFullName = "UnityEngine.UIElements.Column";

[MenuItem(GenerateCsMenuItemName)]
private static void GenerateGenCs(MenuCommand command)
{
Expand All @@ -30,7 +38,10 @@ public static void GenerateGenCs(VisualTreeAsset asset)
var settings = CodeGenSettings.FromUxml(uxml);
if (UxmlParser.TryGetElementsWithName(uxml, out var elements))
{
GenerateScript(asset.name, elements, GetGenCsFilePath(path), settings);
var validElements = elements
.Where(e => !IgnoredTagFullnames.Contains(e.FullyQualifiedTypeName))
.ToArray();
GenerateScript(asset.name, validElements, GetGenCsFilePath(path), settings);
}
}

Expand All @@ -48,9 +59,28 @@ private static string GetFieldAssigment(UxmlElement element, CodeGenSettings set
var name = element.NameAttribute;
var varName = settings.FieldPrefix + CodeGeneration.UssNameToVariableName(name);

if (element.FullyQualifiedTypeName == ColumnFullName)
{
return GetFieldAssigmentForColumn(element, settings, varName, name);
}

return $"{varName} = root.Q<{type}>(\"{name}\");";
}

private static string GetFieldAssigmentForColumn(UxmlElement element, CodeGenSettings settings, string varName,
string name)
{
var multiColumnElement = element.XElement.Parent?.Parent?.ToUxmlElement();

if (string.IsNullOrEmpty(multiColumnElement?.NameAttribute))
return $"// Could not find \"{name}\" MultiColumn parent with a name.";

var multiColumnEleVarName = settings.FieldPrefix +
CodeGeneration.UssNameToVariableName(multiColumnElement
.NameAttribute);
return $"{varName} = {multiColumnEleVarName}.columns[\"{name}\"];";
}

private static void GenerateScript(string scriptName, UxmlElement[] uxmlElements, string path,
CodeGenSettings settings)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ namespace QuickEye.UIToolkit.Editor
{
internal class UxmlElement
{
public readonly XElement XElement;
public readonly string Namespace;
public readonly string TypeName;
public readonly string NameAttribute;
public string FullyQualifiedTypeName => $"{Namespace}.{TypeName}";
public bool IsUnityEngineType => Namespace == "UnityEngine.UIElements";

public UxmlElement(XElement xElement)
{
XElement = xElement;
var localName = xElement.Name.LocalName;

Namespace = xElement.Name.NamespaceName != XNamespace.None
Expand All @@ -22,6 +24,16 @@ public UxmlElement(XElement xElement)
? localName.Split('.').Last()
: localName;
NameAttribute = xElement.Attribute("name")?.Value;
if (FullyQualifiedTypeName == "UnityEngine.UIElements.Instance")
TypeName = "TemplateContainer";
}
}

internal static class XElementExtensions
{
public static UxmlElement ToUxmlElement(this XElement xElement)
{
return new UxmlElement(xElement);
}
}
}

0 comments on commit 594fb47

Please sign in to comment.