vanilla-wow-addons – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 --
2 -- ZubanLib Library Functions
3 -- Version: 0.1.2
4 -- Author: Zuban (chumpnet@hotmail.com)
5 --
6  
7 ZubanLib = {
8 Const = {
9 Texture = {
10 HAWK = "Interface\\Icons\\Spell_Nature_RavenForm";
11 MONKEY = "Interface\\Icons\\Ability_Hunter_AspectOfTheMonkey";
12 CHEETAH = "Interface\\Icons\\Ability_Mount_JungleTiger";
13 };
14 };
15  
16 -- action bar
17 Action = {
18 BAG_VALUE = -2;
19 NUM_SLOTS = 120;
20 };
21  
22 -- paper doll
23 Inventory = {
24 BAG_VALUE = -1;
25 NUM_SLOTS = 18;
26 };
27  
28 -- bags
29 Container = {};
30  
31 -- spellbook
32 Spell = {};
33  
34 -- buffs
35 Buff = {};
36  
37 -- write
38 IO = {
39 Chat = {};
40 Screen = {};
41 };
42  
43 -- stores extra cursor values
44 Cursor = {};
45 };
46  
47 --
48 -- Util functions
49 --
50  
51 --
52 -- http://lua-users.org/wiki/RetiredLuaFaq
53 --
54 function ZubanLib.Clone(t) -- return a copy of the table t
55 local new = {}; -- create a new table
56 local i, v = next(t, nil); -- i is an index of t, v = t[i]
57  
58 while i do
59 if (type(v) == "table") then
60 v = ZubanLib.Clone(v);
61 end
62  
63 new[i] = v;
64 i, v = next(t, i); -- get next index
65 end
66  
67 return new;
68 end
69  
70 function ZubanLib.IsBoolean(value)
71 return (value == true or value == false);
72 end
73  
74 function ZubanLib.ToString(value)
75 if (value == nil) then
76 return "nil";
77 elseif (ZubanLib.IsBoolean(value)) then
78 if (value) then
79 return "true";
80 else
81 return "false";
82 end
83 else
84 return value;
85 end
86 end
87  
88 function ZubanLib.InCombat()
89 if (MGplayer ~= nil) then
90 -- compatibility with MiniGroup addon created by Kaitlin
91 return MGplayer.inCombat;
92 elseif (PlayerFrame ~= nil) then
93 return PlayerFrame.inCombat;
94 else
95 return nil;
96 end
97 end
98  
99 function ZubanLib.SetScale(frame, scale)
100 return frame:SetScale(UIParent:GetScale() * scale);
101 end
102  
103 --
104 -- Action functions
105 --
106  
107 function ZubanLib.Action.GoToPage(page)
108 page = tonumber(page);
109  
110 if (page > 0) then
111 if (CURRENT_ACTIONBAR_PAGE ~= page) then
112 CURRENT_ACTIONBAR_PAGE = page;
113 ChangeActionBarPage();
114 end
115 end
116 end
117  
118 function ZubanLib.Action.FindSlotByName(name, rank)
119 local textName, textRank;
120  
121 for slot = 1, ZubanLib.Action.NUM_SLOTS do
122 GameTooltip:SetAction(slot);
123 textName = GameTooltipTextLeft1:GetText();
124 textRank = GameTooltipTextRight1:GetText();
125  
126 if (textName == name and (rank == nil or rank == "" or textRank == rank)) then
127 return slot;
128 end
129 end
130  
131 return nil;
132 end
133  
134 function ZubanLib.Action.FindSlotByTexture(name)
135 local count, text, texture;
136  
137 for slot = 1, ZubanLib.Action.NUM_SLOTS do
138 count = GetActionCount(slot);
139 text = GetActionText(slot);
140 texture = GetActionTexture(slot);
141  
142 if (count == 0 and text == nil and texture == name) then
143 return slot;
144 end
145 end
146  
147 return nil;
148 end
149  
150 --
151 -- Inventory functions
152 --
153  
154 function ZubanLib.Inventory.FindSlotByName(name)
155 local itemName;
156  
157 for slot = 1, ZubanLib.Inventory.NUM_SLOTS do
158 itemName = GetInventoryItemName(slot);
159  
160 if(itemName == name) then
161 return slot;
162 end
163 end
164  
165 return nil;
166 end
167  
168 --
169 -- Container functions
170 --
171  
172 function ZubanLib.Container.FindBagSlotByName(name)
173 local itemLink;
174  
175 for bag = 0, NUM_BAG_FRAMES do
176 for slot = 1, GetContainerNumSlots(bag) do
177 itemLink = GetContainerItemLink(bag, slot);
178  
179 if (itemLink) then
180 --ZubanLib.IO.Chat.Write(itemLink);
181 if (itemLink == name) then
182 return bag, slot;
183 end
184 end
185 end
186 end
187  
188 return nil, nil;
189 end
190  
191 function ZubanLib.Container.CountByName(name)
192 local total = 0;
193 local itemLink;
194 local itemcount;
195  
196 for bag = 0, NUM_BAG_FRAMES do
197 for slot = 1, GetContainerNumSlots(bag) do
198 itemLink = GetContainerItemLink(bag, slot);
199  
200 if (itemLink ~= nil) then
201 local _,_,itemLink = string.find(itemLink, "^.*%[(.*)%].*$");
202 local _,_,name = string.find(name, "^.*%[(.*)%].*$");
203  
204 if (itemLink == name) then
205 _, itemcount = GetContainerItemInfo(bag, slot);
206 total = total + itemcount;
207 end
208 end
209 end
210 end
211  
212 return total;
213 end
214  
215 function ZubanLib.FindBagSlotByName(name)
216 local slot = ZubanLib.Inventory.FindSlotByName(name);
217  
218 if (slot) then
219 return ZubanLib.Inventory.BAG_VALUE, slot;
220 else
221 return ZubanLib.Container.FindSlotByName(name);
222 end
223 end
224  
225 --
226 -- spellbook functions
227 --
228  
229 function ZubanLib.Spell.FindIDByName(name, subName, bookType)
230 local numSkillLineTabs = GetNumSpellTabs();
231 local tabName, texture, offset, numSpells;
232 local skillLineTab;
233 local spellName, subSpellName;
234  
235 if (not bookType) then
236 bookType = BOOKTYPE_SPELL;
237 end
238  
239 for i = 1, numSkillLineTabs do
240 tabName, texture, offset, numSpells = GetSpellTabInfo(i);
241  
242 for id = offset + 1, offset + numSpells do
243 spellName, subSpellName = GetSpellName(id, bookType);
244  
245 --ZubanLib.IO.Chat.Write(id.." "..spellName.." "..subSpellName.." "..name);
246  
247 if (spellName == name and (not subName or subName == subSpellName)) then
248 return id;
249 end
250 end
251 end
252  
253 return nil;
254 end
255  
256 function ZubanLib.Spell.CastSpellByName(name, subName, bookType)
257 if (not bookType) then
258 bookType = BOOKTYPE_SPELL;
259 end
260  
261 local id = ZubanLib.Spell.FindIDByName(name, subName, bookType);
262  
263 if (id) then
264 return CastSpell(id, bookType);
265 end
266 end
267  
268 function ZubanLib.Buff.FindIDByTexture(name, unit)
269 if (not unit) then
270 unit = "player";
271 end
272  
273 local id = 1;
274 local buff = UnitBuff(unit, id);
275  
276 while (buff) do
277 if (buff == name) then
278 return id;
279 end
280  
281 id = id + 1;
282 buff = UnitBuff(unit, id);
283 end
284  
285 return nil;
286 end
287  
288 --
289 -- output functions
290 --
291  
292 function ZubanLib.IO.Chat.Warn(message)
293 ZubanLib.IO.Chat.Write(message, 1, 0.2, 0.2);
294 end
295  
296 function ZubanLib.IO.Chat.Write(message, red, green, blue, frame)
297 if (not red) then
298 red = 1;
299 end
300  
301 if (not green) then
302 green = 1;
303 end
304  
305 if (not blue) then
306 blue = 1;
307 end
308  
309 if (frame) then
310 frame:AddMessage(message, red, green, blue, 1.0, UIERRORS_HOLD_TIME);
311 elseif (DEFAULT_CHAT_FRAME) then
312 DEFAULT_CHAT_FRAME:AddMessage(message, red, green, blue);
313 end
314 end
315  
316 function ZubanLib.IO.Screen.Warn(message)
317 ZubanLib.IO.Screen.Write(message, 1, 0.2, 0.2);
318 end
319  
320 function ZubanLib.IO.Screen.Write(message, red, green, blue)
321 ZubanLib.IO.Chat.Write(message, red, green, blue, UIErrorsFrame);
322 end
323  
324 --
325 -- cursor object
326 --
327  
328 local BasePickupContainer = PickupContainerItem;
329  
330 function PickupContainerItem(bag, slot)
331 local result = BasePickupContainer(bag, slot);
332  
333 if (CursorHasItem()) then
334 ZubanLib.Cursor:PickupContainerItem(bag, slot);
335 end
336  
337 return result;
338 end
339  
340 local BasePickupAction = PickupAction;
341  
342 function PickupAction(slot)
343 ZubanLib.Cursor:PickupAction(slot);
344 return BasePickupAction(slot);
345 end
346  
347 local BasePickupSpell = PickupSpell;
348  
349 function PickupSpell(id, bookType)
350 local result = BasePickupSpell(id, bookType);
351  
352 if (CursorHasSpell()) then
353 ZubanLib.Cursor:PickupSpell(id, bookType);
354 end
355  
356 return result;
357 end
358  
359 function ZubanLib.Cursor:PickupContainerItem(bag, slot)
360 local link = GetContainerItemLink(bag, slot);
361 local texture, itemcount, locked, quality, readable = GetContainerItemInfo(bag, slot);
362  
363 self.Attributes = {};
364 self.Attributes.Location = "Container";
365 self.Attributes.Name = link;
366 self.Attributes.Bag = bag;
367 self.Attributes.Slot = slot;
368 self.Attributes.Texture = texture;
369 self.Attributes.Count = itemcount;
370 self.Attributes.Locked = locked;
371 self.Attributes.Quality = quality;
372 self.Attributes.Readable = readable;
373  
374 --self:Write();
375 end
376  
377 function ZubanLib.Cursor:PickupAction(slot)
378 local count = GetActionCount(slot);
379 local text = GetActionText(slot);
380 local texture = GetActionTexture(slot);
381  
382 self.Attributes = {};
383 self.Attributes.Location = "Action";
384 self.Attributes.Slot = slot;
385 self.Attributes.Count = count;
386 self.Attributes.Text = text;
387 self.Attributes.Texture = texture;
388  
389 if (GameTooltip ~= nil) then
390 GameTooltipTextLeft1:SetText("");
391 GameTooltipTextRight1:SetText(""); -- clear the rank
392 GameTooltip:SetAction(slot);
393  
394 local name = GameTooltipTextLeft1:GetText();
395 local rank = GameTooltipTextRight1:GetText();
396  
397 self.Attributes.Name = name;
398 self.Attributes.Rank = rank;
399  
400 --[[
401 local lines = GameTooltip:NumLines();
402 self.Attributes.Lines = lines;
403  
404 for i = 1, lines do
405 textLeft = getglobal("GameTooltipTextLeft"..i);
406 if (textLeft ~= nil) then
407 self.Attributes["Left"..i] = textLeft:GetText();
408 end
409  
410 textRight = getglobal("GameTooltipTextRight"..i);
411 if (textRight ~= nil) then
412 self.Attributes["Right"..i] = textRight:GetText();
413 end
414 end
415 ]]
416 end
417  
418 --self:Write();
419 end
420  
421 function ZubanLib.Cursor:PickupSpell(id, bookType)
422 local name, rank = GetSpellName(id, bookType);
423 local texture = GetSpellTexture(id, bookType);
424 --local start, duration, enable = GetSpellCooldown (id, bookType);
425 --local passive = IsSpellPassive(id, bookType);
426  
427 self.Attributes = {};
428 self.Attributes.Location = "Spell";
429 self.Attributes.ID = id;
430 self.Attributes.BookType = bookType;
431 self.Attributes.Name = name;
432 self.Attributes.Rank = rank;
433 self.Attributes.Texture = texture;
434 --self.Attributes.Start = start;
435 --self.Attributes.Duration = duration;
436 --self.Attributes.Enable = enable;
437 --self.Attributes.Passive = passive;
438  
439 --self:Write();
440 end
441  
442 function ZubanLib.Cursor:Clear()
443 self.Attributes = {};
444 end
445  
446 function ZubanLib.Cursor.WriteAttribute(key, value)
447 ZubanLib.IO.Chat.Write(ToString(key)..": "..ToString(value));
448 end
449  
450 function ZubanLib.Cursor:Write()
451 if (self.Attributes ~= nil) then
452 table.foreach(self.Attributes, ZubanLib.Cursor.WriteAttribute);
453 end
454 end