From cc7c66f9f590ede02cc780bf71566db1a5d02e9d Mon Sep 17 00:00:00 2001 From: Simon Jackson Date: Thu, 23 Feb 2017 15:18:16 +0000 Subject: [PATCH] Updated Scroll Snap Base to resolve #119 --- Scripts/Layout/ScrollSnapBase.cs | 740 ++++++++++++++++--------------- 1 file changed, 374 insertions(+), 366 deletions(-) diff --git a/Scripts/Layout/ScrollSnapBase.cs b/Scripts/Layout/ScrollSnapBase.cs index 295c1e5..110b49c 100644 --- a/Scripts/Layout/ScrollSnapBase.cs +++ b/Scripts/Layout/ScrollSnapBase.cs @@ -4,444 +4,452 @@ using UnityEngine.EventSystems; namespace UnityEngine.UI.Extensions { - public class ScrollSnapBase : MonoBehaviour, IBeginDragHandler, IDragHandler - { - internal RectTransform _screensContainer; - internal bool _isVertical; + public class ScrollSnapBase : MonoBehaviour, IBeginDragHandler, IDragHandler + { + internal RectTransform _screensContainer; + internal bool _isVertical; - internal int _screens = 1; + internal int _screens = 1; - internal float _scrollStartPosition; - internal float _childSize; - private float _childPos, _maskSize; - internal Vector2 _childAnchorPoint; - internal ScrollRect _scroll_rect; - internal Vector3 _lerp_target; - internal bool _lerp; - internal bool _pointerDown = false; - internal bool _settled = true; - internal Vector3 _startPosition = new Vector3(); - [Tooltip("The currently active page")] - internal int _currentPage; - internal int _previousPage; - internal int _halfNoVisibleItems; - private int _bottomItem, _topItem; + internal float _scrollStartPosition; + internal float _childSize; + private float _childPos, _maskSize; + internal Vector2 _childAnchorPoint; + internal ScrollRect _scroll_rect; + internal Vector3 _lerp_target; + internal bool _lerp; + internal bool _pointerDown = false; + internal bool _settled = true; + internal Vector3 _startPosition = new Vector3(); + [Tooltip("The currently active page")] + internal int _currentPage; + internal int _previousPage; + internal int _halfNoVisibleItems; + private int _bottomItem, _topItem; - [Serializable] - public class SelectionChangeStartEvent : UnityEvent { } - [Serializable] - public class SelectionPageChangedEvent : UnityEvent { } - [Serializable] - public class SelectionChangeEndEvent : UnityEvent { } + [Serializable] + public class SelectionChangeStartEvent : UnityEvent { } + [Serializable] + public class SelectionPageChangedEvent : UnityEvent { } + [Serializable] + public class SelectionChangeEndEvent : UnityEvent { } - [Tooltip("The screen / page to start the control on\n*Note, this is a 0 indexed array")] - [SerializeField] - public int StartingScreen = 0; + [Tooltip("The screen / page to start the control on\n*Note, this is a 0 indexed array")] + [SerializeField] + public int StartingScreen = 0; - [Tooltip("The distance between two pages based on page height, by default pages are next to each other")] - [SerializeField] - [Range(1, 8)] - public float PageStep = 1; + [Tooltip("The distance between two pages based on page height, by default pages are next to each other")] + [SerializeField] + [Range(0, 8)] + public float PageStep = 1; - [Tooltip("The gameobject that contains toggles which suggest pagination. (optional)")] - public GameObject Pagination; + [Tooltip("The gameobject that contains toggles which suggest pagination. (optional)")] + public GameObject Pagination; - [Tooltip("Button to go to the previous page. (optional)")] - public GameObject PrevButton; + [Tooltip("Button to go to the previous page. (optional)")] + public GameObject PrevButton; - [Tooltip("Button to go to the next page. (optional)")] - public GameObject NextButton; + [Tooltip("Button to go to the next page. (optional)")] + public GameObject NextButton; - [Tooltip("Transition speed between pages. (optional)")] - public float transitionSpeed = 7.5f; + [Tooltip("Transition speed between pages. (optional)")] + public float transitionSpeed = 7.5f; - [Tooltip("Fast Swipe makes swiping page next / previous (optional)")] - public Boolean UseFastSwipe = false; + [Tooltip("Fast Swipe makes swiping page next / previous (optional)")] + public Boolean UseFastSwipe = false; - [Tooltip("How far swipe has to travel to initiate a page change (optional)")] - public int FastSwipeThreshold = 100; + [Tooltip("How far swipe has to travel to initiate a page change (optional)")] + public int FastSwipeThreshold = 100; - [Tooltip("Speed at which the ScrollRect will keep scrolling before slowing down and stopping (optional)")] - public int SwipeVelocityThreshold = 200; + [Tooltip("Speed at which the ScrollRect will keep scrolling before slowing down and stopping (optional)")] + public int SwipeVelocityThreshold = 200; - [Tooltip("The visible bounds area, controls which items are visible/enabled. *Note Should use a RectMask. (optional)")] - public RectTransform MaskArea; + [Tooltip("The visible bounds area, controls which items are visible/enabled. *Note Should use a RectMask. (optional)")] + public RectTransform MaskArea; - [Tooltip("Pixel size to buffer arround Mask Area. (optional)")] - public float MaskBuffer = 1; - - public int CurrentPage - { - get - { - return _currentPage; - } - internal set - { - if ((value != _currentPage && value >= 0 && value < _screensContainer.childCount) || (value == 0 && _screensContainer.childCount == 0)) - { - _previousPage = _currentPage; - _currentPage = value; - if(MaskArea) UpdateVisible(); - if(!_lerp) ScreenChange(); - OnCurrentScreenChange(_currentPage); - } - } - } + [Tooltip("Pixel size to buffer arround Mask Area. (optional)")] + public float MaskBuffer = 1; - [Tooltip("(Experimental)\nBy default, child array objects will use the parent transform\nHowever you can disable this for some interesting effects")] - public bool UseParentTransform = true; + public int CurrentPage + { + get + { + return _currentPage; + } - [Tooltip("Scroll Snap children. (optional)\nEither place objects in the scene as children OR\nPrefabs in this array, NOT BOTH")] - public GameObject[] ChildObjects; - - [SerializeField] - [Tooltip("Event fires when a user starts to change the selection")] - private SelectionChangeStartEvent m_OnSelectionChangeStartEvent = new SelectionChangeStartEvent(); - public SelectionChangeStartEvent OnSelectionChangeStartEvent { get { return m_OnSelectionChangeStartEvent; } set { m_OnSelectionChangeStartEvent = value; } } + internal set + { + if ((value != _currentPage && value >= 0 && value < _screensContainer.childCount) || (value == 0 && _screensContainer.childCount == 0)) + { + _previousPage = _currentPage; + _currentPage = value; + if(MaskArea) UpdateVisible(); + if(!_lerp) ScreenChange(); + OnCurrentScreenChange(_currentPage); + } + } + } - [SerializeField] - [Tooltip("Event fires as the page changes, while dragging or jumping")] - private SelectionPageChangedEvent m_OnSelectionPageChangedEvent = new SelectionPageChangedEvent(); - public SelectionPageChangedEvent OnSelectionPageChangedEvent { get { return m_OnSelectionPageChangedEvent; } set { m_OnSelectionPageChangedEvent = value; } } + [Tooltip("(Experimental)\nBy default, child array objects will use the parent transform\nHowever you can disable this for some interesting effects")] + public bool UseParentTransform = true; - [SerializeField] - [Tooltip("Event fires when the page settles after a user has dragged")] - private SelectionChangeEndEvent m_OnSelectionChangeEndEvent = new SelectionChangeEndEvent(); - public SelectionChangeEndEvent OnSelectionChangeEndEvent { get { return m_OnSelectionChangeEndEvent; } set { m_OnSelectionChangeEndEvent = value; } } + [Tooltip("Scroll Snap children. (optional)\nEither place objects in the scene as children OR\nPrefabs in this array, NOT BOTH")] + public GameObject[] ChildObjects; + [SerializeField] + [Tooltip("Event fires when a user starts to change the selection")] + private SelectionChangeStartEvent m_OnSelectionChangeStartEvent = new SelectionChangeStartEvent(); + public SelectionChangeStartEvent OnSelectionChangeStartEvent { get { return m_OnSelectionChangeStartEvent; } set { m_OnSelectionChangeStartEvent = value; } } - // Use this for initialization + [SerializeField] + [Tooltip("Event fires as the page changes, while dragging or jumping")] + private SelectionPageChangedEvent m_OnSelectionPageChangedEvent = new SelectionPageChangedEvent(); + public SelectionPageChangedEvent OnSelectionPageChangedEvent { get { return m_OnSelectionPageChangedEvent; } set { m_OnSelectionPageChangedEvent = value; } } + + [SerializeField] + [Tooltip("Event fires when the page settles after a user has dragged")] + private SelectionChangeEndEvent m_OnSelectionChangeEndEvent = new SelectionChangeEndEvent(); + public SelectionChangeEndEvent OnSelectionChangeEndEvent { get { return m_OnSelectionChangeEndEvent; } set { m_OnSelectionChangeEndEvent = value; } } + + // Use this for initialization void Awake() - { - _scroll_rect = gameObject.GetComponent(); + { + _scroll_rect = gameObject.GetComponent(); - if (_scroll_rect.horizontalScrollbar || _scroll_rect.verticalScrollbar) - { - Debug.LogWarning("Warning, using scrollbars with the Scroll Snap controls is not advised as it causes unpredictable results"); - } + if (_scroll_rect.horizontalScrollbar || _scroll_rect.verticalScrollbar) + { + Debug.LogWarning("Warning, using scrollbars with the Scroll Snap controls is not advised as it causes unpredictable results"); + } - if (StartingScreen < 0) - { - StartingScreen = 0; - } + if (StartingScreen < 0) + { + StartingScreen = 0; + } - _screensContainer = _scroll_rect.content; - if (ChildObjects != null && ChildObjects.Length > 0) - { - if (_screensContainer.transform.childCount > 0) - { - Debug.LogError("ScrollRect Content has children, this is not supported when using managed Child Objects\n Either remove the ScrollRect Content children or clear the ChildObjects array"); - return; - } - InitialiseChildObjectsFromArray(); - } - else - { - InitialiseChildObjectsFromScene(); - } + _screensContainer = _scroll_rect.content; + if (ChildObjects != null && ChildObjects.Length > 0) + { + if (_screensContainer.transform.childCount > 0) + { + Debug.LogError("ScrollRect Content has children, this is not supported when using managed Child Objects\n Either remove the ScrollRect Content children or clear the ChildObjects array"); + return; + } - if (NextButton) - NextButton.GetComponent