Fixed non saving 'un/known' flag

Wobin [04-12-14 - 14:07]
Fixed non saving 'un/known' flag
Added in used recipe results in tooltips
Reworked base engine to not use OnUpdate
Filename
SousChef.lua
diff --git a/SousChef.lua b/SousChef.lua
index b8a6ae8..213c759 100644
--- a/SousChef.lua
+++ b/SousChef.lua
@@ -19,10 +19,14 @@ local COOKING_RANK_5 = [[SousChef\media\Five.dds]]
 local COOKING_RANK_6 = [[SousChef\media\Six.dds]]
 local COOKING = { COOKING_RANK_1, COOKING_RANK_2, COOKING_RANK_3, COOKING_RANK_4, COOKING_RANK_5, COOKING_RANK_6 }
 local CANLEARN = [[/esoui/art/loot/loot_finesseitem.dds]]
+local containerHooks = { INVENTORY_BACKPACK, INVENTORY_BANK, INVENTORY_GUILD_BANK }
+local itemQuality = { ITEM_QUALITY_MAGIC = { 0, 0, 1 }, ITEM_QUALITY_NORMAL = {1,1,1}, ITEM_QUALITY_ARCANE = {1, 0, 1}}
 local Pantry = {}
 local Cookbook = {}
+local ReverseCookbook = {}
 local settings = nil
 local slotLines = {}
+local hookedFunctions = {}

 local function GetItemID(link)
 	return tonumber(string.match(string.match(link, "%d+:"), "%d+"))
@@ -36,21 +40,21 @@ local function MatchInGlobalCookbook(bag, slot)
 	return settings.Cookbook[GetItemName(bag,slot):lower():gsub(" ", "")]
 end

-local function AddRankToSlot(row, isGrid)
+local function AddRankToSlot(row)
 	local bagId = row.dataEntry.data.bagId
 	local slotIndex = row.dataEntry.data.slotIndex

 	local rankIcon = slotLines[row:GetName()]
-	if(not rankIcon) then
+	if(not rankIcon) then
 		rankIcon =  WINDOW_MANAGER:CreateControl(row:GetName() .. "SousChef", row, CT_TEXTURE)
 		slotLines[row:GetName()] = rankIcon
 	end

 	-- Allow for ingeniousclown's Inventory Grid View
-	if isGrid then
+	if row:GetWidth() - row:GetHeight() < 5 then	-- if we're mostly square
 		rankIcon:SetDimensions(20,20)
 		rankIcon:SetAnchor(TOPLEFT, row, TOPLEFT, 2)
-	else
+	else
 		rankIcon:SetDimensions(30, 30)
 		rankIcon:SetAnchor(CENTER, row, CENTER, 200)
 	end
@@ -63,6 +67,8 @@ local function AddRankToSlot(row, isGrid)
 			rankIcon:SetColor(settings.colour[1], settings.colour[2], settings.colour[3])
 			rankIcon:SetHidden(false)
 			rankIcon:SetTexture(COOKING[texture])
+
+
 		end
 	else
 		if GetItemType(bagId, slotIndex) == ITEMTYPE_RECIPE then
@@ -83,67 +89,37 @@ local function AddRankToSlot(row, isGrid)
 	end
 end

-local function AddRanks(self)
-	for _,v in pairs(self.activeControls) do
-		AddRankToSlot(v, self.isGrid)
-	end
-end
-
-
-local function CheckNow(self)
-	if(#self.activeControls > 0 and not self:IsHidden()) then
-        AddRanks(self)
-    end
-end
-
-local bufferTime = 100 --ms
-local elapsedTime = 0
-
-function SousChefOnUpdate()
-	elapsedTime = elapsedTime + GetFrameDeltaTimeMilliseconds()
-	if(elapsedTime < bufferTime) then return end
-	elapsedTime = 0
-
-	if(not settings.watching or
-		(BANK:IsHidden() and BACKPACK:IsHidden() and GUILD_BANK:IsHidden() )) then
-		return
-	end
-
-	CheckNow(BANK)
-	CheckNow(BACKPACK)
-	CheckNow(GUILD_BANK)
-end
-
-local function InventoryUpdate( ... )
-	if(settings.watching and
-		not (BANK:IsHidden() and BACKPACK:IsHidden() and GUILD_BANK:IsHidden() )) then
-		SousChef_Controller:SetHandler("OnUpdate", SousChefOnUpdate)
-	end
-end
-
-local function ActiveLayerPush( ... )
-	if(settings.watching) then
-		SousChef_Controller:SetHandler("OnUpdate", SousChefOnUpdate)
+local function AddDetails(row)
+	if not row.dataEntry or not row.dataEntry.data then return end
+	local bagId = row.dataEntry.data.bagId
+	local slotIndex = row.dataEntry.data.slotIndex
+	if ((GetItemCraftingInfo(bagId, slotIndex)) ~= CRAFTING_TYPE_PROVISIONING) then	return end
+	local usableIngredient = ReverseCookbook[GetItemID(GetItemLink(bagId, slotIndex))]
+	if usableIngredient then
+		ItemTooltip:AddLine("Used in:")
+		for i,v in ipairs(usableIngredient) do
+			ItemTooltip:AddLine(v)
+		end
 	end
 end

-local function ActiveLayerPop( ... )
-	SousChef_Controller:SetHandler("OnUpdate", nil)
-end
-
-
 local function ParseRecipes()
 	local lists = GetNumRecipeLists()
 	for listIndex = 1, lists do
 		local name, count = GetRecipeListInfo(listIndex)
 		for recipeIndex = 1, count do
 			if GetRecipeInfo(listIndex, recipeIndex) then
+				-- Store the recipes known
 				Cookbook[(GetRecipeResultItemInfo(listIndex, recipeIndex)):gsub(" ",""):lower() .. "recipe"] = true
 				settings.Cookbook[(GetRecipeResultItemInfo(listIndex, recipeIndex)):gsub(" ",""):lower() .. "recipe"] = true
 				local _, _, ingredientCount, level = GetRecipeInfo(listIndex, recipeIndex)
 				for ingredientIndex = 1, ingredientCount do
 					local link = GetItemID(GetRecipeIngredientItemLink(listIndex, recipeIndex, ingredientIndex, LINK_STYLE_NORMAL))
+					-- Store the fact that the ingredient is used
 					Pantry[link] = level
+					-- Store the recipe it's used in
+					if not ReverseCookbook[link] then ReverseCookbook[link] = {} end
+					table.insert(ReverseCookbook[link], GetRecipeResultItemLink(listIndex, recipeIndex, LINK_STYLE_BRACKETS))
 				end
 			end

@@ -159,7 +135,7 @@ local function SousChefCreateSettings()
   	LAM:AddDropdown(panel, "markLearnt", "Mark if recipes are ",
 						"How do you want Sous Chef to indicate your knowledge of a recipe?",
 						{"known", "unknown"}, function() return settings.checkKnown end,
-						function(self,valueString) settings.checkKnown = valueString end)
+						function(valueString) settings.checkKnown = valueString end)
 	LAM:AddCheckbox(panel, "markAltKnows", "Alternate Character Check", "Indicate if an alt knows the recipe on unknown recipes. Will only work if the above setting is set to 'unknown'",
 						function() return settings.markAlt end,
 						function(value) settings.markAlt = not settings.markAlt end)
