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