diff ReagentMaker.lua @ 3:ed0582126cae

The base features semms to work.
author contrebasse
date Sat, 02 Apr 2011 01:49:39 +0200
parents 04c5b817eead
children cea9633a6d4e
line wrap: on
line diff
--- a/ReagentMaker.lua	Tue Mar 29 22:06:36 2011 +0200
+++ b/ReagentMaker.lua	Sat Apr 02 01:49:39 2011 +0200
@@ -27,196 +27,190 @@
 ---------------------------------------------------
 A.EventsFrame = CreateFrame("Frame")
 
-A.EventsFrame:SetScript("OnEvent", function(self, event)
-	if event == "TRADE_SKILL_SHOW" then
+A.EventsFrame:SetScript("OnEvent", function(self, event, ...)
+	if event == "TRADE_SKILL_UPDATE" then
+		-- Scan availabe recipes
+		A:ScanSimpleRecipes()
+
+		-- Close the external window if the tradeskill changed
+		if A.currentTradeSkill ~= GetTradeSkillLine() then
+			A.MenuFrame:Hide()
+		end
+	elseif event == "TRADE_SKILL_SHOW" then
 		A:Initialize()
 	end -- if
-end); -- function
+end) -- function
 A.EventsFrame:RegisterEvent("TRADE_SKILL_SHOW")
-
+A.EventsFrame:RegisterEvent("TRADE_SKILL_UPDATE")
 
 ---------------------------------------------------
 -- Initialize
 ---------------------------------------------------
 local toInitialize = true
 function A:Initialize()
-	-- This part is done the first time only
-	if toInitialize then
+	A.EventsFrame:UnregisterEvent("TRADE_SKILL_SHOW")
 
-		-- Register clics on reagent's buttons
-		for i=1,7 do
-			local button = _G["TradeSkillReagent"..i];
-			button:HookScript("OnClick", function(btn) A.ToggleMenu(btn, i) end);
-			--button:HookScript("OnEnter", function() self:Entered(button, i) end)
-			--button:HookScript("OnLeave", function() self:Left(button, i) end)
-		end -- for
+	-- 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
 
-		toInitialize = nil
+		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 avter selecting a item in the tradeskill window
+function A.ProcessReagent(btn, ...)
+	-- 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
-
-	-- Scan availabe recipes
-	A:ScanSimpleRecipes()
 end -- function
 
 
 ---------------------------------------------------
 -- Craft items
 ---------------------------------------------------
-do
-	-- WoW functions
-	local GetNumTradeSkills = GetNumTradeSkills
-	local GetTradeSkillInfo = GetTradeSkillInfo
-	local GetTradeSkillItemLink = GetTradeSkillItemLink
-
-
-	function A.findSkillIndex(itemID)
-		for i = 1,GetNumTradeSkills() do
-			local skillName, skillType, numAvailable, isExpanded, serviceType, numSkillUps = GetTradeSkillInfo(i)
-			if skillType == "header" then
-			else
-				if skillName then
-					local ID = A.link2ID(GetTradeSkillItemLink(i))
-					if ID and ID == itemID then
-						return i
-					end -- if
-				end -- if
-			end -- if
-		end -- for
-		A.DEBUG("Tradeskill not found")
-	end -- function
-end -- do
-
-
--- http://www.wowwiki.com/RunSlashCmd
-local _G = _G
-function RunSlashCmd(cmd)
-  local slash, rest = cmd:match("^(%S+)%s*(.-)$")
-  for name, func in pairs(SlashCmdList) do
-     local i, slashCmd = 1
-     repeat
-        slashCmd, i = _G["SLASH_"..name..i], i + 1
-        if slashCmd == slash then
-				A.DEBUG("Spell found !")
-           return true, func(rest)
-        end
-     until not slashCmd
-  end
-A.DEBUG("Spell not found :(")
+-- 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
 
-do
-	-- WoW functions
-	local DoTradeSkill = DoTradeSkill
+-- 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
 
-	local function SplitStack(owner,split)
-		DoTradeSkill(owner.ReagentMakerIndex,tonumber(split))
+	-- 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)
+	-- 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
 
