PageRenderTime 123ms CodeModel.GetById 81ms app.highlight 6ms RepoModel.GetById 34ms app.codeStats 0ms

/Tukui/modules/unitframes/core/oUF/elements/health.lua

http://github.com/Asphyxia/Tukui
Lua | 112 lines | 85 code | 23 blank | 4 comment | 14 complexity | f7d64d577909789e28b6d9a7f766ae09 MD5 | raw file
  1local WoW41 = select(4, GetBuildInfo()) == 40100
  2
  3local parent, ns = ...
  4local oUF = ns.oUF
  5
  6oUF.colors.health = {49/255, 207/255, 37/255}
  7
  8local Update = function(self, event, unit, powerType)
  9	if(self.unit ~= unit or (event == 'UNIT_POWER' and powerType ~= 'HAPPINESS')) then return end
 10	local health = self.Health
 11
 12	if(health.PreUpdate) then health:PreUpdate(unit) end
 13
 14	local min, max = UnitHealth(unit), UnitHealthMax(unit)
 15	local disconnected = not UnitIsConnected(unit)
 16	health:SetMinMaxValues(0, max)
 17
 18	if(disconnected) then
 19		health:SetValue(max)
 20	else
 21		health:SetValue(min)
 22	end
 23
 24	health.disconnected = disconnected
 25
 26	local r, g, b, t
 27	if(health.colorTapping and UnitIsTapped(unit) and not UnitIsTappedByPlayer(unit)) then
 28		t = self.colors.tapped
 29	elseif(health.colorDisconnected and not UnitIsConnected(unit)) then
 30		t = self.colors.disconnected
 31	elseif(health.colorClass and UnitIsPlayer(unit)) or
 32		(health.colorClassNPC and not UnitIsPlayer(unit)) or
 33		(health.colorClassPet and UnitPlayerControlled(unit) and not UnitIsPlayer(unit)) then
 34		local _, class = UnitClass(unit)
 35		t = self.colors.class[class]
 36	elseif(health.colorReaction and UnitReaction(unit, 'player')) then
 37		t = self.colors.reaction[UnitReaction(unit, "player")]
 38	elseif(health.colorSmooth) then
 39		r, g, b = self.ColorGradient(min / max, unpack(health.smoothGradient or self.colors.smooth))
 40	elseif(health.colorHealth) then
 41		t = self.colors.health
 42	end
 43
 44	if(t) then
 45		r, g, b = t[1], t[2], t[3]
 46	end
 47
 48	if(b) then
 49		health:SetStatusBarColor(r, g, b)
 50
 51		local bg = health.bg
 52		if(bg) then local mu = bg.multiplier or 1
 53			bg:SetVertexColor(r * mu, g * mu, b * mu)
 54		end
 55	end
 56
 57	if(health.PostUpdate) then
 58		return health:PostUpdate(unit, min, max)
 59	end
 60end
 61
 62local Path = function(self, ...)
 63	return (self.Health.Override or Update) (self, ...)
 64end
 65
 66local ForceUpdate = function(element)
 67	return Path(element.__owner, 'ForceUpdate', element.__owner.unit)
 68end
 69
 70local Enable = function(self, unit)
 71	local health = self.Health
 72	if(health) then
 73		health.__owner = self
 74		health.ForceUpdate = ForceUpdate
 75
 76		if(health.frequentUpdates and not self:GetScript'OnUpdate') then
 77			self:RegisterEvent('UNIT_HEALTH_FREQUENT', Path)
 78		end
 79
 80		-- XXX: 4.0.6: They overlap, but they don't! So we'll have to eat some double
 81		-- updates. This will probably cost us less than actually running an OnUpdate
 82		-- again.
 83		self:RegisterEvent('UNIT_HEALTH', Path)
 84		self:RegisterEvent("UNIT_MAXHEALTH", Path)
 85		self:RegisterEvent('UNIT_CONNECTION', Path)
 86		self:RegisterEvent('UNIT_POWER', Path)
 87
 88		-- For tapping.
 89		self:RegisterEvent('UNIT_FACTION', Path)
 90
 91		if(not health:GetStatusBarTexture()) then
 92			health:SetStatusBarTexture[[Interface\TargetingFrame\UI-StatusBar]]
 93		end
 94
 95		return true
 96	end
 97end
 98
 99local Disable = function(self)
100	local health = self.Health
101	if(health) then
102		self:UnregisterEvent('UNIT_HEALTH_FREQUENT', Path)
103		self:UnregisterEvent('UNIT_HEALTH', Path)
104		self:UnregisterEvent('UNIT_MAXHEALTH', Path)
105		self:UnregisterEvent('UNIT_CONNECTION', Path)
106		self:UnregisterEvent('UNIT_POWER', Path)
107
108		self:UnregisterEvent('UNIT_FACTION', Path)
109	end
110end
111
112oUF:AddElement('Health', Path, Enable, Disable)