@@ -170,7 +146,6 @@ local function SousChefCreateSettings()

 end

-
 local function SousChef_Loaded(eventCode, addOnName)

 	if(addOnName ~= "SousChef") then
@@ -185,8 +160,7 @@ local function SousChef_Loaded(eventCode, addOnName)
 		Cookbook = {}
 	}

-	--settings = ZO_SavedVars:New("SousChef_Settings", 1, nil, defaults)
-	settings = ZO_SavedVars:NewAccountWide("SousChef_Settings", 1, "Cookbook", defaults)
+	settings = ZO_SavedVars:NewAccountWide("SousChef_Settings", 3, nil, defaults)

     SLASH_COMMANDS["/SousChef"] = function()
     	settings.watching = not settings.watching
@@ -202,27 +176,44 @@ local function SousChef_Loaded(eventCode, addOnName)
     		end
     	end
 	end
-	SLASH_COMMANDS["/SousDebug"] = function()
-	d("Debug mode on")
-	SCCookbook = Cookbook
-	SCPantry = Pantry
-	SCslotList = slotList
-	SCsettings = settings
+	SLASH_COMMANDS["/SCDebug"] = function()
+		d("Debug mode on")
+		SCCookbook = Cookbook
+		SCRCookbook = ReverseCookbook
+		SCPantry = Pantry
+		SCslotList = slotLines
+		SCsettings = settings
 	end
-
-
+
 	SousChefCreateSettings()

 	ParseRecipes()

-	SLASH_COMMANDS["/sc"] = SLASH_COMMANDS["/SousChef"]
-	EVENT_MANAGER:RegisterForEvent("SCActiveLayerPush", EVENT_ACTION_LAYER_PUSHED, ActiveLayerPush)
-	EVENT_MANAGER:RegisterForEvent("SCActiveLayerPop", EVENT_ACTION_LAYER_POPPED, ActiveLayerPop)
-	EVENT_MANAGER:RegisterForEvent("SCInventoryUpdate", EVENT_INVENTORY_SINGLE_SLOT_UPDATE, InventoryUpdate)
-	EVENT_MANAGER:RegisterForEvent("SCGuildBankReady", EVENT_GUILD_BANK_ITEMS_READY, InventoryUpdate)
-	EVENT_MANAGER:RegisterForEvent("SCGuildBankAdded", EVENT_GUILD_BANK_ITEM_ADDED, InventoryUpdate)
-	EVENT_MANAGER:RegisterForEvent("SCGuildBankRemoved", EVENT_GUILD_BANK_ITEM_REMOVED, InventoryUpdate)
-	EVENT_MANAGER:RegisterForEvent("SCLearntRecipe", EVENT_RECIPE_LEARNED, ParseRecipes)
+	SLASH_COMMANDS["/sc"] = SLASH_COMMANDS["/SousChef"]
+
+	-- Now we want to hook into the function that sets the details on the inventory slot
+	for _,v in pairs(PLAYER_INVENTORY.inventories) do
+		local listView = v.listView
+		if listView and listView.dataTypes and listView.dataTypes[1] then
+			hookedFunctions = listView.dataTypes[1].setupCallback
+
+			listView.dataTypes[1].setupCallback =
+				function(rowControl, slot)
+					hookedFunctions(rowControl, slot)
+					AddRankToSlot(rowControl)
+				end
+		end
+	end
+	ZO_ScrollList_RefreshVisible(BACKPACK)
+	ZO_ScrollList_RefreshVisible(BANK)
+	ZO_ScrollList_RefreshVisible(GUILD_BANK)
+
+	-- Hook into the tooltip
+	local tooltip = ZO_InventorySlot_OnMouseEnter
+	ZO_InventorySlot_OnMouseEnter = function(control)
+		tooltip(control)
+		AddDetails(control)
+	end
 end

 local function SousChef_Initialized()