| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using DCL.Models; |
| | 4 | | using System.Collections; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.Assertions; |
| | 8 | | using UnityEngine.UI; |
| | 9 | |
|
| | 10 | | namespace DCL.Components |
| | 11 | | { |
| | 12 | | public class UIContainerStack : UIShape<UIContainerRectReferencesContainer, UIContainerStack.Model> |
| | 13 | | { |
| | 14 | | [System.Serializable] |
| | 15 | | new public class Model : UIShape.Model |
| | 16 | | { |
| 45 | 17 | | public Color color = Color.clear; |
| | 18 | | public StackOrientation stackOrientation = StackOrientation.VERTICAL; |
| 45 | 19 | | public bool adaptWidth = true; |
| 45 | 20 | | public bool adaptHeight = true; |
| | 21 | | public float spacing = 0; |
| | 22 | |
|
| 15 | 23 | | public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); } |
| | 24 | | } |
| | 25 | |
|
| | 26 | | public enum StackOrientation |
| | 27 | | { |
| | 28 | | VERTICAL, |
| | 29 | | HORIZONTAL |
| | 30 | | } |
| | 31 | |
|
| 11 | 32 | | public override string referencesContainerPrefabName => "UIContainerRect"; |
| | 33 | |
|
| 11 | 34 | | public Dictionary<string, GameObject> stackContainers = new Dictionary<string, GameObject>(); |
| | 35 | |
|
| | 36 | | HorizontalOrVerticalLayoutGroup layoutGroup; |
| | 37 | |
|
| 33 | 38 | | public UIContainerStack() { model = new Model(); } |
| | 39 | |
|
| 0 | 40 | | public override int GetClassId() { return (int) CLASS_ID.UI_CONTAINER_STACK; } |
| | 41 | |
|
| | 42 | | public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null) |
| | 43 | | { |
| 0 | 44 | | Debug.LogError( |
| | 45 | | "Aborted UIContainerStack attachment to an entity. UIShapes shouldn't be attached to entities."); |
| 0 | 46 | | } |
| | 47 | |
|
| 0 | 48 | | public override void DetachFrom(IDCLEntity entity, System.Type overridenAttachedType = null) { } |
| | 49 | |
|
| | 50 | | public override IEnumerator ApplyChanges(BaseModel newModel) |
| | 51 | | { |
| 18 | 52 | | referencesContainer.image.color = new Color(model.color.r, model.color.g, model.color.b, model.color.a); |
| 18 | 53 | | referencesContainer.image.raycastTarget = model.color.a >= RAYCAST_ALPHA_THRESHOLD; |
| | 54 | |
|
| 18 | 55 | | if (model.stackOrientation == StackOrientation.VERTICAL && !(layoutGroup is VerticalLayoutGroup)) |
| | 56 | | { |
| 11 | 57 | | Object.DestroyImmediate(layoutGroup, false); |
| 11 | 58 | | layoutGroup = childHookRectTransform.gameObject.AddComponent<VerticalLayoutGroup>(); |
| 11 | 59 | | } |
| 7 | 60 | | else if (model.stackOrientation == StackOrientation.HORIZONTAL && !(layoutGroup is HorizontalLayoutGroup)) |
| | 61 | | { |
| 2 | 62 | | Object.DestroyImmediate(layoutGroup, false); |
| 2 | 63 | | layoutGroup = childHookRectTransform.gameObject.AddComponent<HorizontalLayoutGroup>(); |
| | 64 | | } |
| | 65 | |
|
| 18 | 66 | | layoutGroup.childControlHeight = false; |
| 18 | 67 | | layoutGroup.childControlWidth = false; |
| 18 | 68 | | layoutGroup.childForceExpandWidth = false; |
| 18 | 69 | | layoutGroup.childForceExpandHeight = false; |
| 18 | 70 | | layoutGroup.spacing = model.spacing; |
| | 71 | |
|
| 18 | 72 | | referencesContainer.sizeFitter.adjustHeight = model.adaptHeight; |
| 18 | 73 | | referencesContainer.sizeFitter.adjustWidth = model.adaptWidth; |
| | 74 | |
|
| 18 | 75 | | MarkLayoutDirty(); |
| 18 | 76 | | return null; |
| | 77 | | } |
| | 78 | |
|
| | 79 | | void RefreshContainerForShape(BaseDisposable updatedComponent) |
| | 80 | | { |
| 43 | 81 | | UIShape childComponent = updatedComponent as UIShape; |
| 43 | 82 | | Assert.IsTrue(childComponent != null, "This should never happen!!!!"); |
| | 83 | |
|
| 43 | 84 | | if (((UIShape.Model) childComponent.GetModel()).parentComponent != id) |
| | 85 | | { |
| 26 | 86 | | MarkLayoutDirty(); |
| 26 | 87 | | return; |
| | 88 | | } |
| | 89 | |
|
| 17 | 90 | | GameObject stackContainer = null; |
| | 91 | |
|
| 17 | 92 | | if (!stackContainers.ContainsKey(childComponent.id)) |
| | 93 | | { |
| 17 | 94 | | stackContainer = Object.Instantiate(Resources.Load("UIContainerStackChild")) as GameObject; |
| | 95 | | #if UNITY_EDITOR |
| 17 | 96 | | stackContainer.name = "UIContainerStackChild - " + childComponent.id; |
| | 97 | | #endif |
| 17 | 98 | | stackContainers.Add(childComponent.id, stackContainer); |
| | 99 | |
|
| 17 | 100 | | int oldSiblingIndex = childComponent.referencesContainer.transform.GetSiblingIndex(); |
| 17 | 101 | | childComponent.referencesContainer.transform.SetParent(stackContainer.transform, false); |
| 17 | 102 | | stackContainer.transform.SetParent(referencesContainer.childHookRectTransform, false); |
| 17 | 103 | | stackContainer.transform.SetSiblingIndex(oldSiblingIndex); |
| 17 | 104 | | } |
| | 105 | | else |
| | 106 | | { |
| 0 | 107 | | stackContainer = stackContainers[childComponent.id]; |
| | 108 | | } |
| | 109 | |
|
| 17 | 110 | | MarkLayoutDirty(); |
| 17 | 111 | | } |
| | 112 | |
|
| | 113 | | public override void OnChildAttached(UIShape parentComponent, UIShape childComponent) |
| | 114 | | { |
| 36 | 115 | | RefreshContainerForShape(childComponent); |
| 36 | 116 | | childComponent.OnAppliedChanges -= RefreshContainerForShape; |
| 36 | 117 | | childComponent.OnAppliedChanges += RefreshContainerForShape; |
| 36 | 118 | | } |
| | 119 | |
|
| | 120 | | public override void RefreshDCLLayoutRecursively(bool refreshSize = true, |
| | 121 | | bool refreshAlignmentAndPosition = true) |
| | 122 | | { |
| 34 | 123 | | base.RefreshDCLLayoutRecursively(refreshSize, refreshAlignmentAndPosition); |
| 34 | 124 | | referencesContainer.sizeFitter.RefreshRecursively(); |
| 34 | 125 | | } |
| | 126 | |
|
| | 127 | | public override void OnChildDetached(UIShape parentComponent, UIShape childComponent) |
| | 128 | | { |
| 5 | 129 | | if (parentComponent != this) |
| | 130 | | { |
| 5 | 131 | | return; |
| | 132 | | } |
| | 133 | |
|
| 0 | 134 | | if (stackContainers.ContainsKey(childComponent.id)) |
| | 135 | | { |
| 0 | 136 | | Object.Destroy(stackContainers[childComponent.id]); |
| 0 | 137 | | stackContainers[childComponent.id].transform.SetParent(null); |
| 0 | 138 | | stackContainers[childComponent.id].name += "- Detached"; |
| 0 | 139 | | stackContainers.Remove(childComponent.id); |
| | 140 | | } |
| | 141 | |
|
| 0 | 142 | | childComponent.OnAppliedChanges -= RefreshContainerForShape; |
| 0 | 143 | | RefreshDCLLayout(); |
| 0 | 144 | | } |
| | 145 | |
|
| | 146 | | public override void Dispose() |
| | 147 | | { |
| 1 | 148 | | if (referencesContainer != null) |
| 1 | 149 | | Utils.SafeDestroy(referencesContainer.gameObject); |
| | 150 | |
|
| 1 | 151 | | base.Dispose(); |
| 1 | 152 | | } |
| | 153 | | } |
| | 154 | | } |