annotate CampFireButton.lua @ 106:f857e01b067e

Added tag v1.0beta12 for changeset 08ede537787b
author contrebasse
date Tue, 31 May 2011 00:57:27 +0200
parents 08ede537787b
children af23986010ef
rev   line source
contrebasse@98 1 local addonName, A = ...
contrebasse@98 2
contrebasse@98 3 local CAMPFIRE_ID = 818
contrebasse@98 4 local COOKING_ID = 2550
contrebasse@98 5 local btn
contrebasse@98 6 local cookingName
contrebasse@105 7 local CooldownFrame_SetTimer = CooldownFrame_SetTimer
contrebasse@105 8 local GetSpellCooldown = GetSpellCooldown
contrebasse@105 9
contrebasse@105 10 local function WaitCooldown(self)
contrebasse@105 11 local start, duration, enable = GetSpellCooldown(CAMPFIRE_ID)
contrebasse@105 12 if start>0 then
contrebasse@105 13 CooldownFrame_SetTimer(btn.cooldown,GetSpellCooldown(CAMPFIRE_ID))
contrebasse@105 14 self:SetScript("OnUpdate",nil)
contrebasse@105 15 end
contrebasse@105 16 end
contrebasse@98 17
contrebasse@98 18 -- Create button
contrebasse@98 19 function A.InitialiseCampFireBtn()
contrebasse@103 20 if not GetTradeSkillLine() or InCombatLockdown() then return end
contrebasse@103 21
contrebasse@98 22 -- create the frame
contrebasse@98 23 btn = CreateFrame("Button", nil, TradeSkillFrame, "SecureActionButtonTemplate")
contrebasse@98 24 btn:SetNormalTexture(select(3,GetSpellInfo(CAMPFIRE_ID)))
contrebasse@105 25 btn:SetHighlightTexture("Interface\\BUTTONS\\ButtonHilight-Square")
contrebasse@98 26 btn:SetSize(24,24)
contrebasse@98 27 btn:SetPoint("BOTTOMRIGHT",TradeSkillFrame,"BOTTOMRIGHT",-10,179)
contrebasse@98 28
contrebasse@98 29 -- Set the action
contrebasse@98 30 btn:SetAttribute("type", "spell")
contrebasse@98 31 btn:SetAttribute("spell", CAMPFIRE_ID)
contrebasse@98 32
contrebasse@98 33 -- Set the tooltip
contrebasse@98 34 btn:SetScript("OnEnter",function(self)
contrebasse@98 35 GameTooltip:SetOwner(self, "ANCHOR_TOPLEFT")
contrebasse@98 36 GameTooltip:SetSpellByID(CAMPFIRE_ID)
contrebasse@98 37 GameTooltip:Show()
contrebasse@98 38 end)
contrebasse@98 39 btn:SetScript("OnLeave",function() GameTooltip:Hide(); end)
contrebasse@98 40
contrebasse@98 41 -- Add cooldown
contrebasse@98 42 btn.cooldown = CreateFrame("Cooldown",nil,btn)
contrebasse@98 43 btn.cooldown:SetAllPoints(btn)
contrebasse@98 44
contrebasse@105 45 -- Check if recipe failed due to lack of campfire
contrebasse@105 46 local campfireName = GetSpellInfo(CAMPFIRE_ID)
contrebasse@105 47 local errorMsg
contrebasse@105 48 btn:SetScript("OnEvent",function(self,event,arg1,arg2)
contrebasse@105 49 if not errorMsg or errorMsg==LOCKED_WITH_ITEM then
contrebasse@105 50 local i = GetTradeSkillSelectionIndex()
contrebasse@105 51 if i and i>01 then
contrebasse@105 52 errorMsg = LOCKED_WITH_ITEM:format(GetTradeSkillTools(GetTradeSkillSelectionIndex()))
contrebasse@105 53 end
contrebasse@105 54 end
contrebasse@105 55 if event == "UNIT_SPELLCAST_SUCCEEDED" then
contrebasse@105 56 if arg1 == "player" and arg2 == campfireName then
contrebasse@105 57 self:SetScript("OnUpdate",WaitCooldown)
contrebasse@105 58 --CooldownFrame_SetTimer(self.cooldown,GetSpellCooldown(CAMPFIRE_ID))
contrebasse@105 59 end
contrebasse@105 60 elseif arg1 == errorMsg and GetSpellCooldown(CAMPFIRE_ID)==0 then
contrebasse@105 61 -- Flash the button if the user tried to cook something without fire
contrebasse@105 62 self.cooldown:SetCooldown(0,0)
contrebasse@105 63 end
contrebasse@105 64 end)
contrebasse@98 65 end
contrebasse@98 66
contrebasse@98 67 -- Hide button
contrebasse@98 68 function A.HideCampFireBtn()
contrebasse@101 69 if btn then btn:Hide() end
contrebasse@98 70 end
contrebasse@98 71
contrebasse@98 72 -- Show button if applicable
contrebasse@98 73 function A.ManageCampFireBtn()
contrebasse@98 74 -- Display only if the tradeskill is Cooking
contrebasse@103 75 if not cookingName then cookingName = GetSpellInfo(COOKING_ID) end
contrebasse@103 76 if GetTradeSkillLine() ~= cookingName then
contrebasse@105 77 if btn then btn:Hide(); btn:UnregisterAllEvents() end
contrebasse@98 78 else
contrebasse@103 79 -- create button if necessary
contrebasse@103 80 if not btn then
contrebasse@103 81 A.InitialiseCampFireBtn()
contrebasse@103 82 end
contrebasse@103 83 -- It may not have been created
contrebasse@103 84 if not btn then return end
contrebasse@98 85 btn:Show()
contrebasse@105 86 btn:RegisterEvent("UI_ERROR_MESSAGE")
contrebasse@105 87 btn:RegisterEvent("UNIT_SPELLCAST_SUCCEEDED")
contrebasse@105 88 CooldownFrame_SetTimer(btn.cooldown,GetSpellCooldown(CAMPFIRE_ID))
contrebasse@98 89 end
contrebasse@98 90 end