Back-ported the Reorderable List control from 5.2

--HG--
branch : develop_4.6
pull/413/head
Simon (darkside) Jackson 2015-10-29 21:11:00 +00:00
parent 1c134fb272
commit d89d80f968
12 changed files with 1165 additions and 292 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,5 @@
fileFormatVersion: 2
guid: 2172bd77b99a4a9409c147b803ae67df
folderAsset: yes
DefaultImporter:
userData:

View File

@ -0,0 +1,99 @@
/// Credit Ziboo
/// Sourced from - http://forum.unity3d.com/threads/free-reorderable-list.364600/
using System;
using UnityEngine.Events;
namespace UnityEngine.UI.Extensions
{
[RequireComponent(typeof(RectTransform)), DisallowMultipleComponent]
[AddComponentMenu("UI/Extensions/Re-orderable list")]
public class ReorderableList : MonoBehaviour
{
[Tooltip("Child container with re-orderable items in a layout group")]
public LayoutGroup ContentLayout;
[Tooltip("Parent area to draw the dragged element on top of containers. Defaults to the root Canvas")]
public RectTransform DraggableArea;
[Tooltip("Can items be dragged from the container?")]
public bool IsDraggable = true;
[Tooltip("Should the draggable components be removed or cloned?")]
public bool CloneDraggedObject = false;
[Tooltip("Can new draggable items be dropped in to the container?")]
public bool IsDropable = true;
[Header("UI Re-orderable Events")]
public ReorderableListHandler OnElementDropped = new ReorderableListHandler();
public ReorderableListHandler OnElementGrabbed = new ReorderableListHandler();
public ReorderableListHandler OnElementRemoved = new ReorderableListHandler();
private RectTransform _content;
private ReorderableListContent _listContent;
public RectTransform Content
{
get
{
if (_content == null)
{
_content = ContentLayout.GetComponent<RectTransform>();
}
return _content;
}
}
private void Awake()
{
if (ContentLayout == null)
{
Debug.LogError("You need to have a child LayoutGroup content set for the list: " + name, gameObject);
return;
}
if (DraggableArea == null)
{
DraggableArea = transform.root.GetComponentInChildren<Canvas>().GetComponent<RectTransform>();
}
if (IsDropable && !GetComponent<Graphic>())
{
Debug.LogError("You need to have a Graphic control (such as an Image) for the list [" + name + "] to be droppable", gameObject);
return;
}
_listContent = ContentLayout.gameObject.AddComponent<ReorderableListContent>();
_listContent.Init(this);
}
#region Nested type: ReorderableListEventStruct
[Serializable]
public struct ReorderableListEventStruct
{
public GameObject DroppedObject;
public int FromIndex;
public ReorderableList FromList;
public bool IsAClone;
public GameObject SourceObject;
public int ToIndex;
public ReorderableList ToList;
}
#endregion
#region Nested type: ReorderableListHandler
[Serializable]
public class ReorderableListHandler : UnityEvent<ReorderableListEventStruct>
{
}
public void TestReOrderableListTarget(ReorderableListEventStruct item)
{
Debug.Log("Event Received");
Debug.Log("Hello World, is my item a clone? [" + item.IsAClone + "]");
}
#endregion
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 6b333d67eb08d464d823874f6a1666c2
timeCreated: 1446072130
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 9b8d590979bc3264ab9a7df11a0e8c3c
timeCreated: 1446061891
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,63 @@
/// Credit Ziboo
/// Sourced from - http://forum.unity3d.com/threads/free-reorderable-list.364600/
using System.Collections;
using System.Collections.Generic;
namespace UnityEngine.UI.Extensions
{
public class ReorderableListContent : MonoBehaviour
{
private List<Transform> _cachedChildren;
private List<ReorderableListElement> _cachedListElement;
private ReorderableListElement _ele;
private ReorderableList _extList;
private RectTransform _rect;
public void OnTransformChildrenChanged()
{
StartCoroutine(RefreshChildren());
}
public void Init(ReorderableList extList)
{
_extList = extList;
_rect = GetComponent<RectTransform>();
_cachedChildren = new List<Transform>();
_cachedListElement = new List<ReorderableListElement>();
StartCoroutine(RefreshChildren());
}
private IEnumerator RefreshChildren()
{
//Handle new chilren
for (int i = 0; i < _rect.childCount; i++)
{
if (_cachedChildren.Contains(_rect.GetChild(i)))
continue;
//Get or Create ReorderableListElement
_ele = _rect.GetChild(i).gameObject.GetComponent<ReorderableListElement>() ??
_rect.GetChild(i).gameObject.AddComponent<ReorderableListElement>();
_ele.Init(_extList);
_cachedChildren.Add(_rect.GetChild(i));
_cachedListElement.Add(_ele);
}
//HACK a little hack, if I don't wait one frame I don't have the right deleted children
yield return 0;
//Remove deleted child
for (int i = _cachedChildren.Count - 1; i >= 0; i--)
{
if (_cachedChildren[i] == null)
{
_cachedChildren.RemoveAt(i);
_cachedListElement.RemoveAt(i);
}
}
}
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 252dd148b2c1dbe40b7d938a553e3caf
timeCreated: 1446062045
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,29 @@
/// Credit Ziboo
/// Sourced from - http://forum.unity3d.com/threads/free-reorderable-list.364600/
namespace UnityEngine.UI.Extensions
{
public class ReorderableListDebug : MonoBehaviour
{
public Text DebugLabel;
void Awake()
{
foreach (var list in FindObjectsOfType<ReorderableList>())
{
list.OnElementDropped.AddListener(ElementDropped);
}
}
private void ElementDropped(ReorderableList.ReorderableListEventStruct droppedStruct)
{
DebugLabel.text = "";
DebugLabel.text += "Dropped Object: " + droppedStruct.DroppedObject.name + "\n";
DebugLabel.text += "Is Clone ?: " + droppedStruct.IsAClone + "\n";
if (droppedStruct.IsAClone)
DebugLabel.text += "Source Object: " + droppedStruct.SourceObject.name + "\n";
DebugLabel.text += string.Format("From {0} at Index {1} \n", droppedStruct.FromList.name, droppedStruct.FromIndex);
DebugLabel.text += string.Format("To {0} at Index {1} \n", droppedStruct.ToList.name, droppedStruct.ToIndex);
}
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 86c224aef3e999140b78d1d7135ba33f
timeCreated: 1446072313
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,240 @@
/// Credit Ziboo
/// Sourced from - http://forum.unity3d.com/threads/free-reorderable-list.364600/
using System.Collections.Generic;
using UnityEngine.EventSystems;
namespace UnityEngine.UI.Extensions
{
[RequireComponent(typeof(RectTransform))]
public class ReorderableListElement : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler
{
private readonly List<RaycastResult> _raycastResults = new List<RaycastResult>();
private ReorderableList _currentReorderableListRaycasted;
private RectTransform _draggingObject;
private LayoutElement _draggingObjectLE;
private Vector2 _draggingObjectOriginalSize;
private RectTransform _fakeElement;
private LayoutElement _fakeElementLE;
private int _fromIndex;
private bool _isDragging;
private RectTransform _rect;
private ReorderableList _reorderableList;
#region IBeginDragHandler Members
public void OnBeginDrag(PointerEventData eventData)
{
if (_reorderableList == null)
return;
//Can't drag, return...
if (!_reorderableList.IsDraggable)
{
_draggingObject = null;
return;
}
//If CloneDraggedObject just set draggingObject to this gameobject
if (_reorderableList.CloneDraggedObject == false)
{
_draggingObject = _rect;
_fromIndex = _rect.GetSiblingIndex();
//Send OnElementRemoved Event
if (_reorderableList.OnElementRemoved != null)
{
_reorderableList.OnElementRemoved.Invoke(new ReorderableList.ReorderableListEventStruct
{
DroppedObject = _draggingObject.gameObject,
IsAClone = _reorderableList.CloneDraggedObject,
SourceObject = _reorderableList.CloneDraggedObject ? gameObject : _draggingObject.gameObject,
FromList = _reorderableList,
FromIndex = _fromIndex,
});
}
}
//Else Duplicate
else
{
GameObject clone = (GameObject)Instantiate(gameObject);
_draggingObject = clone.GetComponent<RectTransform>();
}
//Put _dragging object into the draggin area
_draggingObjectOriginalSize = gameObject.GetComponent<RectTransform>().rect.size;
_draggingObjectLE = _draggingObject.GetComponent<LayoutElement>();
_draggingObject.SetParent(_reorderableList.DraggableArea, false);
_draggingObject.SetAsLastSibling();
//Create a fake element for previewing placement
_fakeElement = new GameObject("Fake").AddComponent<RectTransform>();
_fakeElementLE = _fakeElement.gameObject.AddComponent<LayoutElement>();
RefreshSizes();
//Send OnElementGrabbed Event
if (_reorderableList.OnElementGrabbed != null)
{
_reorderableList.OnElementGrabbed.Invoke(new ReorderableList.ReorderableListEventStruct
{
DroppedObject = _draggingObject.gameObject,
IsAClone = _reorderableList.CloneDraggedObject,
SourceObject = _reorderableList.CloneDraggedObject ? gameObject : _draggingObject.gameObject,
FromList = _reorderableList,
FromIndex = _fromIndex,
});
}
_isDragging = true;
}
#endregion
#region IDragHandler Members
public void OnDrag(PointerEventData eventData)
{
if (!_isDragging)
return;
//Set dragging object on cursor
_draggingObject.position = eventData.position;
//Check everything under the cursor to find a ReorderableList
EventSystem.current.RaycastAll(eventData, _raycastResults);
for (int i = 0; i < _raycastResults.Count; i++)
{
_currentReorderableListRaycasted = _raycastResults[i].gameObject.GetComponent<ReorderableList>();
if (_currentReorderableListRaycasted != null)
{
break;
}
}
//If nothing found or the list is not dropable, put the fake element outsite
if (_currentReorderableListRaycasted == null || _currentReorderableListRaycasted.IsDropable == false)
{
RefreshSizes();
_fakeElement.transform.SetParent(_reorderableList.DraggableArea, false);
}
//Else find the best position on the list and put fake element on the right index
else
{
if (_fakeElement.parent != _currentReorderableListRaycasted)
_fakeElement.SetParent(_currentReorderableListRaycasted.Content, false);
float minDistance = float.PositiveInfinity;
int targetIndex = 0;
float dist = 0;
for (int j = 0; j < _currentReorderableListRaycasted.Content.childCount; j++)
{
var c = _currentReorderableListRaycasted.Content.GetChild(j).GetComponent<RectTransform>();
if (_currentReorderableListRaycasted.ContentLayout is VerticalLayoutGroup)
dist = Mathf.Abs(c.position.y - eventData.position.y);
else if (_currentReorderableListRaycasted.ContentLayout is HorizontalLayoutGroup)
dist = Mathf.Abs(c.position.x - eventData.position.x);
else if (_currentReorderableListRaycasted.ContentLayout is GridLayoutGroup)
dist = (Mathf.Abs(c.position.x - eventData.position.x) + Mathf.Abs(c.position.y - eventData.position.y));
if (dist < minDistance)
{
minDistance = dist;
targetIndex = j;
}
}
RefreshSizes();
_fakeElement.SetSiblingIndex(targetIndex);
_fakeElement.gameObject.SetActive(true);
}
}
#endregion
#region IEndDragHandler Members
public void OnEndDrag(PointerEventData eventData)
{
_isDragging = false;
if (_draggingObject != null)
{
//If we have a, ReorderableList that is dropable
//Put the dragged object into the content and at the right index
if (_currentReorderableListRaycasted != null && _currentReorderableListRaycasted.IsDropable)
{
RefreshSizes();
_draggingObject.SetParent(_currentReorderableListRaycasted.Content, false);
_draggingObject.SetSiblingIndex(_fakeElement.GetSiblingIndex());
//Send OnelementDropped Event
if (_reorderableList.OnElementDropped != null)
{
_reorderableList.OnElementDropped.Invoke(new ReorderableList.ReorderableListEventStruct
{
DroppedObject = _draggingObject.gameObject,
IsAClone = _reorderableList.CloneDraggedObject,
SourceObject = _reorderableList.CloneDraggedObject ? gameObject : _draggingObject.gameObject,
FromList = _reorderableList,
FromIndex = _fromIndex,
ToList = _currentReorderableListRaycasted,
ToIndex = _fakeElement.GetSiblingIndex() - 1
});
}
}
//We don't have an ReorderableList
else
{
//If it's a clone, delete it
if (_reorderableList.CloneDraggedObject)
{
Destroy(_draggingObject.gameObject);
}
//Else replace the draggedObject to his first place
else
{
RefreshSizes();
_draggingObject.SetParent(_reorderableList.Content, false);
_draggingObject.SetSiblingIndex(_fromIndex);
}
}
}
//Delete fake element
if (_fakeElement != null)
Destroy(_fakeElement.gameObject);
}
#endregion
private void RefreshSizes()
{
Vector2 size = _draggingObjectOriginalSize;
if (_currentReorderableListRaycasted != null && _currentReorderableListRaycasted.IsDropable)
{
var firstChild = _currentReorderableListRaycasted.Content.GetChild(0);
if (firstChild != null)
{
size = firstChild.GetComponent<RectTransform>().rect.size;
}
}
_draggingObject.sizeDelta = size;
_fakeElementLE.preferredHeight = _draggingObjectLE.preferredHeight = size.y;
_fakeElementLE.preferredWidth = _draggingObjectLE.preferredWidth = size.x;
}
public void Init(ReorderableList reorderableList)
{
_reorderableList = reorderableList;
_rect = GetComponent<RectTransform>();
}
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 916e98f1b982a9a4082fcc45c87b66c5
timeCreated: 1446072130
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: