PageRenderTime 239ms CodeModel.GetById 64ms app.highlight 55ms RepoModel.GetById 118ms app.codeStats 0ms

/Tukui/modules/unitframes/tags/tags.lua

http://github.com/Asphyxia/Tukui
Lua | 168 lines | 146 code | 19 blank | 3 comment | 22 complexity | f0736d0f699c6caea122a9e26f0e8167 MD5 | raw file
  1local ADDON_NAME, ns = ...
  2local oUF = ns.oUF or oUF
  3assert(oUF, "Tukui was unable to locate oUF install.")
  4
  5local T, C, L = unpack(select(2, ...)) -- Import: T - functions, constants, variables; C - config; L - locales
  6if not C["unitframes"].enable == true then return end
  7
  8------------------------------------------------------------------------
  9--	Tags
 10------------------------------------------------------------------------
 11
 12local function ShortenValue(value)
 13	if(value >= 1e6) then
 14		return ('%.2fm'):format(value / 1e6):gsub('%.?0+([km])$', '%1')
 15	elseif(value >= 1e4) then
 16		return ('%.1fk'):format(value / 1e3):gsub('%.?0+([km])$', '%1')
 17	else
 18		return value
 19	end
 20end
 21
 22oUF.TagEvents['Tukui:threat'] = 'UNIT_THREAT_LIST_UPDATE'
 23oUF.Tags['Tukui:threat'] = function(unit)
 24	local tanking, status, percent = UnitDetailedThreatSituation('player', 'target')
 25	if(percent and percent > 0) then
 26		return ('%s%d%%|r'):format(Hex(GetThreatStatusColor(status)), percent)
 27	end
 28end
 29
 30oUF.Tags['Tukui:health'] = function(unit)
 31	local min, max = UnitHealth(unit), UnitHealthMax(unit)
 32	local status = not UnitIsConnected(unit) and 'Offline' or UnitIsGhost(unit) and 'Ghost' or UnitIsDead(unit) and 'Dead'
 33
 34	if(status) then
 35		return status
 36	elseif(unit == 'target' and UnitCanAttack('player', unit)) then
 37		return ('%s (%d|cff0090ff%%|r)'):format(ShortenValue(min), min / max * 100)
 38	elseif(unit == 'player' and min ~= max) then
 39		return ('|cffff8080%d|r %d|cff0090ff%%|r'):format(min - max, min / max * 100)
 40	elseif(min ~= max) then
 41		return ('%s |cff0090ff/|r %s'):format(ShortenValue(min), ShortenValue(max))
 42	else
 43		return max
 44	end
 45end
 46
 47oUF.Tags['Tukui:power'] = function(unit)
 48	local power = UnitPower(unit)
 49	if(power > 0 and not UnitIsDeadOrGhost(unit)) then
 50		local _, type = UnitPowerType(unit)
 51		local colors = _COLORS.power
 52		return ('%s%d|r'):format(Hex(colors[type] or colors['RUNES']), power)
 53	end
 54end
 55
 56oUF.Tags['Tukui:druid'] = function(unit)
 57	local min, max = UnitPower(unit, 0), UnitPowerMax(unit, 0)
 58	if(UnitPowerType(unit) ~= 0 and min ~= max) then
 59		return ('|cff0090ff%d%%|r'):format(min / max * 100)
 60	end
 61end
 62
 63oUF.TagEvents['Tukui:diffcolor'] = 'UNIT_LEVEL'
 64oUF.Tags['Tukui:diffcolor'] = function(unit)
 65	local r, g, b
 66	local level = UnitLevel(unit)
 67	if (level < 1) then
 68		r, g, b = 0.69, 0.31, 0.31
 69	else
 70		local DiffColor = UnitLevel('target') - UnitLevel('player')
 71		if (DiffColor >= 5) then
 72			r, g, b = 0.69, 0.31, 0.31
 73		elseif (DiffColor >= 3) then
 74			r, g, b = 0.71, 0.43, 0.27
 75		elseif (DiffColor >= -2) then
 76			r, g, b = 0.84, 0.75, 0.65
 77		elseif (-DiffColor <= GetQuestGreenRange()) then
 78			r, g, b = 0.33, 0.59, 0.33
 79		else
 80			r, g, b = 0.55, 0.57, 0.61
 81		end
 82	end
 83	return string.format('|cff%02x%02x%02x', r * 255, g * 255, b * 255)
 84end
 85
 86local utf8sub = function(string, i, dots)
 87	if not string then return end
 88	local bytes = string:len()
 89	if (bytes <= i) then
 90		return string
 91	else
 92		local len, pos = 0, 1
 93		while(pos <= bytes) do
 94			len = len + 1
 95			local c = string:byte(pos)
 96			if (c > 0 and c <= 127) then
 97				pos = pos + 1
 98			elseif (c >= 192 and c <= 223) then
 99				pos = pos + 2
100			elseif (c >= 224 and c <= 239) then
101				pos = pos + 3
102			elseif (c >= 240 and c <= 247) then
103				pos = pos + 4
104			end
105			if (len == i) then break end
106		end
107
108		if (len == i and pos <= bytes) then
109			return string:sub(1, pos - 1)..(dots and '...' or '')
110		else
111			return string
112		end
113	end
114end
115
116
117oUF.TagEvents['Tukui:getnamecolor'] = 'UNIT_POWER'
118oUF.Tags['Tukui:getnamecolor'] = function(unit)
119	local reaction = UnitReaction(unit, 'player')
120	if (UnitIsPlayer(unit)) then
121		return _TAGS['raidcolor'](unit)
122	elseif (reaction) then
123		local c = T.oUF_colors.reaction[reaction]
124		return string.format('|cff%02x%02x%02x', c[1] * 255, c[2] * 255, c[3] * 255)
125	else
126		r, g, b = .84,.75,.65
127		return string.format('|cff%02x%02x%02x', r * 255, g * 255, b * 255)
128	end
129end
130
131oUF.TagEvents['Tukui:nameshort'] = 'UNIT_NAME_UPDATE'
132oUF.Tags['Tukui:nameshort'] = function(unit)
133	local name = UnitName(unit)
134	return utf8sub(name, 10, false)
135end
136
137oUF.TagEvents['Tukui:namemedium'] = 'UNIT_NAME_UPDATE'
138oUF.Tags['Tukui:namemedium'] = function(unit)
139	local name = UnitName(unit)
140	return utf8sub(name, 15, true)
141end
142
143oUF.TagEvents['Tukui:namelong'] = 'UNIT_NAME_UPDATE'
144oUF.Tags['Tukui:namelong'] = function(unit)
145	local name = UnitName(unit)
146	return utf8sub(name, 20, true)
147end
148
149oUF.TagEvents['Tukui:dead'] = 'UNIT_HEALTH'
150oUF.Tags['Tukui:dead'] = function(unit)
151	if UnitIsDeadOrGhost(unit) then
152		return L.unitframes_ouf_deaddps
153	end
154end
155
156oUF.TagEvents['Tukui:afk'] = 'PLAYER_FLAGS_CHANGED'
157oUF.Tags['Tukui:afk'] = function(unit)
158	if UnitIsAFK(unit) then
159		return CHAT_FLAG_AFK
160	end
161end
162
163oUF.TagEvents['Tukui:offline'] = 'PLAYER_FLAGS_CHANGED'
164oUF.Tags['Tukui:offline'] = function(unit)
165	if not UnitIsConnected(unit) then
166		return L.unitframes_ouf_offline
167	end
168end