view ReagentMaker.lua @ 1:5fc29ed07094

First atempt at using secure menus, it doesn't open yet...
author contrebasse
date Mon, 28 Mar 2011 22:59:20 +0200
parents eba26c900e99
children 04c5b817eead
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_SHOW" then
		A:Initialize()
	end -- if
end); -- function
A.EventsFrame:RegisterEvent("TRADE_SKILL_SHOW")


---------------------------------------------------
-- Initialize
---------------------------------------------------
local toInitialize = true
function A:Initialize()
	-- This part is done the first time only
	if toInitialize then

		-- Register clics on reagent's buttons
		for i=1,7 do
			local button = _G["TradeSkillReagent"..i];
			button:HookScript("OnClick", function() self:ToggleMenu(button, i) end);
			--button:HookScript("OnEnter", function() self:Entered(button, i) end)
			--button:HookScript("OnLeave", function() self:Left(button, i) end)
		end -- for

		toInitialize = nil
	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 :(")
end

do
	-- WoW functions
	local DoTradeSkill = DoTradeSkill

	local function SplitStack(owner,split)
		DoTradeSkill(owner.ReagentMakerIndex,tonumber(split))
	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
	local ToggleDropDownMenu = ToggleDropDownMenu

	-- Toggles the reagent's menu
	function A:ToggleMenu(button, index)
		-- We want no modifiers
		if IsModifierKeyDown() then return end

		local itemID = A.link2ID(GetTradeSkillReagentItemLink(GetTradeSkillSelectionIndex(), index))
		if itemID and A.data[itemID] then
			A.FillMenu(itemID)
			A.menuOpen(button)
		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()
end

function A:SetState(state)
end

function A:ExpandAll()
end