Updated Scroll Snap Base to resolve #119
parent
48e162ebde
commit
cc7c66f9f5
|
@ -4,444 +4,452 @@ using UnityEngine.EventSystems;
|
||||||
|
|
||||||
namespace UnityEngine.UI.Extensions
|
namespace UnityEngine.UI.Extensions
|
||||||
{
|
{
|
||||||
public class ScrollSnapBase : MonoBehaviour, IBeginDragHandler, IDragHandler
|
public class ScrollSnapBase : MonoBehaviour, IBeginDragHandler, IDragHandler
|
||||||
{
|
{
|
||||||
internal RectTransform _screensContainer;
|
internal RectTransform _screensContainer;
|
||||||
internal bool _isVertical;
|
internal bool _isVertical;
|
||||||
|
|
||||||
internal int _screens = 1;
|
internal int _screens = 1;
|
||||||
|
|
||||||
internal float _scrollStartPosition;
|
internal float _scrollStartPosition;
|
||||||
internal float _childSize;
|
internal float _childSize;
|
||||||
private float _childPos, _maskSize;
|
private float _childPos, _maskSize;
|
||||||
internal Vector2 _childAnchorPoint;
|
internal Vector2 _childAnchorPoint;
|
||||||
internal ScrollRect _scroll_rect;
|
internal ScrollRect _scroll_rect;
|
||||||
internal Vector3 _lerp_target;
|
internal Vector3 _lerp_target;
|
||||||
internal bool _lerp;
|
internal bool _lerp;
|
||||||
internal bool _pointerDown = false;
|
internal bool _pointerDown = false;
|
||||||
internal bool _settled = true;
|
internal bool _settled = true;
|
||||||
internal Vector3 _startPosition = new Vector3();
|
internal Vector3 _startPosition = new Vector3();
|
||||||
[Tooltip("The currently active page")]
|
[Tooltip("The currently active page")]
|
||||||
internal int _currentPage;
|
internal int _currentPage;
|
||||||
internal int _previousPage;
|
internal int _previousPage;
|
||||||
internal int _halfNoVisibleItems;
|
internal int _halfNoVisibleItems;
|
||||||
private int _bottomItem, _topItem;
|
private int _bottomItem, _topItem;
|
||||||
|
|
||||||
[Serializable]
|
[Serializable]
|
||||||
public class SelectionChangeStartEvent : UnityEvent { }
|
public class SelectionChangeStartEvent : UnityEvent { }
|
||||||
[Serializable]
|
[Serializable]
|
||||||
public class SelectionPageChangedEvent : UnityEvent<int> { }
|
public class SelectionPageChangedEvent : UnityEvent<int> { }
|
||||||
[Serializable]
|
[Serializable]
|
||||||
public class SelectionChangeEndEvent : UnityEvent<int> { }
|
public class SelectionChangeEndEvent : UnityEvent<int> { }
|
||||||
|
|
||||||
[Tooltip("The screen / page to start the control on\n*Note, this is a 0 indexed array")]
|
[Tooltip("The screen / page to start the control on\n*Note, this is a 0 indexed array")]
|
||||||
[SerializeField]
|
[SerializeField]
|
||||||
public int StartingScreen = 0;
|
public int StartingScreen = 0;
|
||||||
|
|
||||||
[Tooltip("The distance between two pages based on page height, by default pages are next to each other")]
|
[Tooltip("The distance between two pages based on page height, by default pages are next to each other")]
|
||||||
[SerializeField]
|
[SerializeField]
|
||||||
[Range(1, 8)]
|
[Range(0, 8)]
|
||||||
public float PageStep = 1;
|
public float PageStep = 1;
|
||||||
|
|
||||||
[Tooltip("The gameobject that contains toggles which suggest pagination. (optional)")]
|
[Tooltip("The gameobject that contains toggles which suggest pagination. (optional)")]
|
||||||
public GameObject Pagination;
|
public GameObject Pagination;
|
||||||
|
|
||||||
[Tooltip("Button to go to the previous page. (optional)")]
|
[Tooltip("Button to go to the previous page. (optional)")]
|
||||||
public GameObject PrevButton;
|
public GameObject PrevButton;
|
||||||
|
|
||||||
[Tooltip("Button to go to the next page. (optional)")]
|
[Tooltip("Button to go to the next page. (optional)")]
|
||||||
public GameObject NextButton;
|
public GameObject NextButton;
|
||||||
|
|
||||||
[Tooltip("Transition speed between pages. (optional)")]
|
[Tooltip("Transition speed between pages. (optional)")]
|
||||||
public float transitionSpeed = 7.5f;
|
public float transitionSpeed = 7.5f;
|
||||||
|
|
||||||
[Tooltip("Fast Swipe makes swiping page next / previous (optional)")]
|
[Tooltip("Fast Swipe makes swiping page next / previous (optional)")]
|
||||||
public Boolean UseFastSwipe = false;
|
public Boolean UseFastSwipe = false;
|
||||||
|
|
||||||
[Tooltip("How far swipe has to travel to initiate a page change (optional)")]
|
[Tooltip("How far swipe has to travel to initiate a page change (optional)")]
|
||||||
public int FastSwipeThreshold = 100;
|
public int FastSwipeThreshold = 100;
|
||||||
|
|
||||||
[Tooltip("Speed at which the ScrollRect will keep scrolling before slowing down and stopping (optional)")]
|
[Tooltip("Speed at which the ScrollRect will keep scrolling before slowing down and stopping (optional)")]
|
||||||
public int SwipeVelocityThreshold = 200;
|
public int SwipeVelocityThreshold = 200;
|
||||||
|
|
||||||
[Tooltip("The visible bounds area, controls which items are visible/enabled. *Note Should use a RectMask. (optional)")]
|
[Tooltip("The visible bounds area, controls which items are visible/enabled. *Note Should use a RectMask. (optional)")]
|
||||||
public RectTransform MaskArea;
|
public RectTransform MaskArea;
|
||||||
|
|
||||||
[Tooltip("Pixel size to buffer arround Mask Area. (optional)")]
|
[Tooltip("Pixel size to buffer arround Mask Area. (optional)")]
|
||||||
public float MaskBuffer = 1;
|
public float MaskBuffer = 1;
|
||||||
|
|
||||||
public int CurrentPage
|
public int CurrentPage
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return _currentPage;
|
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("(Experimental)\nBy default, child array objects will use the parent transform\nHowever you can disable this for some interesting effects")]
|
internal set
|
||||||
public bool UseParentTransform = true;
|
{
|
||||||
|
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("Scroll Snap children. (optional)\nEither place objects in the scene as children OR\nPrefabs in this array, NOT BOTH")]
|
[Tooltip("(Experimental)\nBy default, child array objects will use the parent transform\nHowever you can disable this for some interesting effects")]
|
||||||
public GameObject[] ChildObjects;
|
public bool UseParentTransform = true;
|
||||||
|
|
||||||
[SerializeField]
|
[Tooltip("Scroll Snap children. (optional)\nEither place objects in the scene as children OR\nPrefabs in this array, NOT BOTH")]
|
||||||
[Tooltip("Event fires when a user starts to change the selection")]
|
public GameObject[] ChildObjects;
|
||||||
private SelectionChangeStartEvent m_OnSelectionChangeStartEvent = new SelectionChangeStartEvent();
|
|
||||||
public SelectionChangeStartEvent OnSelectionChangeStartEvent { get { return m_OnSelectionChangeStartEvent; } set { m_OnSelectionChangeStartEvent = value; } }
|
|
||||||
|
|
||||||
[SerializeField]
|
[SerializeField]
|
||||||
[Tooltip("Event fires as the page changes, while dragging or jumping")]
|
[Tooltip("Event fires when a user starts to change the selection")]
|
||||||
private SelectionPageChangedEvent m_OnSelectionPageChangedEvent = new SelectionPageChangedEvent();
|
private SelectionChangeStartEvent m_OnSelectionChangeStartEvent = new SelectionChangeStartEvent();
|
||||||
public SelectionPageChangedEvent OnSelectionPageChangedEvent { get { return m_OnSelectionPageChangedEvent; } set { m_OnSelectionPageChangedEvent = value; } }
|
public SelectionChangeStartEvent OnSelectionChangeStartEvent { get { return m_OnSelectionChangeStartEvent; } set { m_OnSelectionChangeStartEvent = value; } }
|
||||||
|
|
||||||
[SerializeField]
|
[SerializeField]
|
||||||
[Tooltip("Event fires when the page settles after a user has dragged")]
|
[Tooltip("Event fires as the page changes, while dragging or jumping")]
|
||||||
private SelectionChangeEndEvent m_OnSelectionChangeEndEvent = new SelectionChangeEndEvent();
|
private SelectionPageChangedEvent m_OnSelectionPageChangedEvent = new SelectionPageChangedEvent();
|
||||||
public SelectionChangeEndEvent OnSelectionChangeEndEvent { get { return m_OnSelectionChangeEndEvent; } set { m_OnSelectionChangeEndEvent = value; } }
|
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
|
// Use this for initialization
|
||||||
void Awake()
|
void Awake()
|
||||||
{
|
{
|
||||||
_scroll_rect = gameObject.GetComponent<ScrollRect>();
|
_scroll_rect = gameObject.GetComponent<ScrollRect>();
|
||||||
|
|
||||||
if (_scroll_rect.horizontalScrollbar || _scroll_rect.verticalScrollbar)
|
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");
|
Debug.LogWarning("Warning, using scrollbars with the Scroll Snap controls is not advised as it causes unpredictable results");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (StartingScreen < 0)
|
if (StartingScreen < 0)
|
||||||
{
|
{
|
||||||
StartingScreen = 0;
|
StartingScreen = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
_screensContainer = _scroll_rect.content;
|
_screensContainer = _scroll_rect.content;
|
||||||
if (ChildObjects != null && ChildObjects.Length > 0)
|
if (ChildObjects != null && ChildObjects.Length > 0)
|
||||||
{
|
{
|
||||||
if (_screensContainer.transform.childCount > 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");
|
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;
|
return;
|
||||||
}
|
}
|
||||||
InitialiseChildObjectsFromArray();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
InitialiseChildObjectsFromScene();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (NextButton)
|
InitialiseChildObjectsFromArray();
|
||||||
NextButton.GetComponent<Button>().onClick.AddListener(() => { NextScreen(); });
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
InitialiseChildObjectsFromScene();
|
||||||
|
}
|
||||||
|
|
||||||
if (PrevButton)
|
if (NextButton)
|
||||||
PrevButton.GetComponent<Button>().onClick.AddListener(() => { PreviousScreen(); });
|
NextButton.GetComponent<Button>().onClick.AddListener(() => { NextScreen(); });
|
||||||
}
|
|
||||||
|
|
||||||
internal void InitialiseChildObjectsFromScene()
|
if (PrevButton)
|
||||||
{
|
PrevButton.GetComponent<Button>().onClick.AddListener(() => { PreviousScreen(); });
|
||||||
int childCount = _screensContainer.childCount;
|
}
|
||||||
ChildObjects = new GameObject[childCount];
|
|
||||||
for (int i = 0; i < childCount; i++)
|
|
||||||
{
|
|
||||||
ChildObjects[i] = _screensContainer.transform.GetChild(i).gameObject;
|
|
||||||
if (MaskArea && ChildObjects[i].activeSelf)
|
|
||||||
{
|
|
||||||
ChildObjects[i].SetActive(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal void InitialiseChildObjectsFromArray()
|
internal void InitialiseChildObjectsFromScene()
|
||||||
{
|
{
|
||||||
int childCount = ChildObjects.Length;
|
int childCount = _screensContainer.childCount;
|
||||||
RectTransform childRect;
|
ChildObjects = new GameObject[childCount];
|
||||||
GameObject child;
|
for (int i = 0; i < childCount; i++)
|
||||||
for (int i = 0; i < childCount; i++)
|
{
|
||||||
{
|
ChildObjects[i] = _screensContainer.transform.GetChild(i).gameObject;
|
||||||
child = GameObject.Instantiate(ChildObjects[i]);
|
if (MaskArea && ChildObjects[i].activeSelf)
|
||||||
//Optionally, use original GO transform when initialising, by default will use parent RectTransform position/rotation
|
{
|
||||||
|
ChildObjects[i].SetActive(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void InitialiseChildObjectsFromArray()
|
||||||
|
{
|
||||||
|
int childCount = ChildObjects.Length;
|
||||||
|
RectTransform childRect;
|
||||||
|
GameObject child;
|
||||||
|
for (int i = 0; i < childCount; i++)
|
||||||
|
{
|
||||||
|
child = GameObject.Instantiate(ChildObjects[i]);
|
||||||
|
//Optionally, use original GO transform when initialising, by default will use parent RectTransform position/rotation
|
||||||
if (UseParentTransform)
|
if (UseParentTransform)
|
||||||
{
|
{
|
||||||
childRect = child.GetComponent<RectTransform>();
|
childRect = child.GetComponent<RectTransform>();
|
||||||
childRect.rotation = _screensContainer.rotation;
|
childRect.rotation = _screensContainer.rotation;
|
||||||
childRect.localScale = _screensContainer.localScale;
|
childRect.localScale = _screensContainer.localScale;
|
||||||
childRect.position = _screensContainer.position;
|
childRect.position = _screensContainer.position;
|
||||||
}
|
}
|
||||||
child.transform.SetParent(_screensContainer.transform);
|
|
||||||
ChildObjects[i] = child;
|
|
||||||
if (MaskArea && ChildObjects[i].activeSelf)
|
|
||||||
{
|
|
||||||
ChildObjects[i].SetActive(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal void UpdateVisible()
|
child.transform.SetParent(_screensContainer.transform);
|
||||||
{
|
ChildObjects[i] = child;
|
||||||
//If there are no objects in the scene or a mask, exit
|
if (MaskArea && ChildObjects[i].activeSelf)
|
||||||
|
{
|
||||||
|
ChildObjects[i].SetActive(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void UpdateVisible()
|
||||||
|
{
|
||||||
|
//If there are no objects in the scene or a mask, exit
|
||||||
if (!MaskArea || ChildObjects == null || ChildObjects.Length < 1 || _screensContainer.childCount < 1)
|
if (!MaskArea || ChildObjects == null || ChildObjects.Length < 1 || _screensContainer.childCount < 1)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
_maskSize = _isVertical ? MaskArea.rect.height : MaskArea.rect.width;
|
_maskSize = _isVertical ? MaskArea.rect.height : MaskArea.rect.width;
|
||||||
_halfNoVisibleItems = (int)Math.Round(_maskSize / (_childSize * MaskBuffer), MidpointRounding.AwayFromZero) / 2;
|
_halfNoVisibleItems = (int)Math.Round(_maskSize / (_childSize * MaskBuffer), MidpointRounding.AwayFromZero) / 2;
|
||||||
_bottomItem = _topItem = 0;
|
_bottomItem = _topItem = 0;
|
||||||
//work out how many items below the current page can be visible
|
//work out how many items below the current page can be visible
|
||||||
for (int i = _halfNoVisibleItems + 1; i > 0 ; i--)
|
|
||||||
{
|
|
||||||
_bottomItem = _currentPage - i < 0 ? 0 : i;
|
|
||||||
if (_bottomItem > 0) break;
|
|
||||||
}
|
|
||||||
//work out how many items above the current page can be visible
|
|
||||||
for (int i = _halfNoVisibleItems + 1; i > 0; i--)
|
for (int i = _halfNoVisibleItems + 1; i > 0; i--)
|
||||||
{
|
{
|
||||||
_topItem = _screensContainer.childCount - _currentPage - i < 0 ? 0 : i;
|
_bottomItem = _currentPage - i < 0 ? 0 : i;
|
||||||
if (_topItem > 0) break;
|
if (_bottomItem > 0) break;
|
||||||
}
|
}
|
||||||
//Set the active items active
|
|
||||||
|
//work out how many items above the current page can be visible
|
||||||
|
for (int i = _halfNoVisibleItems + 1; i > 0; i--)
|
||||||
|
{
|
||||||
|
_topItem = _screensContainer.childCount - _currentPage - i < 0 ? 0 : i;
|
||||||
|
if (_topItem > 0) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Set the active items active
|
||||||
for (int i = CurrentPage - _bottomItem; i < CurrentPage + _topItem; i++)
|
for (int i = CurrentPage - _bottomItem; i < CurrentPage + _topItem; i++)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
ChildObjects[i].SetActive(true);
|
ChildObjects[i].SetActive(true);
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
Debug.Log("Failed to setactive child [" + i + "]");
|
Debug.Log("Failed to setactive child [" + i + "]");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Deactivate items out of visibility at the bottom of the ScrollRect Mask (only on scroll)
|
//Deactivate items out of visibility at the bottom of the ScrollRect Mask (only on scroll)
|
||||||
if (_currentPage > _halfNoVisibleItems) ChildObjects[CurrentPage - _bottomItem].SetActive(false);
|
if (_currentPage > _halfNoVisibleItems) ChildObjects[CurrentPage - _bottomItem].SetActive(false);
|
||||||
//Deactivate items out of visibility at the top of the ScrollRect Mask (only on scroll)
|
//Deactivate items out of visibility at the top of the ScrollRect Mask (only on scroll)
|
||||||
if (_screensContainer.childCount - _currentPage > _topItem) ChildObjects[CurrentPage + _topItem].SetActive(false);
|
if (_screensContainer.childCount - _currentPage > _topItem) ChildObjects[CurrentPage + _topItem].SetActive(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Function for switching screens with buttons
|
||||||
//Function for switching screens with buttons
|
|
||||||
public void NextScreen()
|
public void NextScreen()
|
||||||
{
|
{
|
||||||
if (_currentPage < _screens - 1)
|
if (_currentPage < _screens - 1)
|
||||||
{
|
{
|
||||||
if (!_lerp) StartScreenChange();
|
if (!_lerp) StartScreenChange();
|
||||||
|
|
||||||
_lerp = true;
|
_lerp = true;
|
||||||
CurrentPage = _currentPage + 1;
|
CurrentPage = _currentPage + 1;
|
||||||
GetPositionforPage(_currentPage, ref _lerp_target);
|
GetPositionforPage(_currentPage, ref _lerp_target);
|
||||||
ScreenChange();
|
ScreenChange();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Function for switching screens with buttons
|
//Function for switching screens with buttons
|
||||||
public void PreviousScreen()
|
public void PreviousScreen()
|
||||||
{
|
{
|
||||||
if (_currentPage > 0)
|
if (_currentPage > 0)
|
||||||
{
|
{
|
||||||
if (!_lerp) StartScreenChange();
|
if (!_lerp) StartScreenChange();
|
||||||
|
|
||||||
_lerp = true;
|
_lerp = true;
|
||||||
CurrentPage = _currentPage - 1;
|
CurrentPage = _currentPage - 1;
|
||||||
GetPositionforPage(_currentPage, ref _lerp_target);
|
GetPositionforPage(_currentPage, ref _lerp_target);
|
||||||
ScreenChange();
|
ScreenChange();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Function for switching to a specific screen
|
/// Function for switching to a specific screen
|
||||||
/// *Note, this is based on a 0 starting index - 0 to x
|
/// *Note, this is based on a 0 starting index - 0 to x
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="screenIndex">0 starting index of page to jump to</param>
|
/// <param name="screenIndex">0 starting index of page to jump to</param>
|
||||||
public void GoToScreen(int screenIndex)
|
public void GoToScreen(int screenIndex)
|
||||||
{
|
{
|
||||||
if (screenIndex <= _screens - 1 && screenIndex >= 0)
|
if (screenIndex <= _screens - 1 && screenIndex >= 0)
|
||||||
{
|
{
|
||||||
if (!_lerp) StartScreenChange();
|
if (!_lerp) StartScreenChange();
|
||||||
|
|
||||||
_lerp = true;
|
_lerp = true;
|
||||||
CurrentPage = screenIndex;
|
CurrentPage = screenIndex;
|
||||||
GetPositionforPage(_currentPage, ref _lerp_target);
|
GetPositionforPage(_currentPage, ref _lerp_target);
|
||||||
ScreenChange();
|
ScreenChange();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the closest page for the current Scroll Rect container position
|
/// Gets the closest page for the current Scroll Rect container position
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="pos">Position to test, normally the Scroll Rect container Local position</param>
|
/// <param name="pos">Position to test, normally the Scroll Rect container Local position</param>
|
||||||
/// <returns>Closest Page number (zero indexed array value)</returns>
|
/// <returns>Closest Page number (zero indexed array value)</returns>
|
||||||
internal int GetPageforPosition(Vector3 pos)
|
internal int GetPageforPosition(Vector3 pos)
|
||||||
{
|
{
|
||||||
return _isVertical ?
|
return _isVertical ?
|
||||||
-(int)Math.Round((pos.y - _scrollStartPosition) / _childSize) :
|
-(int)Math.Round((pos.y - _scrollStartPosition) / _childSize) :
|
||||||
-(int)Math.Round((pos.x - _scrollStartPosition) / _childSize);
|
-(int)Math.Round((pos.x - _scrollStartPosition) / _childSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Validates if the current Scroll Rect container position is within the bounds for a page
|
/// Validates if the current Scroll Rect container position is within the bounds for a page
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="pos">Position to test, normally the Scroll Rect container Local position</param>
|
/// <param name="pos">Position to test, normally the Scroll Rect container Local position</param>
|
||||||
/// <returns>True / False, is the position in the bounds of a page</returns>
|
/// <returns>True / False, is the position in the bounds of a page</returns>
|
||||||
internal bool IsRectSettledOnaPage(Vector3 pos)
|
internal bool IsRectSettledOnaPage(Vector3 pos)
|
||||||
{
|
{
|
||||||
return _isVertical ?
|
return _isVertical ?
|
||||||
-((pos.y - _scrollStartPosition) / _childSize) == -(int)Math.Round((pos.y - _scrollStartPosition) / _childSize) :
|
-((pos.y - _scrollStartPosition) / _childSize) == -(int)Math.Round((pos.y - _scrollStartPosition) / _childSize) :
|
||||||
-((pos.x - _scrollStartPosition) / _childSize) == -(int)Math.Round((pos.x - _scrollStartPosition) / _childSize);
|
-((pos.x - _scrollStartPosition) / _childSize) == -(int)Math.Round((pos.x - _scrollStartPosition) / _childSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns the local position for a child page based on the required page number
|
/// Returns the local position for a child page based on the required page number
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="page">Page that the position is required for (Zero indexed array value)</param>
|
/// <param name="page">Page that the position is required for (Zero indexed array value)</param>
|
||||||
/// <param name="target">Outputs the local position for the selected page</param>
|
/// <param name="target">Outputs the local position for the selected page</param>
|
||||||
internal void GetPositionforPage(int page, ref Vector3 target)
|
internal void GetPositionforPage(int page, ref Vector3 target)
|
||||||
{
|
{
|
||||||
_childPos = -_childSize * page;
|
_childPos = -_childSize * page;
|
||||||
if (_isVertical)
|
if (_isVertical)
|
||||||
{
|
{
|
||||||
target.y = _childPos + _scrollStartPosition;
|
target.y = _childPos + _scrollStartPosition;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
target.x = _childPos + _scrollStartPosition;
|
target.x = _childPos + _scrollStartPosition;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Updates the _Lerp target to the closest page and updates the pagination bullets. Each control's update loop will then handle the move.
|
/// Updates the _Lerp target to the closest page and updates the pagination bullets. Each control's update loop will then handle the move.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal void ScrollToClosestElement()
|
internal void ScrollToClosestElement()
|
||||||
{
|
{
|
||||||
_lerp = true;
|
_lerp = true;
|
||||||
CurrentPage = GetPageforPosition(_screensContainer.localPosition);
|
CurrentPage = GetPageforPosition(_screensContainer.localPosition);
|
||||||
GetPositionforPage(_currentPage, ref _lerp_target);
|
GetPositionforPage(_currentPage, ref _lerp_target);
|
||||||
OnCurrentScreenChange(_currentPage);
|
OnCurrentScreenChange(_currentPage);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// notifies pagination indicator and navigation buttons of a screen change
|
/// notifies pagination indicator and navigation buttons of a screen change
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal void OnCurrentScreenChange(int currentScreen)
|
internal void OnCurrentScreenChange(int currentScreen)
|
||||||
{
|
{
|
||||||
ChangeBulletsInfo(currentScreen);
|
ChangeBulletsInfo(currentScreen);
|
||||||
ToggleNavigationButtons(currentScreen);
|
ToggleNavigationButtons(currentScreen);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// changes the bullets on the bottom of the page - pagination
|
/// changes the bullets on the bottom of the page - pagination
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="targetScreen"></param>
|
/// <param name="targetScreen"></param>
|
||||||
private void ChangeBulletsInfo(int targetScreen)
|
private void ChangeBulletsInfo(int targetScreen)
|
||||||
{
|
{
|
||||||
if (Pagination)
|
if (Pagination)
|
||||||
for (int i = 0; i < Pagination.transform.childCount; i++)
|
for (int i = 0; i < Pagination.transform.childCount; i++)
|
||||||
{
|
{
|
||||||
Pagination.transform.GetChild(i).GetComponent<Toggle>().isOn = (targetScreen == i)
|
Pagination.transform.GetChild(i).GetComponent<Toggle>().isOn = (targetScreen == i)
|
||||||
? true
|
? true
|
||||||
: false;
|
: false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// disables the page navigation buttons when at the first or last screen
|
/// disables the page navigation buttons when at the first or last screen
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="targetScreen"></param>
|
/// <param name="targetScreen"></param>
|
||||||
private void ToggleNavigationButtons(int targetScreen) {
|
private void ToggleNavigationButtons(int targetScreen) {
|
||||||
if (PrevButton) {
|
if (PrevButton) {
|
||||||
PrevButton.GetComponent<Button>().interactable = targetScreen > 0;
|
PrevButton.GetComponent<Button>().interactable = targetScreen > 0;
|
||||||
}
|
}
|
||||||
if (NextButton) {
|
|
||||||
NextButton.GetComponent<Button>().interactable = targetScreen < _screensContainer.transform.childCount - 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnValidate()
|
if (NextButton) {
|
||||||
{
|
NextButton.GetComponent<Button>().interactable = targetScreen < _screensContainer.transform.childCount - 1;
|
||||||
var children = gameObject.GetComponent<ScrollRect>().content.childCount;
|
}
|
||||||
if (children != 0 || ChildObjects != null)
|
}
|
||||||
{
|
|
||||||
var childCount = ChildObjects == null || ChildObjects.Length == 0 ? children : ChildObjects.Length;
|
|
||||||
if (StartingScreen > childCount - 1)
|
|
||||||
{
|
|
||||||
StartingScreen = childCount - 1;
|
|
||||||
}
|
|
||||||
if (StartingScreen < 0)
|
|
||||||
{
|
|
||||||
StartingScreen = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (MaskBuffer <= 0)
|
|
||||||
{
|
|
||||||
MaskBuffer = 1;
|
|
||||||
}
|
|
||||||
if (PageStep < 0)
|
|
||||||
{
|
|
||||||
PageStep = 0;
|
|
||||||
}
|
|
||||||
if (PageStep > 8)
|
|
||||||
{
|
|
||||||
PageStep = 9;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
private void OnValidate()
|
||||||
/// Event fires when the user starts to change the page, either via swipe or button.
|
{
|
||||||
/// </summary>
|
var children = gameObject.GetComponent<ScrollRect>().content.childCount;
|
||||||
|
if (children != 0 || ChildObjects != null)
|
||||||
|
{
|
||||||
|
var childCount = ChildObjects == null || ChildObjects.Length == 0 ? children : ChildObjects.Length;
|
||||||
|
if (StartingScreen > childCount - 1)
|
||||||
|
{
|
||||||
|
StartingScreen = childCount - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (StartingScreen < 0)
|
||||||
|
{
|
||||||
|
StartingScreen = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (MaskBuffer <= 0)
|
||||||
|
{
|
||||||
|
MaskBuffer = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (PageStep < 0)
|
||||||
|
{
|
||||||
|
PageStep = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (PageStep > 8)
|
||||||
|
{
|
||||||
|
PageStep = 9;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Event fires when the user starts to change the page, either via swipe or button.
|
||||||
|
/// </summary>
|
||||||
internal void StartScreenChange()
|
internal void StartScreenChange()
|
||||||
{
|
{
|
||||||
OnSelectionChangeStartEvent.Invoke();
|
OnSelectionChangeStartEvent.Invoke();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Event fires when the currently viewed page changes, also updates while the scroll is moving
|
/// Event fires when the currently viewed page changes, also updates while the scroll is moving
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal void ScreenChange()
|
internal void ScreenChange()
|
||||||
{
|
{
|
||||||
OnSelectionPageChangedEvent.Invoke(_currentPage);
|
OnSelectionPageChangedEvent.Invoke(_currentPage);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Event fires when control settles on a page, outputs the new page number
|
/// Event fires when control settles on a page, outputs the new page number
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal void EndScreenChange()
|
internal void EndScreenChange()
|
||||||
{
|
{
|
||||||
OnSelectionChangeEndEvent.Invoke(_currentPage);
|
OnSelectionChangeEndEvent.Invoke(_currentPage);
|
||||||
_settled = true;
|
_settled = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
#region Interfaces
|
#region Interfaces
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Touch screen to start swiping
|
/// Touch screen to start swiping
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="eventData"></param>
|
/// <param name="eventData"></param>
|
||||||
public void OnBeginDrag(PointerEventData eventData)
|
public void OnBeginDrag(PointerEventData eventData)
|
||||||
{
|
{
|
||||||
_pointerDown = true;
|
_pointerDown = true;
|
||||||
_settled = false;
|
_settled = false;
|
||||||
StartScreenChange();
|
StartScreenChange();
|
||||||
_startPosition = _screensContainer.localPosition;
|
_startPosition = _screensContainer.localPosition;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// While dragging do
|
/// While dragging do
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="eventData"></param>
|
/// <param name="eventData"></param>
|
||||||
public void OnDrag(PointerEventData eventData)
|
public void OnDrag(PointerEventData eventData)
|
||||||
{
|
{
|
||||||
_lerp = false;
|
_lerp = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue