view utils.lua @ 86:d571fa85e1b5

Added tag v1.0beta4 for changeset 996aaa818d04
author contrebasse
date Sun, 15 May 2011 12:46:22 +0200
parents 996aaa818d04
children 8dd86b6b76d8
line wrap: on
line source
local addonName, A = ...

-- Lua functions
local tonumber = tonumber
local select = select
local sfind = string.find

-- Wow functions

-- DEBUG Print
function A.DEBUG(msg)
	-- GLOBALS: DEFAULT_CHAT_FRAME
	DEFAULT_CHAT_FRAME:AddMessage(msg or "nil",1,0,0)
end -- function

-- Messages to the user
function A.Warn(msg)
	if not msg then return end
	local event = "UI_INFO_MESSAGE"
	UIErrorsFrame_OnEvent(UIErrorsFrame, event, msg)
end -- function
function A.Error(msg)
	if not msg then return end
	local event = "UI_ERROR_MESSAGE"
	UIErrorsFrame_OnEvent(UIErrorsFrame, event, msg)
end -- function

-- Returns the item ID from its link
function A.link2ID(link)
	return tonumber(select(3,sfind(link or "", "-*:(%d+)[:|].*")) or "")
end -- function

-- Returns the button number for the reagents buttons
function A.buttonNumber(btn)
	-- "TradeSkillReagentN"
	return tonumber(btn:GetName():sub(-1))
end

do
	-- Wow functions
	local GetTradeSkillInfo = GetTradeSkillInfo
	local GetNumTradeSkills = GetNumTradeSkills
	local GetTradeSkillItemLink = GetTradeSkillItemLink

	-- Gives the number of craftable objects
	function A.numMakable(reagentID)
		-- Look for the recipe to make the item
		local reagentIndex = A.findSkillIndex(reagentID)
		if not reagentIndex then return end

		-- Check how many items we can craft
		local skillName, skillType, numReagentMakable, isExpanded, serviceType, numSkillUps = GetTradeSkillInfo(reagentIndex)
		return numReagentMakable, reagentIndex
	end

	-- Find the first tradeskill index of the recipe to make an item
	function A.findSkillIndex(itemID)
		for i = 1,GetNumTradeSkills() do
			local _, skillType = GetTradeSkillInfo(i)
			if skillType == "header" then
			else
				local ID = A.link2ID(GetTradeSkillItemLink(i))
				if ID and ID == itemID then
					return i
				end -- if
			end -- if
		end -- for
	end -- function
end -- do


