| | 1 | | using DCL.Helpers; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.UI; |
| | 7 | |
|
| | 8 | | public interface IColumnsOrganizerComponentView |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Recalculates the size of each columns following the configuration in the model. |
| | 12 | | /// </summary> |
| | 13 | | void RecalculateColumnsSize(); |
| | 14 | | } |
| | 15 | |
|
| | 16 | | public class ColumnsOrganizerComponentView : BaseComponentView, IColumnsOrganizerComponentView |
| | 17 | | { |
| | 18 | | [Header("Prefab References")] |
| | 19 | | [SerializeField] internal HorizontalLayoutGroup horizontalLayoutGroup; |
| | 20 | | [SerializeField] internal RectTransform columnsContainer; |
| | 21 | |
|
| | 22 | | [Header("Configuration")] |
| | 23 | | [SerializeField] internal ColumnsOrganizerComponentModel model; |
| | 24 | |
|
| 138 | 25 | | internal List<RectTransform> currentColumns = new List<RectTransform>(); |
| | 26 | |
|
| | 27 | | public override void RefreshControl() |
| | 28 | | { |
| 0 | 29 | | if (model == null) |
| 0 | 30 | | return; |
| | 31 | |
|
| 0 | 32 | | RecalculateColumnsSize(); |
| 0 | 33 | | } |
| | 34 | |
|
| | 35 | | public override void OnScreenSizeChanged() |
| | 36 | | { |
| 0 | 37 | | base.OnScreenSizeChanged(); |
| | 38 | |
|
| 0 | 39 | | RecalculateColumnsSize(); |
| 0 | 40 | | } |
| | 41 | |
|
| | 42 | | public void RecalculateColumnsSize() |
| | 43 | | { |
| 1 | 44 | | RegisterCurrentColumns(); |
| | 45 | |
|
| 1 | 46 | | horizontalLayoutGroup.spacing = model.spaceBetweenColumns; |
| | 47 | |
|
| 6 | 48 | | float totalWidthOfFixedSizeColumns = model.columnsConfig.Where(x => !x.isPercentage).Sum(x => x.width); |
| 1 | 49 | | float totalWidthOfPercentageColumns = columnsContainer.rect.width - totalWidthOfFixedSizeColumns - (model.spaceB |
| | 50 | |
|
| 8 | 51 | | for (int i = 0; i < currentColumns.Count; i++) |
| | 52 | | { |
| 3 | 53 | | if (model.columnsConfig.Count - 1 >= i) |
| | 54 | | { |
| 3 | 55 | | if (model.columnsConfig[i].isPercentage) |
| | 56 | | { |
| 1 | 57 | | float newWidth = totalWidthOfPercentageColumns * model.columnsConfig[i].width / 100f; |
| 1 | 58 | | if (newWidth < 0) |
| 0 | 59 | | newWidth = 0; |
| | 60 | |
|
| 1 | 61 | | currentColumns[i].sizeDelta = new Vector2( |
| | 62 | | newWidth, |
| | 63 | | currentColumns[i].sizeDelta.y); |
| 1 | 64 | | } |
| | 65 | | else |
| | 66 | | { |
| 2 | 67 | | currentColumns[i].sizeDelta = new Vector2( |
| | 68 | | model.columnsConfig[i].width, |
| | 69 | | currentColumns[i].sizeDelta.y); |
| | 70 | | } |
| | 71 | | } |
| | 72 | | } |
| | 73 | |
|
| 1 | 74 | | StartCoroutine(RefreshAllChildUIComponentsSize()); |
| 1 | 75 | | } |
| | 76 | |
|
| | 77 | | internal void RegisterCurrentColumns() |
| | 78 | | { |
| 1 | 79 | | currentColumns.Clear(); |
| | 80 | |
|
| 8 | 81 | | foreach (RectTransform child in transform) |
| | 82 | | { |
| 3 | 83 | | currentColumns.Add(child); |
| | 84 | | } |
| 1 | 85 | | } |
| | 86 | |
|
| | 87 | | internal IEnumerator RefreshAllChildUIComponentsSize() |
| | 88 | | { |
| 1 | 89 | | yield return null; |
| | 90 | |
|
| 0 | 91 | | for (int i = 0; i < model.uiComponentsToRefreshSize.Count; i++) |
| | 92 | | { |
| 0 | 93 | | model.uiComponentsToRefreshSize[i].OnScreenSizeChanged(); |
| | 94 | | } |
| | 95 | |
|
| 0 | 96 | | Utils.ForceRebuildLayoutImmediate(transform as RectTransform); |
| 0 | 97 | | } |
| | 98 | | } |