diff CampFireButton.lua @ 105:08ede537787b v1.0beta12

Upgrade Campfirebutton
author contrebasse
date Tue, 31 May 2011 00:57:01 +0200
parents c8d527a9fb3a
children af23986010ef
line wrap: on
line diff
--- a/CampFireButton.lua	Sat May 21 14:21:18 2011 +0200
+++ b/CampFireButton.lua	Tue May 31 00:57:01 2011 +0200
@@ -4,6 +4,16 @@
 local COOKING_ID = 2550
 local btn
 local cookingName
+local CooldownFrame_SetTimer = CooldownFrame_SetTimer
+local GetSpellCooldown = GetSpellCooldown
+
+local function WaitCooldown(self)
+	local start, duration, enable = GetSpellCooldown(CAMPFIRE_ID)
+	if start>0 then
+		CooldownFrame_SetTimer(btn.cooldown,GetSpellCooldown(CAMPFIRE_ID))
+		self:SetScript("OnUpdate",nil)
+	end
+end
 
 -- Create button
 function A.InitialiseCampFireBtn()
@@ -12,6 +22,7 @@
 	-- create the frame
 	btn = CreateFrame("Button", nil, TradeSkillFrame, "SecureActionButtonTemplate")
 	btn:SetNormalTexture(select(3,GetSpellInfo(CAMPFIRE_ID)))
+	btn:SetHighlightTexture("Interface\\BUTTONS\\ButtonHilight-Square")
 	btn:SetSize(24,24)
 	btn:SetPoint("BOTTOMRIGHT",TradeSkillFrame,"BOTTOMRIGHT",-10,179)
 
@@ -30,12 +41,27 @@
 	-- Add cooldown
 	btn.cooldown = CreateFrame("Cooldown",nil,btn)
 	btn.cooldown:SetAllPoints(btn)
-	local CooldownFrame_SetTimer = CooldownFrame_SetTimer
-	local GetSpellCooldown = GetSpellCooldown
-	btn:SetScript("OnUpdate",function(self) CooldownFrame_SetTimer(self.cooldown,GetSpellCooldown(CAMPFIRE_ID)); end)
 
-	-- Show if needed
-	A.ManageCampFireBtn()
+	-- Check if recipe failed due to lack of campfire
+	local campfireName = GetSpellInfo(CAMPFIRE_ID)
+	local errorMsg
+	btn:SetScript("OnEvent",function(self,event,arg1,arg2)
+		if not errorMsg or errorMsg==LOCKED_WITH_ITEM then
+			local i = GetTradeSkillSelectionIndex()
+			if i and i>01 then
+				errorMsg = LOCKED_WITH_ITEM:format(GetTradeSkillTools(GetTradeSkillSelectionIndex()))
+			end
+		end
+		if event == "UNIT_SPELLCAST_SUCCEEDED" then
+			if arg1 == "player" and arg2 == campfireName then
+				self:SetScript("OnUpdate",WaitCooldown)
+				--CooldownFrame_SetTimer(self.cooldown,GetSpellCooldown(CAMPFIRE_ID))
+			end
+		elseif arg1 == errorMsg and GetSpellCooldown(CAMPFIRE_ID)==0 then
+			-- Flash the button if the user tried to cook something without fire
+			self.cooldown:SetCooldown(0,0)
+		end
+	end)
 end
 
 -- Hide button
@@ -48,7 +74,7 @@
 	-- Display only if the tradeskill is Cooking
 	if not cookingName then cookingName = GetSpellInfo(COOKING_ID) end
 	if GetTradeSkillLine() ~= cookingName then
-		if btn then btn:Hide() end
+		if btn then btn:Hide(); btn:UnregisterAllEvents() end
 	else
 		-- create button if necessary
 		if not btn then
@@ -57,5 +83,8 @@
 		-- It may not have been created
 		if not btn then return end
 		btn:Show()
+		btn:RegisterEvent("UI_ERROR_MESSAGE")
+		btn:RegisterEvent("UNIT_SPELLCAST_SUCCEEDED")
+		CooldownFrame_SetTimer(btn.cooldown,GetSpellCooldown(CAMPFIRE_ID))
 	end
 end