Skip to content
This repository has been archived by the owner on Jan 18, 2022. It is now read-only.

Bug/worker inspector indentation #1480

Merged
merged 11 commits into from
Sep 14, 2020
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
- Fixed an issue where authority changes returned by `ComponentUpdateSystem.GetAuthorityChangesReceived()` were returned in order from newest to oldest. [#1465](https://github.com/spatialos/gdk-for-unity/pull/1465)
- Fixed a bug where the build system would throw a null reference exception if you don't have a configuration for a worker type. [#1461](https://github.com/spatialos/gdk-for-unity/pull/1461)
- Fixed an incorrect callback registration for entity creation/removal in the `WorldCommandSender`. [#1473](https://github.com/spatialos/gdk-for-unity/pull/1462)
- Fixed the `TextField` indentation issue in the Worker Inspector due to nested `VisualElement` containers. [#1480](https://github.com/spatialos/gdk-for-unity/pull/1480)

## `0.3.10` - 2020-08-18

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private void GenerateConstructor(TypeBlock typeBlock, UnityComponentDetails deta

foreach (var field in details.FieldDetails)
{
mb.TextList(typeGenerator.ToFieldInitialisation(field, "ComponentFoldout"));
mb.TextList(typeGenerator.ToFieldInitialisation(field, "ComponentFoldout", "0"));
}

mb.Line($"InjectComponentIcon(\"{GetComponentIcon(details)}\");");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Improbable.Gdk.CodeGeneration.Model;
Expand Down Expand Up @@ -42,14 +43,14 @@ public string ToFieldDeclaration(UnityFieldDetails fieldDetails)
}
}

public IEnumerable<string> ToFieldInitialisation(UnityFieldDetails fieldDetails, string parentContainer)
public IEnumerable<string> ToFieldInitialisation(UnityFieldDetails fieldDetails, string parentContainer, string nestVar)
{
switch (fieldDetails.FieldType)
{
case SingularFieldType singularFieldType:
var humanReadableName = Formatting.SnakeCaseToHumanReadable(fieldDetails.Name);

foreach (var initializer in GetFieldInitializer(singularFieldType.ContainedType, $"{fieldDetails.CamelCaseName}Field", humanReadableName, false))
foreach (var initializer in GetFieldInitializer(singularFieldType.ContainedType, $"{fieldDetails.CamelCaseName}Field", humanReadableName, nestVar, false))
{
yield return initializer;
}
Expand All @@ -59,7 +60,7 @@ public IEnumerable<string> ToFieldInitialisation(UnityFieldDetails fieldDetails,
case OptionFieldType optionFieldType:
var innerUiType = GetUiFieldType(optionFieldType.ContainedType);

foreach (var initializer in GetFieldInitializer(optionFieldType.ContainedType, $"{fieldDetails.CamelCaseName}InnerField", "Value"))
foreach (var initializer in GetFieldInitializer(optionFieldType.ContainedType, $"{fieldDetails.CamelCaseName}InnerField", "Value", $"{nestVar} + 1"))
{
yield return initializer;
}
Expand All @@ -76,7 +77,7 @@ public IEnumerable<string> ToFieldInitialisation(UnityFieldDetails fieldDetails,
yield return
$"{fieldDetails.CamelCaseName}Field = new PaginatedListView<{innerListType}, {listFieldType.ContainedType.FqnType}>(\"{Formatting.SnakeCaseToHumanReadable(fieldDetails.Name)}\", () => {{";

foreach (var initializer in GetFieldInitializer(listFieldType.ContainedType, "inner", ""))
foreach (var initializer in GetFieldInitializer(listFieldType.ContainedType, "inner", "", $"{nestVar} + 1"))
{
yield return initializer;
}
Expand All @@ -98,7 +99,7 @@ public IEnumerable<string> ToFieldInitialisation(UnityFieldDetails fieldDetails,
$"{fieldDetails.CamelCaseName}Field = new PaginatedMapView<{innerKeyType}, {mapFieldType.KeyType.FqnType}, {innerValueType}, {mapFieldType.ValueType.FqnType}>(\"{Formatting.SnakeCaseToHumanReadable(fieldDetails.Name)}\",";
yield return "() => {";

foreach (var initializer in GetFieldInitializer(mapFieldType.KeyType, "inner", "Key"))
foreach (var initializer in GetFieldInitializer(mapFieldType.KeyType, "inner", "Key", $"{nestVar} + 1"))
{
yield return initializer;
}
Expand All @@ -107,7 +108,7 @@ public IEnumerable<string> ToFieldInitialisation(UnityFieldDetails fieldDetails,

yield return "() => {";

foreach (var initializer in GetFieldInitializer(mapFieldType.ValueType, "inner", "Value"))
foreach (var initializer in GetFieldInitializer(mapFieldType.ValueType, "inner", "Value", $"{nestVar} + 1"))
{
yield return initializer;
}
Expand Down Expand Up @@ -136,22 +137,39 @@ public string ToUiFieldUpdate(UnityFieldDetails fieldDetails, string fieldParent
}
}

private IEnumerable<string> GetFieldInitializer(ContainedType containedType, string uiElementName, string label, bool newVariable = true)
private string ConstructUiElement(ContainedType containedType, string uiElementName, string label, string nestVar, bool newVariable)
{
var inner = GetUiFieldType(containedType);

var builder = new StringBuilder();
if (newVariable)
{
yield return $"var {uiElementName} = new {inner}(\"{label}\");";
builder.Append($"var {uiElementName} = ");
}
else
{
yield return $"{uiElementName} = new {inner}(\"{label}\");";
builder.Append($"{uiElementName} = ");
}

if (containedType.Category == ValueType.Type)
{
builder.Append($"new {inner}(\"{label}\", {nestVar} + 1);");
}
else
{
builder.Append($"new {inner}(\"{label}\");");
}

return builder.ToString();
}

private IEnumerable<string> GetFieldInitializer(ContainedType containedType, string uiElementName, string label, string nestVar, bool newVariable = true)
{
yield return ConstructUiElement(containedType, uiElementName, label, nestVar, newVariable);

// These lines are part of the func to create an inner list item.
if (containedType.Category != ValueType.Type)
{
yield return $"{uiElementName}.labelElement.ShiftRightMargin({nestVar});";
yield return $"{uiElementName}.SetEnabled(false);";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ private TypeBlock GenerateType(UnityTypeDetails details)

private void GenerateConstructor(TypeBlock typeBlock, UnityTypeDetails details)
{
typeBlock.Method($"public {details.Name}Renderer(string label) : base(label)", mb =>
typeBlock.Method($"public {details.Name}Renderer(string label, uint nest) : base(label)", mb =>
{
foreach (var field in details.FieldDetails)
{
mb.TextList(fieldTypeHandler.ToFieldInitialisation(field, "Container"));
mb.TextList(fieldTypeHandler.ToFieldInitialisation(field, "Container", "nest"));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Improbable.Gdk.Debug.WorkerInspector.Codegen.EditmodeTests")]
[assembly: InternalsVisibleTo("Improbable.Gdk.Generated.Debug")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using UnityEngine.UIElements;

namespace Improbable.Gdk.Debug.WorkerInspector.Codegen
{
internal static class VisualElementExtensions
{
public static void ShiftRightMargin(this VisualElement element, uint nest, float offset = -11)
{
var style = element.style;
var val = style.marginRight.value.value;
style.marginRight = new StyleLength(val + offset * nest);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.