using UnityEngine;
using UnityEngine.EventSystems;
namespace UIWidgets {
///
/// ListViewIcons drop support.
/// Receive drops from TreeView and ListViewIcons.
///
[AddComponentMenu("UI/UIWidgets/ListViewIconsDropSupport")]
[RequireComponent(typeof(ListViewIcons))]
public class ListViewIconsDropSupport : MonoBehaviour, IDropSupport>, IDropSupport {
ListViewIcons listView;
///
/// Current ListViewIcons.
///
/// ListViewIcons.
public ListViewIcons ListView {
get {
if (listView==null)
{
listView = GetComponent();
}
return listView;
}
}
///
/// The drop indicator.
///
[SerializeField]
protected ListViewDropIndicator DropIndicator;
#region IDropSupport>
///
/// Determines whether this instance can receive drop with the specified data and eventData.
///
/// true if this instance can receive drop with the specified data and eventData; otherwise, false.
/// Data.
/// Event data.
public bool CanReceiveDrop(TreeNode data, PointerEventData eventData)
{
var result = data.Nodes==null || data.Nodes.Count==0;
if (result)
{
var index = ListView.GetNearestIndex(eventData);
ShowDropIndicator(index);
}
return result;
}
///
/// Process dropped data.
///
/// Data.
/// Event data.
public void Drop(TreeNode data, PointerEventData eventData)
{
var index = ListView.GetNearestIndex(eventData);
var item = new ListViewIconsItemDescription() {
Name = data.Item.Name,
LocalizedName = data.Item.LocalizedName,
Icon = data.Item.Icon,
Value = data.Item.Value
};
AddItem(item, index);
// remove node from tree
data.Parent = null;
HideDropIndicator();
}
///
/// Process canceled drop.
///
/// Data.
/// Event data.
public void DropCanceled(TreeNode data, PointerEventData eventData)
{
HideDropIndicator();
}
#endregion
#region IDropSupport
///
/// Determines whether this instance can receive drop with the specified data and eventData.
///
/// true if this instance can receive drop with the specified data and eventData; otherwise, false.
/// Data.
/// Event data.
public bool CanReceiveDrop(ListViewIconsItemDescription data, PointerEventData eventData)
{
var index = ListView.GetNearestIndex(eventData);
ShowDropIndicator(index);
return true;
}
///
/// Process dropped data.
///
/// Data.
/// Event data.
public void Drop(ListViewIconsItemDescription data, PointerEventData eventData)
{
var index = ListView.GetNearestIndex(eventData);
AddItem(data, index);
HideDropIndicator();
}
///
/// Process canceled drop.
///
/// Data.
/// Event data.
public void DropCanceled(ListViewIconsItemDescription data, PointerEventData eventData)
{
HideDropIndicator();
}
#endregion
///
/// Shows the drop indicator.
///
/// Index.
void ShowDropIndicator(int index)
{
if (DropIndicator!=null)
{
DropIndicator.Show(index, ListView);
}
}
///
/// Hides the drop indicator.
///
void HideDropIndicator()
{
if (DropIndicator!=null)
{
DropIndicator.Hide();
}
}
///
/// Add item to ListViewIcons.
///
/// Item.
/// Index.
void AddItem(ListViewIconsItemDescription item, int index)
{
if (index==-1)
{
ListView.DataSource.Add(item);
}
else
{
ListView.DataSource.Insert(index, item);
}
}
}
}