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