vanilla-wow-addons – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | |
2 | local vmajor, vminor = "1", tonumber(string.sub("$Revision: 1705 $", 12, -3)) |
||
3 | local stubvarname = "TekLibStub" |
||
4 | local libvarname = "Gratuity" |
||
5 | |||
6 | |||
7 | -- Check to see if an update is needed |
||
8 | -- if not then just return out now before we do anything |
||
9 | local libobj = getglobal(libvarname) |
||
10 | if libobj and not libobj:NeedsUpgraded(vmajor, vminor) then return end |
||
11 | |||
12 | |||
13 | local stubobj = getglobal(stubvarname) |
||
14 | if not stubobj then |
||
15 | stubobj = {} |
||
16 | setglobal(stubvarname, stubobj) |
||
17 | |||
18 | |||
19 | -- Instance replacement method, replace contents of old with that of new |
||
20 | function stubobj:ReplaceInstance(old, new) |
||
21 | for k,v in pairs(old) do old[k]=nil end |
||
22 | for k,v in pairs(new) do old[k]=v end |
||
23 | end |
||
24 | |||
25 | |||
26 | -- Get a new copy of the stub |
||
27 | function stubobj:NewStub(name) |
||
28 | local newStub = {} |
||
29 | self:ReplaceInstance(newStub, self) |
||
30 | newStub.libName = name |
||
31 | newStub.lastVersion = '' |
||
32 | newStub.versions = {} |
||
33 | return newStub |
||
34 | end |
||
35 | |||
36 | |||
37 | -- Get instance version |
||
38 | function stubobj:NeedsUpgraded(vmajor, vminor) |
||
39 | local versionData = self.versions[vmajor] |
||
40 | if not versionData or versionData.minor < vminor then return true end |
||
41 | end |
||
42 | |||
43 | |||
44 | -- Get instance version |
||
45 | function stubobj:GetInstance(version) |
||
46 | if not version then version = self.lastVersion end |
||
47 | local versionData = self.versions[version] |
||
48 | if not versionData then print(string.format("<%s> Cannot find library version: %s", self.libName, version or "")) return end |
||
49 | return versionData.instance |
||
50 | end |
||
51 | |||
52 | |||
53 | -- Register new instance |
||
54 | function stubobj:Register(newInstance) |
||
55 | local version,minor = newInstance:GetLibraryVersion() |
||
56 | self.lastVersion = version |
||
57 | local versionData = self.versions[version] |
||
58 | if not versionData then |
||
59 | -- This one is new! |
||
60 | versionData = { |
||
61 | instance = newInstance, |
||
62 | minor = minor, |
||
63 | old = {}, |
||
64 | } |
||
65 | self.versions[version] = versionData |
||
66 | newInstance:LibActivate(self) |
||
67 | return newInstance |
||
68 | end |
||
69 | -- This is an update |
||
70 | local oldInstance = versionData.instance |
||
71 | local oldList = versionData.old |
||
72 | versionData.instance = newInstance |
||
73 | versionData.minor = minor |
||
74 | local skipCopy = newInstance:LibActivate(self, oldInstance, oldList) |
||
75 | table.insert(oldList, oldInstance) |
||
76 | if not skipCopy then |
||
77 | for i, old in ipairs(oldList) do self:ReplaceInstance(old, newInstance) end |
||
78 | end |
||
79 | return newInstance |
||
80 | end |
||
81 | end |
||
82 | |||
83 | ---------------------------- |
||
84 | -- Library Code -- |
||
85 | ---------------------------- |
||
86 | if not libobj then |
||
87 | libobj = stubobj:NewStub(libvarname) |
||
88 | setglobal(libvarname, libobj) |
||
89 | end |
||
90 | |||
91 | local lib = {} |
||
92 | |||
93 | |||
94 | -- Return the library's current version |
||
95 | function lib:GetLibraryVersion() |
||
96 | return vmajor, vminor |
||
97 | end |
||
98 | |||
99 | local methods = { |
||
100 | "SetBagItem", "SetAction", "SetAuctionItem", "SetAuctionSellItem", "SetBuybackItem", |
||
101 | "SetCraftItem", "SetCraftSpell", "SetHyperlink", "SetInboxItem", "SetInventoryItem", |
||
102 | "SetLootItem", "SetLootRollItem", "SetMerchantItem", "SetPetAction", "SetPlayerBuff", |
||
103 | "SetQuestItem", "SetQuestLogItem", "SetQuestRewardSpell", "SetSendMailItem", "SetShapeshift", |
||
104 | "SetSpell", "SetTalent", "SetTrackingSpell", "SetTradePlayerItem", "SetTradeSkillItem", "SetTradeTargetItem", |
||
105 | "SetTrainerService", "SetUnit", "SetUnitBuff", "SetUnitDebuff", |
||
106 | } |
||
107 | |||
108 | -- Activate a new instance of this library |
||
109 | function lib:LibActivate(stub, oldLib, oldList) |
||
110 | local maj, min = self:GetLibraryVersion() |
||
111 | |||
112 | if oldLib then |
||
113 | local omaj, omin = oldLib:GetLibraryVersion() |
||
114 | for _,m in pairs(methods) do |
||
115 | self[m] = oldLib[m] |
||
116 | end |
||
117 | self.vars = oldLib.vars |
||
118 | if self.vars.tooltip then self:CreateSetMethods() end |
||
119 | else |
||
120 | self.vars = {} |
||
121 | end |
||
122 | -- nil return makes stub do object copy |
||
123 | end |
||
124 | |||
125 | |||
126 | function lib:InitCompost() |
||
127 | if not self.vars.compost and CompostLib then self.vars.compost = CompostLib:GetInstance("compost-1") end |
||
128 | end |
||
129 | |||
130 | |||
131 | -- Retreive the tooltip assigned to Gratuity |
||
132 | function lib:GetTooltip(tooltip) |
||
133 | return self.vars.tooltip |
||
134 | end |
||
135 | |||
136 | |||
137 | ------------------------------------------------------------------------------------- |
||
138 | -- Pass a tooltip frame to be used (make sure the tooltip does not have a parent!!!) |
||
139 | -- You can use this line in your XML to define the tooltip: |
||
140 | -- <GameTooltip name="NameGoesHere" inherits="GameTooltipTemplate"/> |
||
141 | -- This *would* be part of the EmbedLib but the game throws an error |
||
142 | -- when you try to declare two tooltips with the same name *growl* |
||
143 | ------------------------------------------------------------------------------------- |
||
144 | -- Returns the stored tooltip (this may not match what you passed!) |
||
145 | -- You need to be careful how you use the tooltip or it can |
||
146 | -- get "detached" from it's owner and stop parsing. Please make sure you read up |
||
147 | -- about tips here: http://www.wowwiki.com/UIOBJECT_GameTooltip |
||
148 | ------------------------------------------------------------------------------------- |
||
149 | function lib:RegisterTooltip(tooltip) |
||
150 | if self.vars.tooltip then return self.vars.tooltip end |
||
151 | |||
152 | assert(tooltip, "No tooltip passed!") |
||
153 | assert(tooltip:IsObjectType("GameTooltip"), "The frame passed was not a GameTooltip!") |
||
154 | |||
155 | self.vars.tooltip = tooltip |
||
156 | tooltip:SetOwner(tooltip, "ANCHOR_NONE") |
||
157 | -- tooltip:SetParent() |
||
158 | |||
159 | local name = tooltip:GetName() |
||
160 | local g = getfenv(0) |
||
161 | |||
162 | self.vars.Llines, self.vars.Rlines = {}, {} |
||
163 | for i=1,30 do |
||
164 | self.vars.Llines[i] = g[name.."TextLeft"..i] |
||
165 | self.vars.Rlines[i] = g[name.."TextRight"..i] |
||
166 | end |
||
167 | |||
168 | if not self.SetBagItem then self:CreateSetMethods() end |
||
169 | |||
170 | return tooltip |
||
171 | end |
||
172 | |||
173 | |||
174 | -- Clears the tooltip completely, none of this "erase left, hide right" crap blizzard does |
||
175 | -- returns true is successful, nil if no tooltip assigned |
||
176 | function lib:Erase() |
||
177 | assert(self.vars.tooltip, "No tooltip declared!") |
||
178 | |||
179 | self.vars.tooltip:ClearLines() -- Ensures tooltip's NumLines is reset |
||
180 | for i=1,30 do self.vars.Rlines[i]:SetText() end -- Clear text from right side (ClearLines only hides them) |
||
181 | |||
182 | return true |
||
183 | end |
||
184 | |||
185 | |||
186 | -- Get the number of lines |
||
187 | -- Arg: endln - If passed and tooltip's NumLines is higher, endln is returned back |
||
188 | function lib:NumLines(endln) |
||
189 | local num = self.vars.tooltip:NumLines() |
||
190 | return endln and num > endln and endln or num or 0 |
||
191 | end |
||
192 | |||
193 | |||
194 | -- If text is found on tooltip then results of string.find are returned |
||
195 | -- Args: |
||
196 | -- txt - The text string to find |
||
197 | -- startln - First tooltip line to check, default 1 |
||
198 | -- endln - Last line to test, default 30 |
||
199 | -- ignoreleft / ignoreright - Causes text on one side of the tooltip to be ignored |
||
200 | function lib:Find(txt, startln, endln, ignoreleft, ignoreright) |
||
201 | assert(txt, "No search string passed") |
||
202 | local t1, t2 = type(startln or 1), type(self:NumLines(endln)) |
||
203 | if (t1 ~= "number" or t2 ~= "number") then print(t1, t2, (startln or 1),self:NumLines(endln)) end |
||
204 | for i=(startln or 1),self:NumLines(endln) do |
||
205 | if not ignoreleft then |
||
206 | local txtl = self.vars.Llines[i]:GetText() |
||
207 | if (txtl and string.find(txtl, txt)) then return string.find(txtl, txt) end |
||
208 | end |
||
209 | |||
210 | if not ignoreright then |
||
211 | local txtr = self.vars.Rlines[i]:GetText() |
||
212 | if (txtr and string.find(txtr, txt)) then return string.find(txtr, txt) end |
||
213 | end |
||
214 | end |
||
215 | end |
||
216 | |||
217 | |||
218 | -- Calls Find many times. |
||
219 | -- Args are passed directly to Find, t1-t10 replace the txt arg |
||
220 | function lib:MultiFind(startln, endln, ignoreleft, ignoreright, t1,t2,t3,t4,t5,t6,t7,t8,t9,t10) |
||
221 | assert(t1, "No search string passed") |
||
222 | if t1 and self:Find(t1, startln, endln, ignoreleft, ignoreright) then return self:Find(t1, startln, endln, ignoreleft, ignoreright) |
||
223 | elseif t2 then return self:MultiFind(startln, endln, ignoreleft, ignoreright, t2,t3,t4,t5,t6,t7,t8,t9,t10) end |
||
224 | end |
||
225 | |||
226 | |||
227 | local babbleCore |
||
228 | -- If text is found on tooltip then results of babbleCore:Deformat are returned |
||
229 | -- Args: |
||
230 | -- txt - The text string to deformat and serach for |
||
231 | -- startln - First tooltip line to check, default 1 |
||
232 | -- endln - Last line to test, default 30 |
||
233 | -- ignoreleft / ignoreright - Causes text on one side of the tooltip to be ignored |
||
234 | function lib:FindDeformat(txt, startln, endln, ignoreleft, ignoreright) |
||
235 | assert(txt, "No search string passed") |
||
236 | if not babbleCore then |
||
237 | assert(BabbleLib, "You must have BabbleLib-Core 1.1 available") |
||
238 | babbleCore = BabbleLib:GetInstance("Core 1.1") |
||
239 | end |
||
240 | |||
241 | for i=(startln or 1),self:NumLines(endln) do |
||
242 | if not ignoreleft then |
||
243 | local txtl = self.vars.Llines[i]:GetText() |
||
244 | if (txtl and babbleCore:Deformat(txtl, txt)) then return babbleCore:Deformat(txtl, txt) end |
||
245 | end |
||
246 | |||
247 | if not ignoreright then |
||
248 | local txtr = self.vars.Rlines[i]:GetText() |
||
249 | if (txtr and babbleCore:Deformat(txtr, txt)) then return babbleCore:Deformat(txtr, txt) end |
||
250 | end |
||
251 | end |
||
252 | end |
||
253 | |||
254 | |||
255 | -- Returns a table of strings pulled from the tooltip, or nil if no strings in tooltip |
||
256 | -- Args: |
||
257 | -- startln - First tooltip line to check, default 1 |
||
258 | -- endln - Last line to test, default 30 |
||
259 | -- ignoreleft / ignoreright - Causes text on one side of the tooltip to be ignored |
||
260 | function lib:GetText(startln, endln, ignoreleft, ignoreright) |
||
261 | self:InitCompost() |
||
262 | local retval |
||
263 | |||
264 | for i=(startln or 1),(endln or 30) do |
||
265 | local txtl, txtr |
||
266 | if not ignoreleft then txtl = self.vars.Llines[i]:GetText() end |
||
267 | if not ignoreright then txtr = self.vars.Rlines[i]:GetText() end |
||
268 | if txtl or txtr then |
||
269 | if not retval then retval = self.vars.compost and self.vars.compost:Acquire() or {} end |
||
270 | local t = self.vars.compost and self.vars.compost:Acquire(txtl, txtr) or {txtl, txtr} |
||
271 | table.insert(retval, t) |
||
272 | end |
||
273 | end |
||
274 | |||
275 | return retval |
||
276 | end |
||
277 | |||
278 | |||
279 | -- Returns the text from a specific line (both left and right unless second arg is true) |
||
280 | -- Args: |
||
281 | -- line - the line number you wish to retrieve |
||
282 | -- getright - if passed the right line will be returned, if not the left will be returned |
||
283 | function lib:GetLine(line, getright) |
||
284 | assert(type(line) == "number", "No line number passed") |
||
285 | if self.vars.tooltip:NumLines() < line then return end |
||
286 | if getright then return self.vars.Rlines[line] and self.vars.Rlines[line]:GetText() |
||
287 | elseif self.vars.Llines[line] then |
||
288 | return self.vars.Llines[line]:GetText(), self.vars.Rlines[line]:GetText() |
||
289 | end |
||
290 | end |
||
291 | |||
292 | |||
293 | ----------------------------------- |
||
294 | -- Set tooltip methods -- |
||
295 | ----------------------------------- |
||
296 | |||
297 | -- These methods are designed to immitate the GameTooltip API |
||
298 | -- CreateSetMethods is called on RegisterTooltip if not yet defined |
||
299 | |||
300 | local testmethods = { |
||
301 | SetAction = function(id) return HasAction(id) end, |
||
302 | } |
||
303 | local gettrue = function() return true end |
||
304 | function lib:CreateSetMethods() |
||
305 | for _,m in pairs(methods) do |
||
306 | local meth = m |
||
307 | local func = testmethods[meth] or gettrue |
||
308 | lib[meth] = function(self,a1,a2,a3,a4) |
||
309 | self:Erase() |
||
310 | if not func(a1,a2,a3,a4) then return end |
||
311 | return self.vars.tooltip[meth](self.vars.tooltip,a1,a2,a3,a4) |
||
312 | end |
||
313 | end |
||
314 | end |
||
315 | |||
316 | |||
317 | -------------------------------- |
||
318 | -- Load this bitch! -- |
||
319 | -------------------------------- |
||
320 | libobj:Register(lib) |