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