-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSecureMenu.lua
executable file
·415 lines (357 loc) · 12.6 KB
/
SecureMenu.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
local addonName, A = ...
-- Lua functions
local ipairs = ipairs
local type = type
-- Wow variables
-- Create the menu frame
local MenuFrame = CreateFrame("Frame","ReagentMaker_ExternalFrame",UIParent)
MenuFrame:Hide()
MenuFrame:SetSize(192,256)
--MenuFrame:SetFrameStrata("DIALOG")
MenuFrame:EnableMouse(true)
MenuFrame:SetPoint("CENTER")
MenuFrame:SetToplevel(true) -- raised if clicked
tinsert(UISpecialFrames,"ReagentMaker_ExternalFrame") -- make it closable with escape
-- Throttling is made in ReagentMaker.lua
MenuFrame:SetScript("OnEvent",function() MenuFrame:Hide() end)
MenuFrame:RegisterEvent("TRADE_SKILL_CLOSE")
MenuFrame:RegisterEvent("PLAYER_REGEN_DISABLED")
MenuFrame:SetScript("OnEnter",function(self)
if self.reagentLink then
GameTooltip:SetOwner(self)
GameTooltip:SetHyperlink(self.reagentLink)
GameTooltip:Show()
GameTooltip:ClearAllPoints()
GameTooltip:SetPoint("TOPRIGHT",self,"TOPLEFT",10,0)
if self.spellLink then
A.tooltipRecipe:SetOwner(GameTooltip)
A.tooltipRecipe:SetHyperlink(self.spellLink)
A.tooltipRecipe:Show()
A.tooltipRecipe:ClearAllPoints()
A.tooltipRecipe:SetPoint("TOPRIGHT",GameTooltip,"BOTTOMRIGHT")
end
end
end)
MenuFrame:SetScript("OnLeave",function()
GameTooltip:Hide()
A.tooltipRecipe:Hide()
end)
-- Hide frame when selecting a recipe which doesn't need this reagent
hooksecurefunc("SelectTradeSkill",function()
local selectedIndex = GetTradeSkillSelectionIndex()
for reagentIndexInRecipe = 1,GetTradeSkillNumReagents(selectedIndex) do
local reagentID = A.link2ID(GetTradeSkillReagentItemLink(selectedIndex, reagentIndexInRecipe))
if reagentID == MenuFrame.itemID or (MenuFrame.superItemID and reagentID == MenuFrame.superItemID) then
return
end
end
MenuFrame:Hide()
end)
A.MenuFrame = MenuFrame
-- Background adaptable vertically
local bg_top = MenuFrame:CreateTexture(nil,"BACKGROUND",nil,0)
bg_top:SetTexture("Interface\\LootFrame\\UI-LootPanel")
bg_top:SetSize(192,80)
bg_top:SetPoint("TOP")
bg_top:SetTexCoord(0,192/256,0,80/256)
local bg_bot = MenuFrame:CreateTexture(nil,"BACKGROUND",nil,0)
bg_bot:SetTexture("Interface\\LootFrame\\UI-LootPanel")
bg_bot:SetSize(192,16)
bg_bot:SetPoint("BOTTOM")
bg_bot:SetTexCoord(0,192/256,240/256,1)
local bg_mid = MenuFrame:CreateTexture(nil,"BACKGROUND",nil,0)
bg_mid:SetTexture("Interface\\LootFrame\\UI-LootPanel")
bg_mid:SetWidth(192)
bg_mid:SetPoint("TOP",bg_top,"BOTTOM")
bg_mid:SetPoint("BOTTOM",bg_bot,"TOP")
bg_mid:SetTexCoord(0,192/256,80/256,240/256)
-- Bouton de fermeture
local CloseButton = CreateFrame("Button",nil,MenuFrame,"UIPanelCloseButton");
CloseButton:SetPoint("TOPRIGHT",0,-10)
-- Main icon
local itemIcon = MenuFrame:CreateTexture(nil,"BACKGROUND",nil,-1)
itemIcon:SetSize(64,64)
itemIcon:SetPoint("TOPLEFT",8,-4)
-- Title
local TitleText = MenuFrame:CreateFontString(nil,"ARTWORK","GameFontHighlight")
--TitleText:SetSize(92,14)
TitleText:SetSize(92,36)
TitleText:SetPoint("TOPRIGHT",CloseButton,"TOPLEFT",4,-8)
TitleText:SetWordWrap(true)
TitleText:SetNonSpaceWrap(false)
TitleText:SetJustifyV("TOP")
local MENU_ENTRY_HEIGHT = 41
local MENU_ENTRY_WIDTH = 147
local MENU_ENTRY_ICON_RATIO = 40/48
local numActiveEntries = 0
local menuEntries = {}
-- Button hovering
local function btnEntered(btn)
if btn.numMakable and btn.numMakable>0 then
btn.textureHighlight:Show()
end
GameTooltip:SetOwner(btn,"ANCHOR_LEFT")
GameTooltip:SetHyperlink(btn.reagentLink)
GameTooltip:Show()
if btn.spellLink and btn.spellLink~=btn.reagentLink then
A.tooltipRecipe:SetOwner(GameTooltip)
A.tooltipRecipe:SetHyperlink(btn.spellLink)
A.tooltipRecipe:Show()
A.tooltipRecipe:ClearAllPoints()
A.tooltipRecipe:SetPoint("TOPRIGHT",GameTooltip,"BOTTOMRIGHT")
end
end
local function btnLeft(btn)
btn.textureHighlight:Hide()
GameTooltip:Hide()
A.tooltipRecipe:Hide()
end
local function createMenuEntry()
local btn = CreateFrame("Button", nil, MenuFrame, "SecureActionButtonTemplate")
table.insert(menuEntries,btn)
btn:Hide()
btn:SetSize(MENU_ENTRY_WIDTH,MENU_ENTRY_HEIGHT)
--btn:SetFrameStrata("DIALOG")
-- Set its position
if #menuEntries>1 then
btn:SetPoint("TOP",menuEntries[#menuEntries-1],"BOTTOM",0,-2)
else
btn:SetPoint("TOPLEFT",MenuFrame,"TOPLEFT",24,-79)
end
local icon = btn:CreateTexture(nil,"BACKGROUND")
icon:SetPoint("TOPLEFT")
icon:SetSize(39,39)
btn.icon = icon
local itemNameBG = btn:CreateTexture(nil,"BACKGROUND")
itemNameBG:SetTexture("Interface\\QuestFrame\\UI-QuestItemNameFrame")
itemNameBG:SetSize(128,64)
itemNameBG:SetPoint("LEFT",icon,"RIGHT",-10,0)
local itemName = btn:CreateFontString(nil,"BACKGROUND","GameFontHighlight")
itemName:SetSize(90,36)
itemName:SetPoint("LEFT",itemNameBG,"LEFT",15,0)
itemName:SetJustifyH("LEFT")
itemName:SetWordWrap(true)
itemName:SetNonSpaceWrap(false)
btn.itemName = itemName
local textureHighlight = btn:CreateTexture(nil,"BORDER")
textureHighlight:Hide()
textureHighlight:SetTexture("Interface\\BUTTONS\\CheckButtonHilight")
--textureHighlight:SetTexture("Interface\\BUTTONS\\ButtonHilight-Square")
textureHighlight:SetBlendMode("ADD")
textureHighlight:SetAllPoints(icon)
btn.textureHighlight = textureHighlight
local countDetail = btn:CreateFontString(nil,"ARTWORK","NumberFontNormal")
countDetail:SetPoint("BOTTOMRIGHT",icon,"BOTTOMRIGHT",-1,1)
countDetail:SetJustifyH("RIGHT")
countDetail:SetJustifyV("BOTTOM")
btn.countDetail = countDetail
local resultNumber = btn:CreateFontString(nil,"ARTWORK","NumberFontNormal")
resultNumber:SetPoint("TOPLEFT",icon,"TOPLEFT",1,-3)
resultNumber:SetJustifyH("LEFT")
resultNumber:SetJustifyV("TOP")
resultNumber:SetFont("Fonts\\ARIALN.TTF", 12, "OUTLINE")
btn.resultNumber = resultNumber
-- Used to choose the number of items to make
btn.SplitStack = A.SplitStack
btn:SetScript("OnEnter", btnEntered)
btn:SetScript("OnLeave", btnLeft)
return btn
end
local function WarnNotMakable(btn)
if not btn.reagentID then
A.Error(A.L["You do not have enough reagents to craft [%s]"]:format(TitleText:GetText()))
else
A.Error(A.L["You do not have enough [%s] to craft [%s]"]:format(btn.itemNameString,TitleText:GetText()))
end
end
local function CraftFromExternal(btn)
local chooseNumberToCraft = IsShiftKeyDown()
A.CraftItemWithRecipe( GetTradeSkillSelectionIndex(),
btn.itemID,
btn.reagent,
MenuFrame.reagentIndexInRecipe,
IsShiftKeyDown(),
btn)
end
-- Update counts and button actions
function MenuFrame.updateCounts()
local anyMakable
for i=1,numActiveEntries do
local btn = menuEntries[i]
local itemCount = GetItemCount(btn.reagentID)
local numMakable
if btn.reagentID then
numMakable = math.floor(itemCount/(btn.reagentsForOneRecipe or 1))
btn.countDetail:SetText(itemCount.."/"..(btn.reagentsForOneRecipe or 1))
else
for _,reagent in pairs(btn.reagentsForOneRecipe) do
itemCount = GetItemCount(reagent[1])
if not itemCount then
numMakable = 0
break
end
if not numMakable then
numMakable = math.floor(itemCount/reagent[2])
else
numMakable = math.min(numMakable,math.floor(itemCount/reagent[2]))
end
if numMakable==0 then break end
end
btn.countDetail:SetText(numMakable)
end
if numMakable>0 then
-- Set action
if type(btn.action)=="function" then
btn:SetScript("PreClick",btn.action)
btn:SetAttribute("type", nil)
btn:SetAttribute("macrotext", nil)
else --if type(action)=="string" then
btn:SetScript("PreClick",nil)
btn:SetAttribute("type", "macro")
btn:SetAttribute("macrotext", btn.action:format(btn.itemNameString))
end -- if
anyMakable = true
btn.countDetail:SetTextColor(1, 1, 1, 1)
btn.icon:SetVertexColor(1,1,1);
btn.itemName:SetTextColor(1,1,1,1)
else
-- Do not disable the button, to be able to show the tooltip
-- Disable only the effects
btn:SetScript("PreClick",WarnNotMakable)
btn:SetAttribute("type", nil)
btn:SetAttribute("macrotext", nil)
btn.countDetail:SetTextColor(1, 0.1, 0.1, 1)
btn.icon:SetVertexColor(0.5, 0.5, 0.5)
btn.itemName:SetTextColor(1,1,1,0.5)
end
btn.numMakable = numMakable
end
local r,g,b = TitleText:GetTextColor()
if anyMakable then
itemIcon:SetVertexColor(1,1,1)
TitleText:SetTextColor(r,g,b,1)
else
itemIcon:SetVertexColor(0.5, 0.5, 0.5)
TitleText:SetTextColor(r,g,b,0.7)
end
end
local function menuAddItem(action,itemID,reagent)
local btn
-- Create a button only if necessary
if numActiveEntries >= #menuEntries then
btn = createMenuEntry()
else
btn = menuEntries[numActiveEntries+1]
end
-- Set text and icon
local name, link, texture, _
if reagent[1] then
name, link, _, _, _, _, _, _, _, texture = GetItemInfo(reagent[1])
if not (name and link and texture) then
-- Will be retried on next OnUpdate
return
end
elseif reagent.spellLink then
--name, rank, icon, powerCost, isFunnel, powerType, castingTime, minRange, maxRange = GetSpellInfo(id)
name, _, texture = GetSpellInfo(A.link2ID(reagent.spellLink))
if not (name and texture) then
-- Will be retried on next OnUpdate
return
end
link = reagent.spellLink
end
btn.itemName:SetText(name)
btn.icon:SetTexture(texture)
-- Set chance to have the item or the number of items created
btn.resultNumber:Hide()
if reagent[3] then
if reagent[3]<1 then
btn.resultNumber:SetText((reagent[3]*100).."%")
btn.resultNumber:Show()
elseif reagent[4] and reagent[3]~=reagent[4] then
btn.resultNumber:SetText(math.min(reagent[3],reagent[4]).."-"..math.max(reagent[3],reagent[4]))
btn.resultNumber:Show()
elseif reagent[3]>1 then
btn.resultNumber:SetText(reagent[3])
btn.resultNumber:Show()
end
end
-- Save params
btn.itemID = itemID
btn.reagent = reagent
btn.reagentID = reagent[1]
btn.reagentLink = link
btn.reagentsForOneRecipe = reagent[2]
btn.spellLink = reagent.spellLink
btn.action = action
btn.itemNameString = name
btn:Show()
-- Increase the entry number
numActiveEntries = numActiveEntries + 1
-- Everything went well
return true
end -- function
-- Function used on OnUpdate tu update the frame if there were errors the previous time
local function reopen()
-- Release OnUpdate frame (could conflict with BAG_UPDATE)
MenuFrame:SetScript("OnUpdate",nil)
-- reopen
A.externalCraftWindow(MenuFrame.itemID,MenuFrame.reagentIndexInRecipe,MenuFrame.superItemID)
end
-- Fill the window and open it
function A.externalCraftWindow(itemID,reagentIndexInRecipe,superItemID)
-- Do not open during combat
if InCombatLockdown() then
A.Error(SPELL_FAILED_AFFECTING_COMBAT)
return
end
-- Save the tradeskill
A.currentTradeSkill = GetTradeSkillLine()
-- Close the previous menu
MenuFrame:Hide()
for i=1,numActiveEntries do
menuEntries[i]:Hide()
end
numActiveEntries = 0
-- Fill the info of the reagent to make
local name, link, quality, _, _, _, _, _, _, texture = GetItemInfo(itemID)
SetPortraitToTexture(itemIcon, texture)
TitleText:SetText(name)
local color = ITEM_QUALITY_COLORS[quality]
TitleText:SetTextColor(color.r, color.g, color.b)
-- Save vars to show the tooltip later
MenuFrame.reagentLink = link
MenuFrame.spellLink = A.isRecipeUnique(A.data[itemID]) and A.data[itemID][1].spellLink
MenuFrame.itemID = itemID
MenuFrame.reagentIndexInRecipe = reagentIndexInRecipe
MenuFrame.superItemID = superItemID -- optional, will be nil if not set
-- Loop over the available recipes
local noSkipped = true -- check if we have to reload the external frame to get all the data
local existsValidEntries -- check if the menu contains at least one item (cross-tradeskill problem)
for _,reagent in ipairs(A.data[itemID]) do
if reagent.macro then
-- Special spell
existsValidEntries = true
noSkipped = menuAddItem(reagent.macro,itemID,reagent) and noSkipped
else
-- Standard tradeskill spell
if not reagent.tradeskillName or reagent.tradeskillName == A.currentTradeSkill then
existsValidEntries = true
noSkipped = menuAddItem(CraftFromExternal,itemID,reagent) and noSkipped
end
end -- if
end -- for
-- do not show an empty menu
if not existsValidEntries then
return false
end
MenuFrame:SetHeight(89 + numActiveEntries*(MENU_ENTRY_HEIGHT+2))
MenuFrame:ClearAllPoints()
MenuFrame:SetPoint("TOPLEFT",TradeSkillFrame,"TOPRIGHT",-2,14)
-- Update counts and set actions
MenuFrame.updateCounts()
MenuFrame:Show()
if not noSkipped then
MenuFrame:SetScript("OnUpdate",reopen)
end
end