comparison ReagentMaker.lua @ 3:ed0582126cae

The base features semms to work.
author contrebasse
date Sat, 02 Apr 2011 01:49:39 +0200
parents 04c5b817eead
children cea9633a6d4e
comparison
equal deleted inserted replaced
2:04c5b817eead 3:ed0582126cae
25 --------------------------------------------------- 25 ---------------------------------------------------
26 -- Manage events 26 -- Manage events
27 --------------------------------------------------- 27 ---------------------------------------------------
28 A.EventsFrame = CreateFrame("Frame") 28 A.EventsFrame = CreateFrame("Frame")
29 29
30 A.EventsFrame:SetScript("OnEvent", function(self, event) 30 A.EventsFrame:SetScript("OnEvent", function(self, event, ...)
31 if event == "TRADE_SKILL_SHOW" then 31 if event == "TRADE_SKILL_UPDATE" then
32 -- Scan availabe recipes
33 A:ScanSimpleRecipes()
34
35 -- Close the external window if the tradeskill changed
36 if A.currentTradeSkill ~= GetTradeSkillLine() then
37 A.MenuFrame:Hide()
38 end
39 elseif event == "TRADE_SKILL_SHOW" then
32 A:Initialize() 40 A:Initialize()
33 end -- if 41 end -- if
34 end); -- function 42 end) -- function
35 A.EventsFrame:RegisterEvent("TRADE_SKILL_SHOW") 43 A.EventsFrame:RegisterEvent("TRADE_SKILL_SHOW")
36 44 A.EventsFrame:RegisterEvent("TRADE_SKILL_UPDATE")
37 45
38 --------------------------------------------------- 46 ---------------------------------------------------
39 -- Initialize 47 -- Initialize
40 --------------------------------------------------- 48 ---------------------------------------------------
41 local toInitialize = true 49 local toInitialize = true
42 function A:Initialize() 50 function A:Initialize()
43 -- This part is done the first time only 51 A.EventsFrame:UnregisterEvent("TRADE_SKILL_SHOW")
44 if toInitialize then 52
45 53 -- Register clics on reagent's buttons
46 -- Register clics on reagent's buttons 54 for i=1,7 do
47 for i=1,7 do 55 local btn = _G["TradeSkillReagent"..i];
48 local button = _G["TradeSkillReagent"..i]; 56 btn:HookScript("OnDoubleClick", A.ProcessReagent);
49 button:HookScript("OnClick", function(btn) A.ToggleMenu(btn, i) end); 57 btn:HookScript("OnEnter", A.btnEntered)
50 --button:HookScript("OnEnter", function() self:Entered(button, i) end) 58 btn:HookScript("OnLeave", A.btnLeft)
51 --button:HookScript("OnLeave", function() self:Left(button, i) end) 59 btn.SplitStack = A.SplitStack
52 end -- for 60
53 61 local textureHighlight = btn:CreateTexture()
54 toInitialize = nil 62 textureHighlight:Hide()
55 end -- if 63 textureHighlight:SetTexture("Interface\\BUTTONS\\CheckButtonHilight")
56 64 textureHighlight:SetBlendMode("ADD")
57 -- Scan availabe recipes 65 --textureHighlight:SetPoint("TOPLEFT")
58 A:ScanSimpleRecipes() 66 --textureHighlight:SetSize(btn:GetHeight(),btn:GetHeight())
67 textureHighlight:SetAllPoints("TradeSkillReagent"..i.."IconTexture")
68 btn.textureHighlight = textureHighlight
69
70 local label = btn:CreateFontString(nil,"ARTWORK","GameFontHighlight")
71 label:SetSize(100,20)
72 label:SetPoint("TOPLEFT",btn,"TOPLEFT",4,-4)
73 label:SetJustifyH("LEFT")
74 label:SetJustifyV("TOP")
75 label:SetFont("Fonts\\FRIZQT__.TTF", 10, "OUTLINE")
76 btn.label = label
77 end -- for
78
79 --[[
80 A.tooltipRecipe = CreateFrame("GameTooltip", "ReagentMaker_tooltipRecipe",UIParent, "GameTooltipTemplate")
81 A.tooltipRecipe:SetFrameStrata("TOOLTIP")
82 A.tooltipRecipe:Hide()
83 A.tooltipReagent = CreateFrame("GameTooltip", "ReagentMaker_tooltipReagent",UIParent, "GameTooltipTemplate")
84 A.tooltipReagent:SetFrameStrata("TOOLTIP")
85 A.tooltipReagent:Hide()
86 --]]
59 end -- function 87 end -- function
60 88
61 89
90 -- Function run avter selecting a item in the tradeskill window
91 function A.ProcessReagent(btn, ...)
92 -- We want no modifiers, or shift to choose the number of reagent to craft
93 if IsModifierKeyDown() and not IsShiftKeyDown() then return end
94 local chooseNumberToCraft = IsShiftKeyDown()
95
96 -- Index of the reagent in the recipe, taken from the button name
97 local reagentRecipeIndex = A.buttonNumber(btn)
98
99 -- ID of the reagent we want to craft
100 local reagentID = A.link2ID(GetTradeSkillReagentItemLink(GetTradeSkillSelectionIndex(), reagentRecipeIndex))
101
102 -- Continue only if the reagent is known
103 if not reagentID or not A.data[reagentID] then return end
104
105 -- If only one recipe is known for the reagent, use it
106 if #(A.data[reagentID]) == 1 and not A.data[reagentID].spell then
107 local numMakable, reagentIndex = A.numMakable(reagentID)
108 if numMakable>0 then
109 A.craft(reagentID,reagentRecipeIndex,reagentIndex,numMakable,chooseNumberToCraft)
110 return
111 end
112 -- If we can make the item needed to make the reagent, open a window to make it
113 -- one step recursion, enables to mill to create an ink
114 if A.data[A.data[reagentID][1][1]] then
115 A.externalCraftWindow(A.data[reagentID][1][1])
116 else
117 print("Sorry, can not make reagent...")
118 end
119 else
120 A.externalCraftWindow(reagentID)
121 end -- if
122 end -- function
123
124
62 --------------------------------------------------- 125 ---------------------------------------------------
63 -- Craft items 126 -- Craft items
64 --------------------------------------------------- 127 ---------------------------------------------------
65 do 128 -- function used after choosing the number of reagent to craft
66 -- WoW functions 129 function A.SplitStack(owner,split)
67 local GetNumTradeSkills = GetNumTradeSkills 130 DoTradeSkill(owner.ReagentMaker_reagentIndex,tonumber(split))
68 local GetTradeSkillInfo = GetTradeSkillInfo 131 owner.ReagentMaker_reagentIndex = nil
69 local GetTradeSkillItemLink = GetTradeSkillItemLink
70
71
72 function A.findSkillIndex(itemID)
73 for i = 1,GetNumTradeSkills() do
74 local skillName, skillType, numAvailable, isExpanded, serviceType, numSkillUps = GetTradeSkillInfo(i)
75 if skillType == "header" then
76 else
77 if skillName then
78 local ID = A.link2ID(GetTradeSkillItemLink(i))
79 if ID and ID == itemID then
80 return i
81 end -- if
82 end -- if
83 end -- if
84 end -- for
85 A.DEBUG("Tradeskill not found")
86 end -- function
87 end -- do
88
89
90 -- http://www.wowwiki.com/RunSlashCmd
91 local _G = _G
92 function RunSlashCmd(cmd)
93 local slash, rest = cmd:match("^(%S+)%s*(.-)$")
94 for name, func in pairs(SlashCmdList) do
95 local i, slashCmd = 1
96 repeat
97 slashCmd, i = _G["SLASH_"..name..i], i + 1
98 if slashCmd == slash then
99 A.DEBUG("Spell found !")
100 return true, func(rest)
101 end
102 until not slashCmd
103 end
104 A.DEBUG("Spell not found :(")
105 end 132 end
106 133
107 do 134 -- Craft the reagent of an item, given it's position in the recipe
108 -- WoW functions 135 function A.craft(reagentID,reagentRecipeIndex,reagentIndex,numReagentMakable,chooseNumber)
109 local DoTradeSkill = DoTradeSkill 136 -- Look at how many we need to make one item for the selected recipe
110 137 local numToMake = 1
111 local function SplitStack(owner,split) 138 local selectedIndex = GetTradeSkillSelectionIndex()
112 DoTradeSkill(owner.ReagentMakerIndex,tonumber(split)) 139 local skillName, skillType, numAvailable, isExpanded, serviceType, numSkillUps = GetTradeSkillInfo(selectedIndex)
140 if numAvailable==0 then
141 local reagentName, reagentTexture, reagentCount, playerReagentCount = GetTradeSkillReagentInfo(selectedIndex, reagentRecipeIndex)
142 numToMake = math.min(reagentCount - playerReagentCount,numReagentMakable)
143 end -- if
144
145 -- Choose number or craft directly
146 if chooseNumber and numReagentMakable>1 then
147 -- the dialog window is linked to the reagent button
148 local btn = _G["TradeSkillReagent"..reagentRecipeIndex]
149
150 -- Store info to be able to run the function later
151 btn.ReagentMaker_reagentIndex = reagentIndex
152
153 -- Open dialog
154 OpenStackSplitFrame(numReagentMakable, btn, "TOP", "BOTTOM")
155
156 -- Fill in the number to make
157 numToMake = tostring(numToMake)
158 for i = 1,numToMake:len() do
159 StackSplitFrame_OnChar(StackSplitFrame,numToMake:gsub(i,i))
160 end
161 StackSplitFrame.typing = 0 -- reinit the frame so tha the entered value will be erased on text entry
162 else
163 DoTradeSkill(reagentIndex,numToMake)
164 end -- if
165 end -- function
166
167
168 -- Button hovering
169 function A.btnEntered(btn)
170 -- Index of the reagent in the recipe, taken from the button name
171 local reagentRecipeIndex = A.buttonNumber(btn)
172
173 -- ID of the reagent we want to craft
174 local reagentLink = GetTradeSkillReagentItemLink(GetTradeSkillSelectionIndex(), reagentRecipeIndex)
175 local reagentID = A.link2ID(reagentLink)
176
177 -- Continue only if the reagent is known
178 if not reagentID or not A.data[reagentID] then return end
179
180 btn.textureHighlight:Show()
181
182 if #(A.data[reagentID]) == 1 and not A.data[reagentID].spell then
183 local numMakable = A.numMakable(reagentID)
184 btn.label:SetText(numMakable)
185 if numMakable==0 then
186 btn.label:SetTextColor(1, 0, 0, 1)
187 else
188 btn.label:SetTextColor(0, 5, 0, 1)
189 end
190 btn.label:Show()
113 end 191 end
114 192
115 function A.craft(btn,...) 193 --[[
116 local index = A.findSkillIndex(btn:GetAttribute(target-item)) 194 A.tooltipRecipe:SetOwner(btn)
117 if index then 195 A.tooltipRecipe:SetHyperlink(GetTradeSkillRecipeLink(A.findSkillIndex(reagentID)))
118 local skillName, skillType, numAvailable, isExpanded, serviceType, numSkillUps = GetTradeSkillInfo(index) 196 A.tooltipRecipe:SetHyperlink(reagentLink)
119 if IsShiftKeyDown() and numAvailable>1 and not spell then 197 A.tooltipRecipe:ClearAllPoints()
120 if not btn.SplitStack then 198 A.tooltipRecipe:SetPoint("TOPLEFT",btn,"TOPRIGHT")
121 btn.SplitStack = SplitStack 199 --A.tooltipRecipe:SetSize(100,100)
122 end 200 A.tooltipRecipe:Show()
123 btn.ReagentMakerIndex = index 201 A.tooltipReagent:SetOwner(btn)
124 202 A.tooltipReagent:SetHyperlink(reagentLink)
125 OpenStackSplitFrame(numAvailable, btn, "TOPLEFT", "TOPRIGHT") 203 A.tooltipReagent:ClearAllPoints()
126 else 204 A.tooltipReagent:SetPoint("TOPLEFT",A.tooltipRecipe,"TOPRIGHT")
127 DoTradeSkill(index,1) 205 A.tooltipReagent:Show()
128 end 206 --]]
129 end
130 end -- function
131 end -- do
132
133
134 ---------------------------------------------------
135 -- Manage the popup menu
136 ---------------------------------------------------
137 do
138 -- Lua functions
139 local tonumber = tonumber
140
141 -- WoW functions
142 local IsModifierKeyDown = IsModifierKeyDown
143 local GetTradeSkillReagentItemLink = GetTradeSkillReagentItemLink
144 local GetTradeSkillSelectionIndex = GetTradeSkillSelectionIndex
145
146 -- Toggles the reagent's menu
147 function A.ToggleMenu(button, index)
148 -- We want no modifiers
149 if IsModifierKeyDown() then return end
150
151 if A.menuIsOpen(button) then
152 A.menuClose()
153 else
154 local itemID = A.link2ID(GetTradeSkillReagentItemLink(GetTradeSkillSelectionIndex(), index))
155 if itemID and A.data[itemID] then
156 A.FillMenu(itemID)
157 A.menuOpen(button)
158 else
159 if A.menuIsOpen() then
160 A.menuClose()
161 end
162 end -- if
163 end -- if
164 end -- function
165 end -- do
166
167 do
168 -- Lua functions
169 local type = type
170
171 -- Wow functions
172 local GetItemInfo = GetItemInfo
173
174 -- persistent vars
175 local Minfo = {notCheckable = true, func = A.craft}
176
177 -- function to define the menu items
178 function A.FillMenu(itemID)
179 -- Title
180 --local itemName, link, quality, iLevel, reqLevel, class, subclass, maxStack, equipSlot, texture, vendorPrice = GetItemInfo(itemID)
181 --Tinfo.text = "Make "..link.." with:"
182
183 -- Loop over the available reciepes
184 for spellID,reagents in pairs(A.data[itemID]) do
185 --name, link, quality, iLevel, reqLevel, class, subclass, maxStack, equipSlot, texture, vendorPrice = GetItemInfo(itemID) or GetItemInfo("itemName") or GetItemInfo("itemLink")
186 -- Right now, manage only sub-recipes with only one reagent
187 if type(reagents[1]) == "number" then
188 local name, link, quality, iLevel, reqLevel, class, subclass, maxStack, equipSlot, texture, vendorPrice = GetItemInfo(reagents[1])
189
190 if link then
191 local text = reagents[2].."x |T"..texture..":0|t "..link
192
193 if reagents.spell then
194 -- Special spell
195 -- Minfo.func = RunMacroText
196 -- Minfo.arg1 = reagents.spell..itemName
197 -- Minfo.arg2 = nil
198 A.menuAddItem(text,action,itemID)
199 else
200 -- Standard tradeskill spell
201 A.menuAddItem(text,A.craft,itemID)
202 end -- if
203 else
204 A.DEBUG("Erreur, objet inconnu :"..reagents[1])
205 end -- if
206 end -- if
207 end -- for
208 end -- function
209 end -- do
210
211
212 ---------------------------------------------------
213 -- Manage the tradeskill window state
214 ---------------------------------------------------
215 function A:SaveState()
216 end 207 end
217 208
218 function A:SetState(state) 209 function A.btnLeft(btn)
210 btn.textureHighlight:Hide()
211 btn.label:Hide()
212 --[[
213 A.tooltipRecipe:Hide()
214 A.tooltipReagent:Hide()
215 --]]
219 end 216 end
220
221 function A:ExpandAll()
222 end