Example #1
0
        public void SnapTo(UIDrop slot, bool ensureRotation = false)
        {
            Init();

            if (slot.ParentWithContainer)
            {
                transform.SetParent(slot.transform);
            }

            if (slot.RestoreAnchorAndPivot)
            {
                RestoreAnchorPivot();
            }

            if (slot.SnapAnchoring)
            {
                RectTransform.anchoredPosition3D = (slot.transform as RectTransform).anchoredPosition3D;
            }

            if (slot.SnapTransformPosition)
            {
                transform.position = slot.transform.position;
            }

            if (ensureRotation)
            {
                transform.rotation = slot.transform.rotation;
            }
        }
Example #2
0
        private void checkHover(PointerEventData eventData)
        {
            hoveredRaycastResults.Clear();
            EventSystem.current.RaycastAll(eventData, hoveredRaycastResults);
            foreach (RaycastResult result in hoveredRaycastResults)
            {
                UIDrop targetSlot = result.gameObject.GetComponent <UIDrop>();
                if (targetSlot != null)
                {
                    //add new hovered object
                    if (!hovered.Contains(targetSlot))
                    {
                        hovered.Add(targetSlot);
                        targetSlot.OnHoverStart();
                        if (targetSlot.Stored != null)
                        {
                            if (!targetSlot.Stored.IsInit)
                            {
                                targetSlot.Stored.Init();
                            }

                            if (targetSlot.Stored.CanvasGroup != null)
                            {
                                targetSlot.Stored.CanvasGroup.blocksRaycasts = false;
                            }
                        }
                    }
                }
            }

            //clean not hovered old objects
            for (int i = hovered.Count - 1; i >= 0; i--)
            {
                RaycastResult found = hoveredRaycastResults.Find(item => item.gameObject == hovered[i].gameObject);
                if (!found.isValid)
                {
                    hovered[i].OnHoverEnd();
                    if (hovered[i].Stored != null)
                    {
                        if (!hovered[i].Stored.IsInit)
                        {
                            hovered[i].Stored.Init();
                        }

                        if (hovered[i].Stored.CanvasGroup != null)
                        {
                            hovered[i].Stored.CanvasGroup.blocksRaycasts = true;
                        }
                    }

                    hovered.RemoveAt(i);
                }
            }
        }
Example #3
0
 protected virtual void OnSlotChanged(UIDrop from, UIDrop to)
 {
 }
Example #4
0
        public void OnEndDrag(PointerEventData eventData)
        {
            if (Dragging[eventData.pointerId].OnEndDrag)
            {
                return;
            }

            //Debug.Log("OnEndDrag " + eventData.pointerId);

            UIDrop droppedTarget = null;

            foreach (UIDrop targetSlot in hovered)
            {
                //Debug.Log("Hovering " + targetSlot.transform.name);

                if (targetSlot != CurrentContainer && targetSlot.CanDrop(eventData, this)
                    ) //prevent self-container swap and drop check
                {
                    UIDrag other = targetSlot.Stored;
                    UIDrop destinationContainer = targetSlot;
                    UIDrop originContainer      = CurrentContainer;

                    if (targetSlot.SwapItems)
                    {
                        if (targetSlot.Stored != null)
                        {
                            other.SnapTo(originContainer, true); //move other UIDrag to origin UIDrop
                            other.CurrentContainer           = originContainer;
                            other.CanvasGroup.blocksRaycasts = true;
                            originContainer.Stored           = other;

                            destinationContainer.Stored = null;
                            CurrentContainer            = null;

                            Debug.Log("Swapped");
                            Debug.Log("Other[" + other.name + "] container: " + other.CurrentContainer.name +
                                      " | Origin Container[" + originContainer.name + "]: " +
                                      originContainer.Stored.name);

                            other.OnSlotChanged(destinationContainer, originContainer);
                        }
                        else
                        {
                            if (originContainer != null)
                            {
                                originContainer.Stored = null;
                            }

                            CurrentContainer = null;
                        }
                    }
                    else
                    {
                        if (originContainer != null)
                        {
                            originContainer.Stored = null; //remove reference from container
                        }

                        CurrentContainer = null;
                    }

                    droppedTarget = destinationContainer;
                    OnSlotChanged(originContainer, destinationContainer);
                    break;
                }

                Debug.Log("Valid container[" + targetSlot.name + "] = " + (targetSlot != CurrentContainer) +
                          " | Can be dropped = " + targetSlot.CanDrop(eventData, this));
            }

            if (droppedTarget == null)
            {
                Dragging[eventData.pointerId].OnDrop = true; //prevent OnDrop execution when cant drop
                if (DestroyOnDropInvalid)
                {
                    if (CurrentContainer != null)
                    {
                        CurrentContainer.Stored = null; //remove reference from container
                    }

                    Destroy(gameObject);
                    return;
                }

                if (ReturnoToStart)
                {
                    RestoreAll(ReturnWillBeAnimated); //back to slot position
                }
            }

            forceCleanAllHover();
            CanvasGroup.blocksRaycasts = true;
            OnDragEnd(eventData);
            Dragging[eventData.pointerId].OnEndDrag = true;

            if (droppedTarget != null)
            {
                droppedTarget.OnDrop(eventData);
            }
        }