Refined magnetic scroll and dependencies while documenting

Updated example
pull/413/head
Simon (darkside) Jackson 2020-10-20 15:48:07 +01:00
parent 37fa5da1c8
commit 61d0c8ec6d
3 changed files with 49 additions and 54 deletions

@ -1 +1 @@
Subproject commit 9d756f7bd9c2195d34e482c9402b715cf760fc96 Subproject commit 906bad37c84bc2abc156aeef50bcb93b85ea08b3

View File

@ -25,7 +25,7 @@ namespace UnityEngine.UI.Extensions
//if true user will need to call Init() method manually (in case the contend of the scrollview is generated from code or requires special initialization) //if true user will need to call Init() method manually (in case the contend of the scrollview is generated from code or requires special initialization)
[Tooltip("If false, will Init automatically, otherwise you need to call Init() method")] [Tooltip("If false, will Init automatically, otherwise you need to call Init() method")]
public bool InitByUser = false; public bool InitByUser = false;
private ScrollRect _scrollRect; protected ScrollRect _scrollRect;
private ContentSizeFitter _contentSizeFitter; private ContentSizeFitter _contentSizeFitter;
private VerticalLayoutGroup _verticalLayoutGroup; private VerticalLayoutGroup _verticalLayoutGroup;
private HorizontalLayoutGroup _horizontalLayoutGroup; private HorizontalLayoutGroup _horizontalLayoutGroup;
@ -38,7 +38,7 @@ namespace UnityEngine.UI.Extensions
protected List<RectTransform> items = new List<RectTransform>(); protected List<RectTransform> items = new List<RectTransform>();
private Vector2 _newAnchoredPosition = Vector2.zero; private Vector2 _newAnchoredPosition = Vector2.zero;
//TO DISABLE FLICKERING OBJECT WHEN SCROLL VIEW IS IDLE IN BETWEEN OBJECTS //TO DISABLE FLICKERING OBJECT WHEN SCROLL VIEW IS IDLE IN BETWEEN OBJECTS
private float _treshold = 100f; private float _threshold = 100f;
private int _itemCount = 0; private int _itemCount = 0;
private float _recordOffsetX = 0; private float _recordOffsetX = 0;
private float _recordOffsetY = 0; private float _recordOffsetY = 0;
@ -179,7 +179,7 @@ namespace UnityEngine.UI.Extensions
{ {
if (_isHorizontal) if (_isHorizontal)
{ {
if (_scrollRect.transform.InverseTransformPoint(items[i].gameObject.transform.position).x > _disableMarginX + _treshold) if (_scrollRect.transform.InverseTransformPoint(items[i].gameObject.transform.position).x > _disableMarginX + _threshold)
{ {
_newAnchoredPosition = items[i].anchoredPosition; _newAnchoredPosition = items[i].anchoredPosition;
_newAnchoredPosition.x -= _itemCount * _recordOffsetX; _newAnchoredPosition.x -= _itemCount * _recordOffsetX;
@ -197,7 +197,7 @@ namespace UnityEngine.UI.Extensions
if (_isVertical) if (_isVertical)
{ {
if (_scrollRect.transform.InverseTransformPoint(items[i].gameObject.transform.position).y > _disableMarginY + _treshold) if (_scrollRect.transform.InverseTransformPoint(items[i].gameObject.transform.position).y > _disableMarginY + _threshold)
{ {
_newAnchoredPosition = items[i].anchoredPosition; _newAnchoredPosition = items[i].anchoredPosition;
_newAnchoredPosition.y -= _itemCount * _recordOffsetY; _newAnchoredPosition.y -= _itemCount * _recordOffsetY;

View File

@ -5,42 +5,37 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine.EventSystems;
namespace UnityEngine.UI.Extensions namespace UnityEngine.UI.Extensions
{ {
[AddComponentMenu("UI/Extensions/UI Magnetic Infinite Scroll")] [AddComponentMenu("UI/Extensions/UI Magnetic Infinite Scroll")]
public class UI_MagneticInfiniteScroll : UI_InfiniteScroll public class UI_MagneticInfiniteScroll : UI_InfiniteScroll, IDragHandler, IEndDragHandler, IScrollHandler
{ {
public event Action<GameObject> OnNewSelect; public event Action<GameObject> OnNewSelect;
[Tooltip("The pointer to the pivot, the visual element for centering objects.")] [Tooltip("The pointer to the pivot, the visual element for centering objects.")]
[SerializeField] [SerializeField]
private RectTransform pivot = null; private RectTransform pivot = null;
[Tooltip("The pointer to the object container")] [Tooltip("The maximum speed that allows you to activate the magnet to center on the pivot")]
[SerializeField]
private RectTransform content = null;
[Tooltip("the maximum speed that allows you to activate the magnet to center on the pivot")]
[SerializeField] [SerializeField]
private float maxSpeedForMagnetic = 10f; private float maxSpeedForMagnetic = 10f;
[SerializeField] [SerializeField]
[Tooltip("The initial index of the object which must be initially centered")] [Tooltip("The index of the object which must be initially centered")]
private int indexStart = 0; private int indexStart = 0;
[SerializeField] [SerializeField]
[Tooltip("The time to decelerate and aim for the pivot")] [Tooltip("The time to decelerate and aim to the pivot")]
private float timeForDeceleration = 0.05f; private float timeForDeceleration = 0.05f;
[SerializeField]
private ScrollRect scrollRect = null;
private float _pastPositionMouseSpeed; private float _pastPositionMouseSpeed;
private float initMovementDirection = 0; private float _initMovementDirection = 0;
private float _pastPosition = 0; private float _pastPosition = 0;
private float _currentSpeed = 0.0f; private float _currentSpeed = 0.0f;
private float _stopValue = 0.0f; private float _stopValue = 0.0f;
private readonly float _waitForContentSet = 0.1f; private readonly float _waitForContentSet = 0.1f;
private float _currentTime = 0; private float _currentTime = 0;
private int nearestIndex = 0; private int _nearestIndex = 0;
private bool _useMagnetic = true; private bool _useMagnetic = true;
private bool _isStopping = false; private bool _isStopping = false;
@ -56,12 +51,12 @@ namespace UnityEngine.UI.Extensions
private void Update() private void Update()
{ {
if (!content || !pivot || !_useMagnetic || !_isMovement || items == null || scrollRect == null) if (_scrollRect == null || !_scrollRect.content || !pivot || !_useMagnetic || !_isMovement || items == null)
{ {
return; return;
} }
float currentPosition = GetRightAxis(content.anchoredPosition); float currentPosition = GetRightAxis(_scrollRect.content.anchoredPosition);
_currentSpeed = Mathf.Abs(currentPosition - _pastPosition); _currentSpeed = Mathf.Abs(currentPosition - _pastPosition);
_pastPosition = currentPosition; _pastPosition = currentPosition;
if (Mathf.Abs(_currentSpeed) > maxSpeedForMagnetic) if (Mathf.Abs(_currentSpeed) > maxSpeedForMagnetic)
@ -71,21 +66,21 @@ namespace UnityEngine.UI.Extensions
if (_isStopping) if (_isStopping)
{ {
Vector2 anchoredPosition = content.anchoredPosition; Vector2 anchoredPosition = _scrollRect.content.anchoredPosition;
_currentTime += Time.deltaTime; _currentTime += Time.deltaTime;
float valueLerp = _currentTime / timeForDeceleration; float valueLerp = _currentTime / timeForDeceleration;
float newPosition = Mathf.Lerp(GetRightAxis(anchoredPosition), _stopValue, valueLerp); float newPosition = Mathf.Lerp(GetRightAxis(anchoredPosition), _stopValue, valueLerp);
content.anchoredPosition = _isVertical ? new Vector2(anchoredPosition.x, newPosition) : _scrollRect.content.anchoredPosition = _isVertical ? new Vector2(anchoredPosition.x, newPosition) :
new Vector2(newPosition, anchoredPosition.y); new Vector2(newPosition, anchoredPosition.y);
if (newPosition == GetRightAxis(anchoredPosition) && nearestIndex > 0 && nearestIndex < items.Count) if (newPosition == GetRightAxis(anchoredPosition) && _nearestIndex > 0 && _nearestIndex < items.Count)
{ {
_isStopping = false; _isStopping = false;
_isMovement = false; _isMovement = false;
var item = items[nearestIndex]; var item = items[_nearestIndex];
if (item != null && OnNewSelect != null) if (item != null && OnNewSelect != null)
{ {
@ -95,7 +90,7 @@ namespace UnityEngine.UI.Extensions
} }
else else
{ {
float distance = Mathf.Infinity * (-initMovementDirection); float distance = Mathf.Infinity * (-_initMovementDirection);
for (int i = 0; i < items.Count; i++) for (int i = 0; i < items.Count; i++)
{ {
@ -107,35 +102,20 @@ namespace UnityEngine.UI.Extensions
var aux = GetRightAxis(item.position) - GetRightAxis(pivot.position); var aux = GetRightAxis(item.position) - GetRightAxis(pivot.position);
if ((initMovementDirection <= 0 && aux < distance && aux > 0) || if ((_initMovementDirection <= 0 && aux < distance && aux > 0) ||
(initMovementDirection > 0 && aux > distance && aux < 0)) (_initMovementDirection > 0 && aux > distance && aux < 0))
{ {
distance = aux; distance = aux;
nearestIndex = i; _nearestIndex = i;
} }
} }
_isStopping = true; _isStopping = true;
_stopValue = GetAnchoredPositionForPivot(nearestIndex); _stopValue = GetAnchoredPositionForPivot(_nearestIndex);
scrollRect.StopMovement(); _scrollRect.StopMovement();
} }
} }
public void Drag()
{
float currentPosition = GetRightAxis(UIExtensionsInputManager.MousePosition);
initMovementDirection = Mathf.Sign(currentPosition - _pastPositionMouseSpeed);
_pastPositionMouseSpeed = currentPosition;
_useMagnetic = false;
_isStopping = false;
}
public void EndDrag()
{
FinishPrepareMovement();
}
public override void SetNewItems(ref List<Transform> newItems) public override void SetNewItems(ref List<Transform> newItems)
{ {
foreach (var element in newItems) foreach (var element in newItems)
@ -149,22 +129,16 @@ namespace UnityEngine.UI.Extensions
base.SetNewItems(ref newItems); base.SetNewItems(ref newItems);
} }
public void Scroll()
{
initMovementDirection = -UIExtensionsInputManager.MouseScrollDelta.y;
FinishPrepareMovement();
}
public void SetContentInPivot(int index) public void SetContentInPivot(int index)
{ {
float newPos = GetAnchoredPositionForPivot(index); float newPos = GetAnchoredPositionForPivot(index);
Vector2 anchoredPosition = content.anchoredPosition; Vector2 anchoredPosition = _scrollRect.content.anchoredPosition;
if (content) if (_scrollRect.content)
{ {
content.anchoredPosition = _isVertical ? new Vector2(anchoredPosition.x, newPos) : _scrollRect.content.anchoredPosition = _isVertical ? new Vector2(anchoredPosition.x, newPos) :
new Vector2(newPos, anchoredPosition.y); new Vector2(newPos, anchoredPosition.y);
_pastPosition = GetRightAxis(content.anchoredPosition); _pastPosition = GetRightAxis(_scrollRect.content.anchoredPosition);
} }
} }
@ -200,5 +174,26 @@ namespace UnityEngine.UI.Extensions
{ {
return _isVertical ? vector.y : vector.x; return _isVertical ? vector.y : vector.x;
} }
public void OnDrag(PointerEventData eventData)
{
float currentPosition = GetRightAxis(UIExtensionsInputManager.MousePosition);
_initMovementDirection = Mathf.Sign(currentPosition - _pastPositionMouseSpeed);
_pastPositionMouseSpeed = currentPosition;
_useMagnetic = false;
_isStopping = false;
}
public void OnEndDrag(PointerEventData eventData)
{
FinishPrepareMovement();
}
public void OnScroll(PointerEventData eventData)
{
_initMovementDirection = -UIExtensionsInputManager.MouseScrollDelta.y;
FinishPrepareMovement();
}
} }
} }