| | 1 | | using DCL; |
| | 2 | | using DCL.Controllers; |
| | 3 | | using DCL.ECS7.InternalComponents; |
| | 4 | | using DCL.ECSRuntime; |
| | 5 | | using DCL.Models; |
| | 6 | | using System; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using UnityEngine.UIElements; |
| | 9 | |
|
| | 10 | | namespace ECSSystems.ScenesUiSystem |
| | 11 | | { |
| | 12 | | public class ECSScenesUiSystem : IDisposable |
| | 13 | | { |
| | 14 | | private readonly UIDocument uiDocument; |
| | 15 | | private readonly IInternalECSComponent<InternalUiContainer> internalUiContainerComponent; |
| | 16 | | private readonly IWorldState worldState; |
| | 17 | | private readonly BaseList<IParcelScene> loadedScenes; |
| | 18 | | private readonly BaseVariable<bool> loadingHudVisibleVariable; |
| | 19 | |
|
| | 20 | | private int lastSceneNumber; |
| | 21 | | private bool isPendingSceneUI; |
| | 22 | | private IParcelScene currentScene; |
| | 23 | |
|
| 8 | 24 | | public ECSScenesUiSystem(UIDocument uiDocument, |
| | 25 | | IInternalECSComponent<InternalUiContainer> internalUiContainerComponent, |
| | 26 | | BaseList<IParcelScene> loadedScenes, |
| | 27 | | IWorldState worldState, |
| | 28 | | BaseVariable<bool> loadingHudVisibleVariable) |
| | 29 | | { |
| 8 | 30 | | this.uiDocument = uiDocument; |
| 8 | 31 | | this.internalUiContainerComponent = internalUiContainerComponent; |
| 8 | 32 | | this.worldState = worldState; |
| 8 | 33 | | this.loadedScenes = loadedScenes; |
| 8 | 34 | | this.loadingHudVisibleVariable = loadingHudVisibleVariable; |
| | 35 | |
|
| 8 | 36 | | lastSceneNumber = -1; |
| 8 | 37 | | isPendingSceneUI = true; |
| 8 | 38 | | currentScene = null; |
| | 39 | |
|
| 8 | 40 | | loadedScenes.OnRemoved += LoadedScenesOnOnRemoved; |
| 8 | 41 | | loadingHudVisibleVariable.OnChange += LoadingHudVisibleOnOnChange; |
| | 42 | |
|
| 8 | 43 | | LoadingHudVisibleOnOnChange(loadingHudVisibleVariable.Get(), false); |
| 8 | 44 | | } |
| | 45 | |
|
| | 46 | | public void Dispose() |
| | 47 | | { |
| 1 | 48 | | loadedScenes.OnRemoved -= LoadedScenesOnOnRemoved; |
| 1 | 49 | | loadingHudVisibleVariable.OnChange -= LoadingHudVisibleOnOnChange; |
| 1 | 50 | | } |
| | 51 | |
|
| | 52 | | public void Update() |
| | 53 | | { |
| 15 | 54 | | int currentSceneNumber = worldState.GetCurrentSceneNumber(); |
| 15 | 55 | | bool sceneChanged = lastSceneNumber != currentSceneNumber; |
| 15 | 56 | | lastSceneNumber = currentSceneNumber; |
| | 57 | |
|
| 15 | 58 | | HashSet<IParcelScene> scenesUiToSort = ApplyParenting(uiDocument, internalUiContainerComponent, currentScene |
| | 59 | |
|
| | 60 | | // If parenting detects that the order for ui elements has changed, it should sort the ui tree |
| 15 | 61 | | if (scenesUiToSort.Count > 0) |
| | 62 | | { |
| 0 | 63 | | SortSceneUiTree(internalUiContainerComponent, scenesUiToSort); |
| | 64 | | } |
| | 65 | |
|
| | 66 | | // clear UI if scene changed |
| 15 | 67 | | if (sceneChanged && !isPendingSceneUI) |
| | 68 | | { |
| 4 | 69 | | ClearCurrentSceneUI(uiDocument, currentScene, internalUiContainerComponent); |
| 4 | 70 | | isPendingSceneUI = currentSceneNumber > 0; |
| | 71 | | } |
| | 72 | |
|
| 15 | 73 | | if (sceneChanged && currentScene != null && currentSceneNumber != currentScene.sceneData.sceneNumber) |
| | 74 | | { |
| 5 | 75 | | currentScene = null; |
| | 76 | | } |
| | 77 | |
|
| | 78 | | // UI not set for current scene yet |
| 15 | 79 | | if (isPendingSceneUI) |
| | 80 | | { |
| | 81 | | // we get current scene reference |
| 12 | 82 | | currentScene ??= GetCurrentScene(currentSceneNumber, loadedScenes); |
| | 83 | |
|
| | 84 | | // we apply current scene UI |
| 12 | 85 | | if (currentScene != null) |
| | 86 | | { |
| 9 | 87 | | if (ApplySceneUI(internalUiContainerComponent, uiDocument, currentScene)) |
| | 88 | | { |
| 7 | 89 | | isPendingSceneUI = false; |
| | 90 | | } |
| | 91 | | } |
| | 92 | | } |
| 15 | 93 | | } |
| | 94 | |
|
| | 95 | | private void LoadedScenesOnOnRemoved(IParcelScene scene) |
| | 96 | | { |
| 0 | 97 | | if (scene.sceneData.sceneNumber == lastSceneNumber) |
| | 98 | | { |
| 0 | 99 | | lastSceneNumber = -1; |
| | 100 | | } |
| 0 | 101 | | } |
| | 102 | |
|
| | 103 | | private void LoadingHudVisibleOnOnChange(bool current, bool previous) |
| | 104 | | { |
| 10 | 105 | | SetDocumentActive(uiDocument, !current); |
| 10 | 106 | | } |
| | 107 | |
|
| | 108 | | internal static HashSet<IParcelScene> ApplyParenting(UIDocument uiDocument, |
| | 109 | | IInternalECSComponent<InternalUiContainer> internalUiContainerComponent, int currentSceneNumber) |
| | 110 | | { |
| 20 | 111 | | HashSet<IParcelScene> scenesToSort = new HashSet<IParcelScene>(); |
| | 112 | |
|
| | 113 | | // check for orphan ui containers |
| 20 | 114 | | var allContainers = internalUiContainerComponent.GetForAll(); |
| | 115 | |
|
| 102 | 116 | | for (int i = 0; i < allContainers.Count; i++) |
| | 117 | | { |
| 31 | 118 | | var uiContainerData = allContainers[i].value; |
| | 119 | |
|
| | 120 | | // check if ui should be sort (only current and global scenes) |
| 31 | 121 | | if (uiContainerData.model.shouldSort |
| | 122 | | && (uiContainerData.scene.isPersistent || uiContainerData.scene.sceneData.sceneNumber == currentScen |
| | 123 | | { |
| 1 | 124 | | scenesToSort.Add(uiContainerData.scene); |
| | 125 | | } |
| | 126 | |
|
| | 127 | | // add global scenes ui but |
| | 128 | | // skip non-global scenes ui containers on root entity since no parenting is needed |
| 31 | 129 | | if (uiContainerData.entity.entityId == SpecialEntityId.SCENE_ROOT_ENTITY) |
| | 130 | | { |
| 25 | 131 | | var model = uiContainerData.model; |
| | 132 | |
|
| 25 | 133 | | if (uiContainerData.scene.isPersistent && model.parentElement == null) |
| | 134 | | { |
| 2 | 135 | | uiDocument.rootVisualElement.Add(model.rootElement); |
| 2 | 136 | | model.parentElement = uiDocument.rootVisualElement; |
| 2 | 137 | | internalUiContainerComponent.PutFor(uiContainerData.scene, uiContainerData.entity, model); |
| | 138 | | } |
| | 139 | |
|
| 2 | 140 | | continue; |
| | 141 | | } |
| | 142 | |
|
| | 143 | | // skip containers with parent already set |
| 6 | 144 | | if (uiContainerData.model.parentElement != null) |
| | 145 | | continue; |
| | 146 | |
|
| 5 | 147 | | InternalUiContainer parentDataModel = GetParentContainerModel( |
| | 148 | | internalUiContainerComponent, |
| | 149 | | uiContainerData.scene, |
| | 150 | | uiContainerData.model.parentId); |
| | 151 | |
|
| | 152 | | // apply parenting |
| 5 | 153 | | if (parentDataModel != null) |
| | 154 | | { |
| 4 | 155 | | var currentContainerModel = uiContainerData.model; |
| 4 | 156 | | parentDataModel.rootElement.Add(uiContainerData.model.rootElement); |
| 4 | 157 | | currentContainerModel.parentElement = parentDataModel.rootElement; |
| | 158 | |
|
| 4 | 159 | | internalUiContainerComponent.PutFor(uiContainerData.scene, uiContainerData.model.parentId, parentDat |
| 4 | 160 | | internalUiContainerComponent.PutFor(uiContainerData.scene, uiContainerData.entity, currentContainerM |
| | 161 | | } |
| | 162 | | } |
| | 163 | |
|
| 20 | 164 | | return scenesToSort; |
| | 165 | | } |
| | 166 | |
|
| | 167 | | internal static void ClearCurrentSceneUI(UIDocument uiDocument, IParcelScene currentScene, |
| | 168 | | IInternalECSComponent<InternalUiContainer> internalUiContainerComponent) |
| | 169 | | { |
| 5 | 170 | | IECSReadOnlyComponentData<InternalUiContainer> currentSceneContainer = |
| | 171 | | internalUiContainerComponent.GetFor(currentScene, SpecialEntityId.SCENE_ROOT_ENTITY); |
| | 172 | |
|
| 5 | 173 | | if (currentSceneContainer == null) |
| 0 | 174 | | return; |
| | 175 | |
|
| 5 | 176 | | InternalUiContainer model = currentSceneContainer.model; |
| 5 | 177 | | model.parentElement = null; |
| | 178 | |
|
| 5 | 179 | | if (uiDocument.rootVisualElement.Contains(model.rootElement)) |
| | 180 | | { |
| 5 | 181 | | uiDocument.rootVisualElement.Remove(model.rootElement); |
| | 182 | | } |
| | 183 | |
|
| 5 | 184 | | internalUiContainerComponent.PutFor(currentScene, SpecialEntityId.SCENE_ROOT_ENTITY, model); |
| 5 | 185 | | } |
| | 186 | |
|
| | 187 | | internal static IParcelScene GetCurrentScene(int sceneNumber, IList<IParcelScene> loadedScenes) |
| | 188 | | { |
| 14 | 189 | | if (sceneNumber <= 0) |
| 2 | 190 | | return null; |
| | 191 | |
|
| 12 | 192 | | IParcelScene currentScene = null; |
| | 193 | |
|
| 30 | 194 | | for (int i = 0; i < loadedScenes.Count; i++) |
| | 195 | | { |
| 12 | 196 | | if (loadedScenes[i].sceneData.sceneNumber == sceneNumber) |
| | 197 | | { |
| 9 | 198 | | currentScene = loadedScenes[i]; |
| 9 | 199 | | break; |
| | 200 | | } |
| | 201 | | } |
| | 202 | |
|
| 12 | 203 | | return currentScene; |
| | 204 | | } |
| | 205 | |
|
| | 206 | | internal static bool ApplySceneUI(IInternalECSComponent<InternalUiContainer> internalUiContainerComponent, |
| | 207 | | UIDocument uiDocument, IParcelScene currentScene) |
| | 208 | | { |
| 11 | 209 | | IECSReadOnlyComponentData<InternalUiContainer> sceneRootUiContainer = |
| | 210 | | internalUiContainerComponent.GetFor(currentScene, SpecialEntityId.SCENE_ROOT_ENTITY); |
| | 211 | |
|
| 11 | 212 | | if (sceneRootUiContainer != null) |
| | 213 | | { |
| 8 | 214 | | var model = sceneRootUiContainer.model; |
| 8 | 215 | | uiDocument.rootVisualElement.Insert(0, model.rootElement); |
| 8 | 216 | | model.parentElement = uiDocument.rootVisualElement; |
| 8 | 217 | | internalUiContainerComponent.PutFor(currentScene, SpecialEntityId.SCENE_ROOT_ENTITY, model); |
| 8 | 218 | | return true; |
| | 219 | | } |
| | 220 | |
|
| 3 | 221 | | return false; |
| | 222 | | } |
| | 223 | |
|
| | 224 | | private static InternalUiContainer GetParentContainerModel(IInternalECSComponent<InternalUiContainer> internalUi |
| | 225 | | IParcelScene scene, long parentId) |
| | 226 | | { |
| 5 | 227 | | InternalUiContainer parentDataModel = |
| | 228 | | internalUiContainerComponent.GetFor(scene, parentId)?.model; |
| | 229 | |
|
| | 230 | | // create root entity ui container if needed |
| 5 | 231 | | if (parentDataModel == null && parentId == SpecialEntityId.SCENE_ROOT_ENTITY) |
| | 232 | | { |
| 3 | 233 | | parentDataModel = new InternalUiContainer(); |
| | 234 | | } |
| | 235 | |
|
| 5 | 236 | | return parentDataModel; |
| | 237 | | } |
| | 238 | |
|
| | 239 | | internal static void SortSceneUiTree(IInternalECSComponent<InternalUiContainer> internalUiContainerComponent, |
| | 240 | | ICollection<IParcelScene> scenesToSort) |
| | 241 | | { |
| 2 | 242 | | Dictionary<VisualElement, Dictionary<long, RightOfData>> sortSceneTree = new Dictionary<VisualElement, Dicti |
| | 243 | |
|
| | 244 | | // Setup UI sorting |
| 2 | 245 | | var allContainers = internalUiContainerComponent.GetForAll(); |
| | 246 | |
|
| 24 | 247 | | for (int i = 0; i < allContainers.Count; i++) |
| | 248 | | { |
| 10 | 249 | | var uiContainerData = allContainers[i].value; |
| | 250 | |
|
| 10 | 251 | | if (!scenesToSort.Contains(uiContainerData.scene)) |
| | 252 | | continue; |
| | 253 | |
|
| 10 | 254 | | InternalUiContainer model = uiContainerData.model; |
| | 255 | |
|
| | 256 | | // If not parented yet, we skip it |
| 10 | 257 | | if (model.parentElement == null) |
| | 258 | | continue; |
| | 259 | |
|
| | 260 | | // Ignore root scene UI container |
| 8 | 261 | | if (uiContainerData.entity.entityId == SpecialEntityId.SCENE_ROOT_ENTITY) |
| | 262 | | continue; |
| | 263 | |
|
| 8 | 264 | | if (!sortSceneTree.TryGetValue(model.parentElement, out var sceneTreeDictionary)) |
| | 265 | | { |
| 2 | 266 | | sceneTreeDictionary = new Dictionary<long, RightOfData>(); |
| 2 | 267 | | sortSceneTree[model.parentElement] = sceneTreeDictionary; |
| | 268 | | } |
| | 269 | |
|
| 8 | 270 | | sceneTreeDictionary[model.rigthOf] = new RightOfData(model.rootElement, |
| | 271 | | uiContainerData.entity.entityId); |
| | 272 | |
|
| 8 | 273 | | model.shouldSort = false; |
| 8 | 274 | | internalUiContainerComponent.PutFor(uiContainerData.scene, uiContainerData.entity.entityId, model); |
| | 275 | | } |
| | 276 | |
|
| | 277 | | // Apply UI sorting |
| 8 | 278 | | foreach (var pairs in sortSceneTree) |
| | 279 | | { |
| 2 | 280 | | VisualElement parentElement = pairs.Key; |
| 2 | 281 | | Dictionary<long, RightOfData> sceneSort = pairs.Value; |
| | 282 | |
|
| 2 | 283 | | int index = 0; |
| 2 | 284 | | long nextElementId = 0; |
| | 285 | |
|
| 10 | 286 | | while (sceneSort.TryGetValue(nextElementId, out RightOfData rightOfData)) |
| | 287 | | { |
| 8 | 288 | | sceneSort.Remove(nextElementId); |
| 8 | 289 | | parentElement.Remove(rightOfData.element); |
| 8 | 290 | | parentElement.Insert(index, rightOfData.element); |
| 8 | 291 | | nextElementId = rightOfData.id; |
| 8 | 292 | | index++; |
| 8 | 293 | | } |
| | 294 | | } |
| 2 | 295 | | } |
| | 296 | |
|
| | 297 | | private static void SetDocumentActive(UIDocument uiDocument, bool active) |
| | 298 | | { |
| 10 | 299 | | uiDocument.rootVisualElement.style.display = active ? DisplayStyle.Flex : DisplayStyle.None; |
| 10 | 300 | | } |
| | 301 | |
|
| | 302 | | private readonly struct RightOfData |
| | 303 | | { |
| | 304 | | public readonly long id; |
| | 305 | | public readonly VisualElement element; |
| | 306 | |
|
| | 307 | | public RightOfData(VisualElement element, long id) |
| | 308 | | { |
| 8 | 309 | | this.id = id; |
| 8 | 310 | | this.element = element; |
| 8 | 311 | | } |
| | 312 | | } |
| | 313 | | } |
| | 314 | | } |