view ReagentMaker.lua @ 15:cc56799582f2

Use local references to global functions to speed up scanning
author contrebasse
date Wed, 06 Apr 2011 00:28:13 +0200
parents 5eb02d3e1647
children 5336b3a19d4f
line wrap: on
line source
local addonName, A = ...

-- @todo clean the A table
-- @todo check local copy of globals functions
-- @todo add support for sub-recipes (like ink plus milling)
-- @todo show a tooltip
-- @todo show number of craftable items
-- @todo show owned items
-- @todo add support for dez ?
-- @todo add support for cross tradeskill, like mining + forge/ingé
-- @todo icon on reagent's button

---------------------------------------------------
-- Variables
---------------------------------------------------
-- Used by findglobals
-- GLOBALS: _G, CreateFrame, DEFAULT_CHAT_FRAME

-- Lua functions

-- Wow functions

-- constant vars

---------------------------------------------------
-- Manage events
---------------------------------------------------
A.EventsFrame = CreateFrame("Frame")

A.EventsFrame:SetScript("OnEvent", function(self, event, ...)
	if event == "TRADE_SKILL_UPDATE" then
		-- Close the external window if the tradeskill changed
		if A.currentTradeSkill ~= GetTradeSkillLine() then
			A.MenuFrame:Hide()
		end

		if IsTradeSkillGuild() or IsTradeSkillLinked() then
			A.MenuFrame:Hide()
			return
		end

		-- Scan availabe recipes
		A:ScanSimpleRecipes()

	elseif event == "TRADE_SKILL_SHOW" then
		if IsTradeSkillGuild() or IsTradeSkillLinked() then
			A.MenuFrame:Hide()
			return
		end
		A:Initialize()
		A.EventsFrame:UnregisterEvent("TRADE_SKILL_SHOW")
	end -- if
end) -- function
A.EventsFrame:RegisterEvent("TRADE_SKILL_SHOW")
A.EventsFrame:RegisterEvent("TRADE_SKILL_UPDATE")

---------------------------------------------------
-- Initialize
---------------------------------------------------
function A:Initialize()

	-- Register clics on reagent's buttons
	for i=1,7 do
		local btn = _G["TradeSkillReagent"..i];
		btn:HookScript("OnDoubleClick", A.ProcessReagent);
		btn:HookScript("OnEnter", A.btnEntered)
		btn:HookScript("OnLeave", A.btnLeft)
		btn.SplitStack = A.SplitStack

		local textureHighlight = btn:CreateTexture()
		textureHighlight:Hide()
		textureHighlight:SetTexture("Interface\\BUTTONS\\CheckButtonHilight")
		textureHighlight:SetBlendMode("ADD")
		--textureHighlight:SetPoint("TOPLEFT")
		--textureHighlight:SetSize(btn:GetHeight(),btn:GetHeight())
		textureHighlight:SetAllPoints("TradeSkillReagent"..i.."IconTexture")
		btn.textureHighlight = textureHighlight

		local label = btn:CreateFontString(nil,"ARTWORK","GameFontHighlight")
		label:SetSize(100,20)
		label:SetPoint("TOPLEFT",btn,"TOPLEFT",4,-4)
		label:SetJustifyH("LEFT")
		label:SetJustifyV("TOP")
		label:SetFont("Fonts\\FRIZQT__.TTF", 10, "OUTLINE")
		btn.label = label
	end -- for

	A.tooltipRecipe = CreateFrame("GameTooltip", "ReagentMaker_tooltipRecipe",UIParent, "GameTooltipTemplate")
	A.tooltipRecipe:SetFrameStrata("TOOLTIP")
	A.tooltipRecipe:Hide()
	--[[
	A.tooltipReagent = CreateFrame("GameTooltip", "ReagentMaker_tooltipReagent",UIParent, "GameTooltipTemplate")
	A.tooltipReagent:SetFrameStrata("TOOLTIP")
	A.tooltipReagent:Hide()
	--]]
end -- function

-- Function run after selecting a item in the tradeskill window
function A.ProcessReagent(btn, ...)
	-- Do not manage guild tradeskill
	if IsTradeSkillGuild() or IsTradeSkillLinked() then return end

	-- We want no modifiers, or shift to choose the number of reagent to craft
	if IsModifierKeyDown() and not IsShiftKeyDown() then return end
	local chooseNumberToCraft = IsShiftKeyDown()

	-- Index of the reagent in the recipe, taken from the button name
	local reagentRecipeIndex = A.buttonNumber(btn)

	-- ID of the reagent we want to craft
	local reagentID = A.link2ID(GetTradeSkillReagentItemLink(GetTradeSkillSelectionIndex(), reagentRecipeIndex))

	-- Continue only if the reagent is known
	if not reagentID or not A.data[reagentID] then return end

	-- If only one recipe is known for the reagent, use it
	if #(A.data[reagentID]) == 1 and not A.data[reagentID].spell then
		local numMakable, reagentIndex = A.numMakable(reagentID)
		if numMakable>0 then
			A.craft(reagentID,reagentRecipeIndex,reagentIndex,numMakable,chooseNumberToCraft)
			return
		end
		-- If we can make the item needed to make the reagent, open a window to make it
		-- one step recursion, enables to mill to create an ink
		if A.data[A.data[reagentID][1][1]] then
			A.externalCraftWindow(A.data[reagentID][1][1])
		else
			print("Sorry, can not make reagent...")
		end
	else
		A.externalCraftWindow(reagentID)
	end -- if
