/// <inheritdoc />
    protected override void ValidateLayout()
    {
        // clear existing children
        for (int i = 0; i < this.rectTransform.childCount; ++i)
        {
            Destroy(this.rectTransform.GetChild(i).gameObject);
        }

        // Layout our objects
        float xOffset = 0;

        foreach (var item in this.inventory)
        {
            if (item == null)
            {
                continue;
            }

            PortableItemController obj = Instantiate(this.prefab, Vector3.zero, Quaternion.identity, this.rectTransform);
            obj.Initialize(item, this);
            RectTransform itemTransform = obj.transform as RectTransform;
            // Set the anchor to the bottom left.
            itemTransform.anchorMin = Vector2.zero;
            itemTransform.anchorMax = Vector2.zero;
            // Set the anchor position which is the offset from the anchor. We do
            // half first and half later to avoid overlapping due to the different
            // sizes of our objects.
            itemTransform.anchoredPosition = new Vector2(xOffset, 0f);
            itemTransform.pivot            = Vector2.zero;
            xOffset += item.worldSprite.rect.width + spacing;
        }
    }
    /// <inheritdoc />
    /// <remarks>
    /// Arranges the panel so that objects of the same type are clustered.
    /// </remarks>
    protected override void ValidateLayout()
    {
        // The underlying inventory should be a ShelfInventory
        ShelfInventory inventory = this.inventory as ShelfInventory;

        if (inventory == null)
        {
            throw new System.InvalidCastException("Inventory could not be cast to a ShelfInventory");
        }

        // clear existing children
        for (int i = 0; i < this.rectTransform.childCount; ++i)
        {
            Destroy(this.rectTransform.GetChild(i).gameObject);
        }

        // get all of our objects by type so we can sort them
        // FIXME: could keep the dict around if allocating it is slow
        var itemsByType = new Dictionary <string, List <PortableItem> >();

        foreach (PortableItem item in inventory)
        {
            if (item == null)
            {
                continue;
            }
            if (!itemsByType.ContainsKey(item.name))
            {
                itemsByType[item.name] = new List <PortableItem>();
            }
            itemsByType[item.name].Add(item);
        }

        // Layout our objects
        float xOffset = 0;

        foreach (var items in itemsByType.Values)
        {
            foreach (var item in items)
            {
                PortableItemController obj = Instantiate(this.prefab, Vector3.zero, Quaternion.identity, this.rectTransform);
                obj.Initialize(item, this);
                RectTransform itemTransform = obj.transform as RectTransform;
                // Set the anchor to the bottom left.
                itemTransform.anchorMin = Vector2.zero;
                itemTransform.anchorMax = Vector2.zero;
                // Set the anchor position which is the offset from the anchor. We do
                // half first and half later to avoid overlapping due to the different
                // sizes of our objects.
                itemTransform.anchoredPosition = new Vector2(xOffset + item.worldSprite.pivot.x, 0f);
                xOffset += item.worldSprite.rect.width;
            }
        }
    }
예제 #3
0
    /// <inheritdoc />
    /// <remarks>
    /// Takes the dragged object and attempts to delete it. If the object cannot
    /// be deleted the <c>HandleDropError</c> method will be called with the
    /// appropriate <c>InventoryError</c> value.
    /// </remarks>
    public void OnDrop(PointerEventData eventData)
    {
        PortableItemController obj  = eventData.pointerDrag?.GetComponent <PortableItemController>();
        PortableItem           item = obj?.Item;

        if (item == null || !whitelist.Matches(item.details))
        {
            this.HandleDropError(InventoryError.InvalidItem);
            return;
        }
        Destroy(eventData.pointerDrag);
        item.inventory.Remove(item);
    }
예제 #4
0
 /// <summary>
 /// Logic for re-validating the view of this inventory whenever the
 /// underlying inventory is changed.
 /// </summary>
 protected virtual void ValidateLayout()
 {
     // clear existing children
     for (int i = 0; i < this.rectTransform.childCount; ++i)
     {
         Destroy(this.rectTransform.GetChild(i).gameObject);
     }
     // instantiate new ones
     foreach (PortableItem item in this.inventory)
     {
         PortableItemController obj = Instantiate(this.prefab, Vector3.zero, Quaternion.identity, this.rectTransform);
         obj.Initialize(item, this);
     }
 }
