Esempio n. 1
0
        public void OnDrag(PointerEventData eventData)
        {
            // move the dragged object with the mouse
            if (dragging)
            {
                transform.position = eventData.position;
            }

            // highlight squares that can receive this component?
            Slot slot = GetSlotUnderMouse();

            if (slot != currentSlotOver)
            {
                if (slot)
                {
                    slot.OnDraggableEnter();
                }
                if (currentSlotOver)
                {
                    currentSlotOver.OnDraggableExit();
                }
                ObjectContainer oldContainer = currentSlotOver != null ? currentSlotOver.container : null;
                ObjectContainer newContainer = slot != null ? slot.container : null;
                if (oldContainer != null)
                {
                    oldContainer.OnDraggableExit();
                }
                if (newContainer != null)
                {
                    newContainer.OnDraggableEnter();
                }

                currentSlotOver = slot;
            }
        }
Esempio n. 2
0
        public void OnEndDrag(PointerEventData eventData)
        {
            if (!dragging)
            {
                return;
            }

            // raycast to find what we're dropping over
            Slot target = GetSlotUnderMouse();

            ObjectContainer containerTarget = null;
            ObjectContainer containerDrag   = slot.container;

            bool legal = true;

            // check that this move is OK with both containers - they can both veto it
            if (target)
            {
                containerTarget = target.container;

                // if there is a container and we can't drop into it for game logic reasons, cancel the drag
                if (containerTarget != null)
                {
                    legal = containerTarget.CanDrop(this, target);
                }
            }

            // we're Ok to move
            if (legal)
            {
                Slot fromSlot = slot;
                SwapWith(target,
                         containerDrag != null ? containerDrag.IsReadOnly() : true,
                         containerTarget != null ? containerTarget.IsReadOnly() : true);
                // game logic - let both containers know about the update
                if (containerTarget != null)
                {
                    containerTarget.Drop(target, containerDrag);
                }
                if (containerDrag != null)
                {
                    containerDrag.Drop(fromSlot, containerTarget);
                }
            }
            else
            {
                // allow us to make a sound or anything similar that the rejecting container has specified
                containerTarget.onDragFail.Invoke();
            }

            // return to our parent slot now
            transform.SetParent(slot.GetSlot());

            dragging = false;
            current  = null;

            // call the dragexit functions when we're placing, to unhighlight everything.
            // at the slot level...
            if (currentSlotOver)
            {
                currentSlotOver.OnDraggableExit();
            }
            // ...and at the container level
            ObjectContainer oldContainer = currentSlotOver != null ? currentSlotOver.container : null;

            if (oldContainer != null)
            {
                oldContainer.OnDraggableExit();
            }
            currentSlotOver = null;

            if (canvas)
            {
                canvas.sortingOrder = 0;
            }
        }