Thread: "sort by level"
View Single Post
07/17/14, 11:54 AM   #6
merlight
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 671
From the description and images I think it sorts items in inventory in a more natural way. Like swords always before bows and staves, and such. I don't use it, but many times cursed why I have gloves made from various materials scattered all over my bag That's how it could be useful.

Anyway, when you asked the question, I took it as an excersise. The UI code is probably a mess, I was just picking pieces to make it work, but I managed to add a "Level" sorting header to the inventory, and with the way I've overridden sortFn, you can replace orderByItemLevel with custom comparison function.

Lua Code:
  1. local function orderByItemLevel(data1, data2)
  2.     local lv1 = GetItemLevel(data1.bagId, data1.slotIndex)
  3.     local lv2 = GetItemLevel(data2.bagId, data2.slotIndex)
  4.     return lv1 < lv2
  5. end
  6.  
  7. local function initializeCustomInventorySortFn(inventory)
  8.     inventory.sortFn = function(entry1, entry2)
  9.         local sortKey = inventory.currentSortKey
  10.         local sortOrder = inventory.currentSortOrder
  11.         local res
  12.         if type(sortKey) == "function" then
  13.             if inventory.currentSortOrder == ZO_SORT_ORDER_UP then
  14.                 res = sortKey(entry1.data, entry2.data)
  15.             else
  16.                 res = sortKey(entry2.data, entry1.data)
  17.             end
  18.         else
  19.             local sortKeys = ZO_Inventory_GetDefaultHeaderSortKeys()
  20.             res = ZO_TableOrderingFunction(entry1.data, entry2.data, sortKey, sortKeys, sortOrder)
  21.         end
  22.         return res
  23.     end
  24. end
  25.  
  26. -- call from EVENT_ADD_ON_LOADED
  27. local function experimentalSortInventoryByLevel()
  28.     local invSortBy = ZO_PlayerInventorySortBy
  29.     local nameHeader = invSortBy:GetNamedChild("Name")
  30.     local levelHeader = CreateControlFromVirtual("$(parent)Level", invSortBy, "ZO_SortHeader")
  31.     levelHeader:SetAnchor(RIGHT, nameHeader, RIGHT, -5, 0)
  32.     levelHeader:SetDimensions(80, 20)
  33.     ZO_SortHeader_Initialize(levelHeader, "Level", orderByItemLevel,
  34.                              ZO_SORT_ORDER_UP, TEXT_ALIGN_RIGHT, "ZoFontHeader")
  35.  
  36.     local inventory = PLAYER_INVENTORY.inventories[INVENTORY_BACKPACK]
  37.     initializeCustomInventorySortFn(inventory)
  38.     inventory.sortHeaders:AddHeader(levelHeader)
  39. end
  Reply With Quote