unitframes.lua 5.43 KB
Newer Older
1 2
local SLE, T, E, L, V, P, G = unpack(select(2, ...)) 
local UF = E:GetModule('UnitFrames');
Darth Predator's avatar
Darth Predator committed
3
local SUF = SLE:NewModule("UnitFrames", "AceEvent-3.0")
4 5 6
local RC = LibStub("LibRangeCheck-2.0")
--GLOBALS: hooksecurefunc, CreateFrame
local _G = _G
Darth Predator's avatar
moar  
Darth Predator committed
7
local UnitGetTotalAbsorbs = UnitGetTotalAbsorbs
8
local UnitHonorLevel = UnitHonorLevel
Darth Predator's avatar
Darth Predator committed
9
local UnitIsPVP = UnitIsPVP
10 11

function SUF:NewTags()
12 13 14
	_G["ElvUF"].Tags.Events['health:current:sl-rehok'] = 'UNIT_HEALTH_FREQUENT UNIT_MAXHEALTH UNIT_CONNECTION PLAYER_FLAGS_CHANGED'
	_G["ElvUF"].Tags.Methods['health:current:sl-rehok'] = function(unit)
		local status = UnitIsDead(unit) and L["Dead"] or UnitIsGhost(unit) and L["Ghost"] or not UnitIsConnected(unit) and L["Offline"]
Repooc's avatar
Repooc committed
15

16 17 18
		if (status) then
			return status
		else
Repooc's avatar
Repooc committed
19
			local curHealth = UnitHealth(unit)
20
			local perHealth = (UnitHealth(unit)/UnitHealthMax(unit))*100
Repooc's avatar
Repooc committed
21

Repooc's avatar
Repooc committed
22 23 24 25 26 27 28 29 30
			if curHealth >= 1e9 then
				return format("%.2fB", curHealth / 1e9) .. " | " .. format("%.0f", perHealth)
			elseif curHealth >= 1e6 then
				return format("%.2fM", curHealth / 1e6) .. " | " .. format("%.0f", perHealth)
			elseif curHealth >= 1e3 then
				return format("%.0fk", curHealth / 1e3) .. " | " .. format("%.0f", perHealth)
			else
				return format("%d", curHealth) .. " | " .. format("%.1f", perHealth)
			end
31
		end
Repooc's avatar
Repooc committed
32
	end
33

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
	_G["ElvUF"].Tags.Methods["range:sl"] = function(unit)
		local name, server = T.UnitName(unit)
		local rangeText = ''
		local min, max = RC:GetRange(unit)
		local curMin = min
		local curMax = max

		if(server and server ~= "") then
			name = T.format("%s-%s", name, server)
		end

		if min and max and (name ~= T.UnitName('player')) then
			rangeText = curMin.."-"..curMax
		end
		return rangeText
	end

	_G["ElvUF"].Tags.Events['absorbs:sl-short'] = 'UNIT_ABSORB_AMOUNT_CHANGED'
	_G["ElvUF"].Tags.Methods['absorbs:sl-short'] = function(unit)
		local absorb = UnitGetTotalAbsorbs(unit) or 0
		if absorb == 0 then
			return 0
		else
			return E:ShortValue(absorb)
		end
	end

	_G["ElvUF"].Tags.Events['absorbs:sl-full'] = 'UNIT_ABSORB_AMOUNT_CHANGED'
	_G["ElvUF"].Tags.Methods['absorbs:sl-full'] = function(unit)
		local absorb = UnitGetTotalAbsorbs(unit) or 0
		if absorb == 0 then
			return 0
		else
			return absorb
		end
	end
Darth Predator's avatar
Darth Predator committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

	_G["ElvUF"].Tags.Methods['sl:pvptimer'] = function(unit)
		if (UnitIsPVPFreeForAll(unit) or UnitIsPVP(unit)) then
			local timer = GetPVPTimer()

			if timer ~= 301000 and timer ~= -1 then
				local mins = floor((timer / 1000) / 60)
				local secs = floor((timer / 1000) - (mins * 60))
				return ("%01.f:%02.f"):format(mins, secs)
			else
				return ""
			end
		else
			return ""
		end
	end
Darth Predator's avatar
Darth Predator committed
86 87

	_G["ElvUF"].Tags.Events['sl:pvplevel'] = "HONOR_LEVEL_UPDATE UNIT_FACTION"
88
	_G["ElvUF"].Tags.Methods['sl:pvplevel'] = function(unit)
