| | 1 | | using DCL.Helpers; |
| | 2 | | using DCL.Models; |
| | 3 | | using System.Collections; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using DCL.Components.Interfaces; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.Assertions; |
| | 8 | | using UnityEngine.UI; |
| | 9 | | using Object = UnityEngine.Object; |
| | 10 | | using Decentraland.Sdk.Ecs6; |
| | 11 | | using MainScripts.DCL.Components; |
| | 12 | |
|
| | 13 | | namespace DCL.Components |
| | 14 | | { |
| | 15 | | public class UIShape<ReferencesContainerType, ModelType> : UIShape |
| | 16 | | where ReferencesContainerType : UIReferencesContainer |
| | 17 | | where ModelType : UIShape.Model |
| | 18 | | { |
| | 19 | | public const float RAYCAST_ALPHA_THRESHOLD = 0.01f; |
| | 20 | |
|
| | 21 | | public UIShape() { } |
| | 22 | |
|
| | 23 | | new public ModelType model { get { return base.model as ModelType; } set { base.model = value; } } |
| | 24 | |
|
| | 25 | | new public ReferencesContainerType referencesContainer { get { return base.referencesContainer as ReferencesCont |
| | 26 | |
|
| | 27 | | public override ComponentUpdateHandler CreateUpdateHandler() { return new UIShapeUpdateHandler<ReferencesContain |
| | 28 | |
|
| | 29 | | bool raiseOnAttached; |
| | 30 | | bool firstApplyChangesCall; |
| | 31 | |
|
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// This is called by UIShapeUpdateHandler before calling ApplyChanges. |
| | 35 | | /// </summary> |
| | 36 | | public void PreApplyChanges(BaseModel newModel) |
| | 37 | | { |
| | 38 | | model = (ModelType) newModel; |
| | 39 | |
|
| | 40 | | raiseOnAttached = false; |
| | 41 | | firstApplyChangesCall = false; |
| | 42 | |
|
| | 43 | | if (referencesContainer == null) |
| | 44 | | { |
| | 45 | | referencesContainer = InstantiateUIGameObject<ReferencesContainerType>(referencesContainerPrefabName); |
| | 46 | |
|
| | 47 | | raiseOnAttached = true; |
| | 48 | | firstApplyChangesCall = true; |
| | 49 | | } |
| | 50 | | else if (ReparentComponent(referencesContainer.rectTransform, model.parentComponent)) |
| | 51 | | { |
| | 52 | | raiseOnAttached = true; |
| | 53 | | } |
| | 54 | | } |
| | 55 | |
|
| | 56 | | public override void RaiseOnAppliedChanges() |
| | 57 | | { |
| | 58 | | RefreshDCLLayout(); |
| | 59 | |
|
| | 60 | | #if UNITY_EDITOR |
| | 61 | | SetComponentDebugName(); |
| | 62 | | #endif |
| | 63 | |
|
| | 64 | | // We hide the component visibility when it's created (first applychanges) |
| | 65 | | // as it has default values and appears in the middle of the screen |
| | 66 | | if (firstApplyChangesCall) |
| | 67 | | referencesContainer.canvasGroup.alpha = 0f; |
| | 68 | | else |
| | 69 | | referencesContainer.canvasGroup.alpha = model.visible ? model.opacity : 0f; |
| | 70 | |
|
| | 71 | | referencesContainer.canvasGroup.blocksRaycasts = model.visible && model.isPointerBlocker; |
| | 72 | |
|
| | 73 | | base.RaiseOnAppliedChanges(); |
| | 74 | |
|
| | 75 | | if (raiseOnAttached && parentUIComponent != null) |
| | 76 | | { |
| | 77 | | UIReferencesContainer[] parents = referencesContainer.GetComponentsInParent<UIReferencesContainer>(true) |
| | 78 | |
|
| | 79 | | for (int i = 0; i < parents.Length; i++) |
| | 80 | | { |
| | 81 | | UIReferencesContainer parent = parents[i]; |
| | 82 | | if (parent.owner != null) |
| | 83 | | { |
| | 84 | | parent.owner.OnChildAttached(parentUIComponent, this); |
| | 85 | | } |
| | 86 | | } |
| | 87 | | } |
| | 88 | | } |
| | 89 | | } |
| | 90 | |
|
| | 91 | | public class UIShape : BaseDisposable, IUIRefreshable |
| | 92 | | { |
| | 93 | | [System.Serializable] |
| | 94 | | public class Model : BaseModel |
| | 95 | | { |
| | 96 | | public string name; |
| | 97 | | public string parentComponent; |
| 643 | 98 | | public bool visible = true; |
| 643 | 99 | | public float opacity = 1f; |
| 643 | 100 | | public string hAlign = "center"; |
| 643 | 101 | | public string vAlign = "center"; |
| 643 | 102 | | public UIValue width = new (100f); |
| 643 | 103 | | public UIValue height = new (50f); |
| 643 | 104 | | public UIValue positionX = new (0f); |
| 643 | 105 | | public UIValue positionY = new (0f); |
| 643 | 106 | | public bool isPointerBlocker = true; |
| | 107 | | public string onClick; |
| | 108 | |
|
| | 109 | | public override BaseModel GetDataFromJSON(string json) => |
| 54 | 110 | | Utils.SafeFromJson<Model>(json); |
| | 111 | |
|
| | 112 | | public override BaseModel GetDataFromPb(ComponentBodyPayload pbModel) |
| | 113 | | { |
| | 114 | |
|
| 0 | 115 | | switch (pbModel.PayloadCase) |
| | 116 | | { |
| | 117 | | case ComponentBodyPayload.PayloadOneofCase.UiShape: |
| 0 | 118 | | var uiShapeModel = new Model(); |
| 0 | 119 | | if (pbModel.UiShape.HasName) uiShapeModel.name = pbModel.UiShape.Name; |
| 0 | 120 | | if (pbModel.UiShape.HasParentComponent) uiShapeModel.parentComponent = pbModel.UiShape.ParentCom |
| 0 | 121 | | if (pbModel.UiShape.HasVisible) uiShapeModel.visible = pbModel.UiShape.Visible; |
| 0 | 122 | | if (pbModel.UiShape.HasOpacity) uiShapeModel.opacity = pbModel.UiShape.Opacity; |
| 0 | 123 | | if (pbModel.UiShape.HasHAlign) uiShapeModel.hAlign = pbModel.UiShape.HAlign; |
| 0 | 124 | | if (pbModel.UiShape.HasVAlign) uiShapeModel.vAlign = pbModel.UiShape.VAlign; |
| 0 | 125 | | if (pbModel.UiShape.Width != null) uiShapeModel.width = SDK6DataMapExtensions.FromProtobuf(uiSha |
| 0 | 126 | | if (pbModel.UiShape.Height != null) uiShapeModel.height = SDK6DataMapExtensions.FromProtobuf(uiS |
| 0 | 127 | | if (pbModel.UiShape.PositionX != null) uiShapeModel.positionX = SDK6DataMapExtensions.FromProtob |
| 0 | 128 | | if (pbModel.UiShape.PositionY != null) uiShapeModel.positionY = SDK6DataMapExtensions.FromProtob |
| 0 | 129 | | if (pbModel.UiShape.HasIsPointerBlocker) uiShapeModel.isPointerBlocker = pbModel.UiShape.IsPoint |
| 0 | 130 | | return uiShapeModel; |
| | 131 | |
|
| | 132 | | case ComponentBodyPayload.PayloadOneofCase.UiScreenSpaceShape: |
| 0 | 133 | | var screenSpaceModel = new Model(); |
| 0 | 134 | | if (pbModel.UiScreenSpaceShape.HasName) screenSpaceModel.name = pbModel.UiScreenSpaceShape.Name; |
| 0 | 135 | | if (pbModel.UiScreenSpaceShape.HasParentComponent) screenSpaceModel.parentComponent = pbModel.Ui |
| 0 | 136 | | if (pbModel.UiScreenSpaceShape.HasVisible) screenSpaceModel.visible = pbModel.UiScreenSpaceShape |
| 0 | 137 | | if (pbModel.UiScreenSpaceShape.HasOpacity) screenSpaceModel.opacity = pbModel.UiScreenSpaceShape |
| 0 | 138 | | if (pbModel.UiScreenSpaceShape.HasHAlign) screenSpaceModel.hAlign = pbModel.UiScreenSpaceShape.H |
| 0 | 139 | | if (pbModel.UiScreenSpaceShape.HasVAlign) screenSpaceModel.vAlign = pbModel.UiScreenSpaceShape.V |
| 0 | 140 | | if (pbModel.UiScreenSpaceShape.Width != null) screenSpaceModel.width = SDK6DataMapExtensions.Fro |
| 0 | 141 | | if (pbModel.UiScreenSpaceShape.Height != null) screenSpaceModel.height = SDK6DataMapExtensions.F |
| 0 | 142 | | if (pbModel.UiScreenSpaceShape.PositionX != null) screenSpaceModel.positionX = SDK6DataMapExtens |
| 0 | 143 | | if (pbModel.UiScreenSpaceShape.PositionY != null) screenSpaceModel.positionY = SDK6DataMapExtens |
| 0 | 144 | | if (pbModel.UiScreenSpaceShape.HasIsPointerBlocker) screenSpaceModel.isPointerBlocker = pbModel. |
| 0 | 145 | | return screenSpaceModel; |
| | 146 | |
|
| | 147 | | case ComponentBodyPayload.PayloadOneofCase.UiFullScreenShape: |
| 0 | 148 | | var fullScreenModel = new Model(); |
| 0 | 149 | | if (pbModel.UiFullScreenShape.HasName) fullScreenModel.name = pbModel.UiFullScreenShape.Name; |
| 0 | 150 | | if (pbModel.UiFullScreenShape.HasParentComponent) fullScreenModel.parentComponent = pbModel.UiFu |
| 0 | 151 | | if (pbModel.UiFullScreenShape.HasVisible) fullScreenModel.visible = pbModel.UiFullScreenShape.Vi |
| 0 | 152 | | if (pbModel.UiFullScreenShape.HasOpacity) fullScreenModel.opacity = pbModel.UiFullScreenShape.Op |
| 0 | 153 | | if (pbModel.UiFullScreenShape.HasHAlign) fullScreenModel.hAlign = pbModel.UiFullScreenShape.HAli |
| 0 | 154 | | if (pbModel.UiFullScreenShape.HasVAlign) fullScreenModel.vAlign = pbModel.UiFullScreenShape.VAli |
| 0 | 155 | | if (pbModel.UiFullScreenShape.Width != null) fullScreenModel.width = SDK6DataMapExtensions.FromP |
| 0 | 156 | | if (pbModel.UiFullScreenShape.Height != null) fullScreenModel.height = SDK6DataMapExtensions.Fro |
| 0 | 157 | | if (pbModel.UiFullScreenShape.PositionX != null) fullScreenModel.positionX = SDK6DataMapExtensio |
| 0 | 158 | | if (pbModel.UiFullScreenShape.PositionY != null) fullScreenModel.positionY = SDK6DataMapExtensio |
| 0 | 159 | | if (pbModel.UiFullScreenShape.HasIsPointerBlocker) fullScreenModel.isPointerBlocker = pbModel.Ui |
| 0 | 160 | | return fullScreenModel; |
| | 161 | |
|
| | 162 | | default: |
| 0 | 163 | | return Utils.SafeUnimplemented<UIShape, Model>(expected: ComponentBodyPayload.PayloadOneofCase.U |
| | 164 | | } |
| | 165 | | } |
| | 166 | | } |
| | 167 | |
|
| 149 | 168 | | public override string componentName => GetDebugName(); |
| 0 | 169 | | public virtual string referencesContainerPrefabName => ""; |
| | 170 | | public UIReferencesContainer referencesContainer; |
| | 171 | | public RectTransform childHookRectTransform; |
| | 172 | |
|
| 213 | 173 | | public bool isLayoutDirty { get; private set; } |
| | 174 | | protected System.Action OnLayoutRefresh; |
| | 175 | |
|
| 138 | 176 | | private BaseVariable<Vector2Int> screenSize => DataStore.i.screen.size; |
| 55 | 177 | | private BaseVariable<Dictionary<int, Queue<IUIRefreshable>>> dirtyShapesBySceneVariable => DataStore.i.HUDs.dirt |
| 1265 | 178 | | public UIShape parentUIComponent { get; protected set; } |
| | 179 | |
|
| 130 | 180 | | public UIShape() |
| | 181 | | { |
| 130 | 182 | | screenSize.OnChange += OnScreenResize; |
| 130 | 183 | | model = new Model(); |
| 130 | 184 | | } |
| | 185 | |
|
| | 186 | | private void OnScreenResize(Vector2Int current, Vector2Int previous) |
| | 187 | | { |
| 2 | 188 | | if (GetRootParent() == this) |
| 2 | 189 | | RequestRefresh(); |
| 2 | 190 | | } |
| | 191 | |
|
| 0 | 192 | | public override int GetClassId() { return (int) CLASS_ID.UI_IMAGE_SHAPE; } |
| | 193 | |
|
| | 194 | | public string GetDebugName() |
| | 195 | | { |
| 149 | 196 | | Model model = (Model) this.model; |
| | 197 | |
|
| 149 | 198 | | if (string.IsNullOrEmpty(model.name)) |
| | 199 | | { |
| 142 | 200 | | return GetType().Name; |
| | 201 | | } |
| | 202 | | else |
| | 203 | | { |
| 7 | 204 | | return GetType().Name + " - " + model.name; |
| | 205 | | } |
| | 206 | | } |
| | 207 | |
|
| 0 | 208 | | public override IEnumerator ApplyChanges(BaseModel newJson) { return null; } |
| | 209 | |
|
| | 210 | |
|
| | 211 | | internal T InstantiateUIGameObject<T>(string prefabPath) where T : UIReferencesContainer |
| | 212 | | { |
| 80 | 213 | | Model model = (Model) this.model; |
| | 214 | |
|
| 80 | 215 | | GameObject uiGameObject = null; |
| | 216 | |
|
| 80 | 217 | | bool targetParentExists = !string.IsNullOrEmpty(model.parentComponent) && |
| | 218 | | scene.componentsManagerLegacy.HasSceneSharedComponent(model.parentComponent); |
| | 219 | |
|
| 80 | 220 | | if (targetParentExists) |
| | 221 | | { |
| 23 | 222 | | if (scene.componentsManagerLegacy.HasSceneSharedComponent(model.parentComponent)) |
| | 223 | | { |
| 23 | 224 | | parentUIComponent = (scene.componentsManagerLegacy.GetSceneSharedComponent(model.parentComponent) as |
| | 225 | | } |
| | 226 | | else |
| | 227 | | { |
| 0 | 228 | | parentUIComponent = scene.componentsManagerLegacy.GetSceneSharedComponent<UIScreenSpace>(); |
| | 229 | | } |
| | 230 | | } |
| | 231 | | else |
| | 232 | | { |
| 57 | 233 | | parentUIComponent = scene.componentsManagerLegacy.GetSceneSharedComponent<UIScreenSpace>(); |
| | 234 | | } |
| | 235 | |
|
| 80 | 236 | | uiGameObject = |
| | 237 | | Object.Instantiate( |
| | 238 | | Resources.Load(prefabPath), |
| | 239 | | parentUIComponent?.childHookRectTransform) as GameObject; |
| | 240 | |
|
| 80 | 241 | | referencesContainer = uiGameObject.GetComponent<T>(); |
| | 242 | |
|
| 80 | 243 | | referencesContainer.rectTransform.SetToMaxStretch(); |
| | 244 | |
|
| 80 | 245 | | childHookRectTransform = referencesContainer.childHookRectTransform; |
| | 246 | |
|
| 80 | 247 | | referencesContainer.owner = this; |
| | 248 | |
|
| 80 | 249 | | return referencesContainer as T; |
| | 250 | | } |
| | 251 | |
|
| | 252 | | public virtual void RequestRefresh() |
| | 253 | | { |
| 135 | 254 | | if (isLayoutDirty) return; |
| | 255 | |
|
| 55 | 256 | | isLayoutDirty = true; |
| | 257 | |
|
| 55 | 258 | | var dirtyShapesByScene = dirtyShapesBySceneVariable.Get(); |
| | 259 | |
|
| 55 | 260 | | int sceneDataSceneNumber = scene.sceneData.sceneNumber; |
| 55 | 261 | | if (sceneDataSceneNumber <= 0) sceneDataSceneNumber = 666; |
| | 262 | |
|
| 55 | 263 | | if (!dirtyShapesByScene.ContainsKey(sceneDataSceneNumber)) |
| | 264 | | { |
| 19 | 265 | | dirtyShapesByScene.Add(sceneDataSceneNumber, new Queue<IUIRefreshable>()); |
| | 266 | | } |
| | 267 | |
|
| 55 | 268 | | dirtyShapesByScene[sceneDataSceneNumber].Enqueue(this); |
| 55 | 269 | | } |
| | 270 | |
|
| | 271 | | private void RefreshRecursively() |
| | 272 | | { |
| | 273 | | // We are not using the _Internal here because the method is overridden |
| | 274 | | // by some UI shapes. |
| 55 | 275 | | RefreshDCLLayoutRecursively(refreshSize: true, refreshAlignmentAndPosition: false); |
| 55 | 276 | | FixMaxStretchRecursively(); |
| 55 | 277 | | RefreshDCLLayoutRecursively_Internal(refreshSize: false, refreshAlignmentAndPosition: true); |
| 55 | 278 | | } |
| | 279 | |
|
| | 280 | | public virtual void MarkLayoutDirty( System.Action OnRefresh = null ) |
| | 281 | | { |
| 93 | 282 | | UIShape rootParent = GetRootParent(); |
| | 283 | |
|
| 93 | 284 | | Assert.IsTrue(rootParent != null, "root parent must never be null"); |
| | 285 | |
|
| 93 | 286 | | if (rootParent.referencesContainer == null) |
| 0 | 287 | | return; |
| | 288 | |
|
| 93 | 289 | | rootParent.RequestRefresh(); |
| | 290 | |
|
| 93 | 291 | | if ( OnRefresh != null ) |
| 24 | 292 | | rootParent.OnLayoutRefresh += OnRefresh; |
| 93 | 293 | | } |
| | 294 | |
|
| | 295 | | public void RefreshDCLLayout(bool refreshSize = true, bool refreshAlignmentAndPosition = true) |
| | 296 | | { |
| 365 | 297 | | RectTransform parentRT = referencesContainer.GetComponentInParent<RectTransform>(); |
| | 298 | |
|
| 365 | 299 | | if (refreshSize) |
| | 300 | | { |
| 245 | 301 | | RefreshDCLSize(parentRT); |
| | 302 | | } |
| | 303 | |
|
| 365 | 304 | | if (refreshAlignmentAndPosition) |
| | 305 | | { |
| | 306 | | // Alignment (Alignment uses size so we should always align AFTER resizing) |
| 269 | 307 | | RefreshDCLAlignmentAndPosition(parentRT); |
| | 308 | | } |
| 365 | 309 | | } |
| | 310 | |
|
| | 311 | | protected virtual void RefreshDCLSize(RectTransform parentTransform = null) |
| | 312 | | { |
| 229 | 313 | | if (parentTransform == null) |
| 0 | 314 | | parentTransform = referencesContainer.GetComponentInParent<RectTransform>(); |
| | 315 | |
|
| 229 | 316 | | Model model = (Model) this.model; |
| | 317 | |
|
| 229 | 318 | | referencesContainer.layoutElementRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, |
| | 319 | | model.width.GetScaledValue(parentTransform.rect.width)); |
| 229 | 320 | | referencesContainer.layoutElementRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, |
| | 321 | | model.height.GetScaledValue(parentTransform.rect.height)); |
| 229 | 322 | | } |
| | 323 | |
|
| | 324 | | public void RefreshDCLAlignmentAndPosition(RectTransform parentTransform = null) |
| | 325 | | { |
| 269 | 326 | | if (parentTransform == null) |
| 0 | 327 | | parentTransform = referencesContainer.GetComponentInParent<RectTransform>(); |
| | 328 | |
|
| 269 | 329 | | referencesContainer.layoutElement.ignoreLayout = false; |
| 269 | 330 | | ConfigureAlignment(referencesContainer.layoutGroup); |
| 269 | 331 | | Utils.ForceRebuildLayoutImmediate(parentTransform); |
| 269 | 332 | | referencesContainer.layoutElement.ignoreLayout = true; |
| | 333 | |
|
| 269 | 334 | | Model model = (Model) this.model; |
| | 335 | |
|
| | 336 | | // Reposition |
| 269 | 337 | | Vector3 position = Vector3.zero; |
| 269 | 338 | | position.x = model.positionX.GetScaledValue(parentTransform.rect.width); |
| 269 | 339 | | position.y = model.positionY.GetScaledValue(parentTransform.rect.height); |
| | 340 | |
|
| 269 | 341 | | position = Utils.Sanitize(position); |
| 269 | 342 | | referencesContainer.layoutElementRT.localPosition += position; |
| 269 | 343 | | } |
| | 344 | |
|
| | 345 | | public virtual void RefreshDCLLayoutRecursively(bool refreshSize = true, |
| | 346 | | bool refreshAlignmentAndPosition = true) |
| | 347 | | { |
| 79 | 348 | | RefreshDCLLayoutRecursively_Internal(refreshSize, refreshAlignmentAndPosition); |
| 79 | 349 | | } |
| | 350 | |
|
| | 351 | | public void RefreshDCLLayoutRecursively_Internal(bool refreshSize = true, |
| | 352 | | bool refreshAlignmentAndPosition = true) |
| | 353 | | { |
| 134 | 354 | | UIShape rootParent = GetRootParent(); |
| | 355 | |
|
| 134 | 356 | | Assert.IsTrue(rootParent != null, "root parent must never be null"); |
| | 357 | |
|
| 134 | 358 | | if (rootParent.referencesContainer == null) |
| 2 | 359 | | return; |
| | 360 | |
|
| 132 | 361 | | Utils.InverseTransformChildTraversal<UIReferencesContainer>( |
| | 362 | | (x) => |
| | 363 | | { |
| 300 | 364 | | if (x.owner != null) |
| 216 | 365 | | x.owner.RefreshDCLLayout(refreshSize, refreshAlignmentAndPosition); |
| 300 | 366 | | }, |
| | 367 | | rootParent.referencesContainer.transform); |
| 132 | 368 | | } |
| | 369 | |
|
| | 370 | | public void FixMaxStretchRecursively() |
| | 371 | | { |
| 55 | 372 | | UIShape rootParent = GetRootParent(); |
| | 373 | |
|
| 55 | 374 | | Assert.IsTrue(rootParent != null, "root parent must never be null"); |
| | 375 | |
|
| 55 | 376 | | if (rootParent.referencesContainer == null) |
| 1 | 377 | | return; |
| | 378 | |
|
| 54 | 379 | | Utils.InverseTransformChildTraversal<UIReferencesContainer>( |
| | 380 | | (x) => |
| | 381 | | { |
| 138 | 382 | | if (x.owner != null) |
| | 383 | | { |
| 96 | 384 | | x.rectTransform.SetToMaxStretch(); |
| | 385 | | } |
| 138 | 386 | | }, |
| | 387 | | rootParent.referencesContainer.transform); |
| 54 | 388 | | } |
| | 389 | |
|
| | 390 | | protected bool ReparentComponent(RectTransform targetTransform, string targetParent) |
| | 391 | | { |
| 69 | 392 | | bool targetParentExists = !string.IsNullOrEmpty(targetParent) && |
| | 393 | | scene.componentsManagerLegacy.HasSceneSharedComponent(targetParent); |
| | 394 | |
|
| 69 | 395 | | if (targetParentExists && parentUIComponent == scene.componentsManagerLegacy.GetSceneSharedComponent(targetP |
| | 396 | | { |
| 22 | 397 | | return false; |
| | 398 | | } |
| | 399 | |
|
| 47 | 400 | | if (parentUIComponent != null) |
| | 401 | | { |
| 47 | 402 | | UIReferencesContainer[] parents = referencesContainer.GetComponentsInParent<UIReferencesContainer>(true) |
| | 403 | |
|
| 190 | 404 | | foreach (var parent in parents) |
| | 405 | | { |
| 48 | 406 | | if (parent.owner != null) |
| | 407 | | { |
| 48 | 408 | | parent.owner.OnChildDetached(parentUIComponent, this); |
| | 409 | | } |
| | 410 | | } |
| | 411 | | } |
| | 412 | |
|
| 47 | 413 | | if (targetParentExists) |
| | 414 | | { |
| 22 | 415 | | parentUIComponent = scene.componentsManagerLegacy.GetSceneSharedComponent(targetParent) as UIShape; |
| | 416 | | } |
| | 417 | | else |
| | 418 | | { |
| 25 | 419 | | parentUIComponent = scene.componentsManagerLegacy.GetSceneSharedComponent<UIScreenSpace>(); |
| | 420 | | } |
| | 421 | |
|
| 47 | 422 | | targetTransform.SetParent(parentUIComponent.childHookRectTransform, false); |
| 47 | 423 | | return true; |
| | 424 | | } |
| | 425 | |
|
| | 426 | | public UIShape GetRootParent() |
| | 427 | | { |
| 290 | 428 | | UIShape parent = null; |
| | 429 | |
|
| 290 | 430 | | if (parentUIComponent != null && !(parentUIComponent is UIScreenSpace)) |
| | 431 | | { |
| 6 | 432 | | parent = parentUIComponent.GetRootParent(); |
| | 433 | | } |
| | 434 | | else |
| | 435 | | { |
| 284 | 436 | | parent = this; |
| | 437 | | } |
| | 438 | |
|
| 290 | 439 | | return parent; |
| | 440 | | } |
| | 441 | |
|
| | 442 | | protected void ConfigureAlignment(LayoutGroup layout) |
| | 443 | | { |
| 269 | 444 | | Model model = (Model) this.model; |
| 269 | 445 | | switch (model.vAlign) |
| | 446 | | { |
| | 447 | | case "top": |
| 7 | 448 | | switch (model.hAlign) |
| | 449 | | { |
| | 450 | | case "left": |
| 2 | 451 | | layout.childAlignment = TextAnchor.UpperLeft; |
| 2 | 452 | | break; |
| | 453 | | case "right": |
| 2 | 454 | | layout.childAlignment = TextAnchor.UpperRight; |
| 2 | 455 | | break; |
| | 456 | | default: |
| 3 | 457 | | layout.childAlignment = TextAnchor.UpperCenter; |
| 3 | 458 | | break; |
| | 459 | | } |
| | 460 | |
|
| | 461 | | break; |
| | 462 | | case "bottom": |
| 26 | 463 | | switch (model.hAlign) |
| | 464 | | { |
| | 465 | | case "left": |
| 8 | 466 | | layout.childAlignment = TextAnchor.LowerLeft; |
| 8 | 467 | | break; |
| | 468 | | case "right": |
| 16 | 469 | | layout.childAlignment = TextAnchor.LowerRight; |
| 16 | 470 | | break; |
| | 471 | | default: |
| 2 | 472 | | layout.childAlignment = TextAnchor.LowerCenter; |
| 2 | 473 | | break; |
| | 474 | | } |
| | 475 | |
|
| | 476 | | break; |
| | 477 | | default: // center |
| 236 | 478 | | switch (model.hAlign) |
| | 479 | | { |
| | 480 | | case "left": |
| 2 | 481 | | layout.childAlignment = TextAnchor.MiddleLeft; |
| 2 | 482 | | break; |
| | 483 | | case "right": |
| 2 | 484 | | layout.childAlignment = TextAnchor.MiddleRight; |
| 2 | 485 | | break; |
| | 486 | | default: |
| 232 | 487 | | layout.childAlignment = TextAnchor.MiddleCenter; |
| | 488 | | break; |
| | 489 | | } |
| | 490 | |
|
| | 491 | | break; |
| | 492 | | } |
| 232 | 493 | | } |
| | 494 | |
|
| | 495 | | protected void SetComponentDebugName() |
| | 496 | | { |
| 149 | 497 | | if (referencesContainer == null || model == null) |
| | 498 | | { |
| 0 | 499 | | return; |
| | 500 | | } |
| | 501 | |
|
| 149 | 502 | | referencesContainer.name = componentName; |
| 149 | 503 | | } |
| | 504 | |
|
| | 505 | | public override void Dispose() |
| | 506 | | { |
| | 507 | |
|
| 8 | 508 | | if (childHookRectTransform) |
| 8 | 509 | | Utils.SafeDestroy(childHookRectTransform.gameObject); |
| | 510 | |
|
| 8 | 511 | | screenSize.OnChange -= OnScreenResize; |
| | 512 | |
|
| 8 | 513 | | base.Dispose(); |
| 8 | 514 | | } |
| | 515 | |
|
| 127 | 516 | | public virtual void OnChildAttached(UIShape parentComponent, UIShape childComponent) { } |
| | 517 | |
|
| 43 | 518 | | public virtual void OnChildDetached(UIShape parentComponent, UIShape childComponent) { } |
| | 519 | | public void Refresh() |
| | 520 | | { |
| 55 | 521 | | RefreshRecursively(); |
| 55 | 522 | | isLayoutDirty = false; |
| | 523 | |
|
| 55 | 524 | | OnLayoutRefresh?.Invoke(); |
| 55 | 525 | | OnLayoutRefresh = null; |
| 55 | 526 | | } |
| | 527 | | } |
| | 528 | | } |