PageRenderTime 26ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/Tukui/modules/actionbars/Cooldowns.lua

http://github.com/Asphyxia/Tukui
Lua | 154 lines | 110 code | 17 blank | 27 comment | 14 complexity | 07f01ef5ec72a2b4e9fc2e420b22bcc6 MD5 | raw file
  1. local T, C, L = unpack(select(2, ...)) -- Import: T - functions, constants, variables; C - config; L - locales
  2. --[[
  3. An edited lightweight OmniCC for Tukui
  4. A featureless, 'pure' version of OmniCC.
  5. This version should work on absolutely everything, but I've removed pretty much all of the options
  6. --]]
  7. if IsAddOnLoaded("OmniCC") or IsAddOnLoaded("ncCooldown") or C["cooldown"].enable ~= true then return end
  8. --constants!
  9. OmniCC = true --hack to work around detection from other addons for OmniCC
  10. local ICON_SIZE = 36 --the normal size for an icon (don't change this)
  11. local DAY, HOUR, MINUTE = 86400, 3600, 60 --used for formatting text
  12. local DAYISH, HOURISH, MINUTEISH = 3600 * 23.5, 60 * 59.5, 59.5 --used for formatting text at transition points
  13. local HALFDAYISH, HALFHOURISH, HALFMINUTEISH = DAY/2 + 0.5, HOUR/2 + 0.5, MINUTE/2 + 0.5 --used for calculating next update times
  14. --configuration settings
  15. local MIN_SCALE = 0.5 --the minimum scale we want to show cooldown counts at, anything below this will be hidden
  16. local MIN_DURATION = 2.5 --the minimum duration to show cooldown text for
  17. local EXPIRING_DURATION = C["cooldown"].treshold --the minimum number of seconds a cooldown must be to use to display in the expiring format
  18. local EXPIRING_FORMAT = T.RGBToHex(1, 0, 0)..'%.1f|r' --format for timers that are soon to expire
  19. local SECONDS_FORMAT = T.RGBToHex(1, 1, 0)..'%d|r' --format for timers that have seconds remaining
  20. local MINUTES_FORMAT = T.RGBToHex(1, 1, 1)..'%dm|r' --format for timers that have minutes remaining
  21. local HOURS_FORMAT = T.RGBToHex(0.4, 1, 1)..'%dh|r' --format for timers that have hours remaining
  22. local DAYS_FORMAT = T.RGBToHex(0.4, 0.4, 1)..'%dh|r' --format for timers that have days remaining
  23. --local bindings!
  24. local floor = math.floor
  25. local min = math.min
  26. local GetTime = GetTime
  27. --returns both what text to display, and how long until the next update
  28. local function getTimeText(s)
  29. --format text as seconds when below a minute
  30. if s < MINUTEISH then
  31. local seconds = tonumber(T.Round(s))
  32. if seconds > EXPIRING_DURATION then
  33. return SECONDS_FORMAT, seconds, s - (seconds - 0.51)
  34. else
  35. return EXPIRING_FORMAT, s, 0.051
  36. end
  37. --format text as minutes when below an hour
  38. elseif s < HOURISH then
  39. local minutes = tonumber(T.Round(s/MINUTE))
  40. return MINUTES_FORMAT, minutes, minutes > 1 and (s - (minutes*MINUTE - HALFMINUTEISH)) or (s - MINUTEISH)
  41. --format text as hours when below a day
  42. elseif s < DAYISH then
  43. local hours = tonumber(T.Round(s/HOUR))
  44. return HOURS_FORMAT, hours, hours > 1 and (s - (hours*HOUR - HALFHOURISH)) or (s - HOURISH)
  45. --format text as days
  46. else
  47. local days = tonumber(T.Round(s/DAY))
  48. return DAYS_FORMAT, days, days > 1 and (s - (days*DAY - HALFDAYISH)) or (s - DAYISH)
  49. end
  50. end
  51. --stops the timer
  52. local function Timer_Stop(self)
  53. self.enabled = nil
  54. self:Hide()
  55. end
  56. --forces the given timer to update on the next frame
  57. local function Timer_ForceUpdate(self)
  58. self.nextUpdate = 0
  59. self:Show()
  60. end
  61. --adjust font size whenever the timer's parent size changes
  62. --hide if it gets too tiny
  63. local function Timer_OnSizeChanged(self, width, height)
  64. local fontScale = T.Round(width) / ICON_SIZE
  65. if fontScale == self.fontScale then
  66. return
  67. end
  68. self.fontScale = fontScale
  69. if fontScale < MIN_SCALE then
  70. self:Hide()
  71. else
  72. self.text:SetFont(C.media.pixelfont, C["datatext"].fontsize, "MONOCHROMEOUTLINE")
  73. if self.enabled then
  74. Timer_ForceUpdate(self)
  75. end
  76. end
  77. end
  78. --update timer text, if it needs to be
  79. --hide the timer if done
  80. local function Timer_OnUpdate(self, elapsed)
  81. if self.nextUpdate > 0 then
  82. self.nextUpdate = self.nextUpdate - elapsed
  83. else
  84. local remain = self.duration - (GetTime() - self.start)
  85. if tonumber(T.Round(remain)) > 0 then
  86. if (self.fontScale * self:GetEffectiveScale() / UIParent:GetScale()) < MIN_SCALE then
  87. self.text:SetText('')
  88. self.nextUpdate = 1
  89. else
  90. local formatStr, time, nextUpdate = getTimeText(remain)
  91. self.text:SetFormattedText(formatStr, time)
  92. self.nextUpdate = nextUpdate
  93. end
  94. else
  95. Timer_Stop(self)
  96. end
  97. end
  98. end
  99. --returns a new timer object
  100. local function Timer_Create(self)
  101. --a frame to watch for OnSizeChanged events
  102. --needed since OnSizeChanged has funny triggering if the frame with the handler is not shown
  103. local scaler = CreateFrame('Frame', nil, self)
  104. scaler:SetAllPoints(self)
  105. local timer = CreateFrame('Frame', nil, scaler); timer:Hide()
  106. timer:SetAllPoints(scaler)
  107. timer:SetScript('OnUpdate', Timer_OnUpdate)
  108. local text = timer:CreateFontString(nil, 'OVERLAY')
  109. text:Point("CENTER", 2, 0)
  110. text:SetJustifyH("CENTER")
  111. timer.text = text
  112. Timer_OnSizeChanged(timer, scaler:GetSize())
  113. scaler:SetScript('OnSizeChanged', function(self, ...) Timer_OnSizeChanged(timer, ...) end)
  114. self.timer = timer
  115. return timer
  116. end
  117. --hook the SetCooldown method of all cooldown frames
  118. --ActionButton1Cooldown is used here since its likely to always exist
  119. --and I'd rather not create my own cooldown frame to preserve a tiny bit of memory
  120. hooksecurefunc(getmetatable(ActionButton1Cooldown).__index, 'SetCooldown', function(self, start, duration)
  121. if self.noOCC then return end
  122. --start timer
  123. if start > 0 and duration > MIN_DURATION then
  124. local timer = self.timer or Timer_Create(self)
  125. timer.start = start
  126. timer.duration = duration
  127. timer.enabled = true
  128. timer.nextUpdate = 0
  129. if timer.fontScale >= MIN_SCALE then timer:Show() end
  130. --stop timer
  131. else
  132. local timer = self.timer
  133. if timer then
  134. Timer_Stop(timer)
  135. end
  136. end
  137. end)