예제 #5
0
    /// <inheritdoc />
    void Start()
    {
        // Clear existing weeds.
        for (int i = 0; i < this.rectTransform.childCount; ++i)
        {
            GameObject child = this.rectTransform.GetChild(i).gameObject;
            if (LawnController.IsWeed(child))
            {
                Destroy(child);
            }
        }

        // Add new weeds.
        for (int weedIndex = 0; weedIndex < this.lawn.Capacity; ++weedIndex)
        {
            PortableItem weed = this.lawn[weedIndex];
            if (weed == null)
            {
                continue;
            }

            Vector2 position;
            if (layout.FindPosition(this.rectTransform, out position))
            {
                PortableItemController obj = GameObject.Instantiate(this.prefab, this.rectTransform);
                obj.Initialize(weed);
                // Set the anchor to the center because rect.min, rect.max, and the
                // collider are all relative to the center.
                RectTransform itemTransform = obj.transform as RectTransform;
                itemTransform.anchorMin.Set(0.5f, 0.5f);
                itemTransform.anchorMax.Set(0.5f, 0.5f);
                itemTransform.anchoredPosition = position;
            }
            else
            {
                // Couldn't place it after 100 attempts; clear it from the inventory so
                // we don't count it in the score.
                this.lawn[weedIndex] = null;
            }
        }
        this.OrderLawn();
    }
예제 #6
0
    /// <inheritdoc />
    /// <remarks>
    /// Takes the dragged object and attempts to add it to the inventory. If
    /// the object cannot be added the <c>HandleDropError</c> method will be
    /// called with the appropriate <c>Error</c> value.
    /// </remarks>
    public override void OnDrop(PointerEventData eventData)
    {
        // if it's not a portable object what are we doing dragging it into our
        // inventory
        PortableItemController obj = eventData.pointerDrag?.GetComponent <PortableItemController>();

        if (obj == null)
        {
            this.HandleDropError(InventoryError.InvalidItem);
            return;
        }

        // Try to add it and check for errors.
        InventoryError err = this.inventory.Set(index, obj.Item);

        if (err != InventoryError.NoError)
        {
            this.HandleDropError(err);
            return;
        }
    }
예제 #7
0
    /// <summary>
    /// Logic for re-validating the view of this inventory whenever the
    /// underlying inventory is changed.
    /// </summary>
    protected override void ValidateLayout()
    {
        // clear existing children
        for (int i = 0; i < this.rectTransform.childCount; ++i)
        {
            Destroy(this.rectTransform.GetChild(i).gameObject);
        }
        // add a new one
        PortableItem item = this.inventory[this.index];

        if (item != null)
        {
            PortableItemController obj = Instantiate(this.prefab, Vector3.zero, Quaternion.identity, this.rectTransform);
            obj.Initialize(item, this);
            RectTransform itemTransform = obj.transform as RectTransform;
            // Center the object in the cell.
            itemTransform.pivot            = new Vector2(0.5f, 0.5f);
            itemTransform.anchorMin        = new Vector2(0.5f, 0.5f);
            itemTransform.anchorMax        = new Vector2(0.5f, 0.5f);
            itemTransform.anchoredPosition = Vector2.zero;
        }
    }
예제 #8
0
    /// <inheritdoc />
    private void Start()
    {
        // Clear all existing children.
        for (int i = 0; i < this.rectTransform.childCount; ++i)
        {
            Destroy(this.rectTransform.GetChild(i).gameObject);
        }

        // Layout the items.
        for (int itemIndex = 0; itemIndex < this.inventory.Capacity; ++itemIndex)
        {
            PortableItem item = this.inventory[itemIndex];
            if (item == null)
            {
                continue;
            }

            Vector2 position;
            if (this.layout.FindPosition(this.rectTransform, out position))
            {
                PortableItemController obj = Instantiate(this.prefab, this.rectTransform);
                obj.Initialize(item, this);
                // Set the anchor to the center because rect.min, rect.max, and the
                // collider are all relative to the center.
                RectTransform itemTransform = obj.transform as RectTransform;
                itemTransform.anchorMin.Set(0.5f, 0.5f);
                itemTransform.anchorMax.Set(0.5f, 0.5f);
                itemTransform.anchoredPosition = position;
                itemTransform.pivot.Set(0.5f, 0.5f);
            }
            else
            {
                // Couldn't place it after 100 attempts; clear it from the inventory so
                // we don't count it in the score.
                this.inventory[itemIndex] = null;
            }
        }
    }
예제 #9
0
 /// <summary>
 /// Initialize the cell before it can be used.
 /// </summary>
 /// <param name="inventory">The underlying inventory.</param>
 /// <param name="index">The inventory index this cell represents.</param>
 public void Initialize(PortableItemController prefab, Inventory inventory, int index)
 {
     this.prefab    = prefab;
     this.inventory = inventory;
     this.index     = index;
 }