Quantcast
WoWInterface - View Single Post - How could this be written neater?
View Single Post
05-07-17, 05:13 PM   #3
MunkDev
A Scalebane Royal Guard
 
MunkDev's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2015
Posts: 429
(1) You're doing checkups on tables that may not exist. If you don't intend to add tables for every class and spec, anyone else using your addon that is not a destruction warlock will get errors.

(2) What's the point in your holder frame and not just using the AuraTrack frame you already created to show your auras?

(3) You don't need to upvalue the global environment just to grab a few functions. This process is quick and painless and only happens once on login anyway.

(4) Unless there's a specific reason why you're using a set max of 6 auras, you can use a frame factory / frame pool to spawn your trackers instead of creating them on load. I'll show you an example of how this works with the native implementation already in the UI.

Lua Code:
  1. -- Upvalue variables
  2. local GetSpellInfo = GetSpellInfo;
  3. local UnitAura = UnitAura;
  4.  
  5. -- Local variables
  6. local size = 40;
  7. local point = {"CENTER", UIParent, "CENTER", 0, -110};
  8. local gap = 2;
  9.  
  10. local buffIDList = {
  11.     ["WARLOCK"] = {
  12.         [3] = { -- Destruction
  13.             235156,
  14.             215165,
  15.             119899,
  16.         },
  17.     },
  18. };
  19.  
  20. -- AuraTrack
  21. local AuraTrack = CreateFrame("Frame", "AuraTrack", UIParent);
  22. AuraTrack.numTotalObjects = 0;
  23. AuraTrack:SetPoint(unpack(point));
  24. AuraTrack:RegisterEvent("ACTIVE_TALENT_GROUP_CHANGED");
  25. AuraTrack:RegisterEvent("PLAYER_LOGIN");
  26. AuraTrack:RegisterEvent("ADDON_LOADED");
  27. AuraTrack:SetScript("OnEvent", function(self, event, ...)
  28.     self[event](self, ...);
  29. end);
  30.  
  31.  
  32. function AuraTrack:UNIT_AURA(...)
  33.     -- Release all the currently active objects
  34.     -- See the :ResetTracker function for what it does to individual objects.
  35.     self:ReleaseAll();
  36.  
  37.     -- Get the buffs to track for the current spec
  38.     -- Assert a table exists for the player's class and spec
  39.     local buffIDs = self.buffIDList;
  40.  
  41.     if buffIDs then
  42.  
  43.         -- Iterate over buff IDs
  44.         local prevButton;
  45.         for i, buffID in pairs(buffIDs) do
  46.             local spellName = GetSpellInfo(buffID);
  47.             local _, _, icon, count, _, duration, expires = UnitAura(..., spellName);
  48.             if duration and duration > 0 then
  49.                 -- Acquire a button from the object pool
  50.                 local button = self:Acquire();
  51.  
  52.                 -- Assign your stuff
  53.                 button.spellID = buffID; -- not sure this is necessary with this approach.
  54.                 button.cd:SetCooldown(expires - duration, duration);
  55.                 button.icon:SetTexture(icon);
  56.                 button.count:SetText((count and count > 0) and count or "");
  57.                
  58.                 -- Show and set the point for the button
  59.                 button:Show();
  60.                 if prevButton then
  61.                     button:SetPoint("LEFT", prevButton, "RIGHT", gap, 0);
  62.                 else
  63.                     button:SetPoint("LEFT");
  64.                 end
  65.  
  66.                 -- Reference this button so the next one knows where to anchor
  67.                 prevButton = button;
  68.             end
  69.         end
  70.     end
  71.  
  72.     -- Update width to match active objects.
  73.     -- numActiveObjects is provided by the object pool mixin.
  74.     self:SetSize(((size + gap) * self.numActiveObjects) - gap, size);
  75. end
  76.  
  77. -- Update spec and the buff table when the spec actually changes
  78. function AuraTrack:ACTIVE_TALENT_GROUP_CHANGED()
  79.     local _, class = UnitClass("player");
  80.     local spec = GetSpecialization();
  81.     self.buffIDList = buffIDList[class] and buffIDList[class][spec];
  82. end
  83.  
  84. function AuraTrack:ADDON_LOADED(...)
  85.     if ... == "MyAuraTracker" then
  86.         self.ADDON_LOADED = nil;
  87.         self:UnregisterEvent("ADDON_LOADED");
  88.         self:RegisterUnitEvent("UNIT_AURA", "player");
  89.     end
  90. end
  91.  
  92. function AuraTrack:PLAYER_LOGIN()
  93.     -- Run the spec check on login
  94.     self:ACTIVE_TALENT_GROUP_CHANGED();
  95.     self:UnregisterEvent("PLAYER_LOGIN");
  96.     self.PLAYER_LOGIN = nil;
  97. end
  98.  
  99. -- Related to creating and recycling active trackers
  100. function AuraTrack:CreateTracker()
  101.     -- This counter is only here to generate non-garbage frame names
  102.     self.numTotalObjects = self.numTotalObjects + 1;
  103.  
  104.     local id = self.numTotalObjects;
  105.     local button = CreateFrame("Frame", "$parentButton"..id, self);
  106.  
  107.     button:Hide();
  108.     button:SetSize(size, size);
  109.  
  110.     local cd = CreateFrame("Cooldown", "$parentCooldown", button, "CooldownFrameTemplate");
  111.     cd:SetAllPoints(button);
  112.  
  113.     local icon = button:CreateTexture(nil, "BORDER");
  114.     icon:SetAllPoints(button);
  115.  
  116.     local count = button:CreateFontString(nil, "OVERLAY");
  117.     count:SetFontObject(NumberFontNormal);
  118.     count:SetPoint("TOP", button, "BOTTOM", 0, -2);
  119.  
  120.     button.cd = cd;
  121.     button.icon = icon;
  122.     button.count = count;
  123.  
  124.     return button;
  125. end
  126.  
  127. function AuraTrack:ResetTracker(button)
  128.     button.spellID = nil;
  129.     button:ClearAllPoints();
  130.     button:Hide();
  131. end
  132.  
  133. -- Mix in the object pool functions
  134. Mixin(AuraTrack, ObjectPoolMixin);
  135. -- Give the object pool creation and resetting functions
  136. AuraTrack:OnLoad(AuraTrack.CreateTracker, AuraTrack.ResetTracker);
__________________

Last edited by MunkDev : 05-07-17 at 05:35 PM.
  Reply With Quote