| | 1 | | using DCL.Helpers; |
| | 2 | | using DCL.Interface; |
| | 3 | | using DCL.Models; |
| | 4 | | using System.Collections; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.UI; |
| | 7 | | using Color = UnityEngine.Color; |
| | 8 | | using Decentraland.Sdk.Ecs6; |
| | 9 | | using MainScripts.DCL.Components; |
| | 10 | |
|
| | 11 | | namespace DCL.Components |
| | 12 | | { |
| | 13 | | public class UIScrollRect : UIShape<UIScrollRectRefContainer, UIScrollRect.Model> |
| | 14 | | { |
| | 15 | | [System.Serializable] |
| | 16 | | public new class Model : UIShape.Model |
| | 17 | | { |
| | 18 | | public float valueX = 0; |
| | 19 | | public float valueY = 0; |
| 26 | 20 | | public Color backgroundColor = Color.clear; |
| | 21 | | public bool isHorizontal = false; |
| 26 | 22 | | public bool isVertical = true; |
| | 23 | | public float paddingTop = 0f; |
| | 24 | | public float paddingRight = 0f; |
| | 25 | | public float paddingBottom = 0f; |
| | 26 | | public float paddingLeft = 0f; |
| | 27 | | public string OnChanged; |
| | 28 | |
|
| | 29 | | public override BaseModel GetDataFromJSON(string json) => |
| 8 | 30 | | Utils.SafeFromJson<Model>(json); |
| | 31 | |
|
| | 32 | | public override BaseModel GetDataFromPb(ComponentBodyPayload pbModel) |
| | 33 | | { |
| 0 | 34 | | if (pbModel.PayloadCase != ComponentBodyPayload.PayloadOneofCase.UiScrollRect) |
| 0 | 35 | | return Utils.SafeUnimplemented<UIScrollRect, Model>(expected: ComponentBodyPayload.PayloadOneofCase. |
| | 36 | |
|
| 0 | 37 | | var pb = new Model(); |
| 0 | 38 | | if (pbModel.UiScrollRect.HasName) pb.name = pbModel.UiScrollRect.Name; |
| 0 | 39 | | if (pbModel.UiScrollRect.HasParentComponent) pb.parentComponent = pbModel.UiScrollRect.ParentComponent; |
| 0 | 40 | | if (pbModel.UiScrollRect.HasVisible) pb.visible = pbModel.UiScrollRect.Visible; |
| 0 | 41 | | if (pbModel.UiScrollRect.HasOpacity) pb.opacity = pbModel.UiScrollRect.Opacity; |
| 0 | 42 | | if (pbModel.UiScrollRect.HasHAlign) pb.hAlign = pbModel.UiScrollRect.HAlign; |
| 0 | 43 | | if (pbModel.UiScrollRect.HasVAlign) pb.vAlign = pbModel.UiScrollRect.VAlign; |
| 0 | 44 | | if (pbModel.UiScrollRect.Width != null) pb.width = SDK6DataMapExtensions.FromProtobuf(pb.width, pbModel. |
| 0 | 45 | | if (pbModel.UiScrollRect.Height != null) pb.height = SDK6DataMapExtensions.FromProtobuf(pb.height, pbMod |
| 0 | 46 | | if (pbModel.UiScrollRect.PositionX != null) pb.positionX = SDK6DataMapExtensions.FromProtobuf(pb.positio |
| 0 | 47 | | if (pbModel.UiScrollRect.PositionY != null) pb.positionY = SDK6DataMapExtensions.FromProtobuf(pb.positio |
| 0 | 48 | | if (pbModel.UiScrollRect.HasIsPointerBlocker) pb.isPointerBlocker = pbModel.UiScrollRect.IsPointerBlocke |
| | 49 | |
|
| 0 | 50 | | if (pbModel.UiScrollRect.HasValueX) pb.valueX = pbModel.UiScrollRect.ValueX; |
| 0 | 51 | | if (pbModel.UiScrollRect.HasValueY) pb.valueY = pbModel.UiScrollRect.ValueY; |
| 0 | 52 | | if (pbModel.UiScrollRect.BackgroundColor != null) pb.backgroundColor = pbModel.UiScrollRect.BackgroundCo |
| 0 | 53 | | if (pbModel.UiScrollRect.IsHorizontal) pb.isHorizontal = pbModel.UiScrollRect.IsHorizontal; |
| 0 | 54 | | if (pbModel.UiScrollRect.IsVertical) pb.isVertical = pbModel.UiScrollRect.IsVertical; |
| 0 | 55 | | if (pbModel.UiScrollRect.HasPaddingTop) pb.paddingTop = pbModel.UiScrollRect.PaddingTop; |
| 0 | 56 | | if (pbModel.UiScrollRect.HasPaddingRight) pb.paddingRight = pbModel.UiScrollRect.PaddingRight; |
| 0 | 57 | | if (pbModel.UiScrollRect.HasPaddingBottom) pb.paddingBottom = pbModel.UiScrollRect.PaddingBottom; |
| 0 | 58 | | if (pbModel.UiScrollRect.HasPaddingLeft) pb.paddingLeft = pbModel.UiScrollRect.PaddingLeft; |
| 0 | 59 | | if (pbModel.UiScrollRect.HasOnChanged) pb.OnChanged = pbModel.UiScrollRect.OnChanged; |
| | 60 | |
|
| 0 | 61 | | return pb; |
| | 62 | | } |
| | 63 | | } |
| | 64 | |
|
| 5 | 65 | | public override string referencesContainerPrefabName => "UIScrollRect"; |
| | 66 | |
|
| 15 | 67 | | public UIScrollRect() { model = new Model(); } |
| | 68 | |
|
| | 69 | | public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null) |
| | 70 | | { |
| 0 | 71 | | Debug.LogError( |
| | 72 | | "Aborted UIScrollRectShape attachment to an entity. UIShapes shouldn't be attached to entities."); |
| 0 | 73 | | } |
| | 74 | |
|
| 0 | 75 | | public override void DetachFrom(IDCLEntity entity, System.Type overridenAttachedType = null) { } |
| | 76 | |
|
| | 77 | | public override void OnChildAttached(UIShape parent, UIShape childComponent) |
| | 78 | | { |
| 9 | 79 | | base.OnChildAttached(parent, childComponent); |
| 9 | 80 | | childComponent.OnAppliedChanges -= RefreshContainerForShape; |
| 9 | 81 | | childComponent.OnAppliedChanges += RefreshContainerForShape; |
| 9 | 82 | | RefreshContainerForShape(childComponent); |
| 9 | 83 | | } |
| | 84 | |
|
| | 85 | | public override void OnChildDetached(UIShape parent, UIShape childComponent) |
| | 86 | | { |
| 4 | 87 | | base.OnChildDetached(parent, childComponent); |
| 4 | 88 | | childComponent.OnAppliedChanges -= RefreshContainerForShape; |
| 4 | 89 | | } |
| | 90 | |
|
| | 91 | | void RefreshContainerForShape(BaseDisposable updatedComponent) |
| | 92 | | { |
| 12 | 93 | | MarkLayoutDirty( () => |
| | 94 | | { |
| 12 | 95 | | referencesContainer.fitter.RefreshRecursively(); |
| 12 | 96 | | AdjustChildHook(); |
| 12 | 97 | | referencesContainer.scrollRect.Rebuild(CanvasUpdate.MaxUpdateValue); |
| 12 | 98 | | } |
| | 99 | | ); |
| 12 | 100 | | } |
| | 101 | |
|
| | 102 | | void AdjustChildHook() |
| | 103 | | { |
| 24 | 104 | | UIScrollRectRefContainer rc = referencesContainer; |
| 24 | 105 | | rc.childHookRectTransform.SetParent(rc.layoutElementRT, false); |
| 24 | 106 | | rc.childHookRectTransform.SetToMaxStretch(); |
| 24 | 107 | | rc.childHookRectTransform.SetParent(rc.content, true); |
| 24 | 108 | | RefreshDCLLayoutRecursively(false, true); |
| 24 | 109 | | } |
| | 110 | |
|
| | 111 | | public override void RefreshDCLLayoutRecursively(bool refreshSize = true, |
| | 112 | | bool refreshAlignmentAndPosition = true) |
| | 113 | | { |
| 36 | 114 | | base.RefreshDCLLayoutRecursively(refreshSize, refreshAlignmentAndPosition); |
| 36 | 115 | | referencesContainer.fitter.RefreshRecursively(); |
| 36 | 116 | | } |
| | 117 | |
|
| | 118 | | public override IEnumerator ApplyChanges(BaseModel newModel) |
| | 119 | | { |
| 12 | 120 | | UIScrollRectRefContainer rc = referencesContainer; |
| | 121 | |
|
| 12 | 122 | | rc.contentBackground.color = model.backgroundColor; |
| | 123 | |
|
| | 124 | | // Apply padding |
| 12 | 125 | | rc.paddingLayoutGroup.padding.bottom = Mathf.RoundToInt(model.paddingBottom); |
| 12 | 126 | | rc.paddingLayoutGroup.padding.top = Mathf.RoundToInt(model.paddingTop); |
| 12 | 127 | | rc.paddingLayoutGroup.padding.left = Mathf.RoundToInt(model.paddingLeft); |
| 12 | 128 | | rc.paddingLayoutGroup.padding.right = Mathf.RoundToInt(model.paddingRight); |
| | 129 | |
|
| 12 | 130 | | rc.scrollRect.horizontal = model.isHorizontal; |
| 12 | 131 | | rc.scrollRect.vertical = model.isVertical; |
| | 132 | |
|
| 12 | 133 | | rc.HScrollbar.value = model.valueX; |
| 12 | 134 | | rc.VScrollbar.value = model.valueY; |
| | 135 | |
|
| 12 | 136 | | rc.scrollRect.onValueChanged.AddListener(OnChanged); |
| | 137 | |
|
| 12 | 138 | | MarkLayoutDirty( () => |
| | 139 | | { |
| 12 | 140 | | referencesContainer.fitter.RefreshRecursively(); |
| 12 | 141 | | AdjustChildHook(); |
| 12 | 142 | | } |
| | 143 | | ); |
| 12 | 144 | | return null; |
| | 145 | | } |
| | 146 | |
|
| 30 | 147 | | void OnChanged(Vector2 scrollingValues) { WebInterface.ReportOnScrollChange(scene.sceneData.sceneNumber, model.O |
| | 148 | |
|
| | 149 | | public override void Dispose() |
| | 150 | | { |
| 1 | 151 | | if (referencesContainer != null) |
| | 152 | | { |
| 1 | 153 | | referencesContainer.scrollRect.onValueChanged.RemoveAllListeners(); |
| 1 | 154 | | Utils.SafeDestroy(referencesContainer.gameObject); |
| | 155 | | } |
| | 156 | |
|
| 1 | 157 | | base.Dispose(); |
| 1 | 158 | | } |
| | 159 | | } |
| | 160 | | } |