예제 #1
0
        public void OnBeginDrag(PointerEventData eventData)
        {
            if(this.itemsAreDraggable)
            {
                foreach(GameObject go in eventData.hovered)
                {
                    UIInventoryGridEntry entry = go.GetComponent<UIInventoryGridEntry>();

                    if(entry != null)
                    {

                        this.entryBeingDragged = entry;
                        this.startPosition = entryBeingDragged.rectTransform.anchoredPosition;
                        this.dragOffset = this.startPosition - new Vector2(Input.mousePosition.x, Input.mousePosition.y);
                        break;
                    }
                }
            }

            this.UpdateDropzones();
        }
예제 #2
0
        public void OnPointerDown(PointerEventData eventData)
        {
            foreach(GameObject go in eventData.hovered)
            {
                UIInventoryGridEntry uiEntry = go.GetComponent<UIInventoryGridEntry>();

                if(uiEntry != null)
                {
                    this.selectedEntry = uiEntry;
                    OnHighlight(uiEntry.entry);
                    break;
                }
            }
        }
예제 #3
0
        /**
         * Updates dropzone states
         */
        public void UpdateDropzones(UIInventoryGridEntry uiEntry = null)
        {
            // Define the entry rect, including current position
            Rect entryRect = new Rect();

            if(uiEntry == null)
            {
                uiEntry = this.entryBeingDragged;
            }

            if(uiEntry != null)
            {
                entryRect = uiEntry.rectTransform.rect;

                entryRect.x = uiEntry.rectTransform.anchoredPosition.x;
                entryRect.y = uiEntry.rectTransform.anchoredPosition.y;

                // TODO: Why is this necessary? A Y coordinate is inverted somewhere
                entryRect.y -= this.slotSize * (uiEntry.entry.item.inventorySize.y - 1);

                // Clamp rect position to grid
                entryRect.x = (float) Math.Round(entryRect.x / this.slotSize) * this.slotSize;
                entryRect.y = (float) Math.Round(entryRect.y / this.slotSize) * this.slotSize;
            }

            // Look for dropzones
            for(int i = 0; i < this.transform.childCount; i++)
            {
                UIDropzone zone = this.transform.GetChild(i).GetComponent<UIDropzone>();

                if(zone != null)
                {
                    // Define the dropzone rect, including position
                    Rect zoneRect = zone.GetImage().rectTransform.rect;

                    zoneRect.x = zone.GetImage().rectTransform.anchoredPosition.x;
                    zoneRect.y = zone.GetImage().rectTransform.anchoredPosition.y;

                    // Set the dropzone hover state based on rect overlap
                    bool isHovering = entryRect.Overlaps(zoneRect);
                    zone.SetHover(isHovering);
                }
            }
        }
예제 #4
0
        public void OnEndDrag(PointerEventData eventData)
        {
            if(this.itemsAreDraggable && this.entryBeingDragged != null)
            {
                // If no zone was found, snap back to the start position
                Vector2 snapPosition = this.startPosition;

                // Look for dropzones
                for(int i = 0; i < this.transform.childCount; i++)
                {
                    UIDropzone zone = this.transform.GetChild(i).GetComponent<UIDropzone>();

                    // If the dropzone is hovered, set that to the snap position
                    if(zone != null && zone.GetHover())
                    {
                        snapPosition = zone.GetImage().rectTransform.anchoredPosition;
                        break;
                    }
                }

                // Get grid position from UI position
                int x = (int)Math.Round(snapPosition.x / this.slotSize);
                int y = -(int)Math.Round(snapPosition.y / this.slotSize);

                // Try to place the entry into the inventory grid
                // If this fails, reset the snap position
                if(!this.inventory.PlaceIntoGrid(x, y, this.entryBeingDragged.entry))
                {
                    snapPosition = this.startPosition;
                }

                this.entryBeingDragged.rectTransform.anchoredPosition = snapPosition;
            }

            this.entryBeingDragged = null;

            this.UpdateDropzones(this.selectedEntry);
        }