2015-10-02 02:29:58 +08:00
|
|
|
/// Credit BinaryX
|
|
|
|
/// Sourced from - http://forum.unity3d.com/threads/scripts-useful-4-6-scripts-collection.264161/page-2#post-1945602
|
2020-07-09 03:38:28 +08:00
|
|
|
/// Updated by simonDarksideJ - removed dependency on a custom ScrollRect script. Now implements drag interfaces and standard Scroll Rect.
|
2016-07-19 04:36:36 +08:00
|
|
|
/// Update by xesenix - rewrote almost the entire code
|
|
|
|
/// - configuration for direction move instead of 2 concurrent class (easier to change direction in editor)
|
|
|
|
/// - supports list layout with horizontal or vertical layout need to match direction with type of layout used
|
|
|
|
/// - dynamic checks if scrolled list size changes and recalculates anchor positions
|
2015-10-02 02:29:58 +08:00
|
|
|
/// and item size based on itemsVisibleAtOnce and size of root container
|
2016-07-19 04:36:36 +08:00
|
|
|
/// if you don't wish to use this auto resize turn of autoLayoutItems
|
|
|
|
/// - fixed current page made it independent from pivot
|
2015-10-02 02:29:58 +08:00
|
|
|
/// - replaced pagination with delegate function
|
|
|
|
using System;
|
|
|
|
using UnityEngine.EventSystems;
|
|
|
|
|
|
|
|
namespace UnityEngine.UI.Extensions
|
|
|
|
{
|
2015-12-03 00:58:06 +08:00
|
|
|
[ExecuteInEditMode]
|
|
|
|
[RequireComponent(typeof(ScrollRect))]
|
|
|
|
[AddComponentMenu("UI/Extensions/Scroll Snap")]
|
2017-07-02 17:09:52 +08:00
|
|
|
public class ScrollSnap : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler, IScrollSnap
|
2015-12-03 00:58:06 +08:00
|
|
|
{
|
2016-07-19 04:36:36 +08:00
|
|
|
// needed because of reversed behaviour of axis Y compared to X
|
2015-12-03 00:58:06 +08:00
|
|
|
// (positions of children lower in children list in horizontal directions grows when in vertical it gets smaller)
|
|
|
|
public enum ScrollDirection
|
|
|
|
{
|
|
|
|
Horizontal,
|
|
|
|
Vertical
|
|
|
|
}
|
2015-10-02 02:29:58 +08:00
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
private ScrollRect _scroll_rect;
|
2015-10-02 02:29:58 +08:00
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
private RectTransform _scrollRectTransform;
|
2015-10-02 02:29:58 +08:00
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
private Transform _listContainerTransform;
|
|
|
|
|
2017-08-11 20:17:10 +08:00
|
|
|
//private RectTransform _rectTransform;
|
2015-10-02 02:29:58 +08:00
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
private int _pages;
|
2015-10-18 01:15:35 +08:00
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
private int _startingPage = 0;
|
2015-10-18 01:15:35 +08:00
|
|
|
|
2020-07-09 03:38:28 +08:00
|
|
|
// anchor points to lerp to see child on certain indexes
|
2017-07-02 17:09:52 +08:00
|
|
|
private Vector3[] _pageAnchorPositions;
|
2015-10-02 02:29:58 +08:00
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
private Vector3 _lerpTarget;
|
2015-10-02 02:29:58 +08:00
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
private bool _lerp;
|
2015-10-02 02:29:58 +08:00
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
// item list related
|
|
|
|
private float _listContainerMinPosition;
|
2015-10-02 02:29:58 +08:00
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
private float _listContainerMaxPosition;
|
2015-10-18 01:15:35 +08:00
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
private float _listContainerSize;
|
2015-10-18 01:15:35 +08:00
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
private RectTransform _listContainerRectTransform;
|
2015-10-02 02:29:58 +08:00
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
private Vector2 _listContainerCachedSize;
|
2015-10-18 01:15:35 +08:00
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
private float _itemSize;
|
2015-10-18 01:15:35 +08:00
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
private int _itemsCount = 0;
|
2015-10-18 01:15:35 +08:00
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
// drag related
|
|
|
|
private bool _startDrag = true;
|
|
|
|
|
|
|
|
private Vector3 _positionOnDragStart = new Vector3();
|
2015-10-18 01:15:35 +08:00
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
private int _pageOnDragStart;
|
2015-10-02 02:29:58 +08:00
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
private bool _fastSwipeTimer = false;
|
2015-10-18 01:15:35 +08:00
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
private int _fastSwipeCounter = 0;
|
|
|
|
|
|
|
|
private int _fastSwipeTarget = 10;
|
2015-12-03 00:58:06 +08:00
|
|
|
|
|
|
|
[Tooltip("Button to go to the next page. (optional)")]
|
2017-07-02 17:09:52 +08:00
|
|
|
public Button NextButton;
|
2015-12-03 00:58:06 +08:00
|
|
|
|
|
|
|
[Tooltip("Button to go to the previous page. (optional)")]
|
2017-07-02 17:09:52 +08:00
|
|
|
public Button PrevButton;
|
2015-12-03 00:58:06 +08:00
|
|
|
|
|
|
|
[Tooltip("Number of items visible in one page of scroll frame.")]
|
|
|
|
[RangeAttribute(1, 100)]
|
2017-07-02 17:09:52 +08:00
|
|
|
public int ItemsVisibleAtOnce = 1;
|
2015-12-03 00:58:06 +08:00
|
|
|
|
|
|
|
[Tooltip("Sets minimum width of list items to 1/itemsVisibleAtOnce.")]
|
2017-07-02 17:09:52 +08:00
|
|
|
public bool AutoLayoutItems = true;
|
2015-12-03 00:58:06 +08:00
|
|
|
|
|
|
|
[Tooltip("If you wish to update scrollbar numberOfSteps to number of active children on list.")]
|
2017-07-02 17:09:52 +08:00
|
|
|
public bool LinkScrolbarSteps = false;
|
2015-12-03 00:58:06 +08:00
|
|
|
|
|
|
|
[Tooltip("If you wish to update scrollrect sensitivity to size of list element.")]
|
2017-07-02 17:09:52 +08:00
|
|
|
public bool LinkScrolrectScrollSensitivity = false;
|
2015-12-03 00:58:06 +08:00
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
public Boolean UseFastSwipe = true;
|
2015-12-03 00:58:06 +08:00
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
public int FastSwipeThreshold = 100;
|
2015-12-03 00:58:06 +08:00
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
public delegate void PageSnapChange(int page);
|
2015-12-03 00:58:06 +08:00
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
public event PageSnapChange onPageChange;
|
2015-12-03 00:58:06 +08:00
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
public ScrollDirection direction = ScrollDirection.Horizontal;
|
2015-12-03 00:58:06 +08:00
|
|
|
|
|
|
|
// Use this for initialization
|
2017-07-02 17:09:52 +08:00
|
|
|
void Start()
|
2015-12-03 00:58:06 +08:00
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
_lerp = false;
|
2015-12-03 00:58:06 +08:00
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
_scroll_rect = gameObject.GetComponent<ScrollRect>();
|
|
|
|
_scrollRectTransform = gameObject.GetComponent<RectTransform>();
|
|
|
|
_listContainerTransform = _scroll_rect.content;
|
|
|
|
_listContainerRectTransform = _listContainerTransform.GetComponent<RectTransform>();
|
2015-12-03 00:58:06 +08:00
|
|
|
|
2017-08-11 20:17:10 +08:00
|
|
|
//_rectTransform = _listContainerTransform.gameObject.GetComponent<RectTransform>();
|
2015-12-03 00:58:06 +08:00
|
|
|
UpdateListItemsSize();
|
|
|
|
UpdateListItemPositions();
|
|
|
|
|
|
|
|
PageChanged(CurrentPage());
|
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
if (NextButton)
|
2015-12-03 00:58:06 +08:00
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
NextButton.GetComponent<Button>().onClick.AddListener(() =>
|
2015-12-03 00:58:06 +08:00
|
|
|
{
|
|
|
|
NextScreen();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
if (PrevButton)
|
2015-12-03 00:58:06 +08:00
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
PrevButton.GetComponent<Button>().onClick.AddListener(() =>
|
2015-12-03 00:58:06 +08:00
|
|
|
{
|
|
|
|
PreviousScreen();
|
|
|
|
});
|
|
|
|
}
|
2017-07-02 17:09:52 +08:00
|
|
|
if (_scroll_rect.horizontalScrollbar != null && _scroll_rect.horizontal)
|
|
|
|
{
|
2018-10-04 05:19:56 +08:00
|
|
|
|
|
|
|
var hscroll = _scroll_rect.horizontalScrollbar.gameObject.GetOrAddComponent<ScrollSnapScrollbarHelper>();
|
2017-07-02 17:09:52 +08:00
|
|
|
hscroll.ss = this;
|
|
|
|
}
|
|
|
|
if (_scroll_rect.verticalScrollbar != null && _scroll_rect.vertical)
|
|
|
|
{
|
2018-10-04 05:19:56 +08:00
|
|
|
var vscroll = _scroll_rect.verticalScrollbar.gameObject.GetOrAddComponent<ScrollSnapScrollbarHelper>();
|
2017-07-02 17:09:52 +08:00
|
|
|
vscroll.ss = this;
|
|
|
|
}
|
2015-12-03 00:58:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void UpdateListItemsSize()
|
|
|
|
{
|
|
|
|
float size = 0;
|
|
|
|
float currentSize = 0;
|
|
|
|
if (direction == ScrollSnap.ScrollDirection.Horizontal)
|
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
size = _scrollRectTransform.rect.width / ItemsVisibleAtOnce;
|
|
|
|
currentSize = _listContainerRectTransform.rect.width / _itemsCount;
|
2015-12-03 00:58:06 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
size = _scrollRectTransform.rect.height / ItemsVisibleAtOnce;
|
|
|
|
currentSize = _listContainerRectTransform.rect.height / _itemsCount;
|
2015-12-03 00:58:06 +08:00
|
|
|
}
|
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
_itemSize = size;
|
2015-12-03 00:58:06 +08:00
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
if (LinkScrolrectScrollSensitivity)
|
2015-12-03 00:58:06 +08:00
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
_scroll_rect.scrollSensitivity = _itemSize;
|
2015-12-03 00:58:06 +08:00
|
|
|
}
|
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
if (AutoLayoutItems && currentSize != size && _itemsCount > 0)
|
2015-12-03 00:58:06 +08:00
|
|
|
{
|
|
|
|
if (direction == ScrollSnap.ScrollDirection.Horizontal)
|
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
foreach (var tr in _listContainerTransform)
|
2015-12-03 00:58:06 +08:00
|
|
|
{
|
|
|
|
GameObject child = ((Transform)tr).gameObject;
|
|
|
|
if (child.activeInHierarchy)
|
|
|
|
{
|
|
|
|
var childLayout = child.GetComponent<LayoutElement>();
|
|
|
|
|
|
|
|
if (childLayout == null)
|
|
|
|
{
|
|
|
|
childLayout = child.AddComponent<LayoutElement>();
|
|
|
|
}
|
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
childLayout.minWidth = _itemSize;
|
2015-12-03 00:58:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
foreach (var tr in _listContainerTransform)
|
2015-12-03 00:58:06 +08:00
|
|
|
{
|
|
|
|
GameObject child = ((Transform)tr).gameObject;
|
|
|
|
if (child.activeInHierarchy)
|
|
|
|
{
|
|
|
|
var childLayout = child.GetComponent<LayoutElement>();
|
|
|
|
|
|
|
|
if (childLayout == null)
|
|
|
|
{
|
|
|
|
childLayout = child.AddComponent<LayoutElement>();
|
|
|
|
}
|
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
childLayout.minHeight = _itemSize;
|
2015-12-03 00:58:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void UpdateListItemPositions()
|
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
if (!_listContainerRectTransform.rect.size.Equals(_listContainerCachedSize))
|
2015-12-03 00:58:06 +08:00
|
|
|
{
|
|
|
|
// checking how many children of list are active
|
|
|
|
int activeCount = 0;
|
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
foreach (var tr in _listContainerTransform)
|
2015-12-03 00:58:06 +08:00
|
|
|
{
|
|
|
|
if (((Transform)tr).gameObject.activeInHierarchy)
|
|
|
|
{
|
|
|
|
activeCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if anything changed since last check reinitialize anchors list
|
2017-07-02 17:09:52 +08:00
|
|
|
_itemsCount = 0;
|
|
|
|
Array.Resize(ref _pageAnchorPositions, activeCount);
|
2015-12-03 00:58:06 +08:00
|
|
|
|
|
|
|
if (activeCount > 0)
|
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
_pages = Mathf.Max(activeCount - ItemsVisibleAtOnce + 1, 1);
|
2015-12-03 00:58:06 +08:00
|
|
|
|
|
|
|
if (direction == ScrollDirection.Horizontal)
|
|
|
|
{
|
|
|
|
// looking for list spanning range min/max
|
2017-07-02 17:09:52 +08:00
|
|
|
_scroll_rect.horizontalNormalizedPosition = 0;
|
|
|
|
_listContainerMaxPosition = _listContainerTransform.localPosition.x;
|
|
|
|
_scroll_rect.horizontalNormalizedPosition = 1;
|
|
|
|
_listContainerMinPosition = _listContainerTransform.localPosition.x;
|
2015-12-03 00:58:06 +08:00
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
_listContainerSize = _listContainerMaxPosition - _listContainerMinPosition;
|
2015-12-03 00:58:06 +08:00
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
for (var i = 0; i < _pages; i++)
|
2015-12-03 00:58:06 +08:00
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
_pageAnchorPositions[i] = new Vector3(
|
|
|
|
_listContainerMaxPosition - _itemSize * i,
|
|
|
|
_listContainerTransform.localPosition.y,
|
|
|
|
_listContainerTransform.localPosition.z
|
2015-12-03 00:58:06 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//Debug.Log ("-------------looking for list spanning range----------------");
|
|
|
|
// looking for list spanning range
|
2017-07-02 17:09:52 +08:00
|
|
|
_scroll_rect.verticalNormalizedPosition = 1;
|
|
|
|
_listContainerMinPosition = _listContainerTransform.localPosition.y;
|
|
|
|
_scroll_rect.verticalNormalizedPosition = 0;
|
|
|
|
_listContainerMaxPosition = _listContainerTransform.localPosition.y;
|
2015-12-03 00:58:06 +08:00
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
_listContainerSize = _listContainerMaxPosition - _listContainerMinPosition;
|
2015-12-03 00:58:06 +08:00
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
for (var i = 0; i < _pages; i++)
|
2015-12-03 00:58:06 +08:00
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
_pageAnchorPositions[i] = new Vector3(
|
|
|
|
_listContainerTransform.localPosition.x,
|
|
|
|
_listContainerMinPosition + _itemSize * i,
|
|
|
|
_listContainerTransform.localPosition.z
|
2015-12-03 00:58:06 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
UpdateScrollbar(LinkScrolbarSteps);
|
|
|
|
_startingPage = Mathf.Min(_startingPage, _pages);
|
2015-12-03 00:58:06 +08:00
|
|
|
ResetPage();
|
|
|
|
}
|
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
if (_itemsCount != activeCount)
|
2015-12-03 00:58:06 +08:00
|
|
|
{
|
|
|
|
PageChanged(CurrentPage());
|
|
|
|
}
|
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
_itemsCount = activeCount;
|
|
|
|
_listContainerCachedSize.Set(_listContainerRectTransform.rect.size.x, _listContainerRectTransform.rect.size.y);
|
2015-12-03 00:58:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void ResetPage()
|
|
|
|
{
|
|
|
|
if (direction == ScrollDirection.Horizontal)
|
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
_scroll_rect.horizontalNormalizedPosition = _pages > 1 ? (float)_startingPage / (float)(_pages - 1) : 0;
|
2015-12-03 00:58:06 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
_scroll_rect.verticalNormalizedPosition = _pages > 1 ? (float)(_pages - _startingPage - 1) / (float)(_pages - 1) : 0;
|
2015-12-03 00:58:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
private void UpdateScrollbar(bool linkSteps)
|
2015-12-03 00:58:06 +08:00
|
|
|
{
|
|
|
|
if (linkSteps)
|
|
|
|
{
|
|
|
|
if (direction == ScrollDirection.Horizontal)
|
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
if (_scroll_rect.horizontalScrollbar != null)
|
2015-12-03 00:58:06 +08:00
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
_scroll_rect.horizontalScrollbar.numberOfSteps = _pages;
|
2015-12-03 00:58:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
if (_scroll_rect.verticalScrollbar != null)
|
2015-12-03 00:58:06 +08:00
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
_scroll_rect.verticalScrollbar.numberOfSteps = _pages;
|
2015-12-03 00:58:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (direction == ScrollDirection.Horizontal)
|
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
if (_scroll_rect.horizontalScrollbar != null)
|
2015-12-03 00:58:06 +08:00
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
_scroll_rect.horizontalScrollbar.numberOfSteps = 0;
|
2015-12-03 00:58:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
if (_scroll_rect.verticalScrollbar != null)
|
2015-12-03 00:58:06 +08:00
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
_scroll_rect.verticalScrollbar.numberOfSteps = 0;
|
2015-12-03 00:58:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LateUpdate()
|
|
|
|
{
|
|
|
|
UpdateListItemsSize();
|
|
|
|
UpdateListItemPositions();
|
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
if (_lerp)
|
2015-12-03 00:58:06 +08:00
|
|
|
{
|
|
|
|
UpdateScrollbar(false);
|
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
_listContainerTransform.localPosition = Vector3.Lerp(_listContainerTransform.localPosition, _lerpTarget, 7.5f * Time.deltaTime);
|
2015-12-03 00:58:06 +08:00
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
if (Vector3.Distance(_listContainerTransform.localPosition, _lerpTarget) < 0.001f)
|
2015-12-03 00:58:06 +08:00
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
_listContainerTransform.localPosition = _lerpTarget;
|
|
|
|
_lerp = false;
|
2015-12-03 00:58:06 +08:00
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
UpdateScrollbar(LinkScrolbarSteps);
|
2015-12-03 00:58:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//change the info bullets at the bottom of the screen. Just for visual effect
|
2017-07-02 17:09:52 +08:00
|
|
|
if (Vector3.Distance(_listContainerTransform.localPosition, _lerpTarget) < 10f)
|
2015-12-03 00:58:06 +08:00
|
|
|
{
|
|
|
|
PageChanged(CurrentPage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
if (_fastSwipeTimer)
|
2015-12-03 00:58:06 +08:00
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
_fastSwipeCounter++;
|
2015-12-03 00:58:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool fastSwipe = false; //to determine if a fast swipe was performed
|
|
|
|
|
|
|
|
|
|
|
|
//Function for switching screens with buttons
|
|
|
|
public void NextScreen()
|
|
|
|
{
|
|
|
|
UpdateListItemPositions();
|
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
if (CurrentPage() < _pages - 1)
|
2015-12-03 00:58:06 +08:00
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
_lerp = true;
|
|
|
|
_lerpTarget = _pageAnchorPositions[CurrentPage() + 1];
|
2015-12-03 00:58:06 +08:00
|
|
|
|
|
|
|
PageChanged(CurrentPage() + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Function for switching screens with buttons
|
|
|
|
public void PreviousScreen()
|
|
|
|
{
|
|
|
|
UpdateListItemPositions();
|
|
|
|
|
|
|
|
if (CurrentPage() > 0)
|
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
_lerp = true;
|
|
|
|
_lerpTarget = _pageAnchorPositions[CurrentPage() - 1];
|
2015-12-03 00:58:06 +08:00
|
|
|
|
|
|
|
PageChanged(CurrentPage() - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Because the CurrentScreen function is not so reliable, these are the functions used for swipes
|
|
|
|
private void NextScreenCommand()
|
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
if (_pageOnDragStart < _pages - 1)
|
2015-12-03 00:58:06 +08:00
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
int targetPage = Mathf.Min(_pages - 1, _pageOnDragStart + ItemsVisibleAtOnce);
|
|
|
|
_lerp = true;
|
2015-12-03 00:58:06 +08:00
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
_lerpTarget = _pageAnchorPositions[targetPage];
|
2015-12-03 00:58:06 +08:00
|
|
|
|
|
|
|
PageChanged(targetPage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Because the CurrentScreen function is not so reliable, these are the functions used for swipes
|
|
|
|
private void PrevScreenCommand()
|
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
if (_pageOnDragStart > 0)
|
2015-12-03 00:58:06 +08:00
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
int targetPage = Mathf.Max(0, _pageOnDragStart - ItemsVisibleAtOnce);
|
|
|
|
_lerp = true;
|
2015-12-03 00:58:06 +08:00
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
_lerpTarget = _pageAnchorPositions[targetPage];
|
2015-12-03 00:58:06 +08:00
|
|
|
|
|
|
|
PageChanged(targetPage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//returns the current screen that the is seeing
|
|
|
|
public int CurrentPage()
|
|
|
|
{
|
|
|
|
float pos;
|
|
|
|
|
|
|
|
if (direction == ScrollDirection.Horizontal)
|
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
pos = _listContainerMaxPosition - _listContainerTransform.localPosition.x;
|
|
|
|
pos = Mathf.Clamp(pos, 0, _listContainerSize);
|
2015-12-03 00:58:06 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
pos = _listContainerTransform.localPosition.y - _listContainerMinPosition;
|
|
|
|
pos = Mathf.Clamp(pos, 0, _listContainerSize);
|
2015-12-03 00:58:06 +08:00
|
|
|
}
|
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
float page = pos / _itemSize;
|
2015-12-03 00:58:06 +08:00
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
return Mathf.Clamp(Mathf.RoundToInt(page), 0, _pages);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Added to provide a uniform interface for the ScrollBarHelper
|
|
|
|
/// </summary>
|
|
|
|
public void SetLerp(bool value)
|
|
|
|
{
|
|
|
|
_lerp = value;
|
2015-12-03 00:58:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void ChangePage(int page)
|
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
if (0 <= page && page < _pages)
|
2015-12-03 00:58:06 +08:00
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
_lerp = true;
|
2015-12-03 00:58:06 +08:00
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
_lerpTarget = _pageAnchorPositions[page];
|
2015-12-03 00:58:06 +08:00
|
|
|
|
|
|
|
PageChanged(page);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//changes the bullets on the bottom of the page - pagination
|
|
|
|
private void PageChanged(int currentPage)
|
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
_startingPage = currentPage;
|
2015-12-03 00:58:06 +08:00
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
if (NextButton)
|
2015-12-03 00:58:06 +08:00
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
NextButton.interactable = currentPage < _pages - 1;
|
2015-12-03 00:58:06 +08:00
|
|
|
}
|
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
if (PrevButton)
|
2015-12-03 00:58:06 +08:00
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
PrevButton.interactable = currentPage > 0;
|
2015-12-03 00:58:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (onPageChange != null)
|
|
|
|
{
|
|
|
|
onPageChange(currentPage);
|
|
|
|
}
|
|
|
|
}
|
2015-10-02 02:29:58 +08:00
|
|
|
|
|
|
|
#region Interfaces
|
2015-12-03 00:58:06 +08:00
|
|
|
public void OnBeginDrag(PointerEventData eventData)
|
|
|
|
{
|
|
|
|
UpdateScrollbar(false);
|
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
_fastSwipeCounter = 0;
|
|
|
|
_fastSwipeTimer = true;
|
2015-12-03 00:58:06 +08:00
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
_positionOnDragStart = eventData.position;
|
|
|
|
_pageOnDragStart = CurrentPage();
|
2015-12-03 00:58:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void OnEndDrag(PointerEventData eventData)
|
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
_startDrag = true;
|
2015-12-03 00:58:06 +08:00
|
|
|
float change = 0;
|
|
|
|
|
|
|
|
if (direction == ScrollDirection.Horizontal)
|
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
change = _positionOnDragStart.x - eventData.position.x;
|
2015-12-03 00:58:06 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
change = -_positionOnDragStart.y + eventData.position.y;
|
2015-12-03 00:58:06 +08:00
|
|
|
}
|
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
if (UseFastSwipe)
|
2015-12-03 00:58:06 +08:00
|
|
|
{
|
|
|
|
fastSwipe = false;
|
2017-07-02 17:09:52 +08:00
|
|
|
_fastSwipeTimer = false;
|
2015-12-03 00:58:06 +08:00
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
if (_fastSwipeCounter <= _fastSwipeTarget)
|
2015-12-03 00:58:06 +08:00
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
if (Math.Abs(change) > FastSwipeThreshold)
|
2015-12-03 00:58:06 +08:00
|
|
|
{
|
|
|
|
fastSwipe = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fastSwipe)
|
|
|
|
{
|
|
|
|
if (change > 0)
|
|
|
|
{
|
|
|
|
NextScreenCommand();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PrevScreenCommand();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
_lerp = true;
|
|
|
|
_lerpTarget = _pageAnchorPositions[CurrentPage()];
|
2015-12-03 00:58:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
_lerp = true;
|
|
|
|
_lerpTarget = _pageAnchorPositions[CurrentPage()];
|
2015-12-03 00:58:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnDrag(PointerEventData eventData)
|
|
|
|
{
|
2017-07-02 17:09:52 +08:00
|
|
|
_lerp = false;
|
2015-12-03 00:58:06 +08:00
|
|
|
|
2017-07-02 17:09:52 +08:00
|
|
|
if (_startDrag)
|
2015-12-03 00:58:06 +08:00
|
|
|
{
|
|
|
|
OnBeginDrag(eventData);
|
2017-07-02 17:09:52 +08:00
|
|
|
_startDrag = false;
|
2015-12-03 00:58:06 +08:00
|
|
|
}
|
|
|
|
}
|
2017-07-02 17:09:52 +08:00
|
|
|
|
|
|
|
public void StartScreenChange() { }
|
2015-10-02 02:29:58 +08:00
|
|
|
#endregion
|
2015-12-03 00:58:06 +08:00
|
|
|
}
|
2015-10-02 02:29:58 +08:00
|
|
|
}
|