< Summary

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