annotate EnchantOnScroll.lua @ 111:af23986010ef v1.1beta0

Rewrote the main part to clarify things, should have removed some hidden nasty bugs.
author contrebasse
date Thu, 02 Jun 2011 23:07:23 +0200
parents 0fd1fd1ba2d9
children f17fc88e971e
rev   line source
contrebasse@98 1 local addonName, A = ...
contrebasse@98 2
contrebasse@98 3 local SCROLL_ID = 38682
contrebasse@98 4 local ENCHANTING_ID = 7411
contrebasse@98 5 local btn
contrebasse@98 6
contrebasse@98 7 local EventsFrame = CreateFrame("Frame",nil,TradeSkillFrame) -- It will be hidden with the TradeSkillFrame
contrebasse@98 8
contrebasse@98 9 local function CheckButtonAvailable(arg1)
contrebasse@98 10 if not btn then return end
contrebasse@98 11
contrebasse@98 12 -- Do not manage guild tradeskill
contrebasse@98 13 -- Check that we're still with the enchanting tradeskill
contrebasse@98 14 if IsTradeSkillGuild() or IsTradeSkillLinked() or GetTradeSkillLine() ~= GetSpellInfo(ENCHANTING_ID) then
contrebasse@98 15 btn:Hide()
contrebasse@98 16 return
contrebasse@98 17 end
contrebasse@98 18
contrebasse@98 19 -- Check that the selected recipe can be crafted, and the crafted thing is an enchant
contrebasse@98 20 local index = GetTradeSkillSelectionIndex()
contrebasse@98 21 if not index then
contrebasse@98 22 btn:Hide()
contrebasse@98 23 return
contrebasse@98 24 end
contrebasse@98 25 local _, _, numAvailable, _, serviceType = GetTradeSkillInfo(index)
contrebasse@98 26
contrebasse@98 27 -- serviceType is localised, but nil if an item is created
contrebasse@98 28 if not serviceType then
contrebasse@98 29 btn:Hide()
contrebasse@98 30 return
contrebasse@98 31 end
contrebasse@98 32
contrebasse@98 33 -- Check that there's scrolls in the bags
contrebasse@98 34 local itemCount = GetItemCount(SCROLL_ID)
contrebasse@98 35 if not itemCount or itemCount==0 then
contrebasse@98 36 btn:Disable()
contrebasse@98 37 btn:Show()
contrebasse@98 38 btn:SetText(A.L["Enchant a scroll (0)"])
contrebasse@98 39 return
contrebasse@98 40 end
contrebasse@98 41 btn:SetText(A.L["Enchant a scroll (%d)"]:format(itemCount))
contrebasse@98 42
contrebasse@98 43 if numAvailable==0 then
contrebasse@98 44 btn:Disable()
contrebasse@98 45 btn:Show()
contrebasse@98 46 return
contrebasse@98 47 end
contrebasse@98 48
contrebasse@98 49 -- It passed the tests
contrebasse@98 50 btn:Enable()
contrebasse@98 51 btn:Show()
contrebasse@98 52 end
contrebasse@98 53 EventsFrame:SetScript("OnEvent",CheckButtonAvailable)
contrebasse@98 54 EventsFrame:RegisterEvent("BAG_UPDATE")
contrebasse@98 55 hooksecurefunc("SelectTradeSkill",CheckButtonAvailable)
contrebasse@98 56
contrebasse@98 57 function A.LoadEnchantOnScroll()
contrebasse@98 58 btn = CreateFrame("Button", nil, TradeSkillFrame, "UIPanelButtonTemplate")
contrebasse@98 59 btn:SetSize(168,22)
contrebasse@98 60 btn:SetPoint("TOPRIGHT",TradeSkillCreateButton,"TOPLEFT",0,0)
contrebasse@98 61 btn:SetText(A.L["Enchant on a scroll"])
contrebasse@98 62 btn:Show()
contrebasse@98 63
contrebasse@98 64 btn:SetScript("OnClick",function()
contrebasse@98 65 -- from http://wowprogramming.com/utils/xmlbrowser/live/AddOns/Blizzard_TradeSkillUI/Blizzard_TradeSkillUI.xml
contrebasse@98 66 DoTradeSkill(TradeSkillFrame.selectedSkill,1)
contrebasse@98 67
contrebasse@98 68 -- From GnomeWorks/ScrollMaking.lua
contrebasse@98 69 UseItemByName(SCROLL_ID)
contrebasse@98 70 end)
contrebasse@98 71 end