Darth Predator's avatar
Darth Predator committed
89 90
		-- if unit ~= "target" and unit ~= "player" then return "" end
		return (UnitIsPVP(unit) and UnitHonorLevel(unit) > 0) and UnitHonorLevel(unit) or ""
91
	end
92 93
end

94
local function UpdateAuraTimer(self, elapsed)
Darth Predator's avatar
Darth Predator committed
95 96 97
	local timervalue, formatid
	local unitID = self:GetParent():GetParent().unitframeType
	local auraType = self:GetParent().type
Darth Predator's avatar
Darth Predator committed
98
	if unitID and E.db.sle.unitframes.unit[unitID] and E.db.sle.unitframes.unit[unitID].auras then
Merathilis's avatar
Merathilis committed
99
		timervalue, formatid, self.nextupdate = E:GetTimeInfo(self.expirationSaved, E.db.sle.unitframes.unit[unitID].auras[auraType].threshold)
Darth Predator's avatar
Darth Predator committed
100
	else
Merathilis's avatar
Merathilis committed
101
		timervalue, formatid, self.nextupdate = E:GetTimeInfo(self.expirationSaved, 4)
Darth Predator's avatar
Darth Predator committed
102
	end
Darth Predator's avatar
Darth Predator committed
103 104 105 106 107 108 109
	local timeColors, timeThreshold = E.TimeColors, E.db.cooldown.threshold
	if E.db.unitframe.cooldown.override and E.TimeColors['unitframe'] then
		timeColors, timeThreshold = E.TimeColors['unitframe'], E.db.unitframe.cooldown.threshold
	end
	if not timeThreshold then
		timeThreshold = E.TimeThreshold
	end
Darth Predator's avatar
Darth Predator committed
110
	if self.text:GetFont() then
Darth Predator's avatar
Darth Predator committed
111
		self.text:SetFormattedText(("%s%s|r"):format(timeColors[formatid], E.TimeFormats[formatid][2]), timervalue)
Darth Predator's avatar
Darth Predator committed
112
	elseif self:GetParent():GetParent().db then
113
		self.text:FontTemplate(E.LSM:Fetch("font", E.db['unitframe'].font), self:GetParent():GetParent().db[auraType].fontSize, E.db['unitframe'].fontOutline)
Darth Predator's avatar
Darth Predator committed
114
		self.text:SetFormattedText(("%s%s|r"):format(timeColors[formatid], E.TimeFormats[formatid][2]), timervalue)
Darth Predator's avatar
Darth Predator committed
115 116 117
	end
end

118
function SUF:Initialize()
119
	if not SLE.initialized or not E.private.unitframe.enable then return end
120 121 122
	--DB convert
	if E.private.sle.unitframe.resizeHealthPrediction then E.private.sle.unitframe.resizeHealthPrediction = nil end

123
	SUF:NewTags()
124
	-- SUF:InitPlayer()
125 126 127 128 129 130 131

	--Raid stuff
	SUF.specNameToRole = {}
	for i = 1, T.GetNumClasses() do
		local _, class, classID = T.GetClassInfo(i)
		SUF.specNameToRole[class] = {}
		for j = 1, T.GetNumSpecializationsForClassID(classID) do
Darth Predator's avatar
Darth Predator committed
132
			local _, spec, _, _, role = T.GetSpecializationInfoForClassID(classID, j)
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
			SUF.specNameToRole[class][spec] = role
		end
	end

	local f = CreateFrame("Frame")
	f:RegisterEvent("PLAYER_ENTERING_WORLD")
	f:SetScript("OnEvent", function(self, event)
		self:UnregisterEvent(event)
		SUF:SetRoleIcons()
		if E.private.sle.unitframe.statusbarTextures.cast then SUF:CastBarHook() end
	end)

	--Hooking to group frames
	hooksecurefunc(UF, "Update_PartyFrames", SUF.Update_GroupFrames)
	hooksecurefunc(UF, "Update_RaidFrames", SUF.Update_GroupFrames)
	hooksecurefunc(UF, "Update_Raid40Frames", SUF.Update_GroupFrames)

Darth Predator's avatar
Darth Predator committed
150 151 152
	--Hook pvp icons
	SUF:UpgradePvPIcon()

153
	SUF:InitStatus()
154 155
	
	hooksecurefunc(UF, "UpdateAuraTimer", UpdateAuraTimer)
156 157

	function SUF:ForUpdateAll()
Darth Predator's avatar
Darth Predator committed
158
		SUF:SetRoleIcons()
159 160 161 162 163
		if E.private.sle.unitframe.statusbarTextures.power then SUF:BuildStatusTable() end
	end
end

SLE:RegisterModule(SUF:GetName())