Back to Home

ESO Lua File v101041

ingame/inventory/inventorywallet.lua

[◄ back to folders ]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
local DATA_TYPE_CURRENCY_ITEM = 1
local LIST_ENTRY_HEIGHT = 52
local CURRENCY_LOCATION_ALL = CURRENCY_LOCATION_MAX_VALUE + 1
-------------------
--InventoryWallet Manager
-------------------
local InventoryWalletManager = ZO_Object:Subclass()
function InventoryWalletManager:New(...)
    local manager = ZO_Object.New(self)
    manager:Initialize(...)
    return manager
end
function InventoryWalletManager:Initialize(container)
    self.container = container
    self.money = GetControl(container, "InfoBarMoney")
    self.freeSlotsLabel = GetControl(container, "InfoBarFreeSlots")
    self.list = GetControl(container, "List")
    ZO_ScrollList_AddDataType(self.list, DATA_TYPE_CURRENCY_ITEM, "ZO_InventoryWalletSlot", LIST_ENTRY_HEIGHT, function(control, data) self:SetUpEntry(control, data) end, nil, nil, ZO_InventorySlot_OnPoolReset)
    self.sortHeadersControl = container:GetNamedChild("SortBy")
    self.sortHeaders = ZO_SortHeaderGroup:New(self.sortHeadersControl, true)
    local function OnSortHeaderClicked(key, order)
        self.sortKey = key
        self.sortOrder = order
        self:ApplySort()
    end
    self.sortHeaders:RegisterCallback(ZO_SortHeaderGroup.HEADER_CLICKED, OnSortHeaderClicked)
    self.sortHeaders:AddHeadersFromContainer()
    self.sortHeaders:SelectHeaderByKey("name")
    self:RegisterEvents()
    WALLET_FRAGMENT = ZO_FadeSceneFragment:New(ZO_InventoryWallet)
    WALLET_FRAGMENT:RegisterCallback("StateChange",  function(oldState, newState)
                                                            if newState == SCENE_FRAGMENT_SHOWING then
                                                                self:UpdateList()
                                                                self:UpdateFreeSlots()
                                                                                self:RefreshCurrency()
                                                            end
                                                        end)
end
function InventoryWalletManager:AddFilterBarButton(currencyLocation, normal, pressed, highlight)
    local button =
    {
        descriptor = currencyLocation,
        normal = normal,
        pressed = pressed, 
        highlight = highlight, 
        callback = function()
            self:OnFilterSelected(currencyLocation)
        end,
        tooltipText = self:GetCurrencyLocationName(currencyLocation),
    }
    ZO_MenuBar_AddButton(self.filterBarControl, button)
end
function InventoryWalletManager:InitializeFilterBar()
    self.filterBarControl = self.container:GetNamedChild("Tabs")
    self.filterBarLabel = self.filterBarControl:GetNamedChild("Active")
    self:AddFilterBarButton(CURRENCY_LOCATION_ACCOUNT, "EsoUI/Art/Inventory/inventory_currencyTab_accountWide_up.dds", "EsoUI/Art/Inventory/inventory_currencyTab_accountWide_down.dds", "EsoUI/Art/Inventory/inventory_currencyTab_accountWide_over.dds")
    self:AddFilterBarButton(CURRENCY_LOCATION_CHARACTER, "EsoUI/Art/Inventory/inventory_currencyTab_onCharacter_up.dds", "EsoUI/Art/Inventory/inventory_currencyTab_onCharacter_down.dds", "EsoUI/Art/Inventory/inventory_currencyTab_onCharacter_over.dds")
    self:AddFilterBarButton(CURRENCY_LOCATION_ALL, "EsoUI/Art/Inventory/inventory_tabIcon_all_up.dds", "EsoUI/Art/Inventory/inventory_tabIcon_all_down.dds", "EsoUI/Art/Inventory/inventory_tabIcon_all_over.dds")
    local SKIP_ANIMATION = true
    ZO_MenuBar_SelectDescriptor(self.filterBarControl, CURRENCY_LOCATION_ALL, SKIP_ANIMATION)
end
function InventoryWalletManager:GetCurrencyLocationName(currencyLocation)
    return currencyLocation == CURRENCY_LOCATION_ALL and GetString(SI_INVENTORY_WALLET_ALL_FILTER) or GetString("SI_CURRENCYLOCATION", currencyLocation)
