PageRenderTime 62ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Grid/GridCooldownText/GridCooldownText.lua

https://code.google.com/p/strongcwowinterface/
Lua | 172 lines | 136 code | 15 blank | 21 comment | 14 complexity | fa6fd6c48d47e10c579a39844d7e9f07 MD5 | raw file
Possible License(s): Unlicense, LGPL-3.0, LGPL-2.1, BSD-3-Clause, GPL-2.0
  1. --------------------------------------------------------------------------------------------
  2. -- GridCooldownText module setup
  3. --------------------------------------------------------------------------------------------
  4. local L = AceLibrary("AceLocale-2.2"):new("GridCooldownText")
  5. local AceOO = AceLibrary("AceOO-2.0")
  6. local media = LibStub("LibSharedMedia-3.0", true)
  7. GridCooldownText = GridStatus:NewModule("GridCooldownText")
  8. GridCooldownText.menuName = L["Cooldown Text"]
  9. GridCooldownText.options = false
  10. GridCooldownText.defaultDB = {
  11. FontSize = 10,
  12. Font = "Arial Narrow",
  13. }
  14. function GridCooldownText:OnEnable()
  15. local opt = GridFrame.options.args.advanced.args
  16. opt["cooldowntext"] = {
  17. type = "group",
  18. name = L["Cooldown Text"],
  19. desc = L["Cooldown Text options."],
  20. --order = 110,
  21. args = {
  22. ["fontsize"] = {
  23. type = "range",
  24. name = L["Cooldown Text Font Size"],
  25. desc = L["Adjust the font size for Cooldown Text."],
  26. --order = 111,
  27. min = 6,
  28. max = 24,
  29. step = 1,
  30. get = function ()
  31. return GridCooldownText.db.profile.FontSize
  32. end,
  33. set = function (v)
  34. GridCooldownText.db.profile.FontSize = v
  35. local font = media and media:Fetch('font', GridCooldownText.db.profile.Font) or STANDARD_TEXT_FONT
  36. GridFrame:WithAllFrames(function (f) f.frame.IconCDText:SetFont(font, v, "OUTLINE") end)
  37. end,
  38. }
  39. }
  40. }
  41. if media then
  42. opt["cooldowntext"].args["font"] = {
  43. type = "text",
  44. name = L["Cooldown Text Font"],
  45. desc = L["Adjust the font setting for Cooldown Text."],
  46. -- order = 112,
  47. validate = media:List("font"),
  48. get = function ()
  49. return GridCooldownText.db.profile.Font
  50. end,
  51. set = function (v)
  52. GridCooldownText.db.profile.Font = v
  53. local font = media:Fetch("font", v)
  54. local fontsize = GridCooldownText.db.profile.FontSize
  55. GridFrame:WithAllFrames(function (f) f.frame.IconCDText:SetFont(font, fontsize, "OUTLINE") end)
  56. end,
  57. }
  58. end
  59. end
  60. --------------------------------------------------------------------------------------------
  61. -- utility functions
  62. --------------------------------------------------------------------------------------------
  63. local function getTimeText(time)
  64. local text
  65. local minutes = math.floor(time/60)
  66. local seconds = math.ceil(time % 60)
  67. if minutes > 89 then
  68. text = format("%dh", math.ceil(time/3600))
  69. elseif minutes > 0 then
  70. --Display: Xm
  71. text = format("%dm", minutes)
  72. else
  73. text = seconds
  74. end
  75. return text
  76. end
  77. --------------------------------------------------------------------------------------------
  78. -- FontString Update code
  79. --------------------------------------------------------------------------------------------
  80. local updatePeriod = 0.1
  81. local activeCooldowns = {}
  82. -- Frame to run the OnUpdate script that will update the FontStrings
  83. local onUpdateFrame = CreateFrame("Frame")
  84. local lastUpdate = 0
  85. onUpdateFrame:SetScript("OnUpdate", function() GridCooldownText_UpdateStrings(arg1) end)
  86. -- variable to store the current time, updated each update Period
  87. local currentTime = GetTime()
  88. function GridCooldownText_UpdateStrings( elapsed )
  89. lastUpdate = lastUpdate + elapsed
  90. if lastUpdate > updatePeriod then --update needed
  91. lastUpdate = 0
  92. currentTime = GetTime()
  93. local activeString = false
  94. for modelFrame, text in pairs(activeCooldowns) do
  95. local hideText = true
  96. if modelFrame:IsShown() then
  97. if text.start < currentTime then
  98. local timeLeft = text.endTime - currentTime
  99. if timeLeft > updatePeriod then
  100. text:SetText(getTimeText(timeLeft))
  101. activeString = true
  102. hideText = false
  103. end
  104. else
  105. hideText = false
  106. --text:SetText("")
  107. end
  108. end
  109. if hideText then
  110. text:Hide()
  111. activeCooldowns[modelFrame] = nil
  112. end
  113. end
  114. if not activeString then
  115. this:Hide()
  116. end
  117. end
  118. end
  119. --------------------------------------------------------------------------------------------
  120. -- f.IconCD.SetCooldown hook
  121. --------------------------------------------------------------------------------------------
  122. local GridCooldownTextFrameClass = AceOO.Class(GridFrame.frameClass)
  123. local _frameClass = nil
  124. if not _frameClass then
  125. _frameClass = GridFrame.frameClass
  126. GridFrame.frameClass = GridCooldownTextFrameClass
  127. end
  128. function GridCooldownTextFrameClass.prototype:CreateFrames()
  129. GridCooldownTextFrameClass.super.prototype.CreateFrames(self)
  130. local f = self.frame
  131. -- set media based on shared media
  132. local font = media and media:Fetch("font", GridCooldownText.db.profile.Font) or STANDARD_TEXT_FONT
  133. local size = GridCooldownText.db.profile.FontSize or 10
  134. -- create cooldown icon text
  135. f.IconCDText = f.IconCD:CreateFontString(nil, "OVERLAY")
  136. f.IconCDText:SetAllPoints(f.IconBG)
  137. f.IconCDText:SetFontObject(NumberFontNormalYellow)
  138. f.IconCDText:SetFont(font, size, "OUTLINE")
  139. f.IconCDText:SetJustifyH("CENTER")
  140. f.IconCDText:SetJustifyV("CENTER")
  141. f.IconCD.SetCooldownOld = f.IconCD.SetCooldown
  142. f.IconCD.SetCooldown = function(self, start, duration)
  143. f.IconCD.SetCooldownOld(self, start, duration)
  144. local text = f.IconCDText
  145. if start > 0 and duration >= 2 then
  146. text.start = start
  147. text.endTime = start + duration
  148. if GetTime() > start then
  149. duration = text.endTime - GetTime()
  150. end
  151. text:SetText(getTimeText(duration))
  152. text:Show()
  153. activeCooldowns[self] = text
  154. onUpdateFrame:Show()
  155. end
  156. end
  157. end