using UnityEngine;
using UnityEngine.EventSystems;
using UIWidgets;
namespace UIWidgetsSamples {
///
/// TileViewSample drag support.
///
[RequireComponent(typeof(TileViewComponentSample))]
public class TileViewSampleDragSupport : DragSupport {
[SerializeField]
public TileViewSample TileView;
[SerializeField]
public TileViewComponentSample DragInfo;
int index = 0;
protected virtual void Start()
{
if (DragInfo!=null)
{
DragInfo.gameObject.SetActive(false);
}
}
///
/// Set Data, which will be passed to Drop component.
///
/// Current event data.
protected override void InitDrag(PointerEventData eventData)
{
var component = GetComponent();
Data = component.Item;
index = component.Index;
ShowDragInfo();
}
///
/// Shows the drag info.
///
protected virtual void ShowDragInfo()
{
if (DragInfo==null)
{
return ;
}
DragInfo.transform.SetParent(DragPoint, false);
DragInfo.transform.localPosition = new Vector3(-5, 5, 0);
DragInfo.SetData(Data);
DragInfo.gameObject.SetActive(true);
}
///
/// Hides the drag info.
///
protected virtual void HideDragInfo()
{
if (DragInfo==null)
{
return ;
}
DragInfo.gameObject.SetActive(false);
}
///
/// Called when drop completed.
///
/// true if Drop component received data; otherwise, false.
public override void Dropped(bool success)
{
HideDragInfo();
// remove used from current ListViewIcons.
if (success && (TileView!=null))
{
var first_index = TileView.DataSource.IndexOf(Data);
var last_index = TileView.DataSource.LastIndexOf(Data);
if (index==first_index)
{
TileView.DataSource.RemoveAt(index);
}
else if ((index+1)==last_index)
{
TileView.DataSource.RemoveAt(index+1);
}
else
{
TileView.DataSource.Remove(Data);
}
}
base.Dropped(success);
}
}
}