diff utils.lua @ 108:618163a6d970 v1.0beta13

Always show counts and bypass all filtering and searches
author contrebasse
date Thu, 02 Jun 2011 14:12:06 +0200
parents d64a19e5a47d
children d60d6b4cab0c
line wrap: on
line diff
--- a/utils.lua	Thu Jun 02 00:57:17 2011 +0200
+++ b/utils.lua	Thu Jun 02 14:12:06 2011 +0200
@@ -43,6 +43,7 @@
 	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)
@@ -52,6 +53,36 @@
 		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)
@@ -77,40 +108,6 @@
 	local filtersState = {}
 	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
-
 	local function ApplyFilters()
 		TradeSkillOnlyShowSkillUps(TradeSkillFrame.filterTbl.hasSkillUp);
 		TradeSkillOnlyShowMakeable(TradeSkillFrame.filterTbl.hasMaterials);
@@ -124,12 +121,16 @@
 		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
@@ -137,31 +138,28 @@
 		ApplyFilters()
 
 		-- Headers
-		--local headerCount = 0		-- use a counter to avoid being bound to header names, which might not be unique.
 		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
+			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() ?
-
-		stateSaved = true
-
-		A.blockScan = nil
 	end
 
 	function A.RestoreActiveFilters()
 		if not stateSaved then return end
-
 		A.blockScan = true
 
 		-- restore headers
@@ -174,18 +172,19 @@
 		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
 
-		--print("restored")
-
-		stateSaved = nil
-
-		A.blockScan = nil
+		--@todo Scroll down to the selected recipe
+		-- with TradeSkillSkillXX:Show() ?
 	end
 end