PageRenderTime 33ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Tukui/modules/unitframes/plugins/oUF_CombatFeedback/oUF_CombatFeedback.lua

http://github.com/Asphyxia/Tukui
Lua | 157 lines | 144 code | 9 blank | 4 comment | 31 complexity | 9290b18001d9300f46835fdb1d414ebd MD5 | raw file
  1. local T, C, L = unpack(select(2, ...)) -- Import: T - functions, constants, variables; C - config; L - locales
  2. if C.unitframes.enable ~= true or C.unitframes.combatfeedback ~= true then return end
  3. local _, ns = ...
  4. local oUF = ns.oUF or oUF
  5. if not oUF then return end
  6. local damage_format = "-%d"
  7. local heal_format = "+%d"
  8. local maxAlpha = 0.6
  9. local updateFrame
  10. local feedback = {}
  11. local originalHeight = {}
  12. local color
  13. local colors = {
  14. STANDARD = { 1, 1, 1 }, -- color for everything not in the list below
  15. -- damage colors
  16. IMMUNE = { 1, 1, 1 },
  17. DAMAGE = { 1, 0, 0 },
  18. CRUSHING = { 1, 0, 0 },
  19. CRITICAL = { 1, 0, 0 },
  20. GLANCING = { 1, 0, 0 },
  21. ABSORB = { 1, 1, 1 },
  22. BLOCK = { 1, 1, 1 },
  23. RESIST = { 1, 1, 1 },
  24. MISS = { 1, 1, 1 },
  25. -- heal colors
  26. HEAL = { 0, 1, 0 },
  27. CRITHEAL = { 0, 1, 0 },
  28. -- energize colors
  29. ENERGIZE = { 0.41, 0.8, 0.94 },
  30. CRITENERGIZE = { 0.41, 0.8, 0.94 },
  31. }
  32. local function createUpdateFrame()
  33. if updateFrame then return end
  34. updateFrame = CreateFrame("Frame")
  35. updateFrame:Hide()
  36. updateFrame:SetScript("OnUpdate", function()
  37. if next(feedback) == nil then
  38. updateFrame:Hide()
  39. return
  40. end
  41. for object, startTime in pairs(feedback) do
  42. local maxalpha = object.CombatFeedbackText.maxAlpha
  43. local elapsedTime = GetTime() - startTime
  44. if ( elapsedTime < COMBATFEEDBACK_FADEINTIME ) then
  45. local alpha = maxalpha*(elapsedTime / COMBATFEEDBACK_FADEINTIME)
  46. object.CombatFeedbackText:SetAlpha(alpha)
  47. elseif ( elapsedTime < (COMBATFEEDBACK_FADEINTIME + COMBATFEEDBACK_HOLDTIME) ) then
  48. object.CombatFeedbackText:SetAlpha(maxalpha)
  49. elseif ( elapsedTime < (COMBATFEEDBACK_FADEINTIME + COMBATFEEDBACK_HOLDTIME + COMBATFEEDBACK_FADEOUTTIME) ) then
  50. local alpha = maxalpha - maxalpha*((elapsedTime - COMBATFEEDBACK_HOLDTIME - COMBATFEEDBACK_FADEINTIME) / COMBATFEEDBACK_FADEOUTTIME)
  51. object.CombatFeedbackText:SetAlpha(alpha)
  52. else
  53. object.CombatFeedbackText:Hide()
  54. feedback[object] = nil
  55. end
  56. end
  57. end)
  58. end
  59. local function combat(self, event, unit, eventType, flags, amount, dtype)
  60. if unit ~= self.unit then return end
  61. if unit == "vehicle" then return end
  62. local FeedbackText = self.CombatFeedbackText
  63. local fColors = FeedbackText.colors
  64. local font, fontHeight, fontFlags = FeedbackText:GetFont()
  65. fontHeight = FeedbackText.origHeight -- always start at original height
  66. local text, arg
  67. color = fColors and fColors.STANDARD or colors.STANDARD
  68. if eventType == "IMMUNE" and not FeedbackText.ignoreImmune then
  69. color = fColors and fColors.IMMUNE or colors.IMMUNE
  70. fontHeight = fontHeight * 0.75
  71. text = CombatFeedbackText[eventType]
  72. elseif eventType == "WOUND" and not FeedbackText.ignoreDamage then
  73. if amount ~= 0 then
  74. if flags == "CRITICAL" then
  75. color = fColors and fColors.CRITICAL or colors.CRITICAL
  76. fontHeight = fontHeight * 1.5
  77. elseif flags == "CRUSHING" then
  78. color = fColors and fColors.CRUSING or colors.CRUSHING
  79. fontHeight = fontHeight * 1.5
  80. elseif flags == "GLANCING" then
  81. color = fColors and fColors.GLANCING or colors.GLANCING
  82. fontHeight = fontHeight * 0.75
  83. else
  84. color = fColors and fColors.DAMAGE or colors.DAMAGE
  85. end
  86. text = damage_format
  87. arg = amount
  88. elseif flags == "ABSORB" then
  89. color = fColors and fColors.ABSORB or colors.ABSORB
  90. fontHeight = fontHeight * 0.75
  91. text = CombatFeedbackText["ABSORB"]
  92. elseif flags == "BLOCK" then
  93. color = fColors and fColors.BLOCK or colors.BLOCK
  94. fontHeight = fontHeight * 0.75
  95. text = CombatFeedbackText["BLOCK"]
  96. elseif flags == "RESIST" then
  97. color = fColors and fColors.RESIST or colors.RESIST
  98. fontHeight = fontHeight * 0.75
  99. text = CombatFeedbackText["RESIST"]
  100. else
  101. color = fColors and fColors.MISS or colors.MISS
  102. text = CombatFeedbackText["MISS"]
  103. end
  104. elseif eventType == "BLOCK" and not FeedbackText.ignoreDamage then
  105. color = fColors and fColors.BLOCK or colors.BLOCK
  106. fontHeight = fontHeight * 0.75
  107. text = CombatFeedbackText[eventType]
  108. elseif eventType == "HEAL" and not FeedbackText.ignoreHeal then
  109. text = heal_format
  110. arg = amount
  111. if flags == "CRITICAL" then
  112. color = fColors and fColors.CRITHEAL or colors.CRITHEAL
  113. fontHeight = fontHeight * 1.3
  114. else
  115. color = fColors and fColors.HEAL or colors.HEAL
  116. end
  117. elseif event == "ENERGIZE" and not FeedbackText.ignoreEnergize then
  118. text = amount
  119. if flags == "CRITICAL" then
  120. color = fColors and fColors.ENERGIZE or colors.ENERGIZE
  121. fontHeight = fontHeight * 1.3
  122. else
  123. color = fColors and fColors.CRITENERGIZE or colors.CRITENERGIZE
  124. end
  125. elseif not FeedbackText.ignoreOther then
  126. text = CombatFeedbackText[eventType]
  127. end
  128. if text then
  129. FeedbackText:SetFont(font,fontHeight,fontFlags)
  130. FeedbackText:SetFormattedText(text, arg)
  131. FeedbackText:SetTextColor(unpack(color))
  132. FeedbackText:SetAlpha(0)
  133. FeedbackText:Show()
  134. feedback[self] = GetTime()
  135. updateFrame:Show() -- start our onupdate
  136. end
  137. end
  138. local function addCombat(object)
  139. if not object.CombatFeedbackText then return end
  140. -- store the original starting height
  141. local font, fontHeight, fontFlags = object.CombatFeedbackText:GetFont()
  142. object.CombatFeedbackText.origHeight = fontHeight
  143. object.CombatFeedbackText.maxAlpha = object.CombatFeedbackText.maxAlpha or maxAlpha
  144. createUpdateFrame()
  145. object:RegisterEvent("UNIT_COMBAT", combat)
  146. end
  147. for k, object in ipairs(oUF.objects) do addCombat(object) end
  148. oUF:RegisterInitCallback(addCombat)