Mercurial > wow > inventory
comparison Summary.lua @ 62:fee06221176f
Seperated the config from Core.lua.
Many other code cleaning up for readability.
Added local references for much used globals.
Moved widgets to a different file for readability.
Re-added global function for slash command handling since we do need it for our chat-hyperlinks.
Fixed queueing to properly use the track at property of virtual groups.
Fixed queueing to display the item id instead of the item link if the item link could not be loaded.
Speed slider at the summary now has an interval of 1% down from 5% and rounds rather than ceils it?s value so 101% will become 100% rather than 105%.
Now using the right stock properties at the summary.
Added a help group to the config.
author | Zerotorescue |
---|---|
date | Wed, 22 Dec 2010 19:56:55 +0100 |
parents | d903b0a151d3 |
children | 7ca83ad9d67a |
comparison
equal
deleted
inserted
replaced
61:d903b0a151d3 | 62:fee06221176f |
---|---|
1 local addon = select(2, ...); | 1 local addon = select(2, ...); -- Get a reference to the main addon object |
2 local mod = addon:NewModule("Summary", "AceEvent-3.0", "AceTimer-3.0"); | 2 local mod = addon:NewModule("Summary", "AceEvent-3.0", "AceTimer-3.0"); -- register a new module, Summary: resposible for building the summary window |
3 | |
4 local AceGUI = LibStub("AceGUI-3.0"); | |
5 | 3 |
6 local unknownItemName = "Unknown (#%d)"; | 4 local unknownItemName = "Unknown (#%d)"; |
7 | 5 |
8 local itemsCache = {}; | 6 local CACHE_ITEMS_TOTAL, CACHE_ITEMS_CURRENT, itemsCache = 0, 0, {}; |
9 | 7 local AceGUI, cacheStart; |
10 local CACHE_ITEMS_TOTAL, CACHE_ITEMS_CURRENT = 0, 0; | 8 |
11 local cacheStart; | 9 local _G = _G; -- prevent looking up of the global table |
10 local printf, sgsub, supper, tinsert, pairs, ceil, GetItemInfo = _G.string.format, _G.string.gsub, _G.string.upper, _G.table.insert, _G.pairs, _G.ceil, _G.GetItemInfo; -- prevent looking up of the most used globals all the time | |
12 | 11 |
13 function mod:OnEnable() | 12 function mod:OnEnable() |
14 self:RegisterWidgets(); | 13 -- Register the summary specific widget |
14 addon:GetModule("Widgets"):InlineGroupWithButton(); | |
15 | |
16 AceGUI = LibStub("AceGUI-3.0"); | |
15 | 17 |
16 -- Register our own slash commands | 18 -- Register our own slash commands |
19 -- /im summary | |
17 addon:RegisterSlash(function() | 20 addon:RegisterSlash(function() |
18 mod:BuildMain(); | 21 mod:BuildMain(); |
19 mod:Build(); | 22 mod:Build(); |
20 end, { "s", "sum", "summary" }, "|Hfunction:InventoriumCommandHandler:summary|h|cff00fff7/im summary|r|h (or /im s) - Show the summary window containing an overview of all items within the groups tracked at this current character."); | 23 end, { "s", "sum", "summary" }, "|Hfunction:InventoriumCommandHandler:summary|h|cff00fff7/im summary|r|h (or /im s) - Show the summary window containing an overview of all items within the groups tracked at this current character."); |
24 | |
25 -- /im reset | |
21 addon:RegisterSlash(function() | 26 addon:RegisterSlash(function() |
22 if mod.frame then | 27 if mod.frame then |
23 mod.frame:SetWidth(650); | 28 mod.frame:SetWidth(650); |
24 mod.frame:SetHeight(600); | 29 mod.frame:SetHeight(600); |
25 | 30 |
43 GameTooltip:Show(); | 48 GameTooltip:Show(); |
44 end | 49 end |
45 | 50 |
46 local function HideTooltip() | 51 local function HideTooltip() |
47 GameTooltip:Hide(); | 52 GameTooltip:Hide(); |
48 end | |
49 | |
50 function mod:RegisterWidgets() | |
51 -- Register InlineGroupWithButton-widget | |
52 -- This widget adds a button next to the header of an inline group | |
53 -- SetPoint doesn't seem usable within AceGUI. | |
54 | |
55 local widgetType = "InlineGroupWithButton"; | |
56 local widgetVersion = 1; | |
57 | |
58 local function Constructor() | |
59 local widget = AceGUI:Create("InlineGroup"); | |
60 widget.type = widgetType; | |
61 | |
62 widget.MakeButton = function(self, buttonSettings) | |
63 if type(buttonSettings) == "table" then | |
64 if not self.btnQueue then | |
65 -- Because widgets are re-used, we don't want to recreate this button | |
66 self.btnQueue = CreateFrame("Button", nil, self.frame, "UIPanelButtonTemplate"); | |
67 self.btnQueue:SetHeight(22); | |
68 self.btnQueue:SetWidth(120); | |
69 end | |
70 self.btnQueue:SetText(buttonSettings.name); | |
71 self.btnQueue:SetPoint("TOPRIGHT", self.frame, "TOPRIGHT", -10, 5); | |
72 | |
73 -- Triggers | |
74 self.btnQueue:SetScript("OnClick", buttonSettings.exec); | |
75 | |
76 -- Tooltip | |
77 self.btnQueue.tooltipTitle = buttonSettings.name; | |
78 self.btnQueue.tooltip = buttonSettings.desc or ""; | |
79 self.btnQueue:SetScript("OnEnter", ShowTooltip); | |
80 self.btnQueue:SetScript("OnLeave", HideTooltip); | |
81 else | |
82 error("settings must be a table - usage: MakeButton(table);"); | |
83 end | |
84 end; | |
85 | |
86 return widget; | |
87 end | |
88 | |
89 AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion); | |
90 end | 53 end |
91 | 54 |
92 function mod:BuildMain() | 55 function mod:BuildMain() |
93 LibStub("AceConfigDialog-3.0"):Close("InventoriumOptions"); | 56 LibStub("AceConfigDialog-3.0"):Close("InventoriumOptions"); |
94 | 57 |
155 end | 118 end |
156 | 119 |
157 -- From http://www.wowwiki.com/API_sort | 120 -- From http://www.wowwiki.com/API_sort |
158 local function pairsByKeys (t, f) | 121 local function pairsByKeys (t, f) |
159 local a = {} | 122 local a = {} |
160 for n in pairs(t) do table.insert(a, n) end | 123 for n in pairs(t) do tinsert(a, n) end |
161 table.sort(a, f) | 124 table.sort(a, f) |
162 local i = 0 -- iterator variable | 125 local i = 0 -- iterator variable |
163 local iter = function () -- iterator function | 126 local iter = function () -- iterator function |
164 i = i + 1 | 127 i = i + 1 |
165 if a[i] == nil then return nil | 128 if a[i] == nil then return nil |
202 mod.scrollFrame:AddChild(lblSpacer); | 165 mod.scrollFrame:AddChild(lblSpacer); |
203 | 166 |
204 -- Speed slider | 167 -- Speed slider |
205 local sdrSpeed = AceGUI:Create("Slider"); | 168 local sdrSpeed = AceGUI:Create("Slider"); |
206 sdrSpeed:SetLabel("Processing speed"); | 169 sdrSpeed:SetLabel("Processing speed"); |
207 sdrSpeed:SetSliderValues(0.01, 5, 0.05); | 170 sdrSpeed:SetSliderValues(0.01, 5, 0.01); -- min, max, interval |
208 sdrSpeed:SetIsPercent(true); | 171 sdrSpeed:SetIsPercent(true); |
209 sdrSpeed:SetRelativeWidth(.3); | 172 sdrSpeed:SetRelativeWidth(.3); |
210 sdrSpeed:SetCallback("OnMouseUp", function(self, event, value) | 173 sdrSpeed:SetCallback("OnMouseUp", function(self, event, value) |
211 addon.db.profile.defaults.summary.speed = ceil( value * 100 / 5 ); | 174 addon.db.profile.defaults.summary.speed = ceil( ( ( value * 100 ) / 5) - .5 ); |
212 | 175 |
213 CACHE_ITEMS_PER_UPDATE = addon.db.profile.defaults.summary.speed; -- max = 20, min = 1 | 176 CACHE_ITEMS_PER_UPDATE = addon.db.profile.defaults.summary.speed; -- max = 20, min = 1 |
214 end); | 177 end); |
215 sdrSpeed:SetValue( addon.db.profile.defaults.summary.speed * 5 / 100 ); | 178 sdrSpeed:SetValue( addon.db.profile.defaults.summary.speed * 5 / 100 ); |
216 sdrSpeed:SetCallback("OnEnter", ShowTooltip); | 179 sdrSpeed:SetCallback("OnEnter", ShowTooltip); |
274 -- Does this group have any items and do we want to track it at this char? | 237 -- Does this group have any items and do we want to track it at this char? |
275 if values.items and trackAt[playerName] then | 238 if values.items and trackAt[playerName] then |
276 | 239 |
277 | 240 |
278 -- Get group settings | 241 -- Get group settings |
279 local minGlobalStock = addon:GetOptionByKey(groupName, "minimumStock"); | 242 local minLocalStock = addon:GetOptionByKey(groupName, "minLocalStock"); |
280 local minLocalStock = addon:GetOptionByKey(groupName, "minimumLocalStock"); | 243 local minGlobalStock = addon:GetOptionByKey(groupName, "minGlobalStock"); |
281 local showWhenBelow = addon:GetOptionByKey(groupName, "summaryThresholdShow"); | 244 local showWhenBelow = addon:GetOptionByKey(groupName, "summaryThresholdShow"); |
282 local priceThreshold = addon:GetOptionByKey(groupName, "priceThreshold"); | 245 local priceThreshold = addon:GetOptionByKey(groupName, "priceThreshold"); |
283 local hideWhenBelowPriceThreshold = addon:GetOptionByKey(groupName, "summaryHidePriceThreshold"); | 246 local hideWhenBelowPriceThreshold = addon:GetOptionByKey(groupName, "summaryHidePriceThreshold"); |
284 local alwaysGetAuctionValue = addon:GetOptionByKey(groupName, "alwaysGetAuctionValue"); | 247 local alwaysGetAuctionValue = addon:GetOptionByKey(groupName, "alwaysGetAuctionValue"); |
285 | 248 |
361 | 324 |
362 -- Sort item list | 325 -- Sort item list |
363 for itemId, _ in pairs(values.items) do | 326 for itemId, _ in pairs(values.items) do |
364 local itemName, itemLink, itemRarity = GetItemInfo(itemId); | 327 local itemName, itemLink, itemRarity = GetItemInfo(itemId); |
365 | 328 |
366 table.insert(itemsCache[groupName], { | 329 tinsert(itemsCache[groupName], { |
367 id = itemId, | 330 id = itemId, |
368 name = itemName or unknownItemName:format(itemId), | 331 name = itemName or printf(unknownItemName, itemId), |
369 link = itemLink, | 332 link = itemLink, |
370 value = ((priceThreshold == 0 and not alwaysGetAuctionValue) and -4) or -3,-- if (no price threshold is set for this item and you don't want to always get auction value), then don't look it up either --addon:GetAuctionValue(itemLink), | 333 value = ((priceThreshold == 0 and not alwaysGetAuctionValue) and -4) or -3,-- if (no price threshold is set for this item and you don't want to always get auction value), then don't look it up either --addon:GetAuctionValue(itemLink), |
371 rarity = itemRarity or 1, | 334 rarity = itemRarity or 1, |
372 count = -3,--addon:GetItemCount(itemId, groupName), | 335 count = -3,--addon:GetItemCount(itemId, groupName), |
373 localCount = -3, | 336 localCount = -3, |
385 table.sort(itemsCache[groupName], function(a, b) | 348 table.sort(itemsCache[groupName], function(a, b) |
386 if sortMethod == "item" and a.rarity == b.rarity then | 349 if sortMethod == "item" and a.rarity == b.rarity then |
387 -- Do a name-compare for items of the same rarity | 350 -- Do a name-compare for items of the same rarity |
388 -- Otherwise epics first, then junk | 351 -- Otherwise epics first, then junk |
389 if sortDirectory == "ASC" then | 352 if sortDirectory == "ASC" then |
390 return a.name:upper() < b.name:upper(); | 353 return supper(a.name) < supper(b.name); |
391 else | 354 else |
392 return a.name:upper() > b.name:upper(); | 355 return supper(a.name) > supper(b.name); |
393 end | 356 end |
394 elseif sortMethod == "item" then | 357 elseif sortMethod == "item" then |
395 if sortDirectory == "ASC" then | 358 if sortDirectory == "ASC" then |
396 return a.rarity > b.rarity; -- the comparers were reversed because we want epics first | 359 return a.rarity > b.rarity; -- the comparers were reversed because we want epics first |
397 else | 360 else |
427 for i, item in pairs(itemsCache[groupName]) do | 390 for i, item in pairs(itemsCache[groupName]) do |
428 -- Go through all items for this group | 391 -- Go through all items for this group |
429 | 392 |
430 if (( item.count / minGlobalStock ) < showWhenBelow or ( item.localCount / minLocalStock ) < showWhenBelow) and (not hideWhenBelowPriceThreshold or priceThreshold == 0 or item.value < 0 or item.value >= priceThreshold) then | 393 if (( item.count / minGlobalStock ) < showWhenBelow or ( item.localCount / minLocalStock ) < showWhenBelow) and (not hideWhenBelowPriceThreshold or priceThreshold == 0 or item.value < 0 or item.value >= priceThreshold) then |
431 -- if the option "hide when below threshold" is disabled or no price threshold is set or the value is above the price threshold or the value could not be determined, proceed | 394 -- if the option "hide when below threshold" is disabled or no price threshold is set or the value is above the price threshold or the value could not be determined, proceed |
432 | 395 |
433 local btnItemLink = AceGUI:Create("ItemLinkButton"); | 396 local btnItemLink = AceGUI:Create("ItemLinkButton"); |
434 btnItemLink:SetUserData("exec", function(_, itemId, _, buttonName) | 397 btnItemLink:SetUserData("exec", function(_, itemId, _, buttonName) |
435 local itemName, itemLink = GetItemInfo(itemId); | 398 local itemName, itemLink = GetItemInfo(itemId); |
436 | 399 |
437 if buttonName == "LeftButton" and IsShiftKeyDown() and itemLink then | 400 if buttonName == "LeftButton" and IsShiftKeyDown() and itemLink then |
463 lblQuantity:SetText(self:DisplayItemCount(item.count, minGlobalStock)); | 426 lblQuantity:SetText(self:DisplayItemCount(item.count, minGlobalStock)); |
464 lblQuantity:SetRelativeWidth(.099); | 427 lblQuantity:SetRelativeWidth(.099); |
465 | 428 |
466 iGroup:AddChild(lblQuantity); | 429 iGroup:AddChild(lblQuantity); |
467 | 430 |
468 --[[ | |
469 -- Required stock | |
470 local lblMinimumStock = AceGUI:Create("Label"); | |
471 lblMinimumStock:SetText(minGlobalStock); | |
472 lblMinimumStock:SetRelativeWidth(.099); | |
473 | |
474 iGroup:AddChild(lblMinimumStock); | |
475 ]] | |
476 | |
477 -- Value | 431 -- Value |
478 local lblValue = AceGUI:Create("Label"); | 432 local lblValue = AceGUI:Create("Label"); |
479 lblValue:SetText(self:DisplayMoney(item.value, priceThreshold)); | 433 lblValue:SetText(self:DisplayMoney(item.value, priceThreshold)); |
480 lblValue:SetRelativeWidth(.099); | 434 lblValue:SetRelativeWidth(.099); |
481 | 435 |
509 | 463 |
510 groupTimes.building = ceil( ( GetTime() - groupStartTime ) * 1000 ); | 464 groupTimes.building = ceil( ( GetTime() - groupStartTime ) * 1000 ); |
511 end | 465 end |
512 | 466 |
513 if groupStartTime and groupTimes then | 467 if groupStartTime and groupTimes then |
514 addon:Debug(("Building of %s took %d ms (%d / %d / %d / %d / %d)."):format(groupName, ceil( ( GetTime() - groupStartTime ) * 1000 ), groupTimes.init or 0, groupTimes.sorting or 0, groupTimes.preparing or 0, groupTimes.building or 0, ceil( ( GetTime() - buildStartTime ) * 1000 ))); | 468 addon:Debug(printf("Building of %s took %d ms (%d / %d / %d / %d / %d).", groupName, ceil( ( GetTime() - groupStartTime ) * 1000 ), groupTimes.init or 0, groupTimes.sorting or 0, groupTimes.preparing or 0, groupTimes.building or 0, ceil( ( GetTime() - buildStartTime ) * 1000 ))); |
515 end | 469 end |
516 end | 470 end |
517 | 471 |
518 mod.scrollFrame:ResumeLayout(); | 472 mod.scrollFrame:ResumeLayout(); |
519 mod.scrollFrame:DoLayout(); | 473 mod.scrollFrame:DoLayout(); |
520 | 474 |
521 addon:Debug(("Done building summary after %d ms."):format(ceil( ( GetTime() - buildStartTime ) * 1000 ))); | 475 addon:Debug(printf("Done building summary after %d ms.", ceil( ( GetTime() - buildStartTime ) * 1000 ))); |
522 | 476 |
523 if CACHE_ITEMS_TOTAL > 0 then | 477 if CACHE_ITEMS_TOTAL > 0 then |
524 cacheStart = GetTime(); | 478 cacheStart = GetTime(); |
525 self:CancelTimer(self.tmrUpdater, true); | 479 self:CancelTimer(self.tmrUpdater, true); |
526 self.tmrUpdater = self:ScheduleRepeatingTimer("UpdateNextItem", .01); -- Once every 100 frames (or once every x frames if you have less than 100 FPS, basically, once every frame) | 480 self.tmrUpdater = self:ScheduleRepeatingTimer("UpdateNextItem", .01); -- Once every 100 frames (or once every x frames if you have less than 100 FPS, basically, once every frame) |
571 | 525 |
572 i = i + 1; | 526 i = i + 1; |
573 CACHE_ITEMS_CURRENT = CACHE_ITEMS_CURRENT + 1; | 527 CACHE_ITEMS_CURRENT = CACHE_ITEMS_CURRENT + 1; |
574 | 528 |
575 if mod.frame then | 529 if mod.frame then |
576 mod.frame:SetStatusText(("Caching auction values and item-counts... %d%% has already been processed."):format(floor(CACHE_ITEMS_CURRENT / CACHE_ITEMS_TOTAL * 100))); | 530 mod.frame:SetStatusText(printf("Caching auction values and item-counts... %d%% has already been processed.", floor(CACHE_ITEMS_CURRENT / CACHE_ITEMS_TOTAL * 100))); |
577 end | 531 end |
578 | 532 |
579 if i >= addon.db.profile.defaults.summary.speed then | 533 if i >= addon.db.profile.defaults.summary.speed then |
580 return; | 534 return; |
581 end | 535 end |
602 | 556 |
603 function mod:ColorCode(num, required) | 557 function mod:ColorCode(num, required) |
604 local percentage = ( num / required ); | 558 local percentage = ( num / required ); |
605 | 559 |
606 if percentage >= addon.db.profile.defaults.colors.green then | 560 if percentage >= addon.db.profile.defaults.colors.green then |
607 return ("|cff00ff00%d|r"):format(num); | 561 return printf("|cff00ff00%d|r", num); |
608 elseif percentage >= addon.db.profile.defaults.colors.yellow then | 562 elseif percentage >= addon.db.profile.defaults.colors.yellow then |
609 return ("|cffffff00%d|r"):format(num); | 563 return printf("|cffffff00%d|r", num); |
610 elseif percentage >= addon.db.profile.defaults.colors.orange then | 564 elseif percentage >= addon.db.profile.defaults.colors.orange then |
611 return ("|cffff9933%d|r"):format(num); | 565 return printf("|cffff9933%d|r", num); |
612 elseif percentage >= addon.db.profile.defaults.colors.red then | 566 elseif percentage >= addon.db.profile.defaults.colors.red then |
613 return ("|cffff0000%d|r"):format(num); | 567 return printf("|cffff0000%d|r", num); |
614 end | 568 end |
615 end | 569 end |
616 | 570 |
617 function mod:DisplayMoney(value, priceThreshold) | 571 function mod:DisplayMoney(value, priceThreshold) |
618 if value == -1 then | 572 if value == -1 then |
624 elseif value == -4 then | 578 elseif value == -4 then |
625 return "|cff00ff00-|r"; | 579 return "|cff00ff00-|r"; |
626 elseif value == -5 then | 580 elseif value == -5 then |
627 return "|cffff9933Error|r"; | 581 return "|cffff9933Error|r"; |
628 elseif priceThreshold and value < priceThreshold then | 582 elseif priceThreshold and value < priceThreshold then |
629 return ("|cffaaaaaa%s|r"):format(addon:ReadableMoney(value or 0, true):gsub("|([a-fA-F0-9]+)([gsc]+)|r", "%2")); | 583 return printf("|cffaaaaaa%s|r", sgsub(addon:ReadableMoney(value or 0, true), "|([a-fA-F0-9]+)([gsc]+)|r", "%2")); |
630 else | 584 else |
631 return addon:ReadableMoney(value or 0, true); | 585 return addon:ReadableMoney(value or 0, true); |
632 end | 586 end |
633 end | 587 end |
634 | 588 |
636 if value == -1 then | 590 if value == -1 then |
637 return "|cffffff00Unknown|r"; | 591 return "|cffffff00Unknown|r"; |
638 elseif value == -3 then | 592 elseif value == -3 then |
639 return "|cffffff00Unknown|r"; | 593 return "|cffffff00Unknown|r"; |
640 else | 594 else |
641 return ("%s / %d"):format(self:ColorCode(value, minimumStock), minimumStock); | 595 return printf("%s / %d", self:ColorCode(value, minimumStock), minimumStock); |
642 end | 596 end |
643 end | 597 end |
644 | 598 |
645 function mod:NumberFormat(num) | 599 function mod:NumberFormat(num) |
646 local formatted = string.gsub(num, "(%d)(%d%d%d)$", "%1,%2", 1); | 600 local formatted = sgsub(num, "(%d)(%d%d%d)$", "%1,%2", 1); |
647 | 601 |
648 while true do | 602 while true do |
649 formatted, matches = string.gsub(formatted, "(%d)(%d%d%d),", "%1,%2,", 1); | 603 formatted, matches = sgsub(formatted, "(%d)(%d%d%d),", "%1,%2,", 1); |
650 | 604 |
651 if matches == 0 then | 605 if matches == 0 then |
652 break; | 606 break; |
653 end | 607 end |
654 end | 608 end |