Quantcast
--[[-----------------------------------------------------------------------
	oUF: Stardust - a layout for the oUF framework
	Copyright (c) 2016 Andrew Mordecai <armordecai@protonmail.ch>
	This code is released under the zlib license; see LICENSE for details.
---------------------------------------------------------------------------
	This file provides a "DispelIcon" element, which may be used in any
	oUF layout and does not depend on any other files in this addon.

	self.DispelIcon = self:CreateTexture(nil, "OVERLAY")
	self.DispelIcon:SetPoint("CENTER")
	self.DispelIcon:SetSize(16, 16)

	To use Blizzard's graphical icons for each debuff type, set
	.showIcons = true on the element. Otherwise, the element's existing
	texture will be colored according to the debuff type.
-------------------------------------------------------------------------]]

local _, ns = ...
local oUF = ns.oUF or oUF
assert(oUF, "DispelIcon element requires oUF")

function Update(self, event, unit)
	local element = self.DispelIcon
	if element.PreUpdate then
		element:PreUpdate()
	end

	local _, _, _, _, dispelType = UnitDebuff(unit, 1, "RAID")
	if dispelType == "" then dispelType = nil end

	local color, r, g, b = dispelType and DebuffTypeColor[dispelType]
	if element.showIcons then
		if dispelType then
			element:SetTexture("Interface\\RAIDFRAME\\Raid-Icon-Debuff"..dispelType)
			element:SetVertexColor(1, 1, 1)
			element:Show()
		else
			element:SetTexture("")
			element:Hide()
		end
	else
		if dispelType then
			r, g, b = color.r, color.g, color.b
			element:SetVertexColor(r, g, b)
			element:Show()
		else
			element:SetVertexColor(1, 1, 1)
			element:Hide()
		end
	end

	if element.PostUpdate then
		element:PostUpdate(dispelType, r, g, b)
	end
end

function Path(self, ...)
	return (self.DispelIcon.Override or Update)(self, ...)
end

function ForceUpdate(element)
	return Path(element.__owner, "ForceUpdate", element.__owner.unit)
end

function Enable(self, unit)
	local element = self.DispelIcon
	if not element then return end

	element.__owner = self
	element.ForceUpdate = ForceUpdate

	self:RegisterEvent("UNIT_AURA", Path)

	return true
end

function Disable(self)
	local element = self.DispelIcon
	if not element then return end

	self:UnregisterEvent("UNIT_AURA", Path)

	element:Hide()
end

oUF:AddElement("DispelIcon", Path, Enable, Disable)