end
function InventoryWalletManager:OnFilterSelected(currencyLocation)
    if self.currencyLocationFilter ~= currencyLocation then
        self.currencyLocationFilter = currencyLocation
        self.filterBarLabel:SetText(self:GetCurrencyLocationName(currencyLocation))
        self:UpdateList()
    end
end
function InventoryWalletManager:RegisterEvents()
    local function RefreshCurrencies()
        if not self.container:IsHidden() then
            self:RefreshCurrency()
            self:UpdateList()
        end
    end
    ZO_InventoryWallet:RegisterForEvent(EVENT_CURRENCY_UPDATE, RefreshCurrencies)
    ZO_InventoryWallet:RegisterForEvent(EVENT_CURRENCY_CAPS_CHANGED, RefreshCurrencies)
    local function UpdateFreeSlots()
        if WALLET_FRAGMENT:IsShowing() then
            self:UpdateFreeSlots()
        end
    end
    ZO_InventoryWallet:RegisterForEvent(EVENT_INVENTORY_FULL_UPDATE, UpdateFreeSlots)
    ZO_InventoryWallet:RegisterForEvent(EVENT_INVENTORY_SINGLE_SLOT_UPDATE, UpdateFreeSlots)
end
do
    local FORMAT_EXTRA_OPTIONS =
    {
        showCap = true,
    }
    function InventoryWalletManager:SetUpEntry(control, data)
        local nameControl = GetControl(control, "Name")
        nameControl:SetText(zo_strformat(SI_CURRENCY_NAME_FORMAT, data.name))
        local amountControl = GetControl(control, "Amount")
        FORMAT_EXTRA_OPTIONS.currencyLocation = GetCurrencyPlayerStoredLocation(data.currencyType)
        amountControl:SetText(ZO_Currency_FormatKeyboard(data.currencyType, data.amount, ZO_CURRENCY_FORMAT_AMOUNT_ICON, FORMAT_EXTRA_OPTIONS))
    end
end
local sortKeys =
{
    name = { },
    amount = { tiebreaker = "name", isNumeric = true },
}
function InventoryWalletManager:SortData()
    local scrollData = ZO_ScrollList_GetDataList(self.list)
    self.sortFunction = self.sortFunction or function(entry1, entry2)
        local sortKey = self.sortKey
        local sortOrder = self.sortOrder
        return ZO_TableOrderingFunction(entry1.data, entry2.data, sortKey, sortKeys, sortOrder)
    end
    table.sort(scrollData, self.sortFunction)
end
function InventoryWalletManager:ApplySort()
    self:SortData()
end
function InventoryWalletManager:RefreshCurrency()
    ZO_CurrencyControl_SetSimpleCurrency(self.money, CURT_MONEY, GetCurrencyAmount(CURT_MONEY, CURRENCY_LOCATION_CHARACTER), ZO_KEYBOARD_CURRENCY_OPTIONS)
end
function InventoryWalletManager:UpdateFreeSlots()
    local numUsedSlots, numSlots = PLAYER_INVENTORY:GetNumSlots(INVENTORY_BACKPACK)
    if(numUsedSlots < numSlots) then
        self.freeSlotsLabel:SetText(zo_strformat(SI_INVENTORY_BACKPACK_REMAINING_SPACES, numUsedSlots, numSlots))
    else
        self.freeSlotsLabel:SetText(zo_strformat(SI_INVENTORY_BACKPACK_COMPLETELY_FULL, numUsedSlots, numSlots))
    end
end
function InventoryWalletManager:UpdateList()
    local scrollData = ZO_ScrollList_GetDataList(self.list)
    local IS_PLURAL = false
    local IS_UPPER = false
    for currencyType = CURT_ITERATION_BEGIN, CURT_ITERATION_END do
        if IsCurrencyValid(currencyType) then
            local currencyPlayerStoredLocation = GetCurrencyPlayerStoredLocation(currencyType)
            if self.currencyLocationFilter == CURRENCY_LOCATION_ALL or currencyPlayerStoredLocation == self.currencyLocationFilter then
                local entryData =
                {
                    name = GetCurrencyName(currencyType, IS_PLURAL, IS_UPPER),
                    currencyType = currencyType,
                    amount = GetCurrencyAmount(currencyType, currencyPlayerStoredLocation)
                }
                table.insert(scrollData, ZO_ScrollList_CreateDataEntry(DATA_TYPE_CURRENCY_ITEM, entryData))
            end
        end
    end
    self:ApplySort()
    self.sortHeadersControl:SetHidden(#scrollData == 0)
end
    INVENTORY_WALLET = InventoryWalletManager:New(control)
end