annotate ReagentMaker.lua @ 81:32c398de6812

Update todo
author contrebasse
date Sun, 15 May 2011 01:35:38 +0200
parents 0e2606039c98
children cf1da2654dc6
rev   line source
contrebasse@25 1 local addonName, A = ...
contrebasse@25 2
contrebasse@25 3 -- @todo clean the A table
contrebasse@25 4 -- @todo check local copy of globals functions
contrebasse@25 5 -- @todo add support for dez ?
contrebasse@41 6 -- @todo add support for hidden recipes, removing filtering
contrebasse@25 7 -- @todo add support for cross tradeskill, like mining + forge/ingé
contrebasse@49 8 -- @todo when a reagent can not be crafted and the recipe has many reagents, do like ReverseEngeneering and go to this recipe (with a one step return button)
contrebasse@49 9 -- @todo scroll to the selected recipe on opening (usefull also for the previous todo)
contrebasse@49 10 -- @todo shift+clic on a reagent name while the serachbar is focused fills the serachbar with the reagent name (idem with the crafted item)
contrebasse@49 11 -- @todo add a button to clear search
contrebasse@81 12 -- @todo add a button to cast a campfire when coocking
contrebasse@25 13
contrebasse@25 14 ---------------------------------------------------
contrebasse@25 15 -- Variables
contrebasse@25 16 ---------------------------------------------------
contrebasse@25 17 -- Used by findglobals
contrebasse@25 18 -- GLOBALS: _G, CreateFrame, DEFAULT_CHAT_FRAME
contrebasse@25 19
contrebasse@25 20 -- Lua functions
contrebasse@25 21
contrebasse@25 22 -- Wow functions
contrebasse@25 23
contrebasse@25 24 -- constant vars
contrebasse@25 25
contrebasse@25 26 ---------------------------------------------------
contrebasse@25 27 -- Manage events
contrebasse@25 28 ---------------------------------------------------
contrebasse@25 29 A.EventsFrame = CreateFrame("Frame")
contrebasse@25 30
contrebasse@25 31 local SCAN_DELAY = 0.2
contrebasse@25 32 local t_throttle = SCAN_DELAY
contrebasse@25 33 local function throttleScan(self, t_elapsed)
contrebasse@25 34 t_throttle = t_throttle - t_elapsed
contrebasse@25 35 if t_throttle<0 then
contrebasse@25 36 self:SetScript("OnUpdate", nil)
contrebasse@25 37
contrebasse@25 38 -- Close the external window if the tradeskill changed
contrebasse@25 39 if A.currentTradeSkill ~= GetTradeSkillLine() then
contrebasse@25 40 A.MenuFrame:Hide()
contrebasse@25 41 end
contrebasse@25 42 if IsTradeSkillGuild() or IsTradeSkillLinked() then
contrebasse@25 43 A.MenuFrame:Hide()
contrebasse@25 44 return
contrebasse@25 45 end
contrebasse@25 46
contrebasse@25 47 -- Scan availabe recipes
contrebasse@27 48 -- Rescan in case of problem
contrebasse@27 49 if not A:ScanSimpleRecipes() then
contrebasse@27 50 t_throttle = SCAN_DELAY
contrebasse@27 51 self:SetScript("OnUpdate", throttleScan)
contrebasse@27 52 end
contrebasse@25 53
contrebasse@25 54 -- Show makables reagents
contrebasse@25 55 A.updateCounts(GetTradeSkillSelectionIndex())
contrebasse@25 56 end
contrebasse@25 57 end
contrebasse@25 58 A.EventsFrame:SetScript("OnEvent", function(self, event)
contrebasse@25 59 if event == "TRADE_SKILL_UPDATE" then
contrebasse@25 60 t_throttle = SCAN_DELAY
contrebasse@25 61 self:SetScript("OnUpdate", throttleScan)
contrebasse@25 62
contrebasse@25 63 elseif event == "TRADE_SKILL_SHOW" then
contrebasse@25 64 A:Initialize()
contrebasse@25 65 A.EventsFrame:UnregisterEvent("TRADE_SKILL_SHOW")
contrebasse@25 66 end -- if
contrebasse@25 67 end) -- function
contrebasse@25 68 A.EventsFrame:RegisterEvent("TRADE_SKILL_SHOW")
contrebasse@25 69 A.EventsFrame:RegisterEvent("TRADE_SKILL_UPDATE")
contrebasse@25 70
contrebasse@25 71 ---------------------------------------------------
contrebasse@25 72 -- Initialize
contrebasse@25 73 ---------------------------------------------------
contrebasse@25 74 function A:Initialize()
contrebasse@25 75
contrebasse@25 76 -- Register clics on reagent's buttons
contrebasse@25 77 for i=1,7 do
contrebasse@25 78 local btn = _G["TradeSkillReagent"..i]
contrebasse@25 79 btn:HookScript("OnDoubleClick", A.ProcessReagent)
contrebasse@25 80 btn:HookScript("OnEnter", A.btnEntered)
contrebasse@25 81 btn:HookScript("OnLeave", A.btnLeft)
contrebasse@25 82 btn.SplitStack = A.SplitStack
contrebasse@25 83
contrebasse@25 84 local textureHighlight = btn:CreateTexture()
contrebasse@25 85 textureHighlight:Hide()
contrebasse@25 86 textureHighlight:SetTexture("Interface\\BUTTONS\\CheckButtonHilight")
contrebasse@25 87 textureHighlight:SetBlendMode("ADD")
contrebasse@25 88 textureHighlight:SetAllPoints("TradeSkillReagent"..i.."IconTexture")
contrebasse@25 89 btn.textureHighlight = textureHighlight
contrebasse@25 90
contrebasse@25 91 local label = btn:CreateFontString(nil,"ARTWORK","GameFontHighlight")
contrebasse@25 92 label:SetSize(100,20)
contrebasse@25 93 label:SetPoint("TOPLEFT",btn,"TOPLEFT",4,-4)
contrebasse@25 94 label:SetJustifyH("LEFT")
contrebasse@25 95 label:SetJustifyV("TOP")
contrebasse@25 96 label:SetFont("Fonts\\FRIZQT__.TTF", 10, "OUTLINE")
contrebasse@25 97 btn.label = label
contrebasse@25 98 end -- for
contrebasse@25 99
contrebasse@77 100 -- Secondary Tooltip
contrebasse@25 101 A.tooltipRecipe = CreateFrame("GameTooltip", "ReagentMaker_tooltipRecipe",UIParent, "GameTooltipTemplate")
contrebasse@25 102 A.tooltipRecipe:SetFrameStrata("TOOLTIP")
contrebasse@25 103 A.tooltipRecipe:Hide()
contrebasse@77 104
contrebasse@77 105 -- Button for enchanting directy on a scroll
contrebasse@77 106 A.LoadEnchantOnScroll()
contrebasse@25 107 end -- function
contrebasse@25 108
contrebasse@25 109 -- Function run after selecting a item in the tradeskill window
contrebasse@25 110 function A.ProcessReagent(btn, ...)
contrebasse@25 111
contrebasse@25 112 -- Do not manage guild tradeskill
contrebasse@25 113 if IsTradeSkillGuild() or IsTradeSkillLinked() then return end
contrebasse@25 114
contrebasse@25 115 -- We want no modifiers, or shift to choose the number of reagent to craft
contrebasse@25 116 if IsModifierKeyDown() and not IsShiftKeyDown() then return end
contrebasse@25 117 local chooseNumberToCraft = IsShiftKeyDown()
contrebasse@25 118
contrebasse@25 119 --A.SaveActiveFilters()
contrebasse@25 120
contrebasse@25 121 -- Index of the reagent in the recipe, taken from the button name
contrebasse@25 122 local reagentRecipeIndex = A.buttonNumber(btn)
contrebasse@25 123
contrebasse@25 124 -- ID of the reagent we want to craft
contrebasse@46 125 local recipeIndex = GetTradeSkillSelectionIndex()
contrebasse@46 126 local reagentID = A.link2ID(GetTradeSkillReagentItemLink(recipeIndex, reagentRecipeIndex))
contrebasse@25 127
contrebasse@25 128 -- Continue only if the reagent is known
contrebasse@25 129 if not reagentID or not A.data[reagentID] then return end
contrebasse@25 130
contrebasse@25 131 -- If only one recipe is known for the reagent, use it
contrebasse@70 132 if #(A.data[reagentID]) == 1 and not A.data[reagentID][1].macro then
contrebasse@25 133 local numMakable, reagentIndex = A.numMakable(reagentID)
contrebasse@25 134 if not numMakable then
contrebasse@79 135 A.Error(A.L["The recipe to make the reagent seems to be hidden, it is not makable. Try to remove the filters on the recipes."])
contrebasse@25 136 return
contrebasse@25 137 end
contrebasse@25 138 if numMakable>0 then
contrebasse@46 139 A.craft(recipeIndex,reagentRecipeIndex,reagentIndex,numMakable,chooseNumberToCraft)
contrebasse@25 140 return
contrebasse@25 141 end
contrebasse@33 142
contrebasse@25 143 -- If we can make the item needed to make the reagent, open a window to make it
contrebasse@25 144 -- one step recursion, enables to mill to create an ink
contrebasse@70 145 if (not A.data[reagentID][1].manyReagents) and A.data[A.data[reagentID][1][1]] then
contrebasse@76 146 if A.externalCraftWindow(A.data[reagentID][1][1],reagentID) ~= false then
contrebasse@76 147 return
contrebasse@76 148 end
contrebasse@25 149 end
contrebasse@76 150
contrebasse@76 151 A.Error(A.L["You do not have enough reagents to craft [%s]"]:format(GetItemInfo(reagentID) or "item #"..reagentID))
contrebasse@38 152 return
contrebasse@25 153 else
contrebasse@25 154 A.externalCraftWindow(reagentID)
contrebasse@25 155 end -- if
contrebasse@25 156 --A.RestoreActiveFilters()
contrebasse@25 157 end -- function
contrebasse@25 158
contrebasse@25 159
contrebasse@25 160 ---------------------------------------------------
contrebasse@25 161 -- Craft items
contrebasse@25 162 ---------------------------------------------------
contrebasse@25 163 -- function used after choosing the number of reagent to craft
contrebasse@25 164 function A.SplitStack(owner,split)
contrebasse@25 165 DoTradeSkill(owner.ReagentMaker_reagentIndex,tonumber(split))
contrebasse@25 166 owner.ReagentMaker_reagentIndex = nil
contrebasse@25 167 end
contrebasse@25 168
contrebasse@25 169 -- Craft the reagent of an item, given it's position in the recipe
contrebasse@46 170 function A.craft(recipeIndex,reagentRecipeIndex,reagentIndex,numReagentMakable,chooseNumber)
contrebasse@25 171 -- Look at how many we need to make one item for the selected recipe
contrebasse@25 172 local numToMake = 1
contrebasse@46 173 local _, _, reagentCount, playerReagentCount = GetTradeSkillReagentInfo(recipeIndex, reagentRecipeIndex)
contrebasse@28 174 -- make enough reagents to craft one more item
contrebasse@46 175 numToMake = math.min(math.floor(playerReagentCount/reagentCount+1)*reagentCount-playerReagentCount,numReagentMakable)
contrebasse@25 176
contrebasse@46 177 -- take into account that some recipe craft more than one item
contrebasse@46 178 -- use the mean between min and max, but make at least one...
contrebasse@46 179 local minMade, maxMade = GetTradeSkillNumMade(reagentIndex)
contrebasse@46 180 numToMake = math.max(math.floor(2*numToMake/(maxMade+minMade)),1)
contrebasse@46 181
contrebasse@25 182 -- Choose number or craft directly
contrebasse@46 183 if chooseNumber then
contrebasse@25 184 -- the dialog window is linked to the reagent button
contrebasse@25 185 local btn = _G["TradeSkillReagent"..reagentRecipeIndex]
contrebasse@25 186
contrebasse@25 187 -- Store info to be able to run the function later
contrebasse@25 188 btn.ReagentMaker_reagentIndex = reagentIndex
contrebasse@25 189
contrebasse@25 190 -- Open dialog
contrebasse@25 191 OpenStackSplitFrame(numReagentMakable, btn, "TOP", "BOTTOM")
contrebasse@25 192
contrebasse@25 193 -- Fill in the number to make
contrebasse@25 194 numToMake = tostring(numToMake)
contrebasse@25 195 for i = 1,numToMake:len() do
contrebasse@25 196 StackSplitFrame_OnChar(StackSplitFrame,numToMake:gsub(i,i))
contrebasse@25 197 end
contrebasse@25 198 StackSplitFrame.typing = 0 -- reinit the frame so tha the entered value will be erased on text entry
contrebasse@25 199 else
contrebasse@25 200 DoTradeSkill(reagentIndex,numToMake)
contrebasse@25 201 end -- if
contrebasse@25 202 end -- function
contrebasse@25 203
contrebasse@25 204
contrebasse@25 205 -- Button hovering
contrebasse@25 206 function A.btnEntered(btn)
contrebasse@25 207 -- Do not manage guild tradeskill
contrebasse@25 208 if IsTradeSkillGuild() or IsTradeSkillLinked() then return end
contrebasse@25 209
contrebasse@25 210 -- Index of the reagent in the recipe, taken from the button name
contrebasse@25 211 local reagentRecipeIndex = A.buttonNumber(btn)
contrebasse@25 212
contrebasse@25 213 -- ID of the reagent we want to craft
contrebasse@25 214 local reagentLink = GetTradeSkillReagentItemLink(GetTradeSkillSelectionIndex(), reagentRecipeIndex)
contrebasse@25 215 local reagentID = A.link2ID(reagentLink)
contrebasse@25 216
contrebasse@25 217 -- Continue only if the reagent is known
contrebasse@25 218 if not reagentID or not A.data[reagentID] then return end
contrebasse@25 219
contrebasse@25 220 btn.textureHighlight:Show()
contrebasse@25 221
contrebasse@70 222 -- Check if the item is made by only one recipe. If not, return
contrebasse@73 223 if not A.isRecipeUnique(A.data[reagentID]) then return end
contrebasse@70 224
contrebasse@25 225 -- Tooltips
contrebasse@70 226 local link = A.data[reagentID][1].spellLink
contrebasse@25 227 if link then
contrebasse@25 228 A.tooltipRecipe:SetOwner(btn)
contrebasse@25 229 A.tooltipRecipe:SetHyperlink(link)
contrebasse@25 230 A.tooltipRecipe:Show()
contrebasse@25 231 A.tooltipRecipe:ClearAllPoints()
contrebasse@25 232 A.tooltipRecipe:SetPoint("BOTTOMLEFT",GameTooltip,"BOTTOMRIGHT")
contrebasse@25 233 end
contrebasse@25 234 end
contrebasse@25 235
contrebasse@25 236 function A.btnLeft(btn)
contrebasse@25 237 btn.textureHighlight:Hide()
contrebasse@25 238 A.tooltipRecipe:Hide()
contrebasse@25 239 end -- function
contrebasse@25 240
contrebasse@25 241 function A.updateCounts(recipeIndex)
contrebasse@25 242 -- Needs an argument
contrebasse@25 243 if not recipeIndex then return end
contrebasse@25 244
contrebasse@25 245 -- Do not manage guild tradeskill
contrebasse@25 246 if IsTradeSkillGuild() or IsTradeSkillLinked() then
contrebasse@25 247 for reagentRecipeIndex = 1,GetTradeSkillNumReagents(recipeIndex) do
contrebasse@70 248 -- If the normal tradeskill hasn't been opened yet, the field 'label' doesn't exists yet
contrebasse@70 249 local label = _G["TradeSkillReagent"..reagentRecipeIndex].label
contrebasse@70 250 if label then
contrebasse@70 251 label:Hide()
contrebasse@70 252 end
contrebasse@25 253 end
contrebasse@25 254 return
contrebasse@25 255 end
contrebasse@25 256
contrebasse@25 257 -- Count makable items and show it
contrebasse@25 258 for reagentRecipeIndex = 1,GetTradeSkillNumReagents(recipeIndex) do
contrebasse@25 259 -- ID of the reagent we want to craft
contrebasse@25 260 local reagentLink = GetTradeSkillReagentItemLink(recipeIndex, reagentRecipeIndex)
contrebasse@25 261 local reagentID = A.link2ID(reagentLink)
contrebasse@25 262
contrebasse@25 263 local label = _G["TradeSkillReagent"..reagentRecipeIndex].label
contrebasse@70 264 if not label then break end -- Shouldn't happen...
contrebasse@25 265
contrebasse@25 266 -- Continue only if the reagent is known
contrebasse@70 267 if not reagentID or not A.data[reagentID] then
contrebasse@25 268 label:Hide()
contrebasse@25 269 else
contrebasse@25 270 -- Count and show
contrebasse@25 271 local numMakable = A.numMakable(reagentID)
contrebasse@70 272 if not numMakable or #(A.data[reagentID]) ~= 1 or A.data[reagentID][1].macro then
contrebasse@70 273 label:SetText("?")
contrebasse@70 274 label:SetTextColor(0, 0.5, 1, 1) -- blue
contrebasse@25 275 else
contrebasse@70 276 label:SetText(numMakable)
contrebasse@70 277 if numMakable==0 then
contrebasse@70 278 label:SetTextColor(1, 0, 0, 1) -- red
contrebasse@70 279 else
contrebasse@70 280 label:SetTextColor(0, 1, 0, 1) -- green
contrebasse@70 281 end
contrebasse@25 282 end -- if
contrebasse@25 283 label:Show()
contrebasse@25 284 end -- if
contrebasse@25 285 end -- for
contrebasse@25 286 end -- function
contrebasse@25 287 hooksecurefunc("SelectTradeSkill",A.updateCounts)