| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL; |
| | 4 | | using DCL.Controllers; |
| | 5 | | using DCL.ECS7.InternalComponents; |
| | 6 | | using DCL.ECSRuntime; |
| | 7 | | using DCL.Models; |
| | 8 | | using UnityEngine.UIElements; |
| | 9 | |
|
| | 10 | | namespace ECSSystems.ScenesUiSystem |
| | 11 | | { |
| | 12 | | public class ECSScenesUiSystem : IDisposable |
| | 13 | | { |
| | 14 | | private class State |
| | 15 | | { |
| | 16 | | public UIDocument uiDocument; |
| | 17 | | public IInternalECSComponent<InternalUiContainer> internalUiContainerComponent; |
| | 18 | | public IWorldState worldState; |
| | 19 | | public BaseList<IParcelScene> loadedScenes; |
| | 20 | | public string lastSceneId; |
| | 21 | | public bool isPendingSceneUI; |
| | 22 | | public IParcelScene currentScene; |
| | 23 | | } |
| | 24 | |
|
| | 25 | | private readonly State state; |
| | 26 | |
|
| 6 | 27 | | public ECSScenesUiSystem(UIDocument uiDocument, |
| | 28 | | IInternalECSComponent<InternalUiContainer> internalUiContainerComponent, |
| | 29 | | BaseList<IParcelScene> loadedScenes, |
| | 30 | | IWorldState worldState) |
| | 31 | | { |
| 6 | 32 | | state = new State() |
| | 33 | | { |
| | 34 | | uiDocument = uiDocument, |
| | 35 | | internalUiContainerComponent = internalUiContainerComponent, |
| | 36 | | worldState = worldState, |
| | 37 | | loadedScenes = loadedScenes, |
| | 38 | | lastSceneId = null, |
| | 39 | | isPendingSceneUI = true, |
| | 40 | | currentScene = null |
| | 41 | | }; |
| | 42 | |
|
| 6 | 43 | | state.loadedScenes.OnRemoved += LoadedScenesOnOnRemoved; |
| 6 | 44 | | } |
| | 45 | |
|
| | 46 | | public void Dispose() |
| | 47 | | { |
| 1 | 48 | | state.loadedScenes.OnRemoved -= LoadedScenesOnOnRemoved; |
| 1 | 49 | | } |
| | 50 | |
|
| | 51 | | public void Update() |
| | 52 | | { |
| 12 | 53 | | string currentSceneId = state.worldState.GetCurrentSceneId(); |
| 12 | 54 | | bool sceneChanged = state.lastSceneId != currentSceneId; |
| 12 | 55 | | state.lastSceneId = currentSceneId; |
| | 56 | |
|
| 12 | 57 | | ApplyParenting(state.uiDocument, state.internalUiContainerComponent); |
| | 58 | |
|
| | 59 | | // clear UI if scene changed |
| 12 | 60 | | if (sceneChanged && !state.isPendingSceneUI) |
| | 61 | | { |
| 3 | 62 | | ClearCurrentSceneUI(state.uiDocument); |
| 3 | 63 | | state.isPendingSceneUI = !string.IsNullOrEmpty(currentSceneId); |
| | 64 | | } |
| 12 | 65 | | if (sceneChanged && state.currentScene != null && currentSceneId != state.currentScene.sceneData.id) |
| | 66 | | { |
| 4 | 67 | | state.currentScene = null; |
| | 68 | | } |
| | 69 | |
|
| | 70 | | // UI not set for current scene yet |
| 12 | 71 | | if (state.isPendingSceneUI) |
| | 72 | | { |
| | 73 | | // we get current scene reference |
| 12 | 74 | | state.currentScene ??= GetCurrentScene(currentSceneId, state.loadedScenes); |
| | 75 | |
|
| | 76 | | // we apply current scene UI |
| 12 | 77 | | if (state.currentScene != null) |
| | 78 | | { |
| 8 | 79 | | if (ApplySceneUI(state.internalUiContainerComponent, state.uiDocument, state.currentScene)) |
| | 80 | | { |
| 6 | 81 | | state.isPendingSceneUI = false; |
| | 82 | | } |
| | 83 | | } |
| | 84 | | } |
| 12 | 85 | | } |
| | 86 | |
|
| | 87 | | private void LoadedScenesOnOnRemoved(IParcelScene scene) |
| | 88 | | { |
| 0 | 89 | | if (scene.sceneData.id == state.lastSceneId) |
| | 90 | | { |
| 0 | 91 | | state.lastSceneId = null; |
| | 92 | | } |
| 0 | 93 | | } |
| | 94 | |
|
| | 95 | | internal static void ApplyParenting(UIDocument uiDocument, IInternalECSComponent<InternalUiContainer> internalUi |
| | 96 | | { |
| | 97 | | // check for orphan ui containers |
| 15 | 98 | | var allContainers = internalUiContainerComponent.GetForAll(); |
| 80 | 99 | | for (int i = 0; i < allContainers.Count; i++) |
| | 100 | | { |
| 25 | 101 | | var uiContainerData = allContainers[i].value; |
| | 102 | |
|
| | 103 | | // add global scenes ui but |
| | 104 | | // skip non-global scenes ui containers on root entity since no parenting is needed |
| 25 | 105 | | if (uiContainerData.entity.entityId == SpecialEntityId.SCENE_ROOT_ENTITY) |
| | 106 | | { |
| 21 | 107 | | var model = uiContainerData.model; |
| 21 | 108 | | if (uiContainerData.scene.isPersistent && model.parentElement == null) |
| | 109 | | { |
| 2 | 110 | | uiDocument.rootVisualElement.Add(model.rootElement); |
| 2 | 111 | | model.parentElement = uiDocument.rootVisualElement; |
| 2 | 112 | | internalUiContainerComponent.PutFor(uiContainerData.scene, uiContainerData.entity, model); |
| | 113 | | } |
| 2 | 114 | | continue; |
| | 115 | | } |
| | 116 | |
|
| | 117 | | // skip containers with parent already set |
| 4 | 118 | | if (uiContainerData.model.parentElement != null) |
| | 119 | | continue; |
| | 120 | |
|
| 4 | 121 | | InternalUiContainer parentDataModel = GetParentContainerModel( |
| | 122 | | internalUiContainerComponent, |
| | 123 | | uiContainerData.scene, |
| | 124 | | uiContainerData.model.parentId); |
| | 125 | |
|
| | 126 | | // apply parenting |
| 4 | 127 | | if (parentDataModel != null) |
| | 128 | | { |
| 3 | 129 | | var currentContainerModel = uiContainerData.model; |
| 3 | 130 | | parentDataModel.rootElement.Add(uiContainerData.model.rootElement); |
| 3 | 131 | | currentContainerModel.parentElement = parentDataModel.rootElement; |
| | 132 | |
|
| 3 | 133 | | internalUiContainerComponent.PutFor(uiContainerData.scene, uiContainerData.model.parentId, parentDat |
| 3 | 134 | | internalUiContainerComponent.PutFor(uiContainerData.scene, uiContainerData.entity, currentContainerM |
| | 135 | | } |
| | 136 | | } |
| 15 | 137 | | } |
| | 138 | |
|
| | 139 | | internal static void ClearCurrentSceneUI(UIDocument uiDocument) |
| | 140 | | { |
| 4 | 141 | | if (uiDocument.rootVisualElement.childCount > 0) |
| | 142 | | { |
| 4 | 143 | | uiDocument.rootVisualElement.RemoveAt(0); |
| | 144 | | } |
| 4 | 145 | | } |
| | 146 | |
|
| | 147 | | internal static IParcelScene GetCurrentScene(string sceneId, IList<IParcelScene> loadedScenes) |
| | 148 | | { |
| 14 | 149 | | if (string.IsNullOrEmpty(sceneId)) |
| 1 | 150 | | return null; |
| | 151 | |
|
| 13 | 152 | | IParcelScene currentScene = null; |
| 44 | 153 | | for (int i = 0; i < loadedScenes.Count; i++) |
| | 154 | | { |
| 17 | 155 | | if (loadedScenes[i].sceneData.id == sceneId) |
| | 156 | | { |
| 8 | 157 | | currentScene = loadedScenes[i]; |
| 8 | 158 | | break; |
| | 159 | | } |
| | 160 | | } |
| | 161 | |
|
| 13 | 162 | | return currentScene; |
| | 163 | | } |
| | 164 | |
|
| | 165 | | internal static bool ApplySceneUI(IInternalECSComponent<InternalUiContainer> internalUiContainerComponent, |
| | 166 | | UIDocument uiDocument, IParcelScene currentScene) |
| | 167 | | { |
| 10 | 168 | | IECSReadOnlyComponentData<InternalUiContainer> sceneRootUiContainer = |
| | 169 | | internalUiContainerComponent.GetFor(currentScene, SpecialEntityId.SCENE_ROOT_ENTITY); |
| | 170 | |
|
| 10 | 171 | | if (sceneRootUiContainer != null) |
| | 172 | | { |
| 7 | 173 | | var model = sceneRootUiContainer.model; |
| 7 | 174 | | uiDocument.rootVisualElement.Insert(0, model.rootElement); |
| 7 | 175 | | model.parentElement = uiDocument.rootVisualElement; |
| 7 | 176 | | internalUiContainerComponent.PutFor(currentScene, SpecialEntityId.SCENE_ROOT_ENTITY, model); |
| 7 | 177 | | return true; |
| | 178 | | } |
| 3 | 179 | | return false; |
| | 180 | | } |
| | 181 | |
|
| | 182 | | private static InternalUiContainer GetParentContainerModel(IInternalECSComponent<InternalUiContainer> internalUi |
| | 183 | | IParcelScene scene, long parentId) |
| | 184 | | { |
| 4 | 185 | | InternalUiContainer parentDataModel = |
| | 186 | | internalUiContainerComponent.GetFor(scene, parentId)?.model; |
| | 187 | |
|
| | 188 | | // create root entity ui container if needed |
| 4 | 189 | | if (parentDataModel == null && parentId == SpecialEntityId.SCENE_ROOT_ENTITY) |
| | 190 | | { |
| 2 | 191 | | parentDataModel = new InternalUiContainer(); |
| | 192 | | } |
| 4 | 193 | | return parentDataModel; |
| | 194 | | } |
| | 195 | | } |
| | 196 | | } |