| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using DCL.Interface; |
| | 4 | | using DCL.Models; |
| | 5 | | using System.Collections; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.UI; |
| | 9 | | using Color = UnityEngine.Color; |
| | 10 | |
|
| | 11 | | namespace DCL.Components |
| | 12 | | { |
| | 13 | | public class UIScrollRect : UIShape<UIScrollRectRefContainer, UIScrollRect.Model> |
| | 14 | | { |
| | 15 | | [System.Serializable] |
| | 16 | | new public 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 | |
|
| 8 | 29 | | public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); } |
| | 30 | | } |
| | 31 | |
|
| 5 | 32 | | public override string referencesContainerPrefabName => "UIScrollRect"; |
| | 33 | |
|
| 15 | 34 | | public UIScrollRect() { model = new Model(); } |
| | 35 | |
|
| | 36 | | public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null) |
| | 37 | | { |
| 0 | 38 | | Debug.LogError( |
| | 39 | | "Aborted UIScrollRectShape attachment to an entity. UIShapes shouldn't be attached to entities."); |
| 0 | 40 | | } |
| | 41 | |
|
| 0 | 42 | | public override void DetachFrom(IDCLEntity entity, System.Type overridenAttachedType = null) { } |
| | 43 | |
|
| | 44 | | public override void OnChildAttached(UIShape parent, UIShape childComponent) |
| | 45 | | { |
| 9 | 46 | | base.OnChildAttached(parent, childComponent); |
| 9 | 47 | | childComponent.OnAppliedChanges -= RefreshContainerForShape; |
| 9 | 48 | | childComponent.OnAppliedChanges += RefreshContainerForShape; |
| 9 | 49 | | RefreshContainerForShape(childComponent); |
| 9 | 50 | | } |
| | 51 | |
|
| | 52 | | public override void OnChildDetached(UIShape parent, UIShape childComponent) |
| | 53 | | { |
| 4 | 54 | | base.OnChildDetached(parent, childComponent); |
| 4 | 55 | | childComponent.OnAppliedChanges -= RefreshContainerForShape; |
| 4 | 56 | | } |
| | 57 | |
|
| | 58 | | void RefreshContainerForShape(BaseDisposable updatedComponent) |
| | 59 | | { |
| 12 | 60 | | MarkLayoutDirty( () => |
| | 61 | | { |
| 12 | 62 | | referencesContainer.fitter.RefreshRecursively(); |
| 12 | 63 | | AdjustChildHook(); |
| 12 | 64 | | referencesContainer.scrollRect.Rebuild(CanvasUpdate.MaxUpdateValue); |
| 12 | 65 | | } |
| | 66 | | ); |
| 12 | 67 | | } |
| | 68 | |
|
| | 69 | | void AdjustChildHook() |
| | 70 | | { |
| 24 | 71 | | UIScrollRectRefContainer rc = referencesContainer; |
| 24 | 72 | | rc.childHookRectTransform.SetParent(rc.layoutElementRT, false); |
| 24 | 73 | | rc.childHookRectTransform.SetToMaxStretch(); |
| 24 | 74 | | rc.childHookRectTransform.SetParent(rc.content, true); |
| 24 | 75 | | RefreshDCLLayoutRecursively(false, true); |
| 24 | 76 | | } |
| | 77 | |
|
| | 78 | | public override void RefreshDCLLayoutRecursively(bool refreshSize = true, |
| | 79 | | bool refreshAlignmentAndPosition = true) |
| | 80 | | { |
| 36 | 81 | | base.RefreshDCLLayoutRecursively(refreshSize, refreshAlignmentAndPosition); |
| 36 | 82 | | referencesContainer.fitter.RefreshRecursively(); |
| 36 | 83 | | } |
| | 84 | |
|
| | 85 | | public override IEnumerator ApplyChanges(BaseModel newModel) |
| | 86 | | { |
| 12 | 87 | | UIScrollRectRefContainer rc = referencesContainer; |
| | 88 | |
|
| 12 | 89 | | rc.contentBackground.color = model.backgroundColor; |
| | 90 | |
|
| | 91 | | // Apply padding |
| 12 | 92 | | rc.paddingLayoutGroup.padding.bottom = Mathf.RoundToInt(model.paddingBottom); |
| 12 | 93 | | rc.paddingLayoutGroup.padding.top = Mathf.RoundToInt(model.paddingTop); |
| 12 | 94 | | rc.paddingLayoutGroup.padding.left = Mathf.RoundToInt(model.paddingLeft); |
| 12 | 95 | | rc.paddingLayoutGroup.padding.right = Mathf.RoundToInt(model.paddingRight); |
| | 96 | |
|
| 12 | 97 | | rc.scrollRect.horizontal = model.isHorizontal; |
| 12 | 98 | | rc.scrollRect.vertical = model.isVertical; |
| | 99 | |
|
| 12 | 100 | | rc.HScrollbar.value = model.valueX; |
| 12 | 101 | | rc.VScrollbar.value = model.valueY; |
| | 102 | |
|
| 12 | 103 | | rc.scrollRect.onValueChanged.AddListener(OnChanged); |
| | 104 | |
|
| 12 | 105 | | MarkLayoutDirty( () => |
| | 106 | | { |
| 12 | 107 | | referencesContainer.fitter.RefreshRecursively(); |
| 12 | 108 | | AdjustChildHook(); |
| 12 | 109 | | } |
| | 110 | | ); |
| 12 | 111 | | return null; |
| | 112 | | } |
| | 113 | |
|
| 26 | 114 | | void OnChanged(Vector2 scrollingValues) { WebInterface.ReportOnScrollChange(scene.sceneData.id, model.OnChanged, |
| | 115 | |
|
| | 116 | | public override void Dispose() |
| | 117 | | { |
| 1 | 118 | | if (referencesContainer != null) |
| | 119 | | { |
| 1 | 120 | | referencesContainer.scrollRect.onValueChanged.RemoveAllListeners(); |
| 1 | 121 | | Utils.SafeDestroy(referencesContainer.gameObject); |
| | 122 | | } |
| | 123 | |
|
| 1 | 124 | | base.Dispose(); |
| 1 | 125 | | } |
| | 126 | | } |
| | 127 | | } |