PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/Tukui/modules/extras/teknicolor.lua

http://github.com/Asphyxia/Tukui
Lua | 156 lines | 106 code | 36 blank | 14 comment | 31 complexity | f4d8901f9b53f70a3efd0601e1789661 MD5 | raw file
  1. local T, C, L = unpack(select(2, ...))
  2. ------------------------------
  3. -- Teknicolor [Credit Tekkub]
  4. ------------------------------
  5. -- Are you local? --
  6. ------------------------------
  7. local colors = {}
  8. local nameclass = {}
  9. local namesnobracket = setmetatable({}, {
  10. __index = function(t, k)
  11. local nc = k and nameclass[k]
  12. local c = nc and colors[nc]
  13. if not c then return end
  14. local v = "|cff".. c.. k.. "|r"
  15. t[k] = v
  16. return v
  17. end,
  18. })
  19. local x = setmetatable({}, {
  20. __index = function(t, k)
  21. local nc = k and nameclass[k]
  22. local c = nc and colors[nc]
  23. if not c then return end
  24. local v = string.format("[|cff%s%s|r]", c, k)
  25. t[k] = v
  26. return v
  27. end,
  28. })
  29. local names = setmetatable({}, {
  30. __index = function(t, k) return x[k] end,
  31. __newindex = function(t, k, v) if colors[v] then nameclass[k] = v end end,
  32. })
  33. local function SetColors()
  34. for i in pairs(x) do x[i] = nil end
  35. for i in pairs(namesnobracket) do namesnobracket[i] = nil end
  36. local cc = CUSTOM_CLASS_COLORS or RAID_CLASS_COLORS
  37. for token,loc_male in pairs(LOCALIZED_CLASS_NAMES_MALE) do
  38. local loc_female = LOCALIZED_CLASS_NAMES_FEMALE[token]
  39. local c = cc[token]
  40. if c then
  41. local hex = string.format("%02x%02x%02x", c.r*255, c.g*255, c.b*255)
  42. colors[loc_male], colors[loc_female], colors[token] = hex, hex, hex
  43. end
  44. end
  45. end
  46. SetColors()
  47. if CUSTOM_CLASS_COLORS then CUSTOM_CLASS_COLORS:RegisterCallback(SetColors) end
  48. SetColors = nil
  49. teknicolor = {}
  50. teknicolor.nametable = names
  51. local f = CreateFrame("Frame")
  52. f:SetScript("OnEvent", function(self, event, ...) if teknicolor[event] then teknicolor[event](teknicolor, event, ...) end end)
  53. function teknicolor:PLAYER_LOGIN()
  54. f:RegisterEvent("FRIENDLIST_UPDATE")
  55. f:RegisterEvent("GUILD_ROSTER_UPDATE")
  56. if IsInGuild() then GuildRoster() end
  57. if GetNumFriends() > 0 then ShowFriends() end
  58. f:UnregisterEvent("PLAYER_LOGIN")
  59. self.PLAYER_LOGIN = nil
  60. end
  61. if IsLoggedIn() then teknicolor:PLAYER_LOGIN() else f:RegisterEvent("PLAYER_LOGIN") end
  62. ------------------------------------
  63. -- Class caching events --
  64. ------------------------------------
  65. function teknicolor:FRIENDLIST_UPDATE()
  66. for i=1,GetNumFriends() do
  67. local name, _, class = GetFriendInfo(i)
  68. if name then names[name] = class end
  69. end
  70. end
  71. function teknicolor:GUILD_ROSTER_UPDATE()
  72. for i=1,GetNumGuildMembers(true) do
  73. local name, _, _, _, _, _, _, _, _, _, engclass = GetGuildRosterInfo(i)
  74. if name then names[name] = engclass end
  75. end
  76. end
  77. ----------------------------------
  78. -- Chatframe coloring --
  79. ----------------------------------
  80. local OFFLINE_MATCH = ERR_FRIEND_OFFLINE_S:gsub("%%s", "(%%S+)")
  81. local ROLL_MATCH = RANDOM_ROLL_RESULT:gsub("%(", "%%("):gsub("%)", "%%)"):gsub("%%s", "(%%S+)"):gsub("%%d", "(%%d+)")
  82. ChatFrame_AddMessageEventFilter("CHAT_MSG_SYSTEM", function(self, event, msg, ...)
  83. local pname = msg:match(OFFLINE_MATCH)
  84. if pname and namesnobracket[pname] then return false, string.format(ERR_FRIEND_OFFLINE_S, namesnobracket[pname]), ... end
  85. local pname, roll, min, max = msg:match(ROLL_MATCH)
  86. if pname and namesnobracket[pname] then return false, string.format(RANDOM_ROLL_RESULT, namesnobracket[pname], roll, min, max), ... end
  87. local name = msg:match("|h%[(.+)%]|h")
  88. if name and names[name] then return false, msg:gsub("|h%["..name.."%]|h", "|h"..names[name].."|h"), ... end
  89. end)
  90. ------------------------------------
  91. -- Friend List Coloring --
  92. ------------------------------------
  93. local origs, butts, inhook = {}, {}
  94. local function NewSetText(frame, str, ...)
  95. if inhook then return end -- Failsafe to avoid the great infinity
  96. inhook = true
  97. local butt = butts[frame]
  98. if butt.buttonType == FRIENDS_BUTTON_TYPE_WOW then
  99. local name, _, class = GetFriendInfo(butt.id)
  100. if name and class and colors[class] then origs[frame](frame, "|cff"..colors[class]..name.."|r", ...) end
  101. elseif butt.buttonType == FRIENDS_BUTTON_TYPE_BNET then
  102. local _, _, _, toonName, toonID, client = BNGetFriendInfo(butt.id)
  103. if toonID and toonName and client == BNET_CLIENT_WOW then
  104. local hasFocus, toonName, client, realmName, faction, race, class, guild, zoneName, level, gameText = BNGetToonInfo(toonID)
  105. if class and colors[class] then
  106. local coop = CanCooperateWithToon(toonID)
  107. local coopcolor = coop and FRIENDS_WOW_NAME_COLOR_CODE or FRIENDS_OTHER_NAME_COLOR_CODE
  108. if coop and ENABLE_COLORBLIND_MODE == "1" then toonName = toonName..CANNOT_COOPERATE_LABEL end
  109. origs[frame](frame, str:gsub("%("..toonName.."%)", "(|cff"..colors[class]..toonName..coopcolor..")"), ...)
  110. end
  111. end
  112. end
  113. inhook = nil
  114. end
  115. for i=1,FRIENDS_TO_DISPLAY do
  116. local f = _G["FriendsFrameFriendsScrollFrameButton"..i.."Name"]
  117. butts[f] = _G["FriendsFrameFriendsScrollFrameButton"..i]
  118. origs[f] = f.SetText
  119. hooksecurefunc(f, "SetText", NewSetText)
  120. end