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
|
|
|
|
/// Updated by ddreaper - removed dependency on a custom ScrollRect script. Now implements drag interfaces and standard Scroll Rect.
|
|
|
|
/// Update by xesenix - rewrited almost entire code
|
|
|
|
/// - configuration for direction move instead of 2 concurrent class (easiear to change direction in editor)
|
|
|
|
/// - supports list layouted with horizontal or vertical layout need to match direction with type of layout used
|
|
|
|
/// - dynamicly checks if scrolled list size changes and recalculates anchor positions
|
|
|
|
/// and item size based on itemsVisibleAtOnce and size of root container
|
|
|
|
/// if you dont wish to use this auto resize turn of autoLayoutItems
|
|
|
|
/// - fixed current page made it independant from pivot
|
|
|
|
/// - replaced pagination with delegate function
|
|
|
|
using System;
|
|
|
|
using UnityEngine.EventSystems;
|
|
|
|
|
|
|
|
namespace UnityEngine.UI.Extensions
|
|
|
|
{
|
|
|
|
[ExecuteInEditMode]
|
2015-10-17 23:16:23 +08:00
|
|
|
[RequireComponent(typeof(ScrollRect))]
|
|
|
|
[AddComponentMenu("UI/Extensions/Scroll Snap")]
|
|
|
|
public class ScrollSnap : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler
|
2015-10-02 02:29:58 +08:00
|
|
|
{
|
|
|
|
// needed becouse of reversed behavior of axis Y compared to X
|
|
|
|
// (positions of children lower in children list in horizontal directions grows when in vertical it gets smaller)
|
2015-10-17 23:16:23 +08:00
|
|
|
public enum ScrollDirection
|
|
|
|
{
|
2015-10-02 02:29:58 +08:00
|
|
|
Horizontal,
|
|
|
|
Vertical
|
2015-10-17 23:16:23 +08:00
|
|
|
}
|
2015-10-02 02:29:58 +08:00
|
|
|
|
|
|
|
public delegate void PageSnapChange(int page);
|
|
|
|
|
|
|
|
public event PageSnapChange onPageChange;
|
|
|
|
|
|
|
|
public ScrollDirection direction = ScrollDirection.Horizontal;
|
|
|
|
|
|
|
|
protected ScrollRect scrollRect;
|
2015-10-17 23:16:23 +08:00
|
|
|
|
2015-10-02 02:29:58 +08:00
|
|
|
protected RectTransform scrollRectTransform;
|
2015-10-17 23:16:23 +08:00
|
|
|
|
2015-10-02 02:29:58 +08:00
|
|
|
protected Transform listContainerTransform;
|
|
|
|
|
2015-10-17 23:16:23 +08:00
|
|
|
protected RectTransform rectTransform;
|
2015-10-02 02:29:58 +08:00
|
|
|
|
|
|
|
int pages;
|
|
|
|
|
|
|
|
protected int startingPage = 0;
|
|
|
|
|
|
|
|
// anchor points to lerp to to see child on certain indexes
|
|
|
|
protected Vector3[] pageAnchorPositions;
|
2015-10-17 23:16:23 +08:00
|
|
|
|
2015-10-02 02:29:58 +08:00
|
|
|
protected Vector3 lerpTarget;
|
2015-10-17 23:16:23 +08:00
|
|
|
|
2015-10-02 02:29:58 +08:00
|
|
|
protected bool lerp;
|
|
|
|
|
|
|
|
// item list related
|
|
|
|
protected float listContainerMinPosition;
|
2015-10-17 23:16:23 +08:00
|
|
|
|
2015-10-02 02:29:58 +08:00
|
|
|
protected float listContainerMaxPosition;
|
2015-10-17 23:16:23 +08:00
|
|
|
|
2015-10-02 02:29:58 +08:00
|
|
|
protected float listContainerSize;
|
2015-10-17 23:16:23 +08:00
|
|
|
|
2015-10-02 02:29:58 +08:00
|
|
|
protected RectTransform listContainerRectTransform;
|
2015-10-17 23:16:23 +08:00
|
|
|
|
2015-10-02 02:29:58 +08:00
|
|
|
protected Vector2 listContainerCachedSize;
|
|
|
|
|
|
|
|
protected float itemSize;
|
2015-10-17 23:16:23 +08:00
|
|
|
|
|
|
|
protected int itemsCount = 0;
|
|
|
|
|
|
|
|
[Tooltip("Button to go to the next page. (optional)")]
|
|
|
|
public Button nextButton;
|
2015-10-02 02:29:58 +08:00
|
|
|
|
2015-10-17 23:16:23 +08:00
|
|
|
[Tooltip("Button to go to the previous page. (optional)")]
|
|
|
|
public Button prevButton;
|
2015-10-02 02:29:58 +08:00
|
|
|
|
|
|
|
[Tooltip("Number of items visible in one page of scroll frame.")]
|
|
|
|
[RangeAttribute(1,100)]
|
|
|
|
public int itemsVisibleAtOnce = 1;
|
|
|
|
|
|
|
|
[Tooltip("Sets minimum width of list items to 1/itemsVisibleAtOnce.")]
|
|
|
|
public bool autoLayoutItems = true;
|
|
|
|
|
|
|
|
[Tooltip("If you wish to update scrollbar numberOfSteps to number of active children on list.")]
|
|
|
|
public bool linkScrolbarSteps = false;
|
2015-10-17 23:16:23 +08:00
|
|
|
|
|
|
|
[Tooltip("If you wish to update scrollrect sensitivity to size of list element.")]
|
|
|
|
public bool linkScrolrectScrollSensitivity = false;
|
|
|
|
|
|
|
|
public Boolean useFastSwipe = true;
|
2015-10-02 02:29:58 +08:00
|
|
|
|
2015-10-17 23:16:23 +08:00
|
|
|
public int fastSwipeThreshold = 100;
|
2015-10-02 02:29:58 +08:00
|
|
|
|
|
|
|
// drag related
|
|
|
|
protected bool startDrag = true;
|
2015-10-17 23:16:23 +08:00
|
|
|
|
2015-10-02 02:29:58 +08:00
|
|
|
protected Vector3 positionOnDragStart = new Vector3();
|
2015-10-17 23:16:23 +08:00
|
|
|
|
2015-10-02 02:29:58 +08:00
|
|
|
protected int pageOnDragStart;
|
|
|
|
|
|
|
|
protected bool fastSwipeTimer = false;
|
2015-10-17 23:16:23 +08:00
|
|
|
|
2015-10-02 02:29:58 +08:00
|
|
|
protected int fastSwipeCounter = 0;
|
2015-10-17 23:16:23 +08:00
|
|
|
|
2015-10-02 02:29:58 +08:00
|
|
|
protected int fastSwipeTarget = 10;
|
|
|
|
|
2015-10-17 23:16:23 +08:00
|
|
|
// Use this for initialization
|
|
|
|
void Awake()
|
2015-10-02 02:29:58 +08:00
|
|
|
{
|
|
|
|
lerp = false;
|
|
|
|
|
2015-10-17 23:16:23 +08:00
|
|
|
scrollRect = gameObject.GetComponent<ScrollRect>();
|
|
|
|
scrollRectTransform = gameObject.GetComponent<RectTransform>();
|
2015-10-02 02:29:58 +08:00
|
|
|
listContainerTransform = scrollRect.content;
|
2015-10-17 23:16:23 +08:00
|
|
|
listContainerRectTransform = listContainerTransform.GetComponent<RectTransform>();
|
2015-10-02 02:29:58 +08:00
|
|
|
|
2015-10-17 23:16:23 +08:00
|
|
|
rectTransform = listContainerTransform.gameObject.GetComponent<RectTransform>();
|
2015-10-02 02:29:58 +08:00
|
|
|
UpdateListItemsSize();
|
|
|
|
UpdateListItemPositions();
|
|
|
|
|
2015-10-17 23:16:23 +08:00
|
|
|
ChangePage(CurrentPage());
|
2015-10-02 02:29:58 +08:00
|
|
|
|
|
|
|
if (nextButton)
|
|
|
|
{
|
2015-10-17 23:16:23 +08:00
|
|
|
nextButton.GetComponent<Button>().onClick.AddListener(() => {
|
|
|
|
NextScreen(); });
|
2015-10-02 02:29:58 +08:00
|
|
|
}
|
|
|
|
|
2015-10-17 23:16:23 +08:00
|
|
|
if (prevButton)
|
2015-10-02 02:29:58 +08:00
|
|
|
{
|
2015-10-17 23:16:23 +08:00
|
|
|
prevButton.GetComponent<Button>().onClick.AddListener(() => {
|
|
|
|
PreviousScreen(); });
|
2015-10-02 02:29:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-17 23:16:23 +08:00
|
|
|
void Start()
|
|
|
|
{
|
|
|
|
Awake();
|
|
|
|
}
|
|
|
|
|
2015-10-02 02:29:58 +08:00
|
|
|
public void UpdateListItemsSize()
|
|
|
|
{
|
|
|
|
float size = 0;
|
2015-10-17 23:16:23 +08:00
|
|
|
float currentSize = 0;
|
2015-10-02 02:29:58 +08:00
|
|
|
if (direction == ScrollSnap.ScrollDirection.Horizontal)
|
|
|
|
{
|
|
|
|
size = scrollRectTransform.rect.width / itemsVisibleAtOnce;
|
2015-10-17 23:16:23 +08:00
|
|
|
currentSize = listContainerRectTransform.rect.width / itemsCount;
|
2015-10-02 02:29:58 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size = scrollRectTransform.rect.height / itemsVisibleAtOnce;
|
2015-10-17 23:16:23 +08:00
|
|
|
currentSize = listContainerRectTransform.rect.height / itemsCount;
|
2015-10-02 02:29:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
itemSize = size;
|
|
|
|
|
2015-10-17 23:16:23 +08:00
|
|
|
if (linkScrolrectScrollSensitivity)
|
|
|
|
{
|
|
|
|
scrollRect.scrollSensitivity = itemSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (autoLayoutItems && currentSize != size && itemsCount > 0)
|
2015-10-02 02:29:58 +08:00
|
|
|
{
|
|
|
|
if (direction == ScrollSnap.ScrollDirection.Horizontal)
|
|
|
|
{
|
|
|
|
foreach (var tr in listContainerTransform)
|
|
|
|
{
|
2015-10-17 23:16:23 +08:00
|
|
|
GameObject child = ((Transform) tr).gameObject;
|
2015-10-02 02:29:58 +08:00
|
|
|
if (child.activeInHierarchy)
|
|
|
|
{
|
2015-10-17 23:16:23 +08:00
|
|
|
var childLayout = child.GetComponent<LayoutElement>();
|
2015-10-02 02:29:58 +08:00
|
|
|
|
|
|
|
if (childLayout == null)
|
|
|
|
{
|
2015-10-17 23:16:23 +08:00
|
|
|
childLayout = child.AddComponent<LayoutElement>();
|
2015-10-02 02:29:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
childLayout.minWidth = itemSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
foreach (var tr in listContainerTransform)
|
|
|
|
{
|
2015-10-17 23:16:23 +08:00
|
|
|
GameObject child = ((Transform) tr).gameObject;
|
2015-10-02 02:29:58 +08:00
|
|
|
if (child.activeInHierarchy)
|
|
|
|
{
|
2015-10-17 23:16:23 +08:00
|
|
|
var childLayout = child.GetComponent<LayoutElement>();
|
2015-10-02 02:29:58 +08:00
|
|
|
|
|
|
|
if (childLayout == null)
|
|
|
|
{
|
2015-10-17 23:16:23 +08:00
|
|
|
childLayout = child.AddComponent<LayoutElement>();
|
2015-10-02 02:29:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
childLayout.minHeight = itemSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void UpdateListItemPositions()
|
|
|
|
{
|
|
|
|
if (!listContainerRectTransform.rect.size.Equals(listContainerCachedSize))
|
|
|
|
{
|
|
|
|
// checking how many children of list are active
|
|
|
|
int activeCount = 0;
|
|
|
|
|
2015-10-17 23:16:23 +08:00
|
|
|
foreach (var tr in listContainerTransform)
|
|
|
|
{
|
|
|
|
if (((Transform) tr).gameObject.activeInHierarchy)
|
|
|
|
{
|
2015-10-02 02:29:58 +08:00
|
|
|
activeCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if anything changed since last check reinitialize anchors list
|
2015-10-17 23:16:23 +08:00
|
|
|
itemsCount = 0;
|
2015-10-02 02:29:58 +08:00
|
|
|
Array.Resize(ref pageAnchorPositions, activeCount);
|
2015-10-17 23:16:23 +08:00
|
|
|
|
2015-10-02 02:29:58 +08:00
|
|
|
if (activeCount > 0)
|
|
|
|
{
|
2015-10-17 23:16:23 +08:00
|
|
|
pages = Mathf.Max(activeCount - itemsVisibleAtOnce + 1, 1);
|
2015-10-02 02:29:58 +08:00
|
|
|
|
|
|
|
if (direction == ScrollDirection.Horizontal)
|
|
|
|
{
|
2015-10-17 23:16:23 +08:00
|
|
|
//Debug.Log ("-------------looking for list spanning range----------------");
|
2015-10-02 02:29:58 +08:00
|
|
|
// looking for list spanning range min/max
|
|
|
|
scrollRect.horizontalNormalizedPosition = 0;
|
|
|
|
listContainerMaxPosition = listContainerTransform.localPosition.x;
|
|
|
|
scrollRect.horizontalNormalizedPosition = 1;
|
|
|
|
listContainerMinPosition = listContainerTransform.localPosition.x;
|
|
|
|
|
|
|
|
listContainerSize = listContainerMaxPosition - listContainerMinPosition;
|
2015-10-17 23:16:23 +08:00
|
|
|
//itemSize = pages > 1 ? listContainerSize / (float)(pages - 1) : listContainerSize / activeCount;
|
|
|
|
//itemSize *= (pages + itemsVisibleAtOnce) / (pages + 1);
|
2015-10-02 02:29:58 +08:00
|
|
|
|
|
|
|
for (var i = 0; i < pages; i ++)
|
|
|
|
{
|
|
|
|
pageAnchorPositions[i] = new Vector3(
|
|
|
|
listContainerMaxPosition - itemSize * i,
|
|
|
|
listContainerTransform.localPosition.y,
|
|
|
|
listContainerTransform.localPosition.z
|
|
|
|
);
|
|
|
|
}
|
2015-10-17 23:16:23 +08:00
|
|
|
|
|
|
|
//Debug.Log (String.Format("min:{0:0.0}, max:{1:0.0}, pages: {2:0}, width: {3:0}", listContainerMinPosition, listContainerMaxPosition, pages, listContainerSize/pages));
|
2015-10-02 02:29:58 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-10-17 23:16:23 +08:00
|
|
|
//Debug.Log ("-------------looking for list spanning range----------------");
|
2015-10-02 02:29:58 +08:00
|
|
|
// looking for list spanning range
|
|
|
|
scrollRect.verticalNormalizedPosition = 1;
|
|
|
|
listContainerMinPosition = listContainerTransform.localPosition.y;
|
|
|
|
scrollRect.verticalNormalizedPosition = 0;
|
|
|
|
listContainerMaxPosition = listContainerTransform.localPosition.y;
|
|
|
|
|
|
|
|
listContainerSize = listContainerMaxPosition - listContainerMinPosition;
|
2015-10-17 23:16:23 +08:00
|
|
|
//itemSize = pages > 1 ? listContainerSize / (float)(pages - 1) : listContainerSize / activeCount;
|
|
|
|
//itemSize *= (pages + itemsVisibleAtOnce) / (pages + 1);
|
2015-10-02 02:29:58 +08:00
|
|
|
|
|
|
|
for (var i = 0; i < pages; i ++)
|
|
|
|
{
|
|
|
|
pageAnchorPositions[i] = new Vector3(
|
|
|
|
listContainerTransform.localPosition.x,
|
|
|
|
listContainerMinPosition + itemSize * i,
|
|
|
|
listContainerTransform.localPosition.z
|
|
|
|
);
|
2015-10-17 23:16:23 +08:00
|
|
|
//Debug.Log (String.Format("pos[{0:0}]={1} {2:0%}", i, pageAnchorPositions[i], pages > 1 ? i/(float)(pages-1) : (float)i));
|
2015-10-02 02:29:58 +08:00
|
|
|
}
|
|
|
|
|
2015-10-17 23:16:23 +08:00
|
|
|
//Debug.Log (String.Format("min:{0:0.0}, max:{1:0.0}, pages: {2:0}, width: {3:0}", listContainerMinPosition, listContainerMaxPosition, pages, listContainerSize/pages));
|
2015-10-02 02:29:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
UpdateScrollbar(linkScrolbarSteps);
|
|
|
|
startingPage = Mathf.Min(startingPage, pages);
|
|
|
|
ResetPage();
|
|
|
|
}
|
2015-10-17 23:16:23 +08:00
|
|
|
|
|
|
|
if (itemsCount != activeCount)
|
|
|
|
{
|
|
|
|
ChangePage(CurrentPage());
|
|
|
|
}
|
|
|
|
|
|
|
|
itemsCount = activeCount;
|
|
|
|
listContainerCachedSize.Set(listContainerRectTransform.rect.size.x, listContainerRectTransform.rect.size.y);
|
2015-10-02 02:29:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void ResetPage()
|
|
|
|
{
|
|
|
|
if (direction == ScrollDirection.Horizontal)
|
|
|
|
{
|
2015-10-17 23:16:23 +08:00
|
|
|
scrollRect.horizontalNormalizedPosition = pages > 1 ? (float) startingPage / (float) (pages - 1) : 0;
|
2015-10-02 02:29:58 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-10-17 23:16:23 +08:00
|
|
|
scrollRect.verticalNormalizedPosition = pages > 1 ? (float) (pages - startingPage - 1) / (float) (pages - 1) : 0;
|
2015-10-02 02:29:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void UpdateScrollbar(bool linkSteps)
|
|
|
|
{
|
|
|
|
if (linkSteps)
|
|
|
|
{
|
|
|
|
if (direction == ScrollDirection.Horizontal)
|
|
|
|
{
|
|
|
|
if (scrollRect.horizontalScrollbar != null)
|
|
|
|
{
|
|
|
|
scrollRect.horizontalScrollbar.numberOfSteps = pages;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (scrollRect.verticalScrollbar != null)
|
|
|
|
{
|
|
|
|
scrollRect.verticalScrollbar.numberOfSteps = pages;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (direction == ScrollDirection.Horizontal)
|
|
|
|
{
|
|
|
|
if (scrollRect.horizontalScrollbar != null)
|
|
|
|
{
|
|
|
|
scrollRect.horizontalScrollbar.numberOfSteps = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (scrollRect.verticalScrollbar != null)
|
|
|
|
{
|
|
|
|
scrollRect.verticalScrollbar.numberOfSteps = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-17 23:16:23 +08:00
|
|
|
void LateUpdate()
|
2015-10-02 02:29:58 +08:00
|
|
|
{
|
|
|
|
UpdateListItemsSize();
|
|
|
|
UpdateListItemPositions();
|
|
|
|
|
|
|
|
if (lerp)
|
|
|
|
{
|
|
|
|
UpdateScrollbar(false);
|
|
|
|
|
2015-10-17 23:16:23 +08:00
|
|
|
listContainerTransform.localPosition = Vector3.Lerp(listContainerTransform.localPosition, lerpTarget, 7.5f * Time.deltaTime);
|
2015-10-02 02:29:58 +08:00
|
|
|
|
|
|
|
if (Vector3.Distance(listContainerTransform.localPosition, lerpTarget) < 0.001f)
|
|
|
|
{
|
|
|
|
listContainerTransform.localPosition = lerpTarget;
|
|
|
|
lerp = false;
|
|
|
|
|
|
|
|
UpdateScrollbar(linkScrolbarSteps);
|
2015-10-17 23:16:23 +08:00
|
|
|
|
|
|
|
//Debug.Log(String.Format("Lerp: {0}/{1} page: {2:0}", lerpTarget, listContainerTransform.localPosition, CurrentPage()));
|
|
|
|
}
|
2015-10-02 02:29:58 +08:00
|
|
|
|
2015-10-17 23:16:23 +08:00
|
|
|
//change the info bullets at the bottom of the screen. Just for visual effect
|
|
|
|
if (Vector3.Distance(listContainerTransform.localPosition, lerpTarget) < 10f)
|
2015-10-02 02:29:58 +08:00
|
|
|
{
|
2015-10-17 23:16:23 +08:00
|
|
|
ChangePage(CurrentPage());
|
|
|
|
}
|
|
|
|
}
|
2015-10-02 02:29:58 +08:00
|
|
|
|
2015-10-17 23:16:23 +08:00
|
|
|
if (fastSwipeTimer)
|
2015-10-02 02:29:58 +08:00
|
|
|
{
|
2015-10-17 23:16:23 +08:00
|
|
|
fastSwipeCounter++;
|
2015-10-02 02:29:58 +08:00
|
|
|
}
|
2015-10-17 23:16:23 +08:00
|
|
|
|
|
|
|
//Debug.Log(String.Format("Update: {0}", listContainerTransform.localPosition));
|
|
|
|
}
|
2015-10-02 02:29:58 +08:00
|
|
|
|
2015-10-17 23:16:23 +08:00
|
|
|
private bool fastSwipe = false; //to determine if a fast swipe was performed
|
2015-10-02 02:29:58 +08:00
|
|
|
|
|
|
|
|
2015-10-17 23:16:23 +08:00
|
|
|
//Function for switching screens with buttons
|
|
|
|
public void NextScreen()
|
2015-10-02 02:29:58 +08:00
|
|
|
{
|
2015-10-17 23:16:23 +08:00
|
|
|
UpdateListItemPositions();
|
2015-10-02 02:29:58 +08:00
|
|
|
|
|
|
|
if (CurrentPage() < pages - 1)
|
|
|
|
{
|
2015-10-17 23:16:23 +08:00
|
|
|
lerp = true;
|
|
|
|
lerpTarget = pageAnchorPositions[CurrentPage() + 1];
|
2015-10-02 02:29:58 +08:00
|
|
|
|
2015-10-17 23:16:23 +08:00
|
|
|
ChangePage(CurrentPage() + 1);
|
|
|
|
}
|
|
|
|
}
|
2015-10-02 02:29:58 +08:00
|
|
|
|
2015-10-17 23:16:23 +08:00
|
|
|
//Function for switching screens with buttons
|
|
|
|
public void PreviousScreen()
|
2015-10-02 02:29:58 +08:00
|
|
|
{
|
2015-10-17 23:16:23 +08:00
|
|
|
UpdateListItemPositions();
|
2015-10-02 02:29:58 +08:00
|
|
|
|
|
|
|
if (CurrentPage() > 0)
|
|
|
|
{
|
2015-10-17 23:16:23 +08:00
|
|
|
lerp = true;
|
|
|
|
lerpTarget = pageAnchorPositions[CurrentPage() - 1];
|
2015-10-02 02:29:58 +08:00
|
|
|
|
2015-10-17 23:16:23 +08:00
|
|
|
ChangePage(CurrentPage() - 1);
|
|
|
|
}
|
|
|
|
}
|
2015-10-02 02:29:58 +08:00
|
|
|
|
2015-10-17 23:16:23 +08:00
|
|
|
//Because the CurrentScreen function is not so reliable, these are the functions used for swipes
|
|
|
|
private void NextScreenCommand()
|
|
|
|
{
|
2015-10-02 02:29:58 +08:00
|
|
|
if (pageOnDragStart < pages - 1)
|
|
|
|
{
|
|
|
|
int targetPage = Mathf.Min(pages - 1, pageOnDragStart + itemsVisibleAtOnce);
|
2015-10-17 23:16:23 +08:00
|
|
|
lerp = true;
|
2015-10-02 02:29:58 +08:00
|
|
|
|
|
|
|
lerpTarget = pageAnchorPositions[targetPage];
|
|
|
|
|
|
|
|
ChangePage(targetPage);
|
2015-10-17 23:16:23 +08:00
|
|
|
}
|
|
|
|
}
|
2015-10-02 02:29:58 +08:00
|
|
|
|
2015-10-17 23:16:23 +08:00
|
|
|
//Because the CurrentScreen function is not so reliable, these are the functions used for swipes
|
|
|
|
private void PrevScreenCommand()
|
2015-10-02 02:29:58 +08:00
|
|
|
{
|
|
|
|
if (pageOnDragStart > 0)
|
|
|
|
{
|
|
|
|
int targetPage = Mathf.Max(0, pageOnDragStart - itemsVisibleAtOnce);
|
2015-10-17 23:16:23 +08:00
|
|
|
lerp = true;
|
2015-10-02 02:29:58 +08:00
|
|
|
|
|
|
|
lerpTarget = pageAnchorPositions[targetPage];
|
|
|
|
|
|
|
|
ChangePage(targetPage);
|
2015-10-17 23:16:23 +08:00
|
|
|
}
|
|
|
|
}
|
2015-10-02 02:29:58 +08:00
|
|
|
|
|
|
|
|
2015-10-17 23:16:23 +08:00
|
|
|
//returns the current screen that the is seeing
|
|
|
|
public int CurrentPage()
|
2015-10-02 02:29:58 +08:00
|
|
|
{
|
|
|
|
float pos;
|
|
|
|
|
|
|
|
if (direction == ScrollDirection.Horizontal)
|
|
|
|
{
|
|
|
|
pos = listContainerMaxPosition - listContainerTransform.localPosition.x;
|
|
|
|
pos = Mathf.Clamp(pos, 0, listContainerSize);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pos = listContainerTransform.localPosition.y - listContainerMinPosition;
|
|
|
|
pos = Mathf.Clamp(pos, 0, listContainerSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
float page = pos / itemSize;
|
|
|
|
|
2015-10-17 23:16:23 +08:00
|
|
|
//Debug.Log (String.Format("min:{0:0.0}, max:{1:0.0}, current:{2:0.0}/{4:0.0}, page:{3:0.0}, target:{5}", listContainerMinPosition, listContainerMaxPosition, pos, page, listContainerTransform.localPosition.x, pageAnchorPositions[Mathf.RoundToInt(page)]));
|
|
|
|
|
2015-10-02 02:29:58 +08:00
|
|
|
return Mathf.Clamp(Mathf.RoundToInt(page), 0, pages);
|
2015-10-17 23:16:23 +08:00
|
|
|
}
|
2015-10-02 02:29:58 +08:00
|
|
|
|
2015-10-17 23:16:23 +08:00
|
|
|
//changes the bullets on the bottom of the page - pagination
|
|
|
|
private void ChangePage(int currentPage)
|
|
|
|
{
|
2015-10-02 02:29:58 +08:00
|
|
|
startingPage = currentPage;
|
|
|
|
|
2015-10-17 23:16:23 +08:00
|
|
|
if (nextButton)
|
|
|
|
{
|
|
|
|
nextButton.interactable = currentPage < pages - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prevButton)
|
|
|
|
{
|
|
|
|
prevButton.interactable = currentPage > 0;
|
|
|
|
}
|
|
|
|
|
2015-10-02 02:29:58 +08:00
|
|
|
if (onPageChange != null)
|
|
|
|
{
|
|
|
|
onPageChange(currentPage);
|
|
|
|
}
|
2015-10-17 23:16:23 +08:00
|
|
|
}
|
2015-10-02 02:29:58 +08:00
|
|
|
|
|
|
|
#region Interfaces
|
2015-10-17 23:16:23 +08:00
|
|
|
public void OnBeginDrag(PointerEventData eventData)
|
|
|
|
{
|
2015-10-02 02:29:58 +08:00
|
|
|
UpdateScrollbar(false);
|
|
|
|
|
|
|
|
fastSwipeCounter = 0;
|
|
|
|
fastSwipeTimer = true;
|
|
|
|
|
|
|
|
positionOnDragStart = eventData.position;
|
2015-10-17 23:16:23 +08:00
|
|
|
pageOnDragStart = CurrentPage();
|
2015-10-02 02:29:58 +08:00
|
|
|
|
2015-10-17 23:16:23 +08:00
|
|
|
//Debug.Log (String.Format("pageOnDragStart:{0:0}", pageOnDragStart));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnEndDrag(PointerEventData eventData)
|
|
|
|
{
|
|
|
|
startDrag = true;
|
2015-10-02 02:29:58 +08:00
|
|
|
float change = 0;
|
|
|
|
|
|
|
|
if (direction == ScrollDirection.Horizontal)
|
|
|
|
{
|
|
|
|
change = positionOnDragStart.x - eventData.position.x;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
change = -positionOnDragStart.y + eventData.position.y;
|
|
|
|
}
|
|
|
|
|
2015-10-17 23:16:23 +08:00
|
|
|
if (useFastSwipe)
|
|
|
|
{
|
|
|
|
fastSwipe = false;
|
|
|
|
fastSwipeTimer = false;
|
|
|
|
|
|
|
|
if (fastSwipeCounter <= fastSwipeTarget)
|
|
|
|
{
|
|
|
|
if (Math.Abs(change) > fastSwipeThreshold)
|
|
|
|
{
|
|
|
|
fastSwipe = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fastSwipe)
|
|
|
|
{
|
2015-10-02 02:29:58 +08:00
|
|
|
if (change > 0)
|
|
|
|
{
|
|
|
|
NextScreenCommand();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PrevScreenCommand();
|
|
|
|
}
|
2015-10-17 23:16:23 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lerp = true;
|
2015-10-02 02:29:58 +08:00
|
|
|
lerpTarget = pageAnchorPositions[CurrentPage()];
|
2015-10-17 23:16:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lerp = true;
|
2015-10-02 02:29:58 +08:00
|
|
|
lerpTarget = pageAnchorPositions[CurrentPage()];
|
2015-10-17 23:16:23 +08:00
|
|
|
}
|
|
|
|
}
|
2015-10-02 02:29:58 +08:00
|
|
|
|
2015-10-17 23:16:23 +08:00
|
|
|
public void OnDrag(PointerEventData eventData)
|
|
|
|
{
|
2015-10-02 02:29:58 +08:00
|
|
|
lerp = false;
|
|
|
|
|
|
|
|
if (startDrag)
|
2015-10-17 23:16:23 +08:00
|
|
|
{
|
|
|
|
OnBeginDrag(eventData);
|
|
|
|
startDrag = false;
|
|
|
|
}
|
|
|
|
}
|
2015-10-02 02:29:58 +08:00
|
|
|
#endregion
|
2015-10-17 23:16:23 +08:00
|
|
|
}
|
2015-10-02 02:29:58 +08:00
|
|
|
}
|