end -- function


---------------------------------------------------
-- Craft items
---------------------------------------------------
-- function used after choosing the number of reagent to craft
function A.SplitStack(owner,split)
	DoTradeSkill(owner.ReagentMaker_reagentIndex,tonumber(split))
	owner.ReagentMaker_reagentIndex = nil
end

-- Craft the reagent of an item, given it's position in the recipe
function A.craft(reagentID,reagentRecipeIndex,reagentIndex,numReagentMakable,chooseNumber)
	-- Look at how many we need to make one item for the selected recipe
	local numToMake = 1
	local selectedIndex = GetTradeSkillSelectionIndex()
	local skillName, skillType, numAvailable, isExpanded, serviceType, numSkillUps = GetTradeSkillInfo(selectedIndex)
	if numAvailable==0 then
		local reagentName, reagentTexture, reagentCount, playerReagentCount = GetTradeSkillReagentInfo(selectedIndex, reagentRecipeIndex)
		numToMake = math.min(reagentCount - playerReagentCount,numReagentMakable)
	end -- if

	-- Choose number or craft directly
	if chooseNumber and numReagentMakable>1 then
		-- the dialog window is linked to the reagent button
		local btn = _G["TradeSkillReagent"..reagentRecipeIndex]

		-- Store info to be able to run the function later
		btn.ReagentMaker_reagentIndex = reagentIndex

		-- Open dialog
		OpenStackSplitFrame(numReagentMakable, btn, "TOP", "BOTTOM")

		-- Fill in the number to make
		numToMake = tostring(numToMake)
		for i = 1,numToMake:len() do
			StackSplitFrame_OnChar(StackSplitFrame,numToMake:gsub(i,i))
		end
		StackSplitFrame.typing = 0 -- reinit the frame so tha the entered value will be erased on text entry
	else
		DoTradeSkill(reagentIndex,numToMake)
	end -- if
end -- function


-- Button hovering
function A.btnEntered(btn)
	-- Do not manage guild tradeskill
	if IsTradeSkillGuild() or IsTradeSkillLinked() then return end

	-- Index of the reagent in the recipe, taken from the button name
	local reagentRecipeIndex = A.buttonNumber(btn)

	-- ID of the reagent we want to craft
	local reagentLink = GetTradeSkillReagentItemLink(GetTradeSkillSelectionIndex(), reagentRecipeIndex)
	local reagentID = A.link2ID(reagentLink)

	-- Continue only if the reagent is known
	if not reagentID or not A.data[reagentID] then return end

	btn.textureHighlight:Show()

	if #(A.data[reagentID]) == 1 and not A.data[reagentID].spell then
		local numMakable = A.numMakable(reagentID)
		btn.label:SetText(numMakable)
		if numMakable==0 then
			btn.label:SetTextColor(1, 0, 0, 1)
		else
			btn.label:SetTextColor(0, 5, 0, 1)
		end
		btn.label:Show()
	end

	-- Tooltips
	local reagentIndex = A.findSkillIndex(reagentID)
	if reagentIndex then
		A.tooltipRecipe:SetOwner(btn)
		if A.data[reagentID].spell then
			A.tooltipRecipe:SetHyperlink(GetSpellLink(A.data[reagentID].spellID))
		else
			A.tooltipRecipe:SetHyperlink(GetTradeSkillRecipeLink(reagentIndex))
		end
		A.tooltipRecipe:Show()
		A.tooltipRecipe:ClearAllPoints()
		--A.tooltipRecipe:SetPoint("TOPLEFT",btn,"TOPRIGHT")
		A.tooltipRecipe:SetPoint("BOTTOMLEFT",GameTooltip,"BOTTOMRIGHT")

		--[[
		if #(A.data[reagentID]) == 1 then
			A.tooltipReagent:SetOwner(A.tooltipRecipe)
			local name, link, quality, iLevel, reqLevel, class, subclass, maxStack, equipSlot, texture, vendorPrice = GetItemInfo(A.data[reagentID][1][1])
			A.tooltipReagent:SetHyperlink(link)
			A.tooltipReagent:Show()
			A.tooltipReagent:ClearAllPoints()
			A.tooltipReagent:SetPoint("BOTTOMLEFT",A.tooltipRecipe,"BOTTOMRIGHT")
		end
		--]]
	end
end

function A.btnLeft(btn)
	btn.textureHighlight:Hide()
	btn.label:Hide()
	A.tooltipRecipe:Hide()
	--A.tooltipReagent:Hide()
end