-- Taken from Datastore_Crafts
-- *** Scanning functions ***
do
	local selectedTradeSkillIndex
	local subClasses, subClassID
	local invSlots, invSlotID
	local haveMats
	local headersState = {}

	local function GetSubClassID()
		-- The purpose of this function is to get the subClassID in a UI independant way
		-- ie: without relying on UIDropDownMenu_GetSelectedID(TradeSkillSubClassDropDown), which uses a hardcoded frame name.

		if GetTradeSkillSubClassFilter(0) then		-- if "All Subclasses" is selected, GetTradeSkillSubClassFilter() will return 1 for all indexes, including 0
			return 1				-- thus return 1 as selected id	(as would be returned by UIDropDownMenu_GetSelectedID(TradeSkillSubClassDropDown))
		end

		local filter
		for i = 1, #subClasses do
			filter = GetTradeSkillSubClassFilter(i)
			if filter then
				return i+1			-- ex: 3rd element of the subClasses array, but 4th in the dropdown due to "All Subclasses", so return i+1
			end
		end
	end

	local function GetInvSlotID()
		-- The purpose of this function is to get the invSlotID in a UI independant way	(same as GetSubClassID)
		-- ie: without relying on UIDropDownMenu_GetSelectedID(TradeSkillInvSlotDropDown), which uses a hardcoded frame name.

		if GetTradeSkillInvSlotFilter(0) then		-- if "All Slots" is selected, GetTradeSkillInvSlotFilter() will return 1 for all indexes, including 0
			return 1				-- thus return 1 as selected id	(as would be returned by  UIDropDownMenu_GetSelectedID(TradeSkillInvSlotDropDown))
		end

		local filter
		for i = 1, #invSlots do
			filter = GetTradeSkillInvSlotFilter(i)
			if filter then
				return i+1			-- ex: 3rd element of the invSlots array, but 4th in the dropdown due to "All Slots", so return i+1
			end
		end
	end

	function A.SaveActiveFilters(headerName)
		A.blockScan = true

		--[[
		print("save")
		print(GetNumTradeSkills())
		selectedTradeSkillIndex = GetTradeSkillSelectionIndex()

		subClasses = { GetTradeSkillSubClasses() }
		invSlots = { GetTradeSkillInvSlots() }
		subClassID = GetSubClassID()
		invSlotID = GetInvSlotID()

		-- Subclasses
		SetTradeSkillSubClassFilter(0, 1, 1)	-- this checks "All subclasses"
		if TradeSkillSubClassDropDown then
			UIDropDownMenu_SetSelectedID(TradeSkillSubClassDropDown, 1)
		end
		print(GetNumTradeSkills())

		-- Inventory slots
		SetTradeSkillInvSlotFilter(0, 1, 1)		-- this checks "All slots"
		if TradeSkillInvSlotDropDown then
			UIDropDownMenu_SetSelectedID(TradeSkillInvSlotDropDown, 1)
		end
		print(GetNumTradeSkills())

		-- Have Materials
		if TradeSkillFrameAvailableFilterCheckButton then
			haveMats = TradeSkillFrameAvailableFilterCheckButton:GetChecked()	-- nil or true
			TradeSkillFrameAvailableFilterCheckButton:SetChecked(false)
		end
		TradeSkillOnlyShowMakeable(false)
		print(GetNumTradeSkills())
		--]]

		-- Headers
		--local headerCount = 0		-- use a counter to avoid being bound to header names, which might not be unique.
		for i = GetNumTradeSkills(), 1, -1 do		-- 1st pass, expand all categories
			local skillName, skillType, _, isExpanded  = GetTradeSkillInfo(i)
			 if (skillType == "header") then
				--headerCount = headerCount + 1
				if not isExpanded and skillName==headerName then
					ExpandTradeSkillSubClass(i)
					--print(GetNumTradeSkills())
					--headersState[headerCount] = true
				end
			end
		end

		--print("saved")

		A.blockScan = nil
	end

	function A.RestoreActiveFilters()
		A.blockScan = true

		print("restore")
		-- Subclasses
		SetTradeSkillSubClassFilter(subClassID-1, 1, 1)	-- this checks the previously checked value

		local frame = TradeSkillSubClassDropDown
		if frame then	-- other addons might nil this frame (delayed load, etc..), so secure DDM calls
			local text = (subClassID == 1) and ALL_SUBCLASSES or subClasses[subClassID-1]
			UIDropDownMenu_SetSelectedID(frame, subClassID)
			UIDropDownMenu_SetText(frame, text);
		end

		subClassID = nil
		wipe(subClasses)
		subClasses = nil

		-- Inventory slots
		invSlotID = invSlotID or 1
		SetTradeSkillInvSlotFilter(invSlotID-1, 1, 1)	-- this checks the previously checked value

		frame = TradeSkillInvSlotDropDown
		if frame then
			local text = (invSlotID == 1) and ALL_INVENTORY_SLOTS or invSlots[invSlotID-1]
			UIDropDownMenu_SetSelectedID(frame, invSlotID)
			UIDropDownMenu_SetText(frame, text);
		end

		invSlotID = nil
		wipe(invSlots)
		invSlots = nil

		-- Have Materials
		if TradeSkillFrameAvailableFilterCheckButton then
			TradeSkillFrameAvailableFilterCheckButton:SetChecked(haveMats or false)
		end
		TradeSkillOnlyShowMakeable(haveMats or false)
		haveMats = nil

		SelectTradeSkill(selectedTradeSkillIndex)
		selectedTradeSkillIndex = nil

		-- Headers
		local headerCount = 0
		for i = GetNumTradeSkills(), 1, -1 do
			local _, skillType  = GetTradeSkillInfo(i)
			if (skillType == "header") then
				headerCount = headerCount + 1
				if headersState[headerCount] then
					CollapseTradeSkillSubClass(i)
				end
			end
		end
		wipe(headersState)
	end
	print("restored")
	
	A.blockScan = nil
end

function A.isRecipeUnique(itemData)
	local unique = true
	
	-- Check if the item is made by only one recipe. If not, return
	if #itemData>1 then
		local spellLink
		for _,v in ipairs(itemData) do
			if not spellLink then
				spellLink = v.spellLink
			else
				if v.spellLink ~= spellLink then
					unique = nil
					break
				end
			end
		end
	end
	
	return unique
end

--[[
function A.isTradeskillUnique(itemData)
	local spellName = itemData[1].spellName
	
	-- Check if the item is made by only one recipe. If not, return
	if #itemData>1 then
		for _,v in ipairs(itemData) do
			if v.spellName ~= spellName then
				spellName = nil
				break
			end
		end
	end
	
	return spellName
end
--]]