Ejemplo n.º 1
0
        /// <summary>
        /// Attempt to move item from to the destination
        /// </summary>
        /// <param name="destination"></param>
        /// <returns></returns>
        private bool AttemptSimpleTransfer(IDragDestination <T> destination, InputButton button)
        {
            //Debug.Log("In simple transfer");
            //Get the current item and number from the source container
            var draggingItem   = m_source.GetItem();
            var draggingNumber = 0;
            var maxCanReceive  = destination.MaxAcceptable(draggingItem);

            if (button == InputButton.Left)
            {
                draggingNumber = m_source.GetNumber();
            }
            else
            {
                draggingNumber = 1;
            }

            //Determine how many of the item can be transfered based on the destination container
            //var maxCanReceive = destination.MaxAcceptable(draggingItem);
            var numToTransfer = Mathf.Min(maxCanReceive, draggingNumber);

            //If there are items to be transfered, descrease count in source and add # of items to destination.
            if (numToTransfer > 0)
            {
                m_source.RemoveItems(numToTransfer);
                destination.AddItems(draggingItem, numToTransfer);
                return(false);
            }
            return(true);
        }
Ejemplo n.º 2
0
        private void DropItemIntoContainer(IDragDestination <T> destination)
        {
            //if the destination is the same as the source
            if (object.ReferenceEquals(destination, _itemSource))
            {
                return;
            }

            //Gets the IDragContainer of the destination
            var destinationContainer = destination as IDragContainer <T>;
            var sourceContainer      = _itemSource as IDragContainer <T>;

            // Swap won't be possible
            //if the destination or the source are null
            //or the item is null
            //or the object of the source and destination are the same
            //then attempt a transfer
            if (destinationContainer == null || sourceContainer == null ||
                destinationContainer.GetItem() == null ||
                object.ReferenceEquals(destinationContainer.GetItem(), sourceContainer.GetItem()))
            {
                AttemptItemTransfer(destination);
                return;
            }

            //if the conditions above are not met then attempt to swap the items
            AttemptSwap(destinationContainer, sourceContainer);
        }
Ejemplo n.º 3
0
        void IEndDragHandler.OnEndDrag(PointerEventData eventData)
        {
            //Sets the position to original
            transform.position = _startPosition;
            //Canvas needs to block raycasts again
            GetComponent <CanvasGroup>().blocksRaycasts = true;
            //sets us to the original parent
            transform.SetParent(_originalParent, true);

            //Creates a destination object
            IDragDestination <T> container = null;

            //if we are not over this current object
            if (!EventSystem.current.IsPointerOverGameObject())
            {
                //set the canvas as our destination
                container = _parentCanvas.GetComponent <IDragDestination <T> >();
            }
            else
            {
                //Get the container we are hovering over
                if (eventData.pointerEnter)
                {
                    container = eventData.pointerEnter.GetComponentInParent <IDragDestination <T> >();
                }
            }

            //if we have a container object then drop our item into the new container.
            if (container != null)
            {
                DropItemIntoContainer(container);
            }
        }
Ejemplo n.º 4
0
        private void DropItemIntoContainer(IDragDestination <T> destination)
        {
            if (ReferenceEquals(destination, _source))
            {
                return;
            }

            if (_source is EquipmentSlotUi sourceEquipmentSlot && sourceEquipmentSlot.IsLocked())
            {
                return;
            }

            if (destination is EquipmentSlotUi destEquipmentSlot && destEquipmentSlot.IsLocked())
            {
                return;
            }

            var destinationContainer = destination as IDragContainer <T>;
            var sourceContainer      = _source as IDragContainer <T>;

            // Swap won't be possible
            if (destinationContainer == null || sourceContainer == null ||
                destinationContainer.GetItem() == null ||
                ReferenceEquals(destinationContainer.GetItem(), sourceContainer.GetItem()))
            {
                AttemptSimpleTransfer(destination);
                return;
            }

            AttemptSwap(destinationContainer, sourceContainer);
        }
Ejemplo n.º 5
0
        private bool AttemptSimpleTransfer(IDragDestination <T> destination)
        {
            var draggingItem   = source.GetItem();
            var draggingNumber = source.GetNumber();

            var acceptable = destination.MaxAcceptable(draggingItem);
            var toTransfer = Mathf.Min(acceptable, draggingNumber);

            if (toTransfer > 0)
            {
                source.RemoveItems(toTransfer);
                destination.AddItems(draggingItem, toTransfer);
                return(false);
            }

            return(true);
        }
Ejemplo n.º 6
0
        private void DropItemIntoContainer(IDragDestination <T> destination)
        {
            if (ReferenceEquals(destination, source))
            {
                return;
            }
            var destinationContainer = destination as IDragContainer <T>;
            var sourceContainer      = source as IDragContainer <T>;

            // swap won't be possible
            if (destinationContainer == null || sourceContainer == null || destinationContainer.GetItem() == null ||
                ReferenceEquals(destinationContainer.GetItem(), sourceContainer.GetItem()))
            {
                AttemptSimpleTransfer(destination);
                return;
            }

            AttemptSwap(destinationContainer, sourceContainer);
        }
Ejemplo n.º 7
0
        private bool AttemptItemTransfer(IDragDestination <T> destination)
        {
            T   draggingItem   = _itemSource.GetItem();
            int draggingNumber = _itemSource.GetNumber();

            int acceptable = destination.MaxAcceptable(draggingItem);
            int toTransfer = Mathf.Min(acceptable, draggingNumber);

            //if we have items to transfer
            if (toTransfer > 0)
            {
                //remove items from the source
                _itemSource.RemoveItems(toTransfer);

                //and add them to the destination
                destination.AddItems(draggingItem, toTransfer);
                return(false);
            }

            return(true);
        }
Ejemplo n.º 8
0
        private void DropItemIntoContainer(IDragDestination <T> destination, InputButton button)
        {
            //check if destination is the same as source.  If so, do nothing and return
            if (object.ReferenceEquals(destination, m_source))
            {
                return;
            }

            var destinationContainer = destination as IDragContainer <T>;   //Cast destination to drag container
            var sourceContainer      = m_source as IDragContainer <T>;      //Cast source to drag container

            //Check if swap is not possible. Must satisfy one of the following:
            //Either the source or destination are not containers
            //Destination is a container but does not contain and item (i.e. its empty)
            //Destination and source are both containers and have the same item
            if (destinationContainer == null || sourceContainer == null || destinationContainer.GetItem() == null || object.ReferenceEquals(destinationContainer.GetItem(), sourceContainer.GetItem()))
            {
                //Can't swap but try to move item from source to destination.
                AttemptSimpleTransfer(destination, button);
                return;
            }

            AttemptSwap(destinationContainer, sourceContainer);
        }