Mercurial > wow > reagentmaker
diff ReagentMaker.lua @ 0:eba26c900e99
Initial commit, save state before using secure menus
author | contrebasse |
---|---|
date | Mon, 28 Mar 2011 22:32:26 +0200 |
parents | |
children | 5fc29ed07094 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ReagentMaker.lua Mon Mar 28 22:32:26 2011 +0200 @@ -0,0 +1,231 @@ +local addonName, A = ... + +-- @todo clean the A table +-- @todo check local copy of globals functions +-- @todo add support for sub-recipes (like ink plus milling) +-- @todo show a tooltip +-- @todo show number of craftable items +-- @todo show owned items +-- @todo add support for dez ? +-- @todo add support for cross tradeskill, like mining + forge/ingé +-- @todo icon on reagent's button + +--------------------------------------------------- +-- Variables +--------------------------------------------------- +-- Used by findglobals +-- GLOBALS: _G, CreateFrame, DEFAULT_CHAT_FRAME + +-- Lua functions + +-- Wow functions + +-- constant vars + +--------------------------------------------------- +-- Manage events +--------------------------------------------------- +A.EventsFrame = CreateFrame("Frame") + +A.EventsFrame:SetScript("OnEvent", function(self, event) + if event == "TRADE_SKILL_SHOW" then + A:Initialize() + end -- if +end); -- function +A.EventsFrame:RegisterEvent("TRADE_SKILL_SHOW") + + +--------------------------------------------------- +-- Initialize +--------------------------------------------------- +function A:Initialize() + -- This part is done the first time only + if not self.MenuFrame then + -- Prepare the menu + self.MenuFrame = CreateFrame("Frame", "ReagentMakerDropDownMenu"); -- Needs a global name + self.MenuFrame.displayMode = "MENU"; + self.MenuFrame.initialize = self.FillMenu; + self.MenuFrame.point = "TOPLEFT"; + self.MenuFrame.relativePoint = "TOPRIGHT"; + + -- Register clics on reagent's buttons + for i=1,7 do + local button = _G["TradeSkillReagent"..i]; + button:HookScript("OnClick", function() self:ToggleMenu(button, i) end); + --button:HookScript("OnEnter", function() self:Entered(button, i) end) + --button:HookScript("OnLeave", function() self:Left(button, i) end) + end -- for + end -- if + + -- Scan availabe recipes + A:ScanSimpleRecipes() +end -- function + + +--------------------------------------------------- +-- Craft items +--------------------------------------------------- +do + -- WoW functions + local GetNumTradeSkills = GetNumTradeSkills + local GetTradeSkillInfo = GetTradeSkillInfo + local GetTradeSkillItemLink = GetTradeSkillItemLink + + + function A.findSkillIndex(itemID) + for i = 1,GetNumTradeSkills() do + local skillName, skillType, numAvailable, isExpanded, serviceType, numSkillUps = GetTradeSkillInfo(i) + if skillType == "header" then + else + if skillName then + local ID = A.link2ID(GetTradeSkillItemLink(i)) + if ID and ID == itemID then + return i + end -- if + end -- if + end -- if + end -- for + A.DEBUG("Tradeskill not found") + end -- function +end -- do + + +-- http://www.wowwiki.com/RunSlashCmd +local _G = _G +function RunSlashCmd(cmd) + local slash, rest = cmd:match("^(%S+)%s*(.-)$") + for name, func in pairs(SlashCmdList) do + local i, slashCmd = 1 + repeat + slashCmd, i = _G["SLASH_"..name..i], i + 1 + if slashCmd == slash then + A.DEBUG("Spell found !") + return true, func(rest) + end + until not slashCmd + end +A.DEBUG("Spell not found :(") +end + +do + -- WoW functions + local DoTradeSkill = DoTradeSkill + + local function SplitStack(owner,split) + DoTradeSkill(owner.ReagentMakerIndex,tonumber(split)) + end + + function A.craft(self,itemID, spell) + -- self is the menu entry + if spell then + CastSpellByName("Mouture") + RunSlashCmd(spell) --CastSpellByName(spell) + --RunSlashCmd("/use "..itemID) --CastSpellByName("/use "..itemID) + return + end + local index = A.findSkillIndex(itemID) + if index then + local skillName, skillType, numAvailable, isExpanded, serviceType, numSkillUps = GetTradeSkillInfo(index) + if IsShiftKeyDown() and numAvailable>1 and not spell then + if not self.SplitStack then + self.SplitStack = SplitStack + end + self.ReagentMakerIndex = index + + OpenStackSplitFrame(numAvailable, self, "TOPLEFT", "TOPRIGHT") + else + DoTradeSkill(index,1) + end + end + end -- function +end -- do + + +--------------------------------------------------- +-- Manage the popup menu +--------------------------------------------------- +do + -- Lua functions + local tonumber = tonumber + + -- WoW functions + local IsModifierKeyDown = IsModifierKeyDown + local GetTradeSkillReagentItemLink = GetTradeSkillReagentItemLink + local GetTradeSkillSelectionIndex = GetTradeSkillSelectionIndex + local ToggleDropDownMenu = ToggleDropDownMenu + + -- Toggles the reagent's menu + function A:ToggleMenu(button, index) + -- We want no modifiers + if IsModifierKeyDown() then return end + + local itemID = A.link2ID(GetTradeSkillReagentItemLink(GetTradeSkillSelectionIndex(), index)) + if itemID and A.data[itemID] then + self.MenuFrame.ReagentMakerItemID = itemID + ToggleDropDownMenu(1, nil, A.MenuFrame, button,-4,0) + end -- if + end -- function +end -- do + +do + -- Lua functions + local type = type + + -- Wow functions + local GetItemInfo = GetItemInfo + local UIDropDownMenu_AddButton = UIDropDownMenu_AddButton + + -- persistent vars + local Minfo = {notCheckable = true, func = A.craft} + local Tinfo = {notCheckable = true, isTitle = true} + + -- function to define the menu items + function A.FillMenu(self, level) + -- Title + local itemName, link, quality, iLevel, reqLevel, class, subclass, maxStack, equipSlot, texture, vendorPrice = GetItemInfo(self.ReagentMakerItemID) + Tinfo.text = "Make "..link.." with:" + UIDropDownMenu_AddButton(Tinfo,level) + + -- Loop over the availible reciepes + for spellID,reagents in pairs(A.data[self.ReagentMakerItemID]) do + --name, link, quality, iLevel, reqLevel, class, subclass, maxStack, equipSlot, texture, vendorPrice = GetItemInfo(itemID) or GetItemInfo("itemName") or GetItemInfo("itemLink") + -- Right now, manage only sub-recipes with only one reagent + if type(reagents[1]) == "number" then + local name, link, quality, iLevel, reqLevel, class, subclass, maxStack, equipSlot, texture, vendorPrice = GetItemInfo(reagents[1]) + + if link then + Minfo.text = "|T"..texture..":0|t "..link + if #reagents>1 and reagents[2] > 1 then + Minfo.text = reagents[2].."x "..Minfo.text + end -- if + + --if reagents.spell then + -- Minfo.func = RunMacroText + -- Minfo.arg1 = reagents.spell..itemName + -- Minfo.arg2 = nil + --else + Minfo.func = A.craft + Minfo.arg1 = self.ReagentMakerItemID + Minfo.arg2 = reagents.spell + --end -- if + UIDropDownMenu_AddButton(Minfo,level) + else + A.DEBUG("Erreur, objet inconnu :"..reagents[1]) + end -- if + end -- if + end -- for + end -- function +end -- do + + +--------------------------------------------------- +-- Manage the tradeskill window state +--------------------------------------------------- +function A:SaveState() +end + +function A:SetState(state) +end + +function A:ExpandAll() +end