Ejemplo n.º 1
0
        public void OnBeginDrag(PointerEventData eventData)
        {
            // start dragging with the right mouse, and only if the container will allow it
            if (Input.GetMouseButton(mouseButton))
            {
                dragging = slot.container.CanDrag(this);
            }

            if (dragging)
            {
                current         = this;
                currentSlotOver = null;

                if (slot.container != null)
                {
                    slot.container.OnDragBegin();
                }

                // become a sibling of our slot's parent, so we're no longer part of the container (and don't disrupt the GridLayout)
                Transform p = slot.transform.parent.parent;
                while (p.GetComponent <Canvas>() == null && p != null)
                {
                    p = p.parent;
                }
                transform.SetParent(p);
                // move this to the very front of the UI, so the dragged element draws over everything
                transform.SetAsLastSibling();

                // lazy initialisation to find the canvas we're a part of
                if (canvas == null)
                {
                    Transform t = transform;
                    while (t != null && canvas == null)
                    {
                        t      = t.parent;
                        canvas = t.GetComponent <Canvas>();
                    }
                }
                // move that canvas forwards, so we're dragged on top of other items, and not behind them
                if (canvas)
                {
                    canvas.sortingOrder = 1;
                }
            }
        }
Ejemplo n.º 2
0
        void SwapWith(Slot _slot, bool readOnlySource, bool readOnlyTarget)
        {
            if (_slot == null)
            {
                // call a virtual function on the container that can eg spawn the item into the world
                if (slot.container != null)
                {
                    slot.container.ThrowAway(this);
                }

                // dispose if we're throwing the item away, no slot was under the mouse
                if (!readOnlySource)
                {
                    SetObject(null);
                }
            }
            else
            {
                // swap the two valid slot items around
                Draggable other = _slot.items?.FirstOrDefault();
                if (other)
                {
                    UnityEngine.Object o = obj;
                    if (!readOnlySource)
                    {
                        SetObject(other.obj);
                        if (other.obj != null && _slot != null)
                        {
                            _slot.onSlot.Invoke();
                        }
                    }
                    if (!readOnlyTarget)
                    {
                        other.SetObject(o, Input.mousePosition);
                        if (o != null && other.slot != null)
                        {
                            other.slot.onSlot.Invoke();
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
 public virtual void ThrowAway(Draggable dragged)
 {
 }
Ejemplo n.º 4
0
 public virtual bool CanDrop(Draggable dragged, Slot slot)
 {
     return(true);
 }
Ejemplo n.º 5
0
 // in the general class, we can drag and drop anything anywhere
 public virtual bool CanDrag(Draggable dragged)
 {
     return(true);
 }
Ejemplo n.º 6
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;
            }
        }