Example #1
0
        void CheckContainer()
        {
            int index = indexable.GetIndex();
            IContainer <Item> container = indexable.GetContainer();

            if (!container.IsEmpty(index))
            {
                GetComponent <Text>().text = container.GetElement(index).Name;
            }
            else
            {
                GetComponent <Text>().text = "";
            }
        }
        //public void Init(int index, Item item, int quantity, float baseWidth, float baseHeight)
        public void StartDragging(GameObject sender, GameObject currentRaycast)
        {
            // Store the source
            source = sender.GetComponent <IIndexable <Item> >();

            // Get the image component
            Image image = GetComponent <Image>();

            // Set icon and resize
            Item item = (source.GetContainer() as IContainer <Item>).GetElement(source.GetIndex());

            image.sprite = item.Icon;
            (transform as RectTransform).sizeDelta = new Vector2(baseWidth * item.SlotShape.x, baseHeight * item.SlotShape.y);
            (transform as RectTransform).pivot     = new Vector2(0f, 1f);
        }
Example #3
0
        void CheckContainer()
        {
            int index = indexable.GetIndex();

            // Get the container
            IContainer <Item> container = indexable.GetContainer();

            // Get the image component
            Image image = GetComponent <Image>();

            // If there an item at index then show its icon
            if (!container.IsEmpty(index))
            {
                image.enabled = true;

                Item item = container.GetElement(index);

                // If is not a big slot then show the icon as it is
                Debug.Log("Type:" + container.GetType().IsSubclassOf(typeof(IBigSlotContainer)));
                if (!item.TakesMoreSlots || !new List <System.Type>(container.GetType().GetInterfaces()).Contains(typeof(IBigSlotContainer)))
                {
                    image.sprite = item.Icon;
                }
                else // It's a big slot, lets do some puzzle
                {
                    if ((container as IBigSlotContainer).IsRoot(index))
                    {
                        image.sprite = SpriteUtil.GetSprite(item.Icon.texture, (int)item.SlotShape.x, (int)item.SlotShape.y, 0, 0);
                    }
                    else
                    {
                        Vector2 coords;

                        if ((container as IBigSlotContainer).TryGetCoordsInBigSlot(index, out coords))
                        {
                            image.sprite = SpriteUtil.GetSprite(item.Icon.texture, (int)item.SlotShape.x, (int)item.SlotShape.y, (int)coords.x, (int)coords.y);
                        }
                    }
                }
            }
            else // No item has been found, hide icon
            {
                image.sprite  = null;
                image.enabled = false;
            }
        }
        void CheckContainer()
        {
            int index = indexable.GetIndex();
            IContainer <Item> container = indexable.GetContainer() as IContainer <Item>;
            // Get the text
            Text text = GetComponent <Text>();

            // Check if there is an item at index
            if (!container.IsEmpty(index))
            {
                if (!container.GetElement(index).TakesMoreSlots || !new List <System.Type>(container.GetType().GetInterfaces()).Contains(typeof(IBigSlotContainer)))
                {
                    text.text = container.GetQuantity(index).ToString();
                }
                else
                {
                    // We show the quantity to the bottom right
                    Item    item = container.GetElement(index);
                    Vector2 coords;
                    (container as IBigSlotContainer).TryGetCoordsInBigSlot(index, out coords);

                    if (coords.x == item.SlotShape.x - 1 && coords.y == item.SlotShape.y - 1)
                    {
                        text.text = container.GetQuantity(index).ToString();
                    }
                    else
                    {
                        text.text = "";
                    }
                }
            }
            else
            {
                text.text = "";
            }
        }
        public void StopDragging(GameObject sender, GameObject currentRaycast)
        {
            if (currentRaycast == null)
            {
                ExitDragMode();
                return;
            }

            // Try move to another slot
            IIndexable <Item> dest = currentRaycast.GetComponent <IIndexable <Item> >();

            if (dest != null)
            {
                // Get destination data
                IContainer <Item> dstContainer = dest.GetContainer();
                int  dstIdx  = dest.GetIndex();
                Item dstItem = dstContainer.GetElement(dstIdx);

                // Get source data
                IContainer <Item> srcContainer = source.GetContainer();
                int  srcIdx  = source.GetIndex();
                Item srcItem = srcContainer.GetElement(source.GetIndex());


                if (srcContainer == dstContainer)              /*********************** Same container ( ex. from inventory to inventory ) *********************/
                {
                    if (dstItem == null || dstItem == srcItem) // Destination is empty or contains the same item
                    {
                        int  srcQ        = srcContainer.GetQuantity(srcIdx);
                        int  dstFreeRoom = srcContainer.GetFreeRoom(dstIdx, srcItem);
                        bool doNothing   = false;

                        // If there is no room or I'm not moving the item at all then do nothing
                        bool multiSlotSupport = new List <System.Type>(srcContainer.GetType().GetInterfaces()).Contains(typeof(IBigSlotContainer));
                        if ((dstFreeRoom <= 0) || ((dstIdx == srcIdx || (multiSlotSupport && (srcContainer as IBigSlotContainer).GetRootIndex(srcIdx) == (srcContainer as IBigSlotContainer).GetRootIndex(dstIdx)))))
                        {
                            doNothing = false;
                        }

                        if (!doNothing)
                        {
                            int max = Mathf.Min(dstFreeRoom, srcQ);
                            Debug.Log("Max:" + max);
                            if (max > 1 && Input.GetKey(KeyCode.LeftControl))
                            {
                                CounterSliderUI.Instance.Show(1, max /*srcContainer.GetQuantity(srcIdx)*/, (int a) =>
                                {
                                    // Move items
                                    srcContainer.Move(srcIdx, dstIdx, a);
                                }, () => { });
                            }
                            else
                            {
                                max = 1;
                                srcContainer.Move(srcIdx, dstIdx, max);
                            }
                        }
                    }
                }
                else /********** Different containers *****************/
                {
                    if (dstItem == null || dstItem == srcItem) // Destination is empty or contains the same item
                    {
                        int  srcQ        = srcContainer.GetQuantity(srcIdx);
                        int  dstFreeRoom = dstContainer.GetFreeRoom(dstIdx, srcItem);
                        bool doNothing   = false;

                        if (dstFreeRoom <= 0)
                        {
                            doNothing = true;
                        }

                        if (!doNothing)
                        {
                            int max = Mathf.Min(dstFreeRoom, srcQ);
                            Debug.Log("Max:" + max);
                            if (max > 1 && Input.GetKey(KeyCode.LeftControl))
                            {
                                CounterSliderUI.Instance.Show(1, max /*srcContainer.GetQuantity(srcIdx)*/, (int a) =>
                                {
                                    // Move items
                                    int q = dstContainer.Insert(dstIdx, srcItem, max);
                                    if (q > 0)
                                    {
                                        srcContainer.Remove(srcIdx, q);
                                    }
                                }, () => { });
                            }
                            else
                            {
                                max = 1;
                                // Move items
                                int q = dstContainer.Insert(dstIdx, srcItem, max);
                                if (q > 0)
                                {
                                    srcContainer.Remove(srcIdx, q);
                                }
                            }
                        }
                    }
                    else // Different items between different containers
                    {
                        int  srcQ        = srcContainer.GetQuantity(srcIdx);
                        int  dstFreeRoom = dstContainer.GetFreeRoom(dstIdx, srcItem);
                        bool doNothing   = false;

                        if (!doNothing)
                        {
                            int max = Mathf.Min(dstFreeRoom, srcQ);
                            Debug.Log("Max:" + max);
                            if (max > 1 && Input.GetKey(KeyCode.LeftControl))
                            {
                                CounterSliderUI.Instance.Show(1, max /*srcContainer.GetQuantity(srcIdx)*/, (int a) =>
                                {
                                    // Move items
                                    TrySwitch(srcIdx, dstIdx, srcItem, dstItem, max, srcContainer, dstContainer);
                                }, () => { });
                            }
                            else
                            {
                                max = 1;
                                TrySwitch(srcIdx, dstIdx, srcItem, dstItem, max, srcContainer, dstContainer);
                            }
                        }
                    }
                }
            }

            ExitDragMode();
        }