-	function A.craft(btn,...)
-		local index = A.findSkillIndex(btn:GetAttribute(target-item))
-		if index then
-			local skillName, skillType, numAvailable, isExpanded, serviceType, numSkillUps = GetTradeSkillInfo(index)
-			if IsShiftKeyDown() and numAvailable>1 and not spell then
-				if not btn.SplitStack then
-					btn.SplitStack = SplitStack
-				end
-				btn.ReagentMakerIndex = index
-
-				OpenStackSplitFrame(numAvailable, btn, "TOPLEFT", "TOPRIGHT")
-			else
-				DoTradeSkill(index,1)
-			end
-		end
-	end -- function
-end -- do
-
-
----------------------------------------------------
--- Manage the popup menu
----------------------------------------------------
-do
-	-- Lua functions
-	local tonumber = tonumber
-
-	-- WoW functions
-	local IsModifierKeyDown = IsModifierKeyDown
-	local GetTradeSkillReagentItemLink = GetTradeSkillReagentItemLink
-	local GetTradeSkillSelectionIndex = GetTradeSkillSelectionIndex
-
-	-- Toggles the reagent's menu
-	function A.ToggleMenu(button, index)
-		-- We want no modifiers
-		if IsModifierKeyDown() then return end
-
-		if A.menuIsOpen(button) then
-			A.menuClose()
-		else
-			local itemID = A.link2ID(GetTradeSkillReagentItemLink(GetTradeSkillSelectionIndex(), index))
-			if itemID and A.data[itemID] then
-				A.FillMenu(itemID)
-				A.menuOpen(button)
-			else
-				if A.menuIsOpen() then
-					A.menuClose()
-				end
-			end -- if
-		end -- if
-	end -- function
-end -- do
-
-do
-	-- Lua functions
-	local type = type
-
-	-- Wow functions
-	local GetItemInfo = GetItemInfo
-
-	-- persistent vars
-	local Minfo = {notCheckable = true, func = A.craft}
-
-	-- function to define the menu items
-	function A.FillMenu(itemID)
-		-- Title
-		--local itemName, link, quality, iLevel, reqLevel, class, subclass, maxStack, equipSlot, texture, vendorPrice = GetItemInfo(itemID)
-		--Tinfo.text = "Make "..link.." with:"
-
-		-- Loop over the available reciepes
-		for spellID,reagents in pairs(A.data[itemID]) do
-			--name, link, quality, iLevel, reqLevel, class, subclass, maxStack, equipSlot, texture, vendorPrice = GetItemInfo(itemID) or GetItemInfo("itemName") or GetItemInfo("itemLink")
-			-- Right now, manage only sub-recipes with only one reagent
-			if type(reagents[1]) == "number" then
-				local name, link, quality, iLevel, reqLevel, class, subclass, maxStack, equipSlot, texture, vendorPrice = GetItemInfo(reagents[1])
-
-				if link then
-					local text = reagents[2].."x |T"..texture..":0|t "..link
-
-					if reagents.spell then
-						-- Special spell
-						--	Minfo.func = RunMacroText
-						--	Minfo.arg1 = reagents.spell..itemName
-						--	Minfo.arg2 = nil
-						A.menuAddItem(text,action,itemID)
-					else
-						-- Standard tradeskill spell
-						A.menuAddItem(text,A.craft,itemID)
-					end -- if
-				else
-					A.DEBUG("Erreur, objet inconnu :"..reagents[1])
-				end -- if
-			end -- if
-		end -- for
-	end -- function
-end -- do
-
-
----------------------------------------------------
--- Manage the tradeskill window state
----------------------------------------------------
-function A:SaveState()
+	--[[
+	A.tooltipRecipe:SetOwner(btn)
+	A.tooltipRecipe:SetHyperlink(GetTradeSkillRecipeLink(A.findSkillIndex(reagentID)))
+	A.tooltipRecipe:SetHyperlink(reagentLink)
+	A.tooltipRecipe:ClearAllPoints()
+	A.tooltipRecipe:SetPoint("TOPLEFT",btn,"TOPRIGHT")
+	--A.tooltipRecipe:SetSize(100,100)
+	A.tooltipRecipe:Show()
+	A.tooltipReagent:SetOwner(btn)
+	A.tooltipReagent:SetHyperlink(reagentLink)
+	A.tooltipReagent:ClearAllPoints()
+	A.tooltipReagent:SetPoint("TOPLEFT",A.tooltipRecipe,"TOPRIGHT")
+	A.tooltipReagent:Show()
+	--]]
 end
 
-function A:SetState(state)
+function A.btnLeft(btn)
+	btn.textureHighlight:Hide()
+	btn.label:Hide()
+	--[[
+	A.tooltipRecipe:Hide()
+	A.tooltipReagent:Hide()
+	--]]
 end
-
-function A:ExpandAll()
-end