using UnityEngine; using UnityEngine.EventSystems; namespace UIWidgets { /// /// ListViewIcons drag support. /// [AddComponentMenu("UI/UIWidgets/ListViewIconsDragSupport")] [RequireComponent(typeof(ListViewIconsItemComponent))] public class ListViewIconsDragSupport : DragSupport { /// /// ListViewIcons. /// [SerializeField] public ListViewIcons ListView; /// /// The drag info. /// [SerializeField] public ListViewIconsItemComponent DragInfo; int index = 0; /// /// Start this instance. /// 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 && (ListView!=null)) { var first_index = ListView.DataSource.IndexOf(Data); var last_index = ListView.DataSource.LastIndexOf(Data); if (index==first_index) { ListView.DataSource.RemoveAt(index); } else if ((index+1)==last_index) { ListView.DataSource.RemoveAt(index+1); } else { ListView.DataSource.Remove(Data); } } base.Dropped(success); } } }