Item Info Filter & Sorters

You can filter and sort the ItemInfoGrid, Inventory Grid, etc… in different ways.

Whether it is through code

//Sort the item once

// Sort the items in the inventory grid directly.
m_InventoryGrid.SortItemIndexes(itemInfoComparer);
//Don't forget to call draw to update
m_InventoryGrid.Draw()


//OR bind a filter/sorter to continously sort and filter.
// Bind a filter/sorter to the inventory grid (this can be done directly on the Item Info Grid).
m_InventoryGrid.BindGridFilterSorter(itemInfoSorterFilter);
//Don't forget to call draw to update
m_InventoryGrid.Draw()

 

Or using a filter sorter component and assiging it in the Inspector or UIDesigner.

Item Info filters and sorters are used by Item Info Grid, Inventory Grid and other components to filter and sort Item Infos from a list.

The available Item Info Filters and Sorters are:

  • Inventory Search Filter: Use an input field to search for an item in an Inventory Grid. It replaces the Bound Filter/Sorter by a sorter of choice while the search is in use.
  • Item Info Category Filter: Filter the item by its category.
  • Item Info Item Collection Filter: Filter the item by the item collection it is stored in.
  • Item Info Amount Sorter: Sorts the Item by amount.
  • Item Info Attribute Value Sorter: Sorts the Item by the value of an attribute. The attribute value must be “IComparable”.
  • Item Info Category Name Sorter: Sorts the Item by the Item Category name.
  • Item Info Name Sorter: Sorts the Item by the Item name.
  • Item Info Multi Filter Sorter: Combine multiple sorter and filters together. The order matters.

Custom Item Info Filters and Sorters.

Creating your custom filter and sorters for Item Info is possible with a little bit of code. Inherit the IFilterSorter<ItemInfo> interface or if you which to create a component inherit from one of those three MonoBehaviours: “Item Info Filter Sorter Base”, “Item Info Filter Base”, “Item Info Sorter Base”.

Filter Sorter Example

An example of multi filter sorter

 

public class ItemInfoMultiFilterSorter : ItemInfoFilterSorterBase
{
    [SerializeField] internal List<ItemInfoFilterSorterBase> m_GridFilters;
    public List<ItemInfoFilterSorterBase> GridFilters => m_GridFilters;
    
    public override ListSlice<ItemInfo> Filter(ListSlice<ItemInfo> input, ref ItemInfo[] outputPooledArray)
    {
        var list = input;
        for (int i = 0; i < m_GridFilters.Count; i++) {
            list = m_GridFilters[i].Filter(list, ref outputPooledArray);
        }
        return list;
    }
    public override bool CanContain(ItemInfo input)
    {
        for (int i = 0; i < m_GridFilters.Count; i++) {
            if (m_GridFilters[i].CanContain(input)) { continue; }
            return false;
        }
        return true;
    }
}

Sorter Example

An example of sorting by name:

public class ItemInfoNameSorter : ItemInfoSorterBase
{
    [SerializeField] protected bool m_Ascending = false;
    protected Comparer<ItemInfo> m_ItemNameComparer;
    public override Comparer<ItemInfo> Comparer => m_ItemNameComparer;
    protected override void Awake()
    {
        base.Awake();
        m_ItemNameComparer = Comparer<ItemInfo>.Create((i1, i2) =>
        {
            if (i1.Item == null && i2.Item == null) { return 0; }
            if (i1.Item == null) { return 1; }
            if (i2.Item == null) { return -1; }
            
            if (m_Ascending) {
                return i2.Item.name.CompareTo(i1.Item.name);
            } else {
                return i1.Item.name.CompareTo(i2.Item.name);
            }
        });
    }
}

Filter Example

An example of a simple Filter by Item Collection:

public class ItemInfoItemCollectionFilter : ItemInfoFilterBase
{
    [Tooltip("The item collections to show, Show all if null.")]
    [SerializeField]    protected ItemCollectionID[] m_ShowItemCollections;
    [Tooltip("The item collections that should not be drawn.")]
    [SerializeField]    protected ItemCollectionID[] m_HideItemCollections =
        { ItemCollectionPurpose.Loadout, ItemCollectionPurpose.Hide };

    // Return true to show the item, false to hide it.
    public override bool Filter(ItemInfo itemInfo)
    {
        var show = m_ShowItemCollections.Length == 0;
                for (int i = 0; i < m_ShowItemCollections.Length; i++) {
            if (!m_ShowItemCollections[i].Compare(itemInfo.ItemCollection)) { continue; }
            show = true;
            break;
        }
        if (show == false) { return false;}
                for (int i = 0; i < m_HideItemCollections.Length; i++) {
            if (m_HideItemCollections[i].Compare(itemInfo.ItemCollection)) { return false; }
        }
        return true;
    }
}