Updated ReorderableList/ReorderableListElement to prevent creating a "Fake" droppable when the item is not transferable

Resolves: https://github.com/Unity-UI-Extensions/com.unity.uiextensions/issues/164
pull/413/head
Simon Jackson 2023-01-01 16:13:40 +00:00
parent 4b1c5412f1
commit 9b23206c9a
3 changed files with 10 additions and 33 deletions

View File

@ -6,7 +6,6 @@ using UnityEngine.Events;
namespace UnityEngine.UI.Extensions namespace UnityEngine.UI.Extensions
{ {
[RequireComponent(typeof(RectTransform)), DisallowMultipleComponent] [RequireComponent(typeof(RectTransform)), DisallowMultipleComponent]
[AddComponentMenu("UI/Extensions/Re-orderable list")] [AddComponentMenu("UI/Extensions/Re-orderable list")]
public class ReorderableList : MonoBehaviour public class ReorderableList : MonoBehaviour
@ -33,9 +32,9 @@ namespace UnityEngine.UI.Extensions
[Tooltip("Should items being dragged over this list have their sizes equalized?")] [Tooltip("Should items being dragged over this list have their sizes equalized?")]
public bool EqualizeSizesOnDrag = false; public bool EqualizeSizesOnDrag = false;
[Tooltip("Maximum number of items this container can hold")]
public int maxItems = int.MaxValue; public int maxItems = int.MaxValue;
[Header("UI Re-orderable Events")] [Header("UI Re-orderable Events")]
public ReorderableListHandler OnElementDropped = new ReorderableListHandler(); public ReorderableListHandler OnElementDropped = new ReorderableListHandler();
public ReorderableListHandler OnElementGrabbed = new ReorderableListHandler(); public ReorderableListHandler OnElementGrabbed = new ReorderableListHandler();
@ -62,7 +61,7 @@ namespace UnityEngine.UI.Extensions
} }
} }
Canvas GetCanvas() public Canvas GetCanvas()
{ {
Transform t = transform; Transform t = transform;
Canvas canvas = null; Canvas canvas = null;
@ -73,8 +72,7 @@ namespace UnityEngine.UI.Extensions
while (canvas == null && lvl < lvlLimit) while (canvas == null && lvl < lvlLimit)
{ {
canvas = t.gameObject.GetComponent<Canvas>(); if (!t.gameObject.TryGetComponent<Canvas>(out canvas))
if (canvas == null)
{ {
t = t.parent; t = t.parent;
} }
@ -95,7 +93,6 @@ namespace UnityEngine.UI.Extensions
private void Start() private void Start()
{ {
if (ContentLayout == null) if (ContentLayout == null)
{ {
Debug.LogError("You need to have a child LayoutGroup content set for the list: " + name, gameObject); Debug.LogError("You need to have a child LayoutGroup content set for the list: " + name, gameObject);
@ -114,7 +111,6 @@ namespace UnityEngine.UI.Extensions
Refresh(); Refresh();
} }
#region Nested type: ReorderableListEventStruct #region Nested type: ReorderableListEventStruct
[Serializable] [Serializable]
@ -136,13 +132,10 @@ namespace UnityEngine.UI.Extensions
#endregion #endregion
#region Nested type: ReorderableListHandler #region Nested type: ReorderableListHandler
[Serializable] [Serializable]
public class ReorderableListHandler : UnityEvent<ReorderableListEventStruct> public class ReorderableListHandler : UnityEvent<ReorderableListEventStruct> { }
{
}
public void TestReOrderableListTarget(ReorderableListEventStruct item) public void TestReOrderableListTarget(ReorderableListEventStruct item)
{ {

View File

@ -21,7 +21,6 @@ namespace UnityEngine.UI.Extensions
if(_rect)StartCoroutine(RefreshChildren()); if(_rect)StartCoroutine(RefreshChildren());
} }
public void OnTransformChildrenChanged() public void OnTransformChildrenChanged()
{ {
if(this.isActiveAndEnabled)StartCoroutine(RefreshChildren()); if(this.isActiveAndEnabled)StartCoroutine(RefreshChildren());

View File

@ -8,7 +8,6 @@ using UnityEngine.EventSystems;
namespace UnityEngine.UI.Extensions namespace UnityEngine.UI.Extensions
{ {
[RequireComponent(typeof(RectTransform), typeof(LayoutElement))] [RequireComponent(typeof(RectTransform), typeof(LayoutElement))]
public class ReorderableListElement : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler public class ReorderableListElement : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler
{ {
@ -24,7 +23,7 @@ namespace UnityEngine.UI.Extensions
[SerializeField] [SerializeField]
private bool isDroppableInSpace = false; private bool isDroppableInSpace = false;
[Tooltip("Can this element be dropped in another container?")]
public bool IsTransferable public bool IsTransferable
{ {
get { return _isTransferable; } get { return _isTransferable; }
@ -61,7 +60,6 @@ namespace UnityEngine.UI.Extensions
#region IBeginDragHandler Members #region IBeginDragHandler Members
public void OnBeginDrag(PointerEventData eventData) public void OnBeginDrag(PointerEventData eventData)
{ {
if (!_canvasGroup) { _canvasGroup = gameObject.GetOrAddComponent<CanvasGroup>(); } if (!_canvasGroup) { _canvasGroup = gameObject.GetOrAddComponent<CanvasGroup>(); }
@ -142,12 +140,9 @@ namespace UnityEngine.UI.Extensions
_isDragging = true; _isDragging = true;
} }
#endregion #endregion
#region IDragHandler Members #region IDragHandler Members
public void OnDrag(PointerEventData eventData) public void OnDrag(PointerEventData eventData)
{ {
if (!_isDragging) if (!_isDragging)
@ -179,7 +174,6 @@ namespace UnityEngine.UI.Extensions
//If nothing found or the list is not dropable, put the fake element outside //If nothing found or the list is not dropable, put the fake element outside
if (_currentReorderableListRaycasted == null || _currentReorderableListRaycasted.IsDropable == false if (_currentReorderableListRaycasted == null || _currentReorderableListRaycasted.IsDropable == false
// || (_oldReorderableListRaycasted != _reorderableList && !IsTransferable)
|| ((_fakeElement.parent == _currentReorderableListRaycasted.Content || ((_fakeElement.parent == _currentReorderableListRaycasted.Content
? _currentReorderableListRaycasted.Content.childCount - 1 ? _currentReorderableListRaycasted.Content.childCount - 1
: _currentReorderableListRaycasted.Content.childCount) >= _currentReorderableListRaycasted.maxItems && !_currentReorderableListRaycasted.IsDisplacable) : _currentReorderableListRaycasted.Content.childCount) >= _currentReorderableListRaycasted.maxItems && !_currentReorderableListRaycasted.IsDisplacable)
@ -194,7 +188,7 @@ namespace UnityEngine.UI.Extensions
} }
} }
//Else find the best position on the list and put fake element on the right index //Else find the best position on the list and put fake element on the right index
else else if (_currentReorderableListRaycasted == _reorderableList || IsTransferable)
{ {
if (_currentReorderableListRaycasted.Content.childCount < _currentReorderableListRaycasted.maxItems && _fakeElement.parent != _currentReorderableListRaycasted.Content) if (_currentReorderableListRaycasted.Content.childCount < _currentReorderableListRaycasted.maxItems && _fakeElement.parent != _currentReorderableListRaycasted.Content)
{ {
@ -245,12 +239,9 @@ namespace UnityEngine.UI.Extensions
} }
} }
#endregion #endregion
#region Displacement #region Displacement
private void displaceElement(int targetIndex, Transform displaced) private void displaceElement(int targetIndex, Transform displaced)
{ {
_displacedFromIndex = targetIndex; _displacedFromIndex = targetIndex;
@ -343,7 +334,6 @@ namespace UnityEngine.UI.Extensions
} }
public void finishDisplacingElement() public void finishDisplacingElement()
{ {
if (_displacedObject.parent == null) if (_displacedObject.parent == null)
@ -355,12 +345,9 @@ namespace UnityEngine.UI.Extensions
_displacedObject = null; _displacedObject = null;
_displacedObjectLE = null; _displacedObjectLE = null;
} }
#endregion #endregion
#region IEndDragHandler Members #region IEndDragHandler Members
public void OnEndDrag(PointerEventData eventData) public void OnEndDrag(PointerEventData eventData)
{ {
_isDragging = false; _isDragging = false;
@ -473,11 +460,9 @@ namespace UnityEngine.UI.Extensions
} }
_canvasGroup.blocksRaycasts = true; _canvasGroup.blocksRaycasts = true;
} }
#endregion #endregion
private void CancelDrag()
void CancelDrag()
{ {
_isDragging = false; _isDragging = false;
//If it's a clone, delete it //If it's a clone, delete it