-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Favorites.lua
289 lines (253 loc) · 9.45 KB
/
Favorites.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
284
285
286
287
288
289
local _, ADDON = ...
local L = ADDON.L
local starButton
--region api
function ADDON.Api:GetFavoriteProfile()
local playerGuid = UnitGUID("player")
local profileIndex = ADDON.settings.favorites.assignments[playerGuid] or 1
if not ADDON.settings.favorites.profiles[profileIndex] then
profileIndex = 1
end
local name = ADDON.settings.favorites.profiles[profileIndex].name
if profileIndex == 1 then
name = ADDON.L.FAVORITE_ACCOUNT_PROFILE
end
return profileIndex, name, ADDON.settings.favorites.profiles[profileIndex].toys
end
function ADDON.Api:HasFavorites()
local _, _, favorites = ADDON.Api:GetFavoriteProfile()
return #favorites > 0
end
function ADDON.Api:GetIsFavorite(itemId)
local _, _, favorites = ADDON.Api:GetFavoriteProfile()
return tContains(favorites, itemId)
end
function ADDON.Api:SetIsFavorite(itemId, value)
local _, _, favorites = ADDON.Api:GetFavoriteProfile()
local hasChange = false
if true == value and not tContains(favorites, itemId) then
table.insert(favorites, itemId)
hasChange = true
elseif false == value then
local i = tIndexOf(favorites, itemId)
if i then
tUnorderedRemove(favorites, i)
hasChange = true
end
end
if hasChange then
ADDON.Events:TriggerEvent("OnFavoritesChanged")
end
end
function ADDON.Api:SetBulkIsFavorites(filteredProvider, inclusive)
local _, _, profileToys = ADDON.Api:GetFavoriteProfile()
local itemsToAdd = CopyValuesAsKeys(filteredProvider)
local hasChange = false
local index = 1
while index <= #profileToys do
local itemId = profileToys[index]
if itemsToAdd[itemId] then
itemsToAdd[itemId] = nil
index = index + 1
elseif inclusive then
-- skip remove
index = index + 1
else
tUnorderedRemove(profileToys, index)
hasChange = true
end
end
local tInsert = table.insert
for itemId, shouldAdd in pairs(itemsToAdd) do
if shouldAdd and PlayerHasToy(itemId) then
tInsert(profileToys, itemId)
hasChange = true
end
end
if hasChange then
ADDON.Events:TriggerEvent("OnFavoritesChanged")
end
end
function ADDON.Api:SwitchFavoriteProfile(newIndex)
local oldIndex = ADDON.Api:GetFavoriteProfile()
if oldIndex ~= newIndex then
ADDON.settings.favorites.assignments[UnitGUID("player")] = newIndex
ADDON.Events:TriggerEvent("OnFavoritesChanged")
ADDON.Events:TriggerEvent("OnFavoriteProfileChanged")
end
end
function ADDON.Api:RemoveFavoriteProfile(index)
if index > 1 then
local profileIndex = ADDON.Api:GetFavoriteProfile()
if profileIndex == index then
ADDON.Api:SwitchFavoriteProfile(1)
end
ADDON.settings.favorites.profiles[index] = nil
-- cleanup all assignments
for guid, profileIndex in pairs(ADDON.settings.favorites.assignments) do
if profileIndex == index then
ADDON.settings.favorites.assignments[guid] = 1
end
end
end
end
--endregion
StaticPopupDialogs["TBE_EDIT_FAVORITE_PROFILE"] = {
text = ADDON.L.ASK_FAVORITE_PROFILE_NAME,
button1 = OKAY,
button2 = CANCEL,
whileDead = 1,
hasEditBox = true,
OnAccept = function (self, profileIndex)
local text = self.editBox:GetText()
if profileIndex == nil then
table.insert(ADDON.settings.favorites.profiles, {
["name"] = text,
["autoFavor"] = false,
["toys"] = {}
})
ADDON.Api:SwitchFavoriteProfile(#ADDON.settings.favorites.profiles)
elseif profileIndex > 1 then
ADDON.settings.favorites.profiles[profileIndex].name = text
ADDON.Events:TriggerEvent("OnFavoriteProfileChanged")
end
end,
OnShow = function (self, profileIndex)
if profileIndex and ADDON.settings.favorites.profiles[profileIndex] then
self.editBox:SetText(ADDON.settings.favorites.profiles[profileIndex].name)
end
end,
timeout = 0,
hideOnEscape = 1,
enterClicksFirstButton = 1,
};
StaticPopupDialogs["TBE_CONFIRM_DELETE_FAVORITE_PROFILE"] = {
text = ADDON.L.CONFIRM_FAVORITE_PROFILE_DELETION,
button1 = YES,
button2 = NO,
OnAccept = function (_, index)
ADDON.Api:RemoveFavoriteProfile(index)
end,
hideOnEscape = 1,
timeout = 0,
whileDead = 1,
}
--region Star Button
function ADDON.UI:BuildFavoriteProfileMenu(root, withEditOptions)
local sortedIndex = {}
local profiles = ADDON.settings.favorites.profiles
for index, profileData in pairs(profiles) do
if profileData then
table.insert(sortedIndex, index)
end
end
table.sort(sortedIndex, function(a, b)
return profiles[a].name < profiles[b].name
end)
for _, index in ipairs(sortedIndex) do
local profileData = profiles[index]
local name = index == 1 and ADDON.L.FAVORITE_ACCOUNT_PROFILE or profileData.name
local singleProfileRoot = root:CreateRadio(name.." ("..(#profileData.toys)..")", function()
return index == ADDON.Api:GetFavoriteProfile()
end, function()
ADDON.Api:SwitchFavoriteProfile(index)
return MenuResponse.Refresh
end)
if withEditOptions then
singleProfileRoot:CreateCheckbox(ADDON.L.FAVOR_AUTO, function()
return profileData.autoFavor
end, function()
profileData.autoFavor = not profileData.autoFavor
return MenuResponse.Refresh
end)
if index > 1 then
singleProfileRoot:CreateButton(PET_RENAME, function()
StaticPopup_Show("TBE_EDIT_FAVORITE_PROFILE", nil, nil, index)
end)
singleProfileRoot:CreateButton(REMOVE, function()
StaticPopup_Show("TBE_CONFIRM_DELETE_FAVORITE_PROFILE", profileData.name, ADDON.L.FAVORITE_ACCOUNT_PROFILE, index)
end)
end
end
end
end
local function CreateFavoritesMenu(_, root)
root:CreateTitle(FAVORITES)
root:SetScrollMode(GetScreenHeight() - 100)
root:CreateButton(L.FAVOR_DISPLAYED, function()
ADDON.Api:SetBulkIsFavorites(ADDON.DataProvider:GetCollection(), true)
end)
root:CreateButton(UNCHECK_ALL, function()
ADDON.Api:SetBulkIsFavorites({})
end)
local _, profileName = ADDON.Api:GetFavoriteProfile()
local profileRoot = root:CreateButton(ADDON.L.FAVORITE_PROFILE..": "..profileName)
profileRoot:CreateButton(ADD, function()
StaticPopup_Show("TBE_EDIT_FAVORITE_PROFILE")
end)
profileRoot:QueueSpacer()
ADDON.UI:BuildFavoriteProfileMenu(profileRoot, true)
end
local function BuildStarButton()
starButton = CreateFrame("DropdownButton", nil, ToyBox)
starButton:SetPoint("RIGHT", ToyBox.searchBox.Left, "LEFT", WOW_PROJECT_ID ~= WOW_PROJECT_MAINLINE and -5 or -10, 0)
starButton:SetSize(16, 16)
local icon = starButton:CreateTexture(nil, "ARTWORK")
icon:SetAtlas("auctionhouse-icon-favorite")
icon:SetAllPoints(starButton)
starButton:SetHighlightAtlas("auctionhouse-icon-favorite", "ADD")
local highlight = starButton:GetHighlightTexture()
highlight:SetAlpha(0.4)
highlight:SetAllPoints(icon)
starButton:HookScript("OnMouseDown", function()
icon:AdjustPointsOffset(1, -1)
end)
starButton:HookScript("OnMouseUp", function()
icon:AdjustPointsOffset(-1, 1)
end)
starButton:SetupMenu(CreateFavoritesMenu)
starButton:RegisterEvent("PLAYER_REGEN_ENABLED")
starButton:RegisterEvent("PLAYER_REGEN_DISABLED")
starButton:SetScript("OnEvent", function(self, event)
self:SetShown(event == "PLAYER_REGEN_ENABLED")
end)
starButton:SetShown(not InCombatLockdown())
ADDON.UI.FavoriteButton = starButton
end
--endregion
ADDON.Events:RegisterCallback("OnLoadUI", BuildStarButton, "favorites")
ADDON.Events:RegisterCallback("OnFavoritesChanged", function()
ADDON.DataProvider:Sort()
end, "sort dataprovider")
-- initial scan of account profile
ADDON.Events:RegisterCallback("OnLogin", function()
local profileIndex, _ , favorites = ADDON.Api:GetFavoriteProfile()
if 1 == profileIndex and not ADDON.settings.favorites.profiles[1].initialScan then
ADDON.settings.favorites.profiles[1].initialScan = true
if 0 == #favorites then
local favoredToys = {}
for itemId, validToy in pairs(ADDON.db.ingameList) do
if validToy then
local _, _, _, isFavorite = C_ToyBox.GetToyInfo(itemId)
if isFavorite then
favoredToys[#favoredToys +1] = itemId
end
end
end
ADDON.Api:SetBulkIsFavorites(favoredToys)
end
end
end, "favorite account scan")
-- auto favor
ADDON.Events:RegisterFrameEventAndCallback("NEW_TOY_ADDED", function(_, itemId)
local currentProfile = ADDON.Api:GetFavoriteProfile()
for index, profileData in pairs(ADDON.settings.favorites.profiles) do
if profileData and profileData.autoFavor then
if index == currentProfile then
ADDON.Api:SetIsFavorite(itemId, true)
elseif not tContains(profileData.toys, itemId) then
table.insert(profileData.toys, itemId)
end
end
end
end, 'auto favor new toy')