-
Notifications
You must be signed in to change notification settings - Fork 4
/
AF_FilterBar.lua
283 lines (224 loc) · 10.1 KB
/
AF_FilterBar.lua
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
local AF = AdvancedFilters
AF.AF_FilterBar = ZO_Object:Subclass()
local AF_FilterBar = AF.AF_FilterBar
function AF_FilterBar:New(inventoryName, groupName, subfilterNames)
local obj = ZO_Object.New(self)
obj:Initialize(inventoryName, groupName, subfilterNames)
return obj
end
function AF_FilterBar:Initialize(inventoryName, groupName, subfilterNames)
--get upper anchor position for subfilter bar
local _,_,_,_,_,offsetY = ZO_PlayerInventorySortBy:GetAnchor()
--parent for the subfilter bar control
local parents = {
["PlayerInventory"] = ZO_PlayerInventory,
["PlayerBank"] = ZO_PlayerBank,
["GuildBank"] = ZO_GuildBank,
["VendorSell"] = ZO_StoreWindow,
["CraftBag"] = ZO_CraftBag,
}
local parent = parents[inventoryName]
--unique identifier
self.name = inventoryName .. groupName
self.control = WINDOW_MANAGER:CreateControlFromVirtual("AF_FilterBar" .. self.name, parent, "AF_Base")
self.control:SetAnchor(TOPLEFT, parent, TOPLEFT, 0, offsetY)
self.label = self.control:GetNamedChild("Label")
self.label:SetModifyTextType(MODIFY_TEXT_TYPE_UPPERCASE)
self.label:SetText(AF.strings["All"])
self.divider = self.control:GetNamedChild("Divider")
self.subfilterButtons = {}
self.activeButton = nil
self.dropdown = WINDOW_MANAGER:CreateControlFromVirtual("AF_FilterBar" .. self.name .. "DropdownFilter", self.control, "ZO_ComboBox")
self.dropdown:SetAnchor(RIGHT, self.control, RIGHT)
self.dropdown:SetHeight(24)
self.dropdown:SetWidth(104)
local function DropdownOnMouseUpHandler(dropdown, mouseButton, upInside)
local comboBox = dropdown.m_comboBox
if mouseButton == 1 and upInside then
if comboBox.m_isDropdownVisible then
comboBox:HideDropdownInternal()
else
comboBox:ShowDropdownInternal()
end
elseif mouseButton == 2 and upInside then
local entries = {
[1] = {
name = AF.strings.ResetToAll,
callback = function()
comboBox:SelectFirstItem()
local button = self:GetCurrentButton()
button.previousDropdownSelection = comboBox.m_sortedItems[1]
local filterType = AF.util.LibFilters:GetCurrentFilterTypeForInventory(self.inventoryType) or LF_VENDOR_BUY
AF.util.LibFilters:RequestUpdate(filterType)
PlaySound(SOUNDS.MENU_BAR_CLICK)
end,
},
[2] = {
name = AF.strings.InvertDropdownFilter,
callback = function()
local button = self:GetCurrentButton()
local filterType = AF.util.LibFilters:GetCurrentFilterTypeForInventory(self.inventoryType) or LF_VENDOR_BUY
local originalCallback = AF.util.LibFilters:GetFilterCallback("AF_DropdownFilter", filterType)
local filterCallback = function(slot)
return not originalCallback(slot)
end
AF.util.LibFilters:UnregisterFilter("AF_DropdownFilter", filterType)
AF.util.LibFilters:RegisterFilter("AF_DropdownFilter", filterType, filterCallback)
AF.util.LibFilters:RequestUpdate(filterType)
PlaySound(SOUNDS.MENU_BAR_CLICK)
end,
},
}
ClearMenu()
for _, entry in ipairs(entries) do
AddCustomMenuItem(entry.name, entry.callback, MENU_ADD_OPTION_LABEL)
end
ShowMenu(dropdown)
end
end
self.dropdown:SetHandler("OnMouseUp", DropdownOnMouseUpHandler)
local comboBox = self.dropdown.m_comboBox
comboBox:SetSortsItems(false)
comboBox.AddMenuItems = function(comboBox)
local button = self:GetCurrentButton()
local self = comboBox
for i = 1, #self.m_sortedItems do
-- The variable item must be defined locally here, otherwise it won't work as an upvalue to the selection helper
local item = self.m_sortedItems[i]
local function OnSelect()
ZO_ComboBox_Base_ItemSelectedClickHelper(self, item)
button.previousDropdownSelection = item
PlaySound(SOUNDS.MENU_BAR_CLICK)
end
AddCustomMenuItem(item.name, OnSelect, nil, self.m_font,
self.m_normalColor, self.m_highlightColor)
end
local submenuCandidates = self.submenuCandidates
for _, submenuCandidate in ipairs(submenuCandidates) do
local entries = {}
for _, callbackEntry in ipairs(submenuCandidate.callbackTable) do
local entry = {
label = AF.strings[callbackEntry.name],
callback = function()
AF.util.ApplyFilter(callbackEntry, "AF_DropdownFilter", true)
button.forceNextDropdownRefresh = true
self.m_selectedItemText:SetText(AF.strings[callbackEntry.name])
self.m_selectedItemData = self:CreateItemEntry(AF.strings[callbackEntry.name],
function(comboBox, itemName, item, selectionChanged)
AF.util.ApplyFilter(callbackEntry,
"AF_DropdownFilter",
selectionChanged or button.forceNextDropdownRefresh)
end)
button.previousDropdownSelection = self.m_selectedItemData
PlaySound(SOUNDS.MENU_BAR_CLICK)
ClearMenu()
end,
}
table.insert(entries, entry)
end
AddCustomSubMenuItem(AF.strings[submenuCandidate.submenuName], entries, "ZoFontGameSmall")
end
end
for _, subfilterName in ipairs(subfilterNames) do
self:AddSubfilter(groupName, subfilterName)
end
end
function AF_FilterBar:AddSubfilter(groupName, subfilterName)
local iconPath = AF.textures[subfilterName]
local icon = {
up = string.format(iconPath, "up"),
down = string.format(iconPath, "down"),
over = string.format(iconPath, "over"),
}
local callback = AF.subfilterCallbacks[groupName][subfilterName].filterCallback
local anchorX = -116 + #self.subfilterButtons * -32
local button = WINDOW_MANAGER:CreateControlFromVirtual(self.control:GetName() .. subfilterName .. "Button", self.control, "AF_Button")
local texture = button:GetNamedChild("Texture")
local highlight = button:GetNamedChild("Highlight")
texture:SetTexture(icon.up)
highlight:SetTexture(icon.over)
button:SetAnchor(RIGHT, self.control, RIGHT, anchorX, 0)
button:SetClickSound(SOUNDS.MENU_BAR_CLICK)
local function OnClicked(thisButton)
if(not thisButton.clickable) then return end
self:ActivateButton(thisButton)
end
local function OnMouseEnter(thisButton)
ZO_Tooltips_ShowTextTooltip(thisButton, TOP, AF.strings[subfilterName])
local clickable = thisButton.clickable
local active = self:GetCurrentButton() == thisButton
if clickable and not active then
highlight:SetHidden(false)
end
end
local function OnMouseExit()
ZO_Tooltips_HideTextTooltip()
highlight:SetHidden(true)
end
button:SetHandler("OnClicked", OnClicked)
button:SetHandler("OnMouseEnter", OnMouseEnter)
button:SetHandler("OnMouseExit", OnMouseExit)
button.name = subfilterName
button.groupName = groupName
button.texture = texture
button.clickable = true
button.filterCallback = callback
button.up = icon.up
button.down = icon.down
self.activeButton = button
table.insert(self.subfilterButtons, button)
end
function AF_FilterBar:ActivateButton(newButton)
local function PopulateDropdown()
local comboBox = self.dropdown.m_comboBox
newButton.dropdownCallbacks = AF.util.BuildDropdownCallbacks(newButton.groupName, newButton.name)
comboBox.submenuCandidates = {}
for _, v in ipairs(newButton.dropdownCallbacks) do
if v.submenuName then
table.insert(comboBox.submenuCandidates, v)
else
local itemEntry = ZO_ComboBox:CreateItemEntry(AF.strings[v.name],
function(comboBox, itemName, item, selectionChanged)
AF.util.ApplyFilter(v, "AF_DropdownFilter",
selectionChanged or newButton.forceNextDropdownRefresh)
end)
comboBox:AddItem(itemEntry)
end
end
comboBox:SetSelectedItemFont("ZoFontGameSmall")
comboBox:SetDropdownFont("ZoFontGameSmall")
end
local name = newButton.name
self.label:SetText(AF.strings[name])
local oldButton = self.activeButton
--hide old down texture
oldButton:GetNamedChild("Texture"):SetTexture(oldButton.up)
oldButton:SetEnabled(true)
--show new down texture
newButton:GetNamedChild("Texture"):SetTexture(newButton.down)
newButton:SetEnabled(false)
--refresh filter
AF.util.ApplyFilter(newButton, "AF_ButtonFilter", true)
--set new active button reference
self.activeButton = newButton
--clear old dropdown data
self.dropdown.m_comboBox.m_sortedItems = {}
--add new dropdown data
PopulateDropdown()
--restore previous dropdown selection
self.dropdown.m_comboBox:SelectItem(newButton.previousDropdownSelection)
--select the first item if there is no previos selection
if not newButton.previousDropdownSelection then
self.dropdown.m_comboBox:SelectFirstItem()
newButton.previousDropdownSelection = self.dropdown.m_comboBox.m_sortedItems[1]
end
end
function AF_FilterBar:GetCurrentButton()
return self.activeButton
end
function AF_FilterBar:SetHidden(shouldHide)
self.control:SetHidden(shouldHide)
end
function AF_FilterBar:SetInventoryType(inventoryType)
self.inventoryType = inventoryType
end