view utils.lua @ 109:b5fd4a61ee64

Tagging as v1.0beta13
author contrebasse
date Thu, 02 Jun 2011 14:12:49 +0200
parents 618163a6d970
children d60d6b4cab0c
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
	--]]
	function A.numMakable(reagentID)
		-- No recipe
		if not A.data[reagentID] then return 0 end

		-- Many recipes
		local n = 0
		local itemCount
		for _,recipe in pairs(A.data[reagentID]) do
			if recipe[1] then -- only one reagent
				itemCount = GetItemCount(recipe[1])
				if not itemCount then return end
				n = n + math.floor(itemCount/recipe[2])
			else -- many reagents
				local m
				for _,reagent in pairs(recipe[2]) do
					itemCount = GetItemCount(reagent[1])
					if not itemCount then return end
					if not m then
						m = math.floor(itemCount/reagent[2])
					else
						m = math.min(m,math.floor(itemCount/reagent[2]))
					end
					if m==0 then break end
				end
				n = n + m
			end -- if
		end -- for
		return n
	end -- function

	-- 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 stateSaved
	local filtersState = {}
	local headersState = {}

	local function ApplyFilters()
		TradeSkillOnlyShowSkillUps(TradeSkillFrame.filterTbl.hasSkillUp);
		TradeSkillOnlyShowMakeable(TradeSkillFrame.filterTbl.hasMaterials);
		SetTradeSkillSubClassFilter(TradeSkillFrame.filterTbl.subClassValue, 1, 1);
		SetTradeSkillInvSlotFilter(TradeSkillFrame.filterTbl.slotValue, 1, 1);
		TradeSkillUpdateFilterBar();
		CloseDropDownMenus();
	end

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

		-- Save filters
		filtersState.text = GetTradeSkillItemNameFilter()
		filtersState.minLevel, filtersState.maxLevel = GetTradeSkillItemLevelFilter()
		filtersState.hasMaterials = TradeSkillFrame.filterTbl.hasMaterials
		filtersState.hasSkillUp = TradeSkillFrame.filterTbl.hasSkillUp
		filtersState.subClassValue = TradeSkillFrame.filterTbl.subClassValue
		filtersState.slotValue = TradeSkillFrame.filterTbl.slotValue

		-- Remove all filters
		SetTradeSkillItemNameFilter(nil)
		SetTradeSkillItemLevelFilter(0, 0)
		TradeSkillFrame.filterTbl.hasMaterials = false
		TradeSkillFrame.filterTbl.hasSkillUp = false
		TradeSkillFrame.filterTbl.subClassValue = -1
		TradeSkillFrame.filterTbl.slotValue = -1
		ApplyFilters()

		-- Headers
		headersState.headerName = headerName
		for i = GetNumTradeSkills(), 1, -1 do		-- 1st pass, expand all categories
			local skillName, skillType, _, isExpanded  = GetTradeSkillInfo(i)
			if (skillType == "header") and skillName==headerName then
				if not isExpanded then
					ExpandTradeSkillSubClass(i)
					table.insert(headersState,true)
				else
					table.insert(headersState,false)
				end
			end
		end

		stateSaved = true
		A.blockScan = nil

		--@todo Scroll down to the selected recipe
		-- with TradeSkillSkillXX:Show() ?
	end

	function A.RestoreActiveFilters()
		if not stateSaved then return end
		A.blockScan = true

		-- restore headers
		for i = GetNumTradeSkills(), 1, -1 do
			local skillName, skillType  = GetTradeSkillInfo(i)
			if (skillType == "header") and skillName==headersState.headerName and table.remove(headersState,1) then
					CollapseTradeSkillSubClass(i)
			end
		end
		wipe(headersState)

		-- restore filters
		SetTradeSkillItemNameFilter(filtersState.text)
		SetTradeSkillItemLevelFilter(filtersState.minLevel, filtersState.maxLevel)
		TradeSkillFrame.filterTbl.hasMaterials = filtersState.hasMaterials
		TradeSkillFrame.filterTbl.hasSkillUp = filtersState.hasSkillUp
		TradeSkillFrame.filterTbl.subClassValue = filtersState.subClassValue
		TradeSkillFrame.filterTbl.slotValue = filtersState.slotValue
		ApplyFilters()

		stateSaved = nil
		A.blockScan = nil

		--@todo Scroll down to the selected recipe
		-- with TradeSkillSkillXX:Show() ?
	end
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
--]]