< Summary

Class:DCL.Components.UIValue
Assembly:DCL.Components.UI
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/UI/UIShape.cs
Covered lines:7
Uncovered lines:7
Coverable lines:14
Total lines:512
Line coverage:50% (7 of 14)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SetPixels(...)0%2100%
SetPercent(...)0%2100%
UIValue(...)0%110100%
GetScaledValue(...)0%3.073080%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/UI/UIShape.cs

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