vanilla-wow-addons – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 --[[
2 Name: Gratuity-2.0
3 Revision: $Rev: 11053 $
4 Author: Tekkub Stoutwrithe (tekkub@gmail.com)
5 Website: http://wiki.wowace.com/index.php/GratuityLib
6 Documentation: http://wiki.wowace.com/index.php/Gratuity-2.0_API_Documentation
7 SVN: svn://svn.wowace.com/root/trunk/GratuityLib/Gratuity-2.0
8 Description: Tooltip parsing library
9 Dependencies: AceLibrary, (optional) Compost-2.0, (optional) Deformat-2.0
10 ]]
11  
12 local vmajor, vminor = "Gratuity-2.0", "$Revision: 11053 $"
13  
14 if not AceLibrary then error(vmajor .. " requires AceLibrary.") end
15 if not AceLibrary:IsNewVersion(vmajor, vminor) then return end
16  
17 local lib = {}
18 local methods = {
19 "SetBagItem", "SetAction", "SetAuctionItem", "SetAuctionSellItem", "SetBuybackItem",
20 "SetCraftItem", "SetCraftSpell", "SetHyperlink", "SetInboxItem", "SetInventoryItem",
21 "SetLootItem", "SetLootRollItem", "SetMerchantItem", "SetPetAction", "SetPlayerBuff",
22 "SetQuestItem", "SetQuestLogItem", "SetQuestRewardSpell", "SetSendMailItem", "SetShapeshift",
23 "SetSpell", "SetTalent", "SetTrackingSpell", "SetTradePlayerItem", "SetTradeSkillItem", "SetTradeTargetItem",
24 "SetTrainerService", "SetUnit", "SetUnitBuff", "SetUnitDebuff",
25 }
26  
27  
28 -- Activate a new instance of this library
29 local function activate(self, oldLib, oldDeactivate)
30 if oldLib then self.vars = oldLib.vars
31 else
32 self.vars = {}
33 self:CreateTooltip()
34 end
35  
36 self:CreateSetMethods()
37  
38 if oldDeactivate then oldDeactivate(oldLib) end
39 end
40  
41  
42 function lib:InitCompost()
43 if not self.vars.compost and AceLibrary:HasInstance("Compost-2.0") then self.vars.compost = AceLibrary("Compost-2.0") end
44 end
45  
46  
47 function lib:CreateTooltip()
48 local tt = CreateFrame("GameTooltip")
49  
50 self.vars.tooltip = tt
51 tt:SetOwner(tt, "ANCHOR_NONE")
52 -- tooltip:SetParent()
53  
54 self.vars.Llines, self.vars.Rlines = {}, {}
55 for i=1,30 do
56 self.vars.Llines[i], self.vars.Rlines[i] = tt:CreateFontString(), tt:CreateFontString()
57 self.vars.Llines[i]:SetFontObject(GameFontNormal)
58 self.vars.Rlines[i]:SetFontObject(GameFontNormal)
59 tt:AddFontStrings(self.vars.Llines[i], self.vars.Rlines[i])
60 end
61 end
62  
63  
64 -- Clears the tooltip completely, none of this "erase left, hide right" crap blizzard does
65 function lib:Erase()
66 self.vars.tooltip:ClearLines() -- Ensures tooltip's NumLines is reset
67 for i=1,30 do self.vars.Rlines[i]:SetText() end -- Clear text from right side (ClearLines only hides them)
68 if not self.vars.tooltip:IsOwned(self.vars.tooltip) then self.vars.tooltip:SetOwner(self.vars.tooltip, "ANCHOR_NONE") end
69 self:assert(self.vars.tooltip:IsOwned(self.vars.tooltip), "Gratuity's tooltip is not scanable")
70 end
71  
72  
73 -- Get the number of lines
74 -- Arg: endln - If passed and tooltip's NumLines is higher, endln is returned back
75 function lib:NumLines(endln)
76 local num = self.vars.tooltip:NumLines()
77 return endln and num > endln and endln or num or 0
78 end
79  
80 local FindDefault = function(str, pattern)
81 return string.find(str, pattern);
82 end;
83  
84 local FindExact = function(str, pattern)
85 if (str == pattern) then
86 return string.find(str, pattern);
87 end;
88 end;
89  
90 -- If text is found on tooltip then results of string.find are returned
91 -- Args:
92 -- txt - The text string to find
93 -- startln - First tooltip line to check, default 1
94 -- endln - Last line to test, default 30
95 -- ignoreleft / ignoreright - Causes text on one side of the tooltip to be ignored
96 -- exact - the compare will be an exact match vs the default behaviour of
97 function lib:Find(txt, startln, endln, ignoreleft, ignoreright, exact)
98 local searchFunction = FindDefault;
99 if (exact == true) then
100 searchFunction = FindExact;
101 end;
102 self:argCheck(txt, 2, "string", "number")
103 local t1, t2 = type(startln or 1), type(self:NumLines(endln))
104 if (t1 ~= "number" or t2 ~= "number") then print(t1, t2, (startln or 1),self:NumLines(endln)) end
105 for i=(startln or 1),self:NumLines(endln) do
106 if not ignoreleft then
107 local txtl = self.vars.Llines[i]:GetText()
108 if (txtl and searchFunction(txtl, txt)) then return string.find(txtl, txt) end
109 end
110  
111 if not ignoreright then
112 local txtr = self.vars.Rlines[i]:GetText()
113 if (txtr and searchFunction(txtr, txt)) then return string.find(txtr, txt) end
114 end
115 end
116 end
117  
118  
119 -- Calls Find many times.
120 -- Args are passed directly to Find, t1-t10 replace the txt arg
121 -- Returns Find results for the first match found, if any
122 function lib:MultiFind(startln, endln, ignoreleft, ignoreright, t1,t2,t3,t4,t5,t6,t7,t8,t9,t10)
123 self:argCheck(t1, 6, "string", "number")
124 if t1 and self:Find(t1, startln, endln, ignoreleft, ignoreright) then return self:Find(t1, startln, endln, ignoreleft, ignoreright)
125 elseif t2 then return self:MultiFind(startln, endln, ignoreleft, ignoreright, t2,t3,t4,t5,t6,t7,t8,t9,t10) end
126 end
127  
128  
129 local deformat
130 -- If text is found on tooltip then results of deformat:Deformat are returned
131 -- Args:
132 -- txt - The text string to deformat and serach for
133 -- startln - First tooltip line to check, default 1
134 -- endln - Last line to test, default 30
135 -- ignoreleft / ignoreright - Causes text on one side of the tooltip to be ignored
136 function lib:FindDeformat(txt, startln, endln, ignoreleft, ignoreright)
137 self:argCheck(txt, 2, "string", "number")
138 if not deformat then
139 self:assert(AceLibrary:HasInstance("Deformat-2.0"), "FindDeformat requires Deformat-2.0 to be available")
140 deformat = AceLibrary("Deformat-2.0")
141 end
142  
143 for i=(startln or 1),self:NumLines(endln) do
144 if not ignoreleft then
145 local txtl = self.vars.Llines[i]:GetText()
146 if (txtl and deformat(txtl, txt)) then return deformat(txtl, txt) end
147 end
148  
149 if not ignoreright then
150 local txtr = self.vars.Rlines[i]:GetText()
151 if (txtr and deformat(txtr, txt)) then return deformat(txtr, txt) end
152 end
153 end
154 end
155  
156  
157 -- Returns a table of strings pulled from the tooltip, or nil if no strings in tooltip
158 -- Args:
159 -- startln - First tooltip line to check, default 1
160 -- endln - Last line to test, default 30
161 -- ignoreleft / ignoreright - Causes text on one side of the tooltip to be ignored
162 function lib:GetText(startln, endln, ignoreleft, ignoreright)
163 self:InitCompost()
164 local retval
165  
166 for i=(startln or 1),(endln or 30) do
167 local txtl, txtr
168 if not ignoreleft then txtl = self.vars.Llines[i]:GetText() end
169 if not ignoreright then txtr = self.vars.Rlines[i]:GetText() end
170 if txtl or txtr then
171 if not retval then retval = self.vars.compost and self.vars.compost:Acquire() or {} end
172 local t = self.vars.compost and self.vars.compost:Acquire(txtl, txtr) or {txtl, txtr}
173 table.insert(retval, t)
174 end
175 end
176  
177 return retval
178 end
179  
180  
181 -- Returns the text from a specific line (both left and right unless second arg is true)
182 -- Args:
183 -- line - the line number you wish to retrieve
184 -- getright - if passed the right line will be returned, if not the left will be returned
185 function lib:GetLine(line, getright)
186 self:argCheck(line, 2, "number")
187 if self.vars.tooltip:NumLines() < line then return end
188 if getright then return self.vars.Rlines[line] and self.vars.Rlines[line]:GetText()
189 elseif self.vars.Llines[line] then
190 return self.vars.Llines[line]:GetText(), self.vars.Rlines[line]:GetText()
191 end
192 end
193  
194  
195 -----------------------------------
196 -- Set tooltip methods --
197 -----------------------------------
198  
199 -- These methods are designed to immitate the GameTooltip API
200 local testmethods = {
201 SetAction = function(id) return HasAction(id) end,
202 }
203 local gettrue = function() return true end
204 function lib:CreateSetMethods()
205 for _,m in pairs(methods) do
206 local meth = m
207 local func = testmethods[meth] or gettrue
208 self[meth] = function(self,a1,a2,a3,a4)
209 self:Erase()
210 if not func(a1,a2,a3,a4) then return end
211 return self:pcall(self.vars.tooltip[meth], self.vars.tooltip,a1,a2,a3,a4) end
212 end
213 end
214  
215  
216 --------------------------------
217 -- Load this bitch! --
218 --------------------------------
219 AceLibrary:Register(lib, vmajor, vminor, activate)
220 lib = nil