/// <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;
            }
        }
    }
 /// <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);
     }
 }
Example #4
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();
    }
Example #5
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;
        }
    }
Example #6
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;
            }
        }
    }