View Single Post
05-08-17, 10:32 AM   #4
Aftermathhqt
A Molten Giant
 
Aftermathhqt's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2009
Posts: 784
If anyone wanna use

Lua Code:
  1. local A, C, M, L = select(2, ...):unpack()
  2.  
  3. A.FormatTime = function(Seconds)
  4.     local Day, Hour, Minutes = 0, 0, 0
  5.    
  6.     if Seconds >= 86400 then
  7.         Day = Seconds / 86400
  8.         Seconds = Seconds % 86400
  9.     end
  10.  
  11.     if Seconds >= 3600 then
  12.         Hour = Seconds / 3600
  13.         Seconds = Seconds % 3600
  14.     end
  15.    
  16.     if Seconds >= 60 then
  17.         Minutes = Seconds / 60
  18.         Seconds = Seconds % 60
  19.     end
  20.    
  21.     if Day > 0 then
  22.         return format("%.2d:%.2d", Day, Hour)
  23.     elseif Hour > 0 then
  24.         return format("%.2d:%.2d", Hour, Minutes)
  25.     elseif Minutes > 0 then
  26.         return format("%.1d:%.2d", Minutes, Seconds)
  27.     else
  28.         if Seconds < 10 then
  29.             return format("%.1f", Seconds)
  30.         else
  31.             return format("%d", Seconds)
  32.         end
  33.     end
  34. end
  35.  
  36. local ICON_SIZE = 36
  37. local MIN_SCALE = 0.5
  38. local MIN_DURATION = 1.5
  39.  
  40. local floor = math.floor
  41. local GetTime = GetTime
  42.  
  43. local function TimerStop(self)
  44.     self.Enabled = nil
  45.     self:Hide()
  46. end
  47.  
  48. local function TimerForceUpdate(self)
  49.     self.NextUpdate = 0
  50.     self:Show()
  51. end
  52.  
  53. local function TimerOnSizeChanged(self, Width)
  54.     local FontScale = floor(Width +.5) / ICON_SIZE
  55.     if FontScale == self.FontScale then
  56.         return
  57.     end
  58.    
  59.     self.FontScale = FontScale
  60.  
  61.     if FontScale < MIN_SCALE then
  62.         self:Hide()
  63.     else
  64.         self.Text:SetFontTemplate(C.Media.Font2, FontScale * C.Cooldowns.FontSize)
  65.  
  66.         if self.Enabled then
  67.             TimerForceUpdate(self)
  68.         end
  69.     end
  70. end
  71.  
  72. local function TimerOnUpdate(self, Elapsed)
  73.     if (self.NextUpdate > 0) then
  74.         self.NextUpdate = self.NextUpdate - Elapsed
  75.         return
  76.     end
  77.    
  78.     local TimeLeft = self.Duration - (GetTime() - self.Start)
  79.  
  80.     if (TimeLeft > 0.05) then
  81.         if (self.FontScale * self:GetEffectiveScale() / UIParent:GetScale()) < MIN_SCALE then
  82.             self.Text:SetText("")
  83.             self.NextUpdate = 500
  84.         else
  85.             local TimeText = A.FormatTime(TimeLeft)
  86.             self.Text:SetText(TimeText)
  87.         end
  88.    
  89.         if (TimeLeft <= 10) then
  90.             self.Text:SetTextColor(unpack(C.Cooldowns.ExpireColor))
  91.         elseif (TimeLeft <= 30) then
  92.             self.Text:SetTextColor(unpack(C.Cooldowns.SecondsColor))
  93.         elseif (TimeLeft <= 60) then
  94.             self.Text:SetTextColor(unpack(C.Cooldowns.SecondsColor2))
  95.         else
  96.             self.Text:SetTextColor(unpack(C.Cooldowns.NormalColor))
  97.         end
  98.     else
  99.         TimerStop(self)
  100.     end
  101. end
  102.  
  103. local function Timer_Create(self)
  104.     local Scaler = CreateFrame("Frame", nil, self)
  105.     Scaler:SetAllPoints(self)
  106.  
  107.     local Timer = CreateFrame("Frame", nil, Scaler)
  108.     Timer:Hide()
  109.     Timer:SetAllPoints(Scaler)
  110.     Timer:SetFrameStrata("HIGH")
  111.     Timer:SetScript("OnUpdate", TimerOnUpdate)
  112.  
  113.     local Text = Timer:CreateFontString(nil, "OVERLAY")
  114.     Text:Point("CENTER", Timer, 1, 0)
  115.     Timer.Text = Text
  116.  
  117.     TimerOnSizeChanged(Timer, Scaler:GetSize())
  118.     Scaler:SetScript("OnSizeChanged", function(self, ...)
  119.         TimerOnSizeChanged(Timer, ...)
  120.     end)
  121.  
  122.     self.Timer = Timer
  123.     return Timer
  124. end
  125.  
  126. local function TimerStart(self, Start, Duration)
  127.     if (self.noOCC) then
  128.         return
  129.     end
  130.    
  131.     if (Start > 0 and Duration > MIN_DURATION) then
  132.         local Timer = self.Timer or Timer_Create(self)
  133.         Timer.Start = Start
  134.         Timer.Duration = Duration
  135.         Timer.Enabled = true
  136.         Timer.NextUpdate = 0
  137.        
  138.         if Timer.FontScale >= MIN_SCALE then
  139.             Timer:Show()
  140.         end
  141.     else
  142.         local Timer = self.Timer
  143.         if (Timer) then
  144.             TimerStop(Timer)
  145.         end
  146.     end
  147. end
  148. hooksecurefunc(getmetatable(ActionButton1Cooldown).__index, "SetCooldown", TimerStart)
  149.  
  150. local Active = {}
  151.  
  152. local function CooldownOnShow(self)
  153.     Active[self] = true
  154. end
  155.  
  156. local function CooldownOnHide(self)
  157.     Active[self] = nil
  158. end
  159.  
  160. local function CooldownShouldUpdateTimer(self, Start, Duration)
  161.     local Timer = self.Timer
  162.     if not (Timer) then return true end
  163.     return Timer.Start ~= Start
  164. end
  165.  
  166. local function CooldownUpdate(self)
  167.     local Button = self:GetParent()
  168.     local Start, Duration, Enable = GetActionCooldown(Button.action)
  169.  
  170.     if CooldownShouldUpdateTimer(self, Start, Duration) then
  171.         TimerStart(self, Start, Duration)
  172.     end
  173. end
  174.  
  175. local EventWatcher = CreateFrame("Frame")
  176. EventWatcher:RegisterEvent("ACTIONBAR_UPDATE_COOLDOWN")
  177. EventWatcher:SetScript("OnEvent", function(self, event)
  178.     for Cooldown in pairs(Active) do
  179.         CooldownUpdate(Cooldown)
  180.     end
  181. end)
  182.  
  183. local function ActionButtonRegister(frame)
  184.     local Cooldown = frame.cooldown
  185.  
  186.     if not (Cooldown.IsHooked) then
  187.         Cooldown:HookScript("OnShow", CooldownOnShow)
  188.         Cooldown:HookScript("OnHide", CooldownOnHide)
  189.         Cooldown.IsHooked = true
  190.     end
  191. end
  192.  
  193. if _G["ActionBarButtonEventsFrame"].frames then
  194.     for i, frame in pairs(_G["ActionBarButtonEventsFrame"].frames) do
  195.         ActionButtonRegister(frame)
  196.     end
  197. end
  198. hooksecurefunc("ActionBarButtonEventsFrame_RegisterFrame", ActionButtonRegister)
  Reply With Quote