Advertisement
Guest User

UnitPowerBarAlt

a guest
Sep 28th, 2012
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 24.84 KB | None | 0 0
  1. ALTERNATE_POWER_INDEX = 10;
  2.  
  3. ALT_POWER_TYPE_HORIZONTAL       = 0;
  4. ALT_POWER_TYPE_VERTICAL         = 1;
  5. ALT_POWER_TYPE_CIRCULAR         = 2;
  6. ALT_POWER_TYPE_PILL             = 3;
  7. --Counter bar uses a different frame
  8. ALT_POWER_TYPE_COUNTER          = 4;
  9.  
  10. DOUBLE_SIZE_FIST_BAR = 199;
  11.  
  12. local altPowerBarTextures = {
  13.     frame = 0,
  14.     background = 1,
  15.     fill = 2,
  16.     spark = 3,
  17.     flash = 4,
  18. }
  19.  
  20. ALT_POWER_TEX_FRAME             = 0;
  21. ALT_POWER_TEX_BACKGROUND        = 1;
  22. ALT_POWER_TEX_FILL              = 2;
  23. ALT_POWER_TEX_SPARK             = 3;
  24. ALT_POWER_TEX_FLASH             = 4;
  25.  
  26. ALT_POWER_BAR_PLAYER_SIZES = {  --Everything else is scaled off of this
  27.     [ALT_POWER_TYPE_HORIZONTAL]     = {x = 256, y = 64},
  28.     [ALT_POWER_TYPE_VERTICAL]       = {x = 64, y = 128},
  29.     [ALT_POWER_TYPE_CIRCULAR]       = {x = 128, y = 128},
  30.     [ALT_POWER_TYPE_PILL]           = {x = 32, y = 64}, --This is the size of a single pill.
  31.     [ALT_POWER_TYPE_COUNTER]        = {x = 32, y = 32},
  32.     doubleCircular                  = {x = 256, y = 256}, --Override for task 55676
  33. }
  34.  
  35. function UnitPowerBarAlt_Initialize(self, unit, scale, updateAllEvent)
  36.     self.unit = unit;
  37.     self.counterBar.unit = unit;
  38.     self.scale = scale;
  39.     if ( updateAllEvent ) then
  40.         UnitPowerBarAlt_SetUpdateAllEvent(self, updateAllEvent)
  41.     end
  42.    
  43.     self:RegisterEvent("UNIT_POWER_BAR_SHOW");
  44.     self:RegisterEvent("UNIT_POWER_BAR_HIDE");
  45.     self:RegisterEvent("PLAYER_ENTERING_WORLD");
  46.     self.pillFrames = {};
  47. end
  48.  
  49. function UnitPowerBarAlt_OnEnter(self)
  50.     local statusFrame = self.statusFrame;
  51.     if ( statusFrame.enabled ) then
  52.         statusFrame:Show();
  53.         UnitPowerBarAltStatus_UpdateText(statusFrame);
  54.     end
  55.     GameTooltip_SetDefaultAnchor(GameTooltip, self);
  56.     GameTooltip:SetText(self.powerName, 1, 1, 1);
  57.     GameTooltip:AddLine(self.powerTooltip, nil, nil, nil, 1);
  58.     GameTooltip:Show();
  59. end
  60.  
  61. function UnitPowerBarAlt_OnLeave(self)
  62.     UnitPowerBarAltStatus_ToggleFrame(self.statusFrame);
  63.     GameTooltip:Hide();
  64. end
  65.  
  66. function UnitPowerBarAlt_OnEvent(self, event, ...)
  67.     local arg1, arg2 = ...;
  68.    
  69.     if ( event == self.updateAllEvent ) then
  70.         UnitPowerBarAlt_UpdateAll(self);
  71.     elseif ( event == "UNIT_POWER_BAR_SHOW" ) then
  72.         if ( arg1 == self.unit ) then
  73.             UnitPowerBarAlt_UpdateAll(self);
  74.         end
  75.     elseif ( event == "UNIT_POWER_BAR_HIDE" ) then
  76.         if ( arg1 == self.unit ) then
  77.             UnitPowerBarAlt_UpdateAll(self);
  78.         end
  79.     elseif ( event == "UNIT_POWER" ) then
  80.         if ( arg1 == self.unit and arg2 == "ALTERNATE" ) then
  81.             local barType, minPower = UnitAlternatePowerInfo(self.unit);
  82.             local currentPower = UnitPower(self.unit, ALTERNATE_POWER_INDEX);
  83.            
  84.             if ( not barType or barType == ALT_POWER_TYPE_COUNTER ) then
  85.                 CounterBar_UpdateCount(self.counterBar, currentPower);
  86.             else
  87.                 UnitPowerBarAlt_SetPower(self, currentPower);
  88.             end
  89.         end
  90.     elseif ( event == "UNIT_MAXPOWER" ) then
  91.         if ( arg1 == self.unit and arg2 == "ALTERNATE" ) then
  92.             local barType, minPower = UnitAlternatePowerInfo(self.unit);
  93.             if ( not barType or barType == ALT_POWER_TYPE_COUNTER ) then
  94.                 CounterBar_SetUp(self.counterBar);
  95.                 return;
  96.             end
  97.             UnitPowerBarAlt_SetMinMaxPower(self, minPower, UnitPowerMax(self.unit, ALTERNATE_POWER_INDEX));
  98.            
  99.             local currentPower = UnitPower(self.unit, ALTERNATE_POWER_INDEX);
  100.             UnitPowerBarAlt_SetPower(self, currentPower, true);
  101.         end
  102.     elseif ( event == "PLAYER_ENTERING_WORLD" ) then
  103.         UnitPowerBarAlt_UpdateAll(self);
  104.     end
  105. end
  106.  
  107. function UnitPowerBarAlt_SetUpdateAllEvent(self, event)
  108.     self.updateAllEvent = event;
  109.     self:RegisterEvent(event);
  110. end
  111.  
  112. local maxPerSecond = 0.7;
  113. local minPerSecond = 0.3;
  114. function GetSmoothProgressChange(value, displayedValue, range, elapsed)
  115.     local minPerSecond = max(minPerSecond, 1/range);    --Make sure we're moving at least 1 unit/second (will only matter if our maximum power is 3 or less);
  116.    
  117.     local diff = displayedValue - value;
  118.     local diffRatio = diff / range;
  119.     local change = range * ((minPerSecond/abs(diffRatio) + maxPerSecond - minPerSecond) * diffRatio) * elapsed;
  120.     if ( abs(change) > abs(diff) or abs(diffRatio) < 0.01 ) then
  121.         return value;
  122.     else
  123.         return displayedValue - change;
  124.     end
  125. end
  126.  
  127. function UnitPowerBarAlt_OnUpdate(self, elapsed)
  128.     if ( self.smooth and  self.value and self.displayedValue and self.value ~= self.displayedValue ) then
  129.         UnitPowerBarAlt_SetDisplayedPower(self, GetSmoothProgressChange(self.value, self.displayedValue, self.range, elapsed));
  130.     end
  131. end
  132.  
  133. function UnitPowerBarAlt_ApplyTextures(frame, unit)
  134.     for textureName, textureIndex in pairs(altPowerBarTextures) do
  135.         local texture = frame[textureName];
  136.         local texturePath, r, g, b = UnitAlternatePowerTextureInfo(unit, textureIndex, frame.timerIndex);
  137.         texture:SetTexture(texturePath);
  138.         texture:SetVertexColor(r, g, b);
  139.     end
  140. end
  141.  
  142. function UnitPowerBarAlt_HideTextures(frame)
  143.     frame.flashAnim:Stop();
  144.     frame.flashOutAnim:Stop();
  145.     for textureName, textureIndex in pairs(altPowerBarTextures) do
  146.         local texture = frame[textureName];
  147.         texture:SetTexture(nil);
  148.         texture:Hide();
  149.     end
  150. end
  151.  
  152. function UnitPowerBarAlt_HidePills(self)
  153.     if ( self.pillFrames ) then
  154.         for i=1, #self.pillFrames do
  155.             self.pillFrames[i]:Hide();
  156.         end
  157.     end
  158. end
  159.  
  160. function UnitPowerBarAlt_SetUp(self, barID)
  161.     local barType, minPower, startInset, endInset, smooth, hideFromOthers, showOnRaid, opaqueSpark, opaqueFlash, powerName, powerTooltip, costString;
  162.     if ( barID ) then
  163.         barType, minPower, startInset, endInset, smooth, hideFromOthers, showOnRaid, opaqueSpark, opaqueFlash, powerName, powerTooltip, costString = GetAlternatePowerInfoByID(barID);
  164.     else
  165.         barType, minPower, startInset, endInset, smooth, hideFromOthers, showOnRaid, opaqueSpark, opaqueFlash, powerName, powerTooltip, costString, barID = UnitAlternatePowerInfo(self.unit);
  166.     end
  167.    
  168.     self.startInset = startInset;
  169.     self.endInset = endInset;
  170.     self.smooth = smooth;
  171.     self.powerName = powerName;
  172.     self.powerTooltip = powerTooltip;
  173.    
  174.     local sizeInfo = ALT_POWER_BAR_PLAYER_SIZES[barType];
  175.     if ( barID == DOUBLE_SIZE_FIST_BAR and self.scale == 1 ) then --Double the player's own power bar for task 55676
  176.         sizeInfo = ALT_POWER_BAR_PLAYER_SIZES.doubleCircular;
  177.     end
  178.     self:SetSize(sizeInfo.x * self.scale, sizeInfo.y * self.scale);
  179.    
  180.     UnitPowerBarAlt_HideTextures(self); --It's up to the SetUp functions to show textures they need.
  181.     UnitPowerBarAlt_HidePills(self);
  182.  
  183.     if ( barType == ALT_POWER_TYPE_PILL or barType == ALT_POWER_TYPE_COUNTER ) then
  184.         self.statusFrame:Hide();
  185.         self.statusFrame.enabled = false;
  186.     else
  187.         self.statusFrame.enabled = true;
  188.         UnitPowerBarAltStatus_ToggleFrame(self.statusFrame);
  189.     end
  190.  
  191.     if ( barType == ALT_POWER_TYPE_HORIZONTAL ) then
  192.         UnitPowerBarAlt_Horizontal_SetUp(self);
  193.     elseif ( barType == ALT_POWER_TYPE_VERTICAL ) then
  194.         UnitPowerBarAlt_Vertical_SetUp(self);
  195.     elseif ( barType == ALT_POWER_TYPE_CIRCULAR ) then
  196.         UnitPowerBarAlt_Circular_SetUp(self);
  197.     elseif ( barType == ALT_POWER_TYPE_PILL ) then
  198.         UnitPowerBarAlt_Pill_SetUp(self);
  199.     elseif ( barType == ALT_POWER_TYPE_COUNTER ) then
  200.         self.counterBar:Show();
  201.         CounterBar_SetUp(self.counterBar);
  202.     else
  203.         error("Currently unhandled bar type: "..(barType or "nil"));
  204.     end
  205.    
  206.     if ( opaqueSpark ) then
  207.         self.spark:SetBlendMode("BLEND");
  208.     else
  209.         self.spark:SetBlendMode("ADD");
  210.     end
  211.    
  212.     if ( opaqueFlash ) then
  213.         self.flash:SetBlendMode("BLEND");
  214.     else
  215.         self.flash:SetBlendMode("ADD");
  216.     end
  217.    
  218.     self:RegisterUnitEvent("UNIT_POWER", self.unit);
  219.     self:RegisterUnitEvent("UNIT_MAXPOWER", self.unit);
  220. end
  221.  
  222.  
  223. function UnitPowerBarAlt_TearDown(self)
  224.     self.fill:SetTexCoord(0, 1, 0, 1);
  225.    
  226.     self.displayedValue = nil;
  227.    
  228.     self:UnregisterEvent("UNIT_POWER");
  229.     self:UnregisterEvent("UNIT_MAXPOWER");
  230. end
  231.  
  232. function UnitPowerBarAlt_UpdateAll(self)
  233.     local barType, minPower, startInset, endInset, smooth, hideFromOthers, showOnRaid = UnitAlternatePowerInfo(self.unit);
  234.     if ( barType and (not hideFromOthers or self.isPlayerBar) ) then
  235.         UnitPowerBarAlt_TearDown(self);
  236.         UnitPowerBarAlt_SetUp(self);
  237.  
  238.         local currentPower = UnitPower(self.unit, ALTERNATE_POWER_INDEX);
  239.         if ( barType ~= ALT_POWER_TYPE_COUNTER ) then
  240.             local maxPower = UnitPowerMax(self.unit, ALTERNATE_POWER_INDEX);
  241.             UnitPowerBarAlt_SetMinMaxPower(self, minPower, maxPower);
  242.            
  243.             UnitPowerBarAlt_SetPower(self, currentPower, true);
  244.         else
  245.             CounterBar_SetUp(self.counterBar);
  246.             CounterBar_UpdateCount(self.counterBar, currentPower, true);
  247.         end
  248.         self:Show();
  249.     else
  250.         UnitPowerBarAlt_TearDown(self);
  251.         self:Hide();
  252.         self.counterBar:Hide();
  253.     end
  254. end
  255.  
  256. function UnitPowerBarAlt_OnFlashPlay(self)
  257.     self:GetParent().flash:SetAlpha(0);
  258. end
  259.  
  260. function UnitPowerBarAlt_OnFlashFinished(self)
  261.     self:GetParent().flash:SetAlpha(1);
  262. end
  263.  
  264. function UnitPowerBarAlt_OnFlashOutFinished(self)
  265.     self:GetParent().flash:Hide();
  266. end
  267.  
  268. function UnitPowerBarAlt_SetPower(self, value, instantUpdate)
  269.     self.value = value;
  270.     if ( not self.smooth or instantUpdate or not self.displayedValue ) then
  271.         UnitPowerBarAlt_SetDisplayedPower(self, value);
  272.     end
  273.    
  274.     if ( value == self.maxPower ) then
  275.         if ( instantUpdate ) then
  276.             self.flash:Show();
  277.             self.flash:SetAlpha(1);
  278.         elseif ( not self.flash:IsShown() ) then
  279.             self.flash:Show();
  280.             self.flashAnim:Play();
  281.         elseif ( not self.flashAnim:IsPlaying() ) then
  282.             self.flash:SetAlpha(1);
  283.         end
  284.         self.flashOutAnim:Stop();
  285.     else
  286.         self.flashAnim:Stop();
  287.         if ( instantUpdate ) then
  288.             self.flash:Hide();
  289.         elseif ( self.flash:IsShown() and not self.flashOutAnim:IsPlaying() ) then
  290.             self.flashOutAnim:Play();
  291.         end
  292.     end
  293. end
  294.  
  295. function UnitPowerBarAlt_SetDisplayedPower(self, value)
  296.     self.displayedValue = value;
  297.     UnitPowerBarAltStatus_UpdateText(self.statusFrame);
  298.     self:UpdateFill();
  299. end
  300.  
  301. function UnitPowerBarAlt_SetMinMaxPower(self, minPower, maxPower)
  302.     self.range = maxPower - minPower;
  303.     self.maxPower = maxPower;
  304.     self.minPower = minPower;
  305.     self:UpdateFill();
  306. end
  307.  
  308. --Horizontal Status Bar
  309. function UnitPowerBarAlt_Horizontal_SetUp(self)
  310.     UnitPowerBarAlt_ApplyTextures(self, self.unit);
  311.    
  312.     self.frame:Show();
  313.     self.background:Show();
  314.     self.fill:Show();
  315.     self.spark:Show();
  316.    
  317.     self.spark:ClearAllPoints();
  318.     self.spark:SetHeight(self:GetHeight());
  319.     self.spark:SetWidth(self:GetHeight()/8);
  320.     self.spark:SetPoint("LEFT", self.fill, "RIGHT", -3 * self.scale, 0);
  321.    
  322.     self.fill:ClearAllPoints();
  323.     self.fill:SetPoint("TOPLEFT");
  324.     self.fill:SetPoint("BOTTOMLEFT");
  325.     self.fill:SetWidth(self:GetWidth());
  326.    
  327.     self.UpdateFill = UnitPowerBarAlt_Horizontal_UpdateFill;
  328. end
  329.  
  330. function UnitPowerBarAlt_Horizontal_UpdateFill(self)
  331.     if ( not self.range or self.range == 0 or not self.displayedValue ) then
  332.         return;
  333.     end
  334.     local ratio = self.displayedValue / self.range;
  335.     local fillAmount = self.startInset + ratio * ((1 - self.endInset) - self.startInset);
  336.     self.fill:SetWidth(max(self:GetWidth() * fillAmount, 1));
  337.     self.fill:SetTexCoord(0, fillAmount, 0, 1);
  338. end
  339.  
  340. --Vertical Status Bar
  341. function UnitPowerBarAlt_Vertical_SetUp(self)
  342.     UnitPowerBarAlt_ApplyTextures(self, self.unit);
  343.    
  344.     self.frame:Show();
  345.     self.background:Show();
  346.     self.fill:Show();
  347.     self.spark:Show();
  348.    
  349.     self.spark:ClearAllPoints();
  350.     self.spark:SetHeight(self:GetHeight()/8);
  351.     self.spark:SetWidth(self:GetWidth());
  352.     self.spark:SetPoint("BOTTOM", self.fill, "TOP", 0, -4 * self.scale);
  353.    
  354.     self.fill:ClearAllPoints();
  355.     self.fill:SetPoint("BOTTOMLEFT");
  356.     self.fill:SetPoint("BOTTOMRIGHT");
  357.     self.fill:SetHeight(self:GetHeight());
  358.    
  359.     self.UpdateFill = UnitPowerBarAlt_Vertical_UpdateFill;
  360. end
  361.  
  362. function UnitPowerBarAlt_Vertical_UpdateFill(self)
  363.     if ( not self.range or self.range == 0 or not self.displayedValue ) then
  364.         return;
  365.     end
  366.     local ratio = self.displayedValue / self.range;
  367.     local fillAmount = self.startInset + ratio * ((1 - self.endInset) - self.startInset);
  368.     self.fill:SetHeight(max(self:GetHeight() * fillAmount, 1));
  369.     self.fill:SetTexCoord(0, 1, 1 - fillAmount, 1);
  370. end
  371.  
  372. --Circular Bar
  373. function UnitPowerBarAlt_Circular_SetUp(self)
  374.     UnitPowerBarAlt_ApplyTextures(self, self.unit);
  375.    
  376.     self.frame:Show();
  377.     self.background:Show();
  378.     self.fill:Show();
  379.    
  380.     self.fill:ClearAllPoints();
  381.     self.fill:SetPoint("CENTER");
  382.     self.fill:SetSize(self:GetWidth(), self:GetHeight());
  383.    
  384.     self.UpdateFill = UnitPowerBarAlt_Circular_UpdateFill;
  385. end
  386.  
  387. function UnitPowerBarAlt_Circular_UpdateFill(self)
  388.     if ( not self.range or self.range == 0 or not self.displayedValue ) then
  389.         return;
  390.     end
  391.     local ratio = self.displayedValue / self.range;
  392.     local height, width = self:GetHeight() * ratio, self:GetWidth() * ratio;
  393.     height = max(height, 1);
  394.     width = max(width, 1);
  395.     self.fill:SetSize(width, height);
  396. end
  397.  
  398. --Pill Bar
  399. function UnitPowerBarAlt_Pill_SetUp(self)
  400.     --UnitPowerBarAlt_ApplyTextures(self, self.unit);   --For Pills, we apply the textures to the individual pill frames.
  401.     local sizeInfo = ALT_POWER_BAR_PLAYER_SIZES[ALT_POWER_TYPE_PILL];
  402.     for i = 1, #self.pillFrames do
  403.         local pillFrame = self.pillFrames[i];
  404.         UnitPowerBarAlt_ApplyTextures(pillFrame, self.unit);
  405.         pillFrame:SetHeight(self:GetHeight());
  406.         pillFrame:SetWidth(sizeInfo.x * self.scale);
  407.     end
  408.    
  409.     self.UpdateFill = UnitPowerBarAlt_Pill_UpdateFill;
  410. end
  411.  
  412. function UnitPowerBarAlt_Pill_UpdateFill(self)
  413.     if ( not self.range or self.range == 0 or not self.displayedValue ) then
  414.         return;
  415.     end
  416.    
  417.     for i=1, self.range do
  418.         local pillVal = self.minPower + i;
  419.         local pillFrame = self.pillFrames[i];
  420.         if ( not pillFrame ) then
  421.             pillFrame = UnitPowerBarAlt_Pill_CreatePillFrame(self);
  422.         end
  423.         if ( pillVal <= self.displayedValue ) then
  424.             pillFrame.flashAway:Stop();
  425.             pillFrame.fill:SetAlpha(1);
  426.             pillFrame.flash:SetAlpha(0);
  427.             if ( pillFrame:IsShown() and not pillFrame.fill:IsShown()) then
  428.                 pillFrame.flashAnim:Play();
  429.             end
  430.             pillFrame.fill:Show();
  431.         else
  432.             if ( pillFrame:IsShown() ) then
  433.                 if ( pillFrame.fill:IsShown() and not pillFrame.flashAway:IsPlaying() ) then
  434.                     pillFrame.flashAnim:Stop();
  435.                     pillFrame.fill:SetAlpha(1);
  436.                     pillFrame.flash:SetAlpha(0);
  437.                     pillFrame.flashAway:Play();
  438.                 end
  439.             else
  440.                 pillFrame.fill:Hide();
  441.             end
  442.         end
  443.         if ( pillVal == floor(self.displayedValue) ) then
  444.             pillFrame.spark:Show();
  445.         else
  446.             pillFrame.spark:Hide();
  447.         end
  448.        
  449.         pillFrame:Show();
  450.     end
  451.     for i=self.range + 1, #self.pillFrames do
  452.         local pillFrame = self.pillFrames[i];
  453.         pillFrame:Hide();
  454.     end
  455.     self:SetWidth(self.range * ALT_POWER_BAR_PLAYER_SIZES[ALT_POWER_TYPE_PILL].x * self.scale);
  456. end
  457.  
  458. function UnitPowerBarAlt_Pill_CreatePillFrame(self)
  459.     local sizeInfo = ALT_POWER_BAR_PLAYER_SIZES[ALT_POWER_TYPE_PILL];
  460.     local pillIndex = #self.pillFrames + 1;
  461.     local pillFrame = CreateFrame("Frame", self:GetName().."Pill"..pillIndex, self, "UnitPowerBarAltPillTemplate");
  462.    
  463.     if ( pillIndex == 1 ) then
  464.         pillFrame:SetPoint("LEFT", self, "LEFT", 0, 0);
  465.     else
  466.         pillFrame:SetPoint("LEFT", self.pillFrames[pillIndex - 1], "RIGHT", 0, 0);
  467.     end
  468.    
  469.     UnitPowerBarAlt_ApplyTextures(pillFrame, self.unit);
  470.    
  471.     pillFrame.flash:SetAlpha(0);
  472.    
  473.     pillFrame:SetHeight(self:GetHeight());
  474.     pillFrame:SetWidth(sizeInfo.x * self.scale);
  475.    
  476.     tinsert(self.pillFrames, pillFrame);
  477.     return pillFrame;
  478. end
  479.  
  480. function UnitPowerBarAlt_Pill_OnFlashAwayFinished(self)
  481.     local pill = self:GetParent();
  482.     pill.fill:Hide();
  483. end
  484.  
  485. -- status text functions
  486. -- self = statusFrame
  487. function UnitPowerBarAltStatus_UpdateText(self)
  488.     local powerBar = self:GetParent();
  489.     if ( powerBar.displayedValue and self:IsShown() ) then
  490.         TextStatusBar_UpdateTextStringWithValues(self, self.text, floor(powerBar.displayedValue), powerBar.minPower, powerBar.maxPower);
  491.     end
  492. end
  493.  
  494. function UnitPowerBarAltStatus_OnEvent(self, event, ...)
  495.     -- self = status frame
  496.     local cvar, value = ...;
  497.     local doUpdate = false;
  498.     if ( self.cvar and cvar == self.cvarLabel ) then
  499.         UnitPowerBarAltStatus_ToggleFrame(self);
  500.     elseif ( cvar == "STATUS_TEXT_PERCENT" ) then
  501.         UnitPowerBarAltStatus_UpdateText(self);
  502.     end
  503. end
  504.  
  505. function UnitPowerBarAltStatus_ToggleFrame(self)
  506.     -- self = status frame
  507.     if ( self.enabled and GetCVarBool(self.cvar) ) then
  508.         self:Show();
  509.         UnitPowerBarAltStatus_UpdateText(self);
  510.     else
  511.         self:Hide();
  512.     end
  513. end
  514.  
  515.  
  516. ---------------------------------
  517. ------- Counter Bar Code --------
  518. ---------------------------------
  519. local COUNTERBAR_CHANGE_TIME = 0.2;
  520. local COUNTERBAR_MAX_DIGIT = 7;
  521. local COUNTERBAR_COLUMNS = 8;
  522. local COUNTERBAR_ROWS = 2;
  523. local COUNTERBAR_NUMBER_WIDTH = 16;
  524. local COUNTERBAR_NUMBER_HEIGHT = 32;
  525. local COUNTERBAR_LEADING_ZERO_INDEX = 11;
  526. local COUNTERBAR_SLASH_INDEX = 10;
  527.  
  528.  
  529.  
  530. function CounterBar_SetUp(self)
  531.     local useFactional, animNumbers, barType = UnitAlternatePowerCounterInfo(self.unit);
  532.  
  533.     local maxValue = UnitPowerMax(self.unit, ALTERNATE_POWER_INDEX);
  534.     CounterBar_SetStyle(self, useFactional, animNumbers, maxValue);
  535.     self:RegisterEvent("UNIT_POWER");
  536.     self:RegisterEvent("UNIT_MAXPOWER");
  537.     self:Show();
  538. end
  539.  
  540.  
  541. function CounterBar_SetStyle(self, useFactional, animNumbers, maxValue)
  542.    
  543.     local texturePath, r, g, b;
  544.     --Set Textures
  545.     texturePath, r, g, b = UnitAlternatePowerTextureInfo(self.unit, 5, self.timerIndex);
  546.     for i=1,COUNTERBAR_MAX_DIGIT do
  547.         local digitFrame = self["digit"..i];
  548.         digitFrame.number:SetTexture(texturePath);
  549.         digitFrame.number:SetVertexColor(r, g, b);
  550.         digitFrame.numberMask:SetTexture(texturePath);
  551.         digitFrame.numberMask:SetVertexColor(r, g, b);
  552.         digitFrame:Show();
  553.     end
  554.    
  555.    
  556.     texturePath, r, g, b = UnitAlternatePowerTextureInfo(self.unit, 0, self.timerIndex);
  557.     self.BG:SetTexture(texturePath, true, false);
  558.     self.BG:SetVertexColor(r, g, b);
  559.     self.BGL:SetTexture(texturePath);
  560.     self.BGL:SetVertexColor(r, g, b);
  561.     self.BGR:SetTexture(texturePath);
  562.     self.BGR:SetVertexColor(r, g, b);
  563.     self.artTop:SetTexture(texturePath);
  564.     self.artTop:SetVertexColor(r, g, b);
  565.     self.artBottom:SetTexture(texturePath);
  566.     self.artBottom:SetVertexColor(r, g, b);
  567.    
  568.     --Set Initial State
  569.     local maxDigits = ceil(log10(maxValue));
  570.     local startIndex = 1;
  571.     if useFactional then
  572.         local count = maxValue;
  573.         for i=1,maxDigits+1 do
  574.             local digitFrame = self["digit"..i];
  575.             local digit = CounterBar_GetDigit(count);
  576.             count = floor(count/10);
  577.             if digit == COUNTERBAR_LEADING_ZERO_INDEX then
  578.                 digit = COUNTERBAR_SLASH_INDEX;
  579.             end
  580.             local l,r,t,b = CounterBar_GetNumberCoord(digit);
  581.             digitFrame.number:SetTexCoord(l,r,t,b);
  582.             digitFrame.numberMask:Hide();
  583.         end
  584.         startIndex = startIndex + maxDigits + 1;
  585.     end
  586.    
  587.     for i=startIndex+maxDigits,COUNTERBAR_MAX_DIGIT do
  588.         self["digit"..i]:Hide();
  589.     end
  590.    
  591.     self:SetWidth((startIndex+maxDigits-1)*COUNTERBAR_NUMBER_WIDTH);
  592.     self.count = 0;
  593.     self.maxValue = maxValue;
  594.     self.fractional = useFactional;
  595.     self.startIndex = startIndex;
  596.     CounterBar_SetNumbers(self);
  597. end
  598.  
  599.  
  600. function CounterBar_GetNumberCoord(digit)
  601.     local l,r,t,b;
  602.     l = (1/COUNTERBAR_COLUMNS) * mod(digit,COUNTERBAR_COLUMNS);
  603.     r = l + (1/COUNTERBAR_COLUMNS);
  604.     t = (1/COUNTERBAR_ROWS) * floor(digit/COUNTERBAR_COLUMNS);
  605.     b = t + (1/COUNTERBAR_ROWS);
  606.     return l,r,t,b;
  607. end
  608.  
  609.  
  610. function CounterBar_GetDigit(count, isFirstDigit)
  611.     local digit;
  612.     if count > 0 or isFirstDigit then
  613.         digit = mod(count, 10);
  614.     else
  615.         digit = COUNTERBAR_LEADING_ZERO_INDEX;
  616.     end
  617.     return digit;
  618. end
  619.  
  620.  
  621. function CounterBar_SetNumbers(self)
  622.     local l,r,t,b;
  623.     local count = self.count;
  624.     for i=self.startIndex,COUNTERBAR_MAX_DIGIT do
  625.         local digitFrame = self["digit"..i];
  626.         local digit = CounterBar_GetDigit(count, i==1);
  627.         count = floor(count/10);
  628.        
  629.         l,r,t,b = CounterBar_GetNumberCoord(digit);
  630.         digitFrame.number:SetTexCoord(l,r,t,b);
  631.         digitFrame.numberMask:Hide();
  632.     end
  633. end
  634.  
  635.  
  636. function CounterBar_UpdateCount(self, newCount, ignoreAnim)
  637.     local count1 = self.count;
  638.     local count2 = min(newCount, self.maxValue);
  639.     if count1 == count2 then
  640.         return;
  641.     end
  642.    
  643.     if ignoreAnim then
  644.         self.count = newCount;
  645.         CounterBar_SetNumbers(self)
  646.         return;
  647.     end
  648.    
  649.    
  650.     self.animUp = count1 < count2;
  651.     for i=self.startIndex,COUNTERBAR_MAX_DIGIT do
  652.         local digitFrame = self["digit"..i];
  653.         local digit1, digit2 = CounterBar_GetDigit(count1), CounterBar_GetDigit(count2);
  654.         count1, count2 = floor(count1/10), floor(count2/10);
  655.         if digit1 ~= digit2 then
  656.             digitFrame.numberMask:SetHeight(0);
  657.             digitFrame.animTime = COUNTERBAR_CHANGE_TIME;
  658.             digitFrame.numberMask:ClearAllPoints()
  659.             local l,r,t,b = CounterBar_GetNumberCoord(digit2);
  660.             if self.animUp then
  661.                 digitFrame.numberMask:SetPoint("BOTTOM", 0 ,0);
  662.                 digitFrame.numberMask:SetTexCoord(l,r,t,t);
  663.             else
  664.                 digitFrame.numberMask:SetPoint("TOP", 0 ,0);
  665.                 digitFrame.numberMask:SetTexCoord(l,r,b,b);
  666.             end
  667.             digitFrame.numberMask:Show();
  668.         end
  669.     end
  670.    
  671.     self.lastOnUpdate = self:GetScript("OnUpdate");
  672.     self:SetScript("OnUpdate", CounterBar_OnUpdate);
  673.     self.count = newCount;
  674. end
  675.  
  676.  
  677. function CounterBar_OnUpdate(self, elapsed)
  678.     local updatingNumbers = false
  679.     local l, t, _, b, r;
  680.  
  681.     for i=1,COUNTERBAR_MAX_DIGIT do
  682.         local digitFrame = self["digit"..i];
  683.         if digitFrame.animTime and digitFrame.animTime > 0 then
  684.             local delta = (elapsed/COUNTERBAR_CHANGE_TIME) * (1/COUNTERBAR_ROWS);
  685.             local deltaT, deltaB = 0, 0;
  686.             if not self.animUp then
  687.                 deltaT = -delta;
  688.                 delta = -delta;
  689.             else
  690.                 deltaB = delta;
  691.             end
  692.            
  693.             --Number shift
  694.             l, t, _, b, r = digitFrame.number:GetTexCoord();
  695.             digitFrame.number:SetTexCoord(l,r,t+delta,b+delta);
  696.            
  697.             --Mask Shift
  698.             l, t, _, b, r = digitFrame.numberMask:GetTexCoord();
  699.             digitFrame.numberMask:SetTexCoord(l,r,t+deltaT,b+deltaB);
  700.             digitFrame.numberMask:SetHeight(COUNTERBAR_NUMBER_HEIGHT * (1.0 - digitFrame.animTime/COUNTERBAR_CHANGE_TIME));
  701.            
  702.             digitFrame.animTime = digitFrame.animTime - elapsed
  703.             updatingNumbers = true;
  704.         end
  705.     end
  706.    
  707.     if not updatingNumbers then
  708.         self:SetScript("OnUpdate", self.lastOnUpdate); -- nil or PlayerBuffTimer_OnUpdate for timers
  709.         CounterBar_SetNumbers(self)
  710.     end
  711. end
  712.  
  713.  
  714.  
  715.  
  716. ---------------------------------
  717. -------- Buff Timer Code --------
  718. ---------------------------------
  719. local PlayerBuffTimers = {}
  720. local numBuffTimers = 0;
  721.  
  722.  
  723. function PlayerBuffTimerManager_OnLoad(self)
  724.     self:RegisterEvent("UNIT_POWER_BAR_TIMER_UPDATE");
  725.     self:RegisterEvent("PLAYER_ENTERING_WORLD");
  726.     self.unit = "player"
  727. end
  728.  
  729.  
  730. function PlayerBuffTimerManager_OnEvent(self, event, ...)
  731.     local arg1 = ...;
  732.     if ( arg1 == self.unit and event == "UNIT_POWER_BAR_TIMER_UPDATE" ) then
  733.         PlayerBuffTimerManager_UpdateTimers(self);
  734.     elseif ( event == "PLAYER_ENTERING_WORLD" ) then
  735.         PlayerBuffTimerManager_UpdateTimers(self);
  736.     end
  737. end
  738.  
  739.  
  740. function PlayerBuffTimerManager_GetTimer(barType)
  741.     local timerFrame;
  742.     local isCounter = barType == ALT_POWER_TYPE_COUNTER;
  743.    
  744.     for i=1,numBuffTimers do
  745.         local frame = _G["BuffTimer"..i];
  746.         if ( frame and not frame:IsShown() and frame.isCounter == isCounter ) then
  747.             timerFrame = frame;
  748.             break;
  749.         end
  750.     end
  751.    
  752.     if ( not timerFrame ) then
  753.         numBuffTimers = numBuffTimers + 1;
  754.         if ( isCounter ) then
  755.             timerFrame = CreateFrame("Frame", "BuffTimer"..numBuffTimers, UIParent, "UnitPowerBarAltCounterTemplate");
  756.             timerFrame.isCounter = true;
  757.            
  758.         else
  759.             timerFrame = CreateFrame("Frame", "BuffTimer"..numBuffTimers, UIParent, "UnitPowerBarAltTemplate");
  760.             timerFrame.isCounter = false;
  761.             timerFrame.scale = 1.0;
  762.         end
  763.         timerFrame.unit = "player"
  764.         timerFrame:SetScript("OnEvent", nil);
  765.     end
  766.    
  767.     timerFrame:SetScript("OnUpdate", PlayerBuffTimer_OnUpdate);
  768.     return timerFrame;
  769. end
  770.  
  771.  
  772. function PlayerBuffTimer_OnUpdate(self, elapsed)
  773.     local timeLeft = self.timerExpiration - GetTime();
  774.     if ( timeLeft <= 0 ) then
  775.         self:SetScript("OnUpdate", nil);
  776.         timeLeft = 0;
  777.     end
  778.    
  779.     if ( self.isCounter ) then
  780.         CounterBar_UpdateCount(self, floor(timeLeft));
  781.     else
  782.         UnitPowerBarAlt_SetPower(self, timeLeft, true);
  783.     end
  784. end
  785.  
  786.  
  787. function PlayerBuffTimerManager_UpdateTimers(self)
  788.     for _, timer in pairs(PlayerBuffTimers) do
  789.         timer.flagForHide = true;
  790.     end
  791.  
  792.     local index = 1;
  793.     local anchorFrame = PlayerPowerBarAlt;
  794.     local duration, expiration, barID, auraID = UnitPowerBarTimerInfo("player", index);
  795.     while ( barID ) do
  796.         if ( not PlayerBuffTimers[auraID] ) then -- this timer is new. add it
  797.             local barType = GetAlternatePowerInfoByID(barID);
  798.             local timer = PlayerBuffTimerManager_GetTimer(barType);
  799.             timer.timerIndex = index;
  800.             if ( timer.isCounter ) then
  801.                 CounterBar_SetStyle(timer, useFactional, animNumbers, duration);
  802.             else
  803.                 UnitPowerBarAlt_TearDown(timer);
  804.                 UnitPowerBarAlt_SetUp(timer, barID);
  805.                 UnitPowerBarAlt_SetMinMaxPower(timer, 0, duration);
  806.                 UnitPowerBarAlt_SetPower(timer, duration, true);
  807.             end
  808.            
  809.             timer.timerExpiration = expiration;
  810.             timer:Show();
  811.             PlayerBuffTimers[auraID] = timer;
  812.         end
  813.  
  814.        
  815.         PlayerBuffTimers[auraID]:SetPoint("BOTTOM", anchorFrame, "TOP", 0, 32);
  816.         anchorFrame = PlayerBuffTimers[auraID];
  817.         PlayerBuffTimers[auraID].flagForHide = false;
  818.         PlayerBuffTimers[auraID].timerIndex = index;
  819.         index = index + 1;
  820.         duration, expiration, barID, auraID = UnitPowerBarTimerInfo("player", index);
  821.     end
  822.    
  823.     for auraID, timer in pairs(PlayerBuffTimers) do
  824.         if ( timer.flagForHide ) then
  825.             PlayerBuffTimers[auraID] = nil;
  826.             if ( not timer.isCounter ) then
  827.                 UnitPowerBarAlt_TearDown(timer);
  828.             end
  829.             timer:Hide();
  830.         end
  831.     end
  832. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement