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