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