Skip to content

Commit

Permalink
feat: UITransform and UIText SDK components (#204)
Browse files Browse the repository at this point in the history
* Create global UIDocument for scenes UI + start implementing UIText systems
* Fix IntegrationTestsSuite
* Move SceneUI folder to DCL/SDKComponents
* Handle UIText removal
* Update SceneUIPlugin.cs
* Update UITextHandlerSystem.cs
* Setup canvas with the default configuration
* Move ProtoConvertUtils and UiElementUtils under SceneUI folder
* Implement UITransform
* Create a style sheet for the main canvas
* Update UITransformHandlerSystem.cs
* Fix tests
* Move the release logic to separated systems
* Make UITransformInstantiationSystem executes first of all the rest of systems
* Apply parenting to UIText
* Set a name for each component
* UITransform: parenting and sorting
* Add an option to clear PB components on Get
* Set default values in UiElementUtils.SetupVisualElement function
* Implement detection of the current scene
* Hide scene UI when we're out of the scene
* UIText test coverage
* Reset processed parcel on realm invalidation
* UITransformInstantiation test coverage
* UITransformUpdate test coverage
* Apply feedback: Add maxSize to AddComponentPool method
* Fix code conventions tests
* Fix async tests
* Upgrade TestFramework to 1.4.2
  * Async Setup/Teardown issue fixed
  * More stable

---------

Co-authored-by: Mikhail Agapov <[email protected]>
  • Loading branch information
sandrade-dcl and mikhail-dcl authored Jan 12, 2024
1 parent f12c930 commit 19f0d41
Show file tree
Hide file tree
Showing 101 changed files with 1,710 additions and 46 deletions.
10 changes: 10 additions & 0 deletions Explorer/Assets/AddressableAssetsData/AssetGroups/UI.asset
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ MonoBehaviour:
m_ReadOnly: 0
m_SerializedLabels: []
FlaggedDuringContentUpdateRestriction: 0
- m_GUID: e0f5a4ab5d8f74640ae94adb6824bd45
m_Address: ScenesUIRootCanvas
m_ReadOnly: 0
m_SerializedLabels: []
FlaggedDuringContentUpdateRestriction: 0
- m_GUID: 295cf8ad8d171d849863eccaf581c9e7
m_Address: ScenesUIStyleSheet
m_ReadOnly: 0
m_SerializedLabels: []
FlaggedDuringContentUpdateRestriction: 0
m_ReadOnly: 0
m_Settings: {fileID: 11400000, guid: fc8a9d2b539788c47a5b305639fa8b34, type: 2}
m_SchemaSet:
Expand Down
3 changes: 3 additions & 0 deletions Explorer/Assets/DCL/AssetsProvision/UI.meta

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

12 changes: 12 additions & 0 deletions Explorer/Assets/DCL/AssetsProvision/UI/AssetReferenceStyleSheet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using UnityEngine.AddressableAssets;
using UnityEngine.UIElements;

namespace DCL.AssetsProvision
{
[Serializable]
public class AssetReferenceStyleSheet : AssetReferenceT<StyleSheet>
{
public AssetReferenceStyleSheet(string guid) : base(guid) { }
}
}

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

11 changes: 11 additions & 0 deletions Explorer/Assets/DCL/AssetsProvision/UI/UIDocumentRef.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using UnityEngine.UIElements;

namespace DCL.AssetsProvision
{
[Serializable]
public class UIDocumentRef : ComponentReference<UIDocument>
{
public UIDocumentRef(string guid) : base(guid) { }
}
}
3 changes: 3 additions & 0 deletions Explorer/Assets/DCL/AssetsProvision/UI/UIDocumentRef.cs.meta

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

Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class DebugLogReportHandler : ReportHandlerBase
// Scene Loading blueish
{ ReportCategory.SCENE_LOADING, ColorUtility.ToHtmlStringRGB(new Color(0.30f, 0.50f, 0.90f)) },
{ ReportCategory.SCENE_FACTORY, ColorUtility.ToHtmlStringRGB(new Color(0.15f, 0.35f, 0.75f)) },
{ ReportCategory.SCENE_UI, ColorUtility.ToHtmlStringRGB(new Color(0.10f, 0.30f, 0.45f)) },

// JavaScript
{ ReportCategory.JAVASCRIPT, ColorUtility.ToHtmlStringRGB(new Color(0.90f, 0.30f, 0.35f)) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public static class ReportCategory
/// </summary>
public const string SCENE_LOADING = nameof(SCENE_LOADING);

/// <summary>
/// Messages related to the scene UI
/// </summary>
public const string SCENE_UI = nameof(SCENE_UI);

/// <summary>
/// Errors reported from JavaScript
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public interface IComponentPoolsRegistry : IDisposable

public static class ComponentPoolsRegistryExtensions
{
public static void AddComponentPool<T>(this IComponentPoolsRegistry componentPoolsRegistry, Action<T> onGet = null, Action<T> onRelease = null) where T: class, new()
public static void AddComponentPool<T>(this IComponentPoolsRegistry componentPoolsRegistry, Action<T> onGet = null, Action<T> onRelease = null, int maxSize = 10000) where T: class, new()
{
componentPoolsRegistry.AddComponentPool(new ComponentPool<T>(onGet, onRelease));
componentPoolsRegistry.AddComponentPool(new ComponentPool<T>(onGet, onRelease, maxSize: maxSize));
}
}
}
1 change: 1 addition & 0 deletions Explorer/Assets/DCL/PluginSystem/DCL.Plugins.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"GUID:6830e2f56251b492e9934f1fafbc8c7d",
"GUID:5ab29fa8ae5769b49ab29e390caca7a4",
"GUID:e56a0d6a94c144c784012e63b6043100",
"GUID:7f6c11dd1400cef4f838168f18ce244a",
"GUID:91cf8206af184dac8e30eb46747e9939",
"GUID:28964ef7dc9441b6b8671b61a8106690",
"GUID:571dc9f8bded0034f98595106462e3d0",
Expand Down
80 changes: 80 additions & 0 deletions Explorer/Assets/DCL/PluginSystem/World/SceneUIPlugin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using Arch.SystemGroups;
using Cysharp.Threading.Tasks;
using DCL.AssetsProvision;
using DCL.Optimization.Pools;
using DCL.PluginSystem.World.Dependencies;
using DCL.SDKComponents.SceneUI.Components;
using DCL.SDKComponents.SceneUI.Utils;
using ECS.ComponentsPooling.Systems;
using ECS.LifeCycle;
using System;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
using UnityEngine.UIElements;
using UITextInstantiationSystem = DCL.SDKComponents.SceneUI.Systems.UIText.UITextInstantiationSystem;
using UITextReleaseSystem = DCL.SDKComponents.SceneUI.Systems.UIText.UITextReleaseSystem;
using UITransformInstantiationSystem = DCL.SDKComponents.SceneUI.Systems.UITransform.UITransformInstantiationSystem;
using UITransformParentingSystem = DCL.SDKComponents.SceneUI.Systems.UITransform.UITransformParentingSystem;
using UITransformReleaseSystem = DCL.SDKComponents.SceneUI.Systems.UITransform.UITransformReleaseSystem;
using UITransformSortingSystem = DCL.SDKComponents.SceneUI.Systems.UITransform.UITransformSortingSystem;
using UITransformUpdateSystem = DCL.SDKComponents.SceneUI.Systems.UITransform.UITransformUpdateSystem;

namespace DCL.PluginSystem.World
{
public class SceneUIPlugin : IDCLWorldPlugin<SceneUIPlugin.Settings>
{
private UIDocument canvas;

private readonly IComponentPoolsRegistry componentPoolsRegistry;
private readonly IAssetsProvisioner assetsProvisioner;

public SceneUIPlugin(ECSWorldSingletonSharedDependencies singletonSharedDependencies, IAssetsProvisioner assetsProvisioner)
{
this.assetsProvisioner = assetsProvisioner;
componentPoolsRegistry = singletonSharedDependencies.ComponentPoolsRegistry;
componentPoolsRegistry.AddComponentPool<VisualElement>(onRelease: UiElementUtils.ReleaseUIElement, maxSize: 200);
componentPoolsRegistry.AddComponentPool<Label>(onRelease: UiElementUtils.ReleaseUIElement, maxSize: 100);
}

public async UniTask InitializeAsync(Settings settings, CancellationToken ct)
{
canvas = (await assetsProvisioner.ProvideInstanceAsync(settings.Canvas, ct: ct)).Value;
StyleSheet scenesUIStyleSheet = (await assetsProvisioner.ProvideMainAssetAsync(settings.StyleSheet, ct)).Value;

canvas.rootVisualElement.styleSheets.Add(scenesUIStyleSheet);
canvas.rootVisualElement.AddToClassList("sceneUIMainCanvas");
canvas.rootVisualElement.pickingMode = PickingMode.Ignore;
}

public void InjectToWorld(ref ArchSystemsWorldBuilder<Arch.Core.World> builder, in ECSWorldInstanceSharedDependencies sharedDependencies, in PersistentEntities persistentEntities, List<IFinalizeWorldSystem> finalizeWorldSystems)
{
UITransformInstantiationSystem.InjectToWorld(ref builder, canvas, componentPoolsRegistry);
UITransformParentingSystem.InjectToWorld(ref builder, sharedDependencies.EntitiesMap, persistentEntities.SceneRoot);
UITransformSortingSystem.InjectToWorld(ref builder, sharedDependencies.EntitiesMap, persistentEntities.SceneRoot);
UITransformUpdateSystem.InjectToWorld(ref builder, canvas, sharedDependencies.SceneStateProvider);
UITransformReleaseSystem.InjectToWorld(ref builder, componentPoolsRegistry);
UITextInstantiationSystem.InjectToWorld(ref builder, componentPoolsRegistry);
UITextReleaseSystem.InjectToWorld(ref builder, componentPoolsRegistry);

finalizeWorldSystems.Add(ReleasePoolableComponentSystem<VisualElement, UITransformComponent>.InjectToWorld(ref builder, componentPoolsRegistry));
finalizeWorldSystems.Add(ReleasePoolableComponentSystem<Label, UITextComponent>.InjectToWorld(ref builder, componentPoolsRegistry));
}

public void InjectToEmptySceneWorld(ref ArchSystemsWorldBuilder<Arch.Core.World> builder, in EmptyScenesWorldSharedDependencies dependencies) { }

public void Dispose() { }

[Serializable]
public class Settings : IDCLPluginSettings
{
[field: Header(nameof(SceneUIPlugin) + "." + nameof(Settings))]
[field: Space]
[field: SerializeField]
public UIDocumentRef Canvas { get; private set; } = null!;

[field: SerializeField]
public AssetReferenceStyleSheet StyleSheet { get; private set; } = null!;
}
}
}
3 changes: 3 additions & 0 deletions Explorer/Assets/DCL/PluginSystem/World/SceneUIPlugin.cs.meta

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

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ MonoBehaviour:
- rid: 6784895267130048515
- rid: 1268264040174714880
- rid: 7387084847885844480
- rid: 4753057106328551424
references:
version: 2
RefIds:
Expand All @@ -26,6 +27,19 @@ MonoBehaviour:
<RaycastFrameBudgetMs>k__BackingField: 3
<PlayerOriginRaycastMaxDistance>k__BackingField: 200
<GlobalInputPropagationBucketThreshold>k__BackingField: 3
- rid: 4753057106328551424
type: {class: SceneUIPlugin/Settings, ns: DCL.PluginSystem.World, asm: DCL.Plugins}
data:
<Canvas>k__BackingField:
m_AssetGUID: e0f5a4ab5d8f74640ae94adb6824bd45
m_SubObjectName:
m_SubObjectType:
m_EditorAssetChanged: 0
<StyleSheet>k__BackingField:
m_AssetGUID: 295cf8ad8d171d849863eccaf581c9e7
m_SubObjectName:
m_SubObjectType:
m_EditorAssetChanged: 0
- rid: 6784895267130048515
type: {class: MaterialsPlugin/Settings, ns: DCL.PluginSystem.World, asm: DCL.Plugins}
data:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
using System.Threading;
using UnityEditor;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.UIElements;
using Object = UnityEngine.Object;

namespace DCL.SDKComponents.AudioSources.Tests.PlayMode
Expand Down Expand Up @@ -106,7 +108,11 @@ private async UniTask InitializationFlowAsync(CancellationToken ct)
CancellationToken ct)
{
// First load the common global plugin
(StaticContainer staticContainer, bool isLoaded) = await StaticContainer.CreateAsync(globalSettingsContainer, web3IdentityCache, ethereumApi, ct);
(StaticContainer staticContainer, bool isLoaded) = await StaticContainer.CreateAsync(
globalSettingsContainer,
web3IdentityCache,
ethereumApi,
ct);

if (!isLoaded)
GameReports.PrintIsDead();
Expand Down
3 changes: 3 additions & 0 deletions Explorer/Assets/DCL/SDKComponents/SceneUI.meta

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

3 changes: 3 additions & 0 deletions Explorer/Assets/DCL/SDKComponents/SceneUI/Components.meta

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using DCL.Optimization.Pools;
using System;
using UnityEngine.UIElements;

namespace DCL.SDKComponents.SceneUI.Components
{
public struct UITextComponent: IPoolableComponentProvider<Label>
{
public Label Label;

Label IPoolableComponentProvider<Label>.PoolableComponent => Label;
Type IPoolableComponentProvider<Label>.PoolableComponentType => typeof(Label);

public void Dispose() { }
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Arch.Core;
using DCL.Optimization.Pools;
using System;
using System.Collections.Generic;
using UnityEngine.Pool;
using UnityEngine.UIElements;

namespace DCL.SDKComponents.SceneUI.Components
{
public struct UITransformComponent : IPoolableComponentProvider<VisualElement>
{
public VisualElement Transform;
public EntityReference Parent;
public HashSet<EntityReference> Children;
public bool IsHidden;

VisualElement IPoolableComponentProvider<VisualElement>.PoolableComponent => Transform;
Type IPoolableComponentProvider<VisualElement>.PoolableComponentType => typeof(VisualElement);

public void Dispose()
{
HashSetPool<EntityReference>.Release(Children);
}
}
}

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

3 changes: 3 additions & 0 deletions Explorer/Assets/DCL/SDKComponents/SceneUI/Defaults.meta

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using DCL.ECSComponents;
using DCL.SDKComponents.SceneUI.Utils;
using ECS.Unity.ColorComponent;
using UnityEngine;
using Font = DCL.ECSComponents.Font;

namespace DCL.SDKComponents.SceneUI.Defaults
{
public static class PBUiTextDefaults
{
public static Color GetColor(this PBUiText self) =>
self.Color?.ToUnityColor() ?? ColorDefaults.COLOR_WHITE;

public static TextAnchor GetTextAlign(this PBUiText self) =>
(self.HasTextAlign ? self.TextAlign : TextAlignMode.TamMiddleCenter).ToUnityTextAlign();

public static Font GetFont(this PBUiText self) =>
self.HasFont ? self.Font : Font.FSansSerif;

public static float GetFontSize(this PBUiText self) =>
self.HasFontSize ? self.FontSize : 10;
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using DCL.ECSComponents;

namespace DCL.SDKComponents.SceneUI.Defaults
{
public static class PBUiTransform_Defaults
{
public static float GetFlexShrink(this PBUiTransform self) =>
self.HasFlexShrink ? self.FlexShrink : 1;

public static YGAlign GetAlignItems(this PBUiTransform self) =>
self.HasAlignItems ? self.AlignItems : YGAlign.YgaStretch;

public static YGAlign GetAlignContent(this PBUiTransform self) =>
self.HasAlignContent ? self.AlignContent : YGAlign.YgaStretch;

public static YGWrap GetFlexWrap(this PBUiTransform self) =>
self.HasFlexWrap ? self.FlexWrap : YGWrap.YgwNoWrap;
}
}

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

3 changes: 3 additions & 0 deletions Explorer/Assets/DCL/SDKComponents/SceneUI/Groups.meta

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Arch.SystemGroups;
using Arch.SystemGroups.Throttling;
using DCL.SDKComponents.SceneUI.Systems;
using ECS.Groups;
using UITransformInstantiationSystem = DCL.SDKComponents.SceneUI.Systems.UITransform.UITransformInstantiationSystem;

namespace DCL.SDKComponents.SceneUI.Groups
{
/// <summary>
/// Denotes the group that instantiates specific components right after the entity transform is handled
/// </summary>
[UpdateInGroup(typeof(SyncedSimulationSystemGroup))]
[UpdateAfter(typeof(UITransformInstantiationSystem))]
[ThrottlingEnabled]
public partial class SceneUIComponentInstantiationGroup { }
}

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

Loading

0 comments on commit 19f0d41

Please sign in to comment.