private void OnItemToggled(int index, bool isOn)
        {
            if (isOn)
            {
                UnityObject item = (UnityObject)listView.GetItem(index);
                if (item == null)
                {
                    OnObjectSelected?.Invoke(null);
                }
                else
                {
                    OnObjectSelected?.Invoke(item);
                }

                Dismiss();
            }
        }
예제 #2
0
        public void OnDrop(PointerEventData eventData)
        {
            if (!canReorderItems || !isDragging)
            {
                return;
            }

            float contentYPos = pointerLastYPos + scroll.content.anchoredPosition.y;
            int   dataIndex   = (int)contentYPos / (int)itemHeight;

            object target = listView.GetItem(dataIndex);

            if (target == null)
            {
                InvokeOnReparent(draggingItem, target, ItemDropAction.SetLastChild);
            }
            else
            {
                int   insertDirection  = 0;
                float relativePosition = contentYPos % itemHeight;
                if (relativePosition < siblingIndexModificationArea)
                {
                    insertDirection = -1;
                }
                else if (relativePosition > itemHeight - siblingIndexModificationArea)
                {
                    insertDirection = 1;
                }

                if (insertDirection != 0)
                {
                    if (insertDirection < 0 && dataIndex > 0)
                    {
                        object newTarget = listView.GetItem(dataIndex - 1);
                        if (newTarget != null)
                        {
                            target          = newTarget;
                            insertDirection = 1;
                        }
                    }
                    else if (insertDirection > 0 && dataIndex < listView.ItemCount - 1)
                    {
                        object newTarget = listView.GetItem(dataIndex + 1);
                        if (newTarget != null)
                        {
                            target          = newTarget;
                            insertDirection = -1;
                        }
                    }
                }

                // Dropped onto self.
                if (target == null || target == draggingItem)
                {
                    StopDragging();

                    return;
                }

                int targetIndex = listView.IndexOf(target);

                switch (insertDirection)
                {
                case 0:
                    if (InvokeOnReparenting(draggingItem, target, ItemDropAction.SetLastChild))
                    {
                        listView.RemoveItem(draggingItem);
                        InvokeOnReparent(draggingItem, target, ItemDropAction.SetLastChild);
                    }

                    break;

                case -1:
                    if (InvokeOnReparenting(draggingItem, target, ItemDropAction.SetPreviousSibling))
                    {
                        listView.MoveItem(draggingItem, targetIndex);
                        InvokeOnReparent(draggingItem, target, ItemDropAction.SetPreviousSibling);
                    }

                    break;

                case 1:
                    if (InvokeOnReparenting(draggingItem, target, ItemDropAction.SetNextSibling))
                    {
                        listView.MoveItem(draggingItem, targetIndex + 1);
                        InvokeOnReparent(draggingItem, target, ItemDropAction.SetNextSibling);
                    }

                    break;
                }

                SelectItemInternal(draggingItem);
            }

            draggingItem = null;
            StopDragging();
        }