vanilla-wow-addons – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 --[[
2 BagnonCore
3 Core bag functionality
4  
5 BagSlots:
6 -2: Key (1.11)
7 -1: Bank (24 slots)
8 0: Main Inventory (16 slots)
9 1, 2, 3, 4: Inventory Bags (16 - 32?)
10 5, 6, 7, 8, 9, 10: Bank Bags (16 - 32?)
11  
12 TODO
13 Add in 1.11 functionality
14 --]]
15  
16 --Local constants
17 local DEFAULT_COLS = 8;
18 local DEFAULT_SPACING = 2;
19  
20 --[[
21 Load settings for the frame
22 --]]
23  
24 function BagnonCore_LoadSettings(frame, bags, title)
25 local frameName = frame:GetName()
26 --make frame close on escape
27 tinsert(UISpecialFrames, frameName);
28  
29 --initialize variables for the frame if there are none
30 if(not BagnonSets[frameName] ) then
31 BagnonSets[frameName] = {
32 stayOnScreen = 1,
33 };
34 end
35  
36 --add what bags are controlled by the frame
37 if(not BagnonSets[frameName].bags) then
38 BagnonSets[frameName].bags = bags;
39 end
40  
41 --set the frame's transparency
42 if(BagnonSets[frameName].alpha) then
43 frame:SetAlpha(BagnonSets[frameName].alpha);
44 end
45  
46 --set the frame's background
47 if(not BagnonSets[frameName].bg or tonumber(BagnonSets[frameName].bg) ) then
48 BagnonSets[frameName].bg = {r = 0, g = 0, b = 0, a = 1};
49 end
50  
51 local bgSets= BagnonSets[frameName].bg;
52 frame:SetBackdropColor(bgSets.r, bgSets.g, bgSets.b, bgSets.a);
53 frame:SetBackdropBorderColor(1, 1, 1, bgSets.a);
54  
55 --set the frame's layer (low, medium, or high)
56 if(BagnonSets[frameName].strata) then
57 BagnonMenu_SetStrata(frame, BagnonSets[frameName].strata);
58 end
59  
60 local bagFrame = getglobal(frameName .. "Bags");
61 if(BagnonSets[frameName].bagsShown) then
62 bagFrame:Show();
63 getglobal(frameName .. "ShowBags"):SetText(BAGNON_HIDEBAGS);
64 end
65  
66 frame:SetClampedToScreen(BagnonSets[frameName].stayOnScreen);
67  
68 --load any settings related to BagnonForever/BagnonKC
69 if(BagnonForever) then
70 frame.player = UnitName("player");
71 frame.defaultBags = bags;
72 end
73  
74 --set the frame's ordering. this is dependent on BagnonForever's data
75 BagnonCore_OrderBags(frame, BagnonSets[frameName].reverse);
76  
77 --set the frame's title
78 frame.title = title;
79 getglobal(frameName .. "Title"):SetText( format(frame.title, UnitName("player") ) );
80 frame:RegisterForClicks("LeftButtonDown", "LeftButtonUp", "RightButtonUp");
81  
82 --create the frame
83 --reposition actually handles rescaling
84 BagnonCore_Reposition(frame);
85 BagnonCore_GenerateFrame(frame);
86 BagnonCore_LayoutFrame(frame, BagnonSets[frameName].cols, BagnonSets[frameName].space);
87 end
88  
89 --[[
90 Generate the frame (add all items, resize)
91 --]]
92  
93 function BagnonCore_GenerateFrame(frame)
94 frame.size = 0;
95 local frameName = frame:GetName();
96  
97 --generate a cached frame
98 if( Bagnon_IsCachedFrame(frame) ) then
99 MoneyFrame_Update(frameName .. "MoneyFrame", BagnonDB_GetMoney(frame.player));
100  
101 for bag in frame.defaultBags do
102 BagnonCore_AddBag(frame, frame.defaultBags[bag]);
103 end
104 --generate a normal frame
105 else
106 MoneyFrame_Update(frameName .. "MoneyFrame", GetMoney());
107  
108 for bag in BagnonSets[frameName].bags do
109 BagnonCore_AddBag(frame, BagnonSets[frameName].bags[bag]);
110 end
111 end
112  
113 BagnonCore_LayoutFrame(frame, BagnonSets[frame:GetName()].cols, BagnonSets[frame:GetName()].space);
114 frame:Show();
115 end
116  
117 --[[
118 Add all the slots of the given bag to the given frame.
119 Increase the total size of frame to include the size of the bag
120 --]]
121  
122 function BagnonCore_AddBag(frame, bagID)
123 local frameName = frame:GetName();
124 local slot, index, bagSize, link, item; --slot refers to the itemslot in <frame>, index refers to the item slot in the bag
125  
126 slot = frame.size;
127  
128 if( Bagnon_IsCachedBag(frame.player, bagID) ) then
129 bagSize = BagnonDB_GetBagData(frame.player, bagID);
130 else
131 if(bagID == KEYRING_CONTAINER) then
132 bagSize = GetKeyRingSize();
133 else
134 bagSize = GetContainerNumSlots(bagID);
135 end
136 end
137  
138 --update used slots
139 for index = 1, bagSize, 1 do
140 slot = slot + 1;
141  
142 item = getglobal( frameName .. "Item".. slot) or BagnonItem_Create(frameName .. "Item".. slot, frame);
143 item:SetID(index);
144 item:GetParent():SetID(bagID);
145 item:Show();
146  
147 BagnonCore_UpdateItemInfo(item);
148 end
149  
150 frame.size = frame.size + bagSize;
151 end
152  
153 --[[
154 Resize and hide any unusable bag slots in the frame.
155 This function needs to know about the size and layout of the frame
156 --]]
157  
158 function BagnonCore_TrimToSize(frame)
159 if( not frame.space ) then
160 return;
161 end
162  
163 local frameName = frame:GetName();
164 local slot, height;
165  
166 --hide unused slots
167 if(frame.size) then
168 local slot = frame.size + 1;
169 local button = getglobal( frameName .. "Item".. slot );
170  
171 while button do
172 button:Hide();
173 slot = slot + 1;
174 button = getglobal( frameName .. "Item".. slot );
175 end
176 end
177  
178 --set the frame's width
179 --correction for any frame that's completely empty
180 if(not frame.size or frame.size == 0 ) then
181 height = 64;
182 frame:SetWidth(256);
183 else
184 --24 is the estimated border width of the frame, 37 is about the width of an item slot
185 if(frame.size < frame.cols) then
186 frame:SetWidth( ( 37 + frame.space ) * frame.size + 23 - frame.space );
187 else
188 frame:SetWidth( ( 37 + frame.space ) * frame.cols + 23 - frame.space );
189 end
190  
191 --set the frame's height, adjusted to fit a bag frame if its shown/there is one
192 height = ( 37 + frame.space ) * math.ceil( frame.size / frame.cols ) + 64 - frame.space;
193 end
194  
195 --adjust for the bag frame
196 local bagFrame = getglobal(frame:GetName() .. "Bags");
197  
198 if( bagFrame and bagFrame:IsShown() ) then
199 frame:SetHeight(height + bagFrame:GetHeight());
200  
201 if(frame:GetWidth() < bagFrame:GetWidth()) then
202 frame:SetWidth(bagFrame:GetWidth());
203 end
204 else
205 frame:SetHeight(height);
206 end
207  
208 --reposition the frame to not be out of bounds
209 BagnonCore_SavePosition(frame);
210 end
211  
212  
213 -- Update all item information for usable slots
214 function BagnonCore_Update(frame)
215 if( not frame.size or Bagnon_IsCachedFrame(frame) ) then
216 return;
217 end
218  
219 local frameName = frame:GetName();
220 local slot;
221  
222 for slot = 1, frame.size, 1 do
223 BagnonCore_UpdateItemInfo( getglobal( frameName .. "Item" .. slot ) );
224 end
225 end
226  
227 -- Update the texture, lock status, and other information about an item
228 function BagnonCore_UpdateItemInfo(item)
229  
230 if( Bagnon_IsCachedItem(item) ) then
231 item.isLink = 1;
232  
233 local link, texture, count = BagnonDB_GetItemData(item);
234 --update texture and count
235 SetItemButtonTexture( item, texture );
236 SetItemButtonCount( item, count );
237 BagnonItem_UpdateLinkBorder(item);
238  
239 if ( texture ) then
240 item.hasItem = 1;
241 else
242 item.hasItem = nil;
243 end
244  
245 --hide cooldown since there isn't one for linked items
246 BagnonItem_UpdateCooldown( item:GetParent():GetID() , item);
247 else
248 item.isLink = nil;
249  
250 local texture, itemCount, locked, quality, readable = GetContainerItemInfo(item:GetParent():GetID(), item:GetID());
251  
252 BagnonItem_UpdateBorder(item);
253 SetItemButtonTexture(item, texture);
254 SetItemButtonCount(item, itemCount);
255 SetItemButtonDesaturated(item, locked, 0.5, 0.5, 0.5);
256  
257 if ( texture ) then
258 BagnonItem_UpdateCooldown( item:GetParent():GetID() , item);
259 item.hasItem = 1;
260 else
261 getglobal(item:GetName() .. "Cooldown"):Hide();
262 item.hasItem = nil;
263 end
264  
265 item.readable = readable;
266 end
267 end
268  
269 -- Layout the frame with given number of columns and button spacing
270 function BagnonCore_LayoutFrame(frame, cols, space)
271 if(not frame.size) then
272 return;
273 end
274  
275 local frameName = frame:GetName();
276  
277 if(not cols) then
278 cols = DEFAULT_COLS;
279 end
280  
281 if(not space) then
282 space = DEFAULT_SPACING;
283 end
284  
285 if(cols == DEFAULT_COLS) then
286 BagnonSets[frameName].cols = nil
287 else
288 BagnonSets[frameName].cols = cols
289 end
290  
291 if(space == DEFAULT_SPACING) then
292 BagnonSets[frameName].space = nil;
293 else
294 BagnonSets[frameName].space = space;
295 end
296  
297 local rows = math.ceil( frame.size / cols );
298 local index = 1;
299 local button;
300  
301 --resize the frame
302 frame.cols = cols;
303 frame.space = space;
304  
305  
306 button = getglobal(frameName .. "Item1");
307 if(button) then
308 button:ClearAllPoints();
309 button:SetPoint("TOPLEFT", frame, "TOPLEFT", 12, -32);
310  
311 for i = 1, rows, 1 do
312 for j = 1, cols, 1 do
313  
314 index = index + 1;
315 button = getglobal(frameName .. "Item" .. index);
316  
317 if(not button) then
318 break;
319 end
320  
321 button:ClearAllPoints();
322 button:SetPoint("LEFT", frameName .. "Item" .. index - 1, "RIGHT", space, 0);
323 end
324  
325 button = getglobal(frameName .. "Item" .. index);
326  
327 if(not button) then
328 break;
329 end
330  
331 button:ClearAllPoints();
332 button:SetPoint("TOP", frameName .. "Item" .. index - cols, "BOTTOM", 0, -space);
333 end
334 end
335  
336 BagnonCore_TrimToSize(frame);
337 end
338  
339 --[[
340 Safe Open/Close/Toggle <frame>
341 --]]
342  
343 function BagnonCore_Open(frameName)
344 local frame = getglobal(frameName);
345 if( frame ) then
346 BagnonCore_GenerateFrame(frame);
347 else
348 LoadAddOn(frameName);
349 end
350 end
351  
352 function BagnonCore_Close(frameName)
353 local frame = getglobal(frameName);
354 if ( frame ) then
355 frame:Hide();
356 end
357 end
358  
359 function BagnonCore_Toggle(frameName)
360 local frame = getglobal(frameName);
361 if( frame ) then
362 if ( frame:IsVisible() ) then
363 BagnonCore_Close(frameName);
364 else
365 BagnonCore_Open(frameName);
366 end
367 else
368 LoadAddOn(frameName);
369 end
370 end
371  
372 --[[
373 Highlight all the slots of <bag>
374 --]]
375  
376 function BagnonCore_HighlightSlots(frame, bagID)
377 if(not frame.size) then return; end
378  
379 local frameName = frame:GetName();
380 local slot;
381  
382 --update only the slots the player can use
383 for slot = 1, frame.size, 1 do
384 local item = getglobal( frameName .. "Item" .. slot );
385  
386 if( item:GetParent():GetID() == bagID ) then
387 item:LockHighlight();
388 end
389 end
390 end
391  
392 function BagnonCore_UnhighlightAll(frame)
393 if(not frame.size) then return; end
394  
395 local frameName = frame:GetName();
396 local slot;
397  
398 --update only the slots the player can use
399 for slot = 1, frame.size, 1 do
400 getglobal( frameName .. "Item" .. slot ):UnlockHighlight();
401 end
402 end
403  
404 --[[
405 Add/Remove <Bag> from the frame
406 --]]
407  
408 function BagnonCore_ToggleBagForFrame(frame, bagID)
409 if(not frame) then return; end
410  
411 local frameName = frame:GetName();
412  
413 --add bag
414 if(not Bagnon_FrameHasBag(frameName, bagID) ) then
415 table.insert(BagnonSets[frameName].bags, bagID);
416 --remove bag
417 else
418 local index;
419  
420 for index in BagnonSets[frameName].bags do
421 if( BagnonSets[frameName].bags[index] and BagnonSets[frameName].bags[index] == bagID) then
422 table.remove(BagnonSets[frame:GetName()].bags, index);
423 end
424 end
425 end
426  
427 BagnonCore_OrderBags(frame, BagnonSets.reverseOrder);
428  
429 --update frame
430 if(frame:IsShown()) then
431 BagnonCore_GenerateFrame(frame);
432 end
433 end
434  
435 --[[
436 Frame Positioning Functions
437 --]]
438  
439 function BagnonCore_StartMoving(frame)
440 if(not BagnonSets[frame:GetName()].locked) then
441 frame.isMoving = 1;
442 frame:StartMoving();
443 end
444 end
445  
446 function BagnonCore_StopMoving(frame)
447 frame.isMoving = nil;
448 frame:StopMovingOrSizing();
449 BagnonCore_SavePosition(frame);
450 end
451  
452 --Place the frame at the last place it was at.
453 --This is used when the frame first loads because the game currently does not remember the last position of a frame that's dynamically loaded
454 function BagnonCore_Reposition(frame)
455 local frameName = frame:GetName();
456  
457 if(not (BagnonSets[frameName] and BagnonSets[frameName].top) ) then
458 return;
459 end
460  
461 local ratio;
462 --this step is to take care of a potential flaw if the UI scale changes between loadings of the frame
463 if(BagnonSets[frameName].parentScale ) then
464 ratio = BagnonSets[frameName].parentScale / frame:GetParent():GetScale();
465 else
466 ratio = 1;
467 end
468  
469 frame:ClearAllPoints();
470 frame:SetScale(BagnonSets[frameName].scale);
471 frame:SetPoint("TOPLEFT", frame:GetParent(), "BOTTOMLEFT", BagnonSets[frameName].left * ratio, BagnonSets[frameName].top * ratio);
472 end
473  
474 --Save the frame's current position. Needed because frames don't remember their positions if dynamically loaded.
475 function BagnonCore_SavePosition(frame)
476 local frameName = frame:GetName();
477  
478 if(not BagnonSets[frameName] ) then
479 BagnonSets[frameName] = {};
480 end
481  
482 BagnonSets[frameName].top = frame:GetTop();
483 BagnonSets[frameName].left = frame:GetLeft();
484 BagnonSets[frameName].scale = frame:GetScale();
485 BagnonSets[frameName].parentScale = frame:GetParent():GetScale();
486 end
487  
488 --[[
489 Tooltip Functions
490 --]]
491  
492 --tooltips for the title
493 function BagnonCore_OnHide()
494 if(BagnonMenu:IsVisible() and BagnonMenu.frame == this) then
495 BagnonMenu:Hide();
496 end
497  
498 if(BagnonForeverMenu and BagnonForeverMenu:IsVisible() and BagnonForeverMenu.frame == this) then
499 BagnonForeverMenu:Hide();
500 end
501 end
502  
503 function BagnonCore_OnEnter()
504 if(BagnonSets.showTooltips) then
505 GameTooltip_SetDefaultAnchor(GameTooltip,this);
506  
507 GameTooltip:SetText(this:GetText(), 1, 1, 1);
508 GameTooltip:AddLine(BAGNON_TITLE_TOOLTIP);
509  
510 if(BagnonForever) then
511 GameTooltip:AddLine(BAGNON_TITLE_FOREVERTOOLTIP);
512 end
513 GameTooltip:Show();
514 end
515 end
516  
517 function BagnonCore_OnLeave()
518 GameTooltip:Hide();
519 end
520  
521 --money frame tooltips, ment to be overriden by forever/kc
522 function BagnonCoreMoney_OnEnter()
523 return;
524 end
525  
526 function BagnonCoreMoney_OnLeave()
527 GameTooltip:Hide();
528 end
529  
530 -- This is a hack that enables tooltips but still allows clicking on the money frame
531 function BagnonCoreMoney_OnClick()
532 local parentName = this:GetParent():GetName();
533  
534 if( MouseIsOver(getglobal(parentName .. "GoldButton")) ) then
535 local parent = this:GetParent();
536 OpenCoinPickupFrame(COPPER_PER_GOLD, MoneyTypeInfo[parent.moneyType].UpdateFunc(), parent);
537 parent.hasPickup = 1;
538 elseif( MouseIsOver(getglobal(parentName .. "SilverButton")) ) then
539 local parent = this:GetParent();
540 OpenCoinPickupFrame(COPPER_PER_SILVER, MoneyTypeInfo[parent.moneyType].UpdateFunc(), parent);
541 parent.hasPickup = 1;
542 elseif( MouseIsOver(getglobal(parentName .. "CopperButton")) ) then
543 local parent = this:GetParent();
544 OpenCoinPickupFrame(1, MoneyTypeInfo[parent.moneyType].UpdateFunc(), parent);
545 parent.hasPickup = 1;
546 end
547 end
548  
549 --[[
550 Rightclick Menu Stuff
551 --]]
552  
553 --this function is there so that it can be overriden
554 function BagnonCore_OnDoubleClick(frame)
555 return;
556 end
557  
558 function BagnonCore_OnClick(frame, mouseButton)
559 if(mouseButton == "RightButton") then
560 BagnonMenu_Show(frame);
561 end
562 end
563  
564 --[[
565 Bag Sorting
566 --]]
567  
568 function BagnonCore_ReverseSort(a, b)
569 if(a == KEYRING_CONTAINER) then
570 return true;
571 elseif(b == KEYRING_CONTAINER) then
572 return false;
573 elseif(a and b) then
574 return a > b;
575 end;
576 end
577  
578 function BagnonCore_NormalSort(a, b)
579 if(a == KEYRING_CONTAINER) then
580 return false;
581 elseif(b == KEYRING_CONTAINER) then
582 return true;
583 elseif(a and b) then
584 return a < b;
585 end
586 end
587  
588 function BagnonCore_OrderBags(frame, reverse)
589 if(reverse) then
590 if(frame) then
591 table.sort(BagnonSets[frame:GetName()].bags, BagnonCore_ReverseSort);
592 if(frame.defaultBags) then
593 table.sort(frame.defaultBags, BagnonCore_ReverseSort);
594 end
595 end
596 else
597 if(frame) then
598 table.sort(BagnonSets[frame:GetName()].bags, BagnonCore_NormalSort);
599 if(frame.defaultBags) then
600 table.sort(frame.defaultBags, BagnonCore_NormalSort);
601 end
602 end
603 end
604 end