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

http://github.com/Asphyxia/Tukui · Lua · 168 lines · 146 code · 19 blank · 3 comment · 57 complexity · f0736d0f699c6caea122a9e26f0e8167 MD5 · raw file

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