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
{
[RequireComponent(typeof(RectTransform)), DisallowMultipleComponent]
[AddComponentMenu("UI/Extensions/Re-orderable list")]
public class ReorderableList : MonoBehaviour
@ -31,11 +30,11 @@ namespace UnityEngine.UI.Extensions
// This sets every item size (when being dragged over this list) to the current size of the first element of this list
[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;
[Header("UI Re-orderable Events")]
public ReorderableListHandler OnElementDropped = new ReorderableListHandler();
public ReorderableListHandler OnElementGrabbed = new ReorderableListHandler();
@ -62,7 +61,7 @@ namespace UnityEngine.UI.Extensions
}
}
Canvas GetCanvas()
public Canvas GetCanvas()
{
Transform t = transform;
Canvas canvas = null;
@ -73,8 +72,7 @@ namespace UnityEngine.UI.Extensions
while (canvas == null && lvl < lvlLimit)
{
canvas = t.gameObject.GetComponent<Canvas>();
if (canvas == null)
if (!t.gameObject.TryGetComponent<Canvas>(out canvas))
{
t = t.parent;
}
@ -95,7 +93,6 @@ namespace UnityEngine.UI.Extensions
private void Start()
{
if (ContentLayout == null)
{
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();
}
#region Nested type: ReorderableListEventStruct
[Serializable]
@ -136,13 +132,10 @@ namespace UnityEngine.UI.Extensions
#endregion
#region Nested type: ReorderableListHandler
[Serializable]
public class ReorderableListHandler : UnityEvent<ReorderableListEventStruct>
{
}
public class ReorderableListHandler : UnityEvent<ReorderableListEventStruct> { }
public void TestReOrderableListTarget(ReorderableListEventStruct item)
{
@ -152,4 +145,4 @@ namespace UnityEngine.UI.Extensions
#endregion
}
}
}

View File

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

View File

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