Thread Tools Display Modes
07-03-17, 11:49 AM   #1
cokedrivers
A Rage Talon Dragon Guard
 
cokedrivers's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2009
Posts: 325
Help with Name Colors

Hello,

What I want to do is in the chat bubbles have the units name color match their reaction to me.

Ive tried for the past 2 hours with everything I try it pops up a error.

Here is the code I'm currently using:
Code:
local function UpdateFrame(frame, guid, name)
	if not frame.text then SkinFrame(frame) end
	frame.inUse = true
	
	if settings.showSender then
		local class
		if guid ~= nil and guid ~= "" then
			_, class, _, _, _, _ = GetPlayerInfoByGUID(guid)
		end
		
		if name then
			local color = RAID_CLASS_COLORS[class] or {  r = 1, g = 1, b = 1 }
			frame.sender:SetText(("|cFF%2x%2x%2x%s|r"):format(color.r * 255, color.g * 255, color.b * 255, name))
			if frame.text:GetWidth() < frame.sender:GetWidth() then
				frame.text:SetWidth(frame.sender:GetWidth())
			end
		end
	end
end
But every thing the is a NPC or my Followers show up white text. I would like the to use my custom colors like this that is my Tooltip addon.

Tooltip Custom Colors:
Code:
CUSTOM_FACTION_BAR_COLORS = {
    [1] = {r = 1, g = 0, b = 0},
    [2] = {r = 1, g = 0, b = 0},
    [3] = {r = 1, g = 1, b = 0},
    [4] = {r = 1, g = 1, b = 0},
    [5] = {r = 0, g = 1, b = 0},
    [6] = {r = 0, g = 1, b = 0},
    [7] = {r = 0, g = 1, b = 0},
    [8] = {r = 0, g = 1, b = 0},
}

function GameTooltip_UnitColor(unit)

    local r, g, b

    if (UnitIsDead(unit) or UnitIsGhost(unit) or UnitIsTapDenied(unit)) then
        r = 0.5
        g = 0.5
        b = 0.5
    elseif (UnitIsPlayer(unit)) then
        if (UnitIsFriend(unit, 'player')) then
            local _, class = UnitClass(unit)
            if ( class ) then
                r = RAID_CLASS_COLORS[class].r
                g = RAID_CLASS_COLORS[class].g
                b = RAID_CLASS_COLORS[class].b
            else
                r = 0.60
                g = 0.60
                b = 0.60
            end
        elseif (not UnitIsFriend(unit, 'player')) then
            r = 1
            g = 0
            b = 0
        end
    elseif (UnitPlayerControlled(unit)) then
        if (UnitCanAttack(unit, 'player')) then
            if (not UnitCanAttack('player', unit)) then
                r = 157/255
                g = 197/255
                b = 255/255
            else
                r = 1
                g = 0
                b = 0
            end
        elseif (UnitCanAttack('player', unit)) then
            r = 1
            g = 1
            b = 0
        elseif (UnitIsPVP(unit)) then
            r = 0
            g = 1
            b = 0
        else
            r = 157/255
            g = 197/255
            b = 255/255
        end
    else
        local reaction = UnitReaction(unit, 'player')

        if (reaction) then
            r = CUSTOM_FACTION_BAR_COLORS[reaction].r
            g = CUSTOM_FACTION_BAR_COLORS[reaction].g
            b = CUSTOM_FACTION_BAR_COLORS[reaction].b
        else
            r = 157/255
            g = 197/255
            b = 255/255
        end
    end

    return r, g, b
end
Ive tried addin the tolltip colors to the chat buble lua and the did this line:

Code:
local color = RAID_CLASS_COLORS[class] or {  r = GameTooltip_UnitColor(unit).r, g = GameTooltip_UnitColor(unit).g, b = GameTooltip_UnitColor(unit).b }
but it give me an error thet there is no unit.

I'm not the greatest at coding but I most of the time can fumble my way threw.

Any help would be great.
Thanks
Coke
  Reply With Quote
07-03-17, 01:48 PM   #2
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,323
You need a UnitID to be able to get reaction colors. I'm guessing since you're using GUID that this is coming from a chat event, which can come from things without valid UnitIDs.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
07-04-17, 07:14 AM   #3
cokedrivers
A Rage Talon Dragon Guard
 
cokedrivers's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2009
Posts: 325
Originally Posted by SDPhantom View Post
You need a UnitID to be able to get reaction colors. I'm guessing since you're using GUID that this is coming from a chat event, which can come from things without valid UnitIDs.
Thanks for the reply.

Here is the whole code I'm trying to use. As I said before I'm not a great code writer.

Code:
------------------------------------------------------------------------
--	 Chat Bubbles
------------------------------------------------------------------------

local settings = {
	fontSender = {"Fonts\\FRIZQT__.TTF", 12},
	showSender = true,
}

-- Events & corresponding cVars
local events = {
	CHAT_MSG_SAY = "chatBubbles", CHAT_MSG_YELL = "chatBubbles",
	CHAT_MSG_PARTY = "chatBubblesParty", CHAT_MSG_PARTY_LEADER = "chatBubblesParty",
	CHAT_MSG_MONSTER_SAY = "chatBubbles", CHAT_MSG_MONSTER_YELL = "chatBubbles", CHAT_MSG_MONSTER_PARTY = "chatBubblesParty",
}

-- WorldFrame frames are always uiScaled it seems
local function FixedScale(len)
	return GetScreenHeight() * len / 768
end

local function RotateCoordPair(x, y, ox, oy, a, asp)
	y = y / asp
	oy = oy / asp
	return ox + (x - ox) * math.cos(a) - (y - oy) * math.sin(a),
		(oy + (y - oy) * math.cos(a) + (x - ox) * math.sin(a)) * asp
end

-- Clip + rotate texture
local function SetRotatedTexCoords(tex, left, right, top, bottom, width, height, angle, originx, originy)
	local ratio, angle, originx, originy = width / height, math.rad(angle), originx or 0.5, originy or 1
	local LRx, LRy = RotateCoordPair(left, top, originx, originy, angle, ratio)
	local LLx, LLy = RotateCoordPair(left, bottom, originx, originy, angle, ratio)
	local ULx, ULy = RotateCoordPair(right, top, originx, originy, angle, ratio)
	local URx, URy = RotateCoordPair(right, bottom, originx, originy, angle, ratio)
	tex:SetTexCoord(LRx, LRy, LLx, LLy, ULx, ULy, URx, URy)
end

-- Skin a new frame
local function SkinFrame(frame)
	for i = 1, select("#", frame:GetRegions()) do
		local region = select(i, frame:GetRegions())
		if region:GetObjectType() == "FontString" then
			frame.text = region
		end
	end
	
	-- Chat text
    frame.text:SetFontObject('SystemFont_Small')
    frame.text:SetJustifyH('LEFT')
	
	-- Sender text
	if settings.showSender then
		frame.sender = frame:CreateFontString(nil, 'OVERLAY', 'NumberFont_Outline_Med')
		frame.sender:SetPoint('BOTTOMLEFT', frame.text, 'TOPLEFT', 0, 4)
		frame.sender:SetJustifyH('LEFT')
	end
	
	-- Border
	frame:ClearAllPoints()
	frame:SetPoint("TOPLEFT", frame.text, -7, settings.showSender and 10 + FixedScale(settings.fontSender[2]) or 7)
	frame:SetPoint("BOTTOMRIGHT", frame.text, 7, -7)
    frame:SetBackdrop({
        bgFile = 'Interface\\Tooltips\\UI-Tooltip-Background',
        edgeFile = 'Interface\\Tooltips\\UI-Tooltip-Border',
		tile = true,
        tileSize = 16,
        edgeSize = 12,
        insets = {left=3, right=3, top=3, bottom=3},
    })
    frame:SetBackdropColor(0, 0, 0, 1)
	
	
	-- Hide Chat Bubble Tail
	local bottom, tail
	for i = 1, select("#", frame:GetRegions()) do
		local region = select(i, frame:GetRegions())
		if region:GetObjectType() == "Texture" then
			if ({region:GetPoint()})[1] == "BOTTOMLEFT" and region:GetPoint(2) then
				bottom = region
			elseif region:GetTexture() == "Interface\\Tooltips\\ChatBubble-Tail" then
				tail = region
			end
		end
	end
	
	tail:ClearAllPoints()
	tail:Hide()
	
	frame:HookScript("OnHide", function() frame.inUse = false end)
end

-- Update a frame
local function UpdateFrame(frame, guid, name)
	if not frame.text then SkinFrame(frame) end
	frame.inUse = true
	
	if settings.showSender then
		local class
		if guid ~= nil and guid ~= "" then
			_, class, _, _, _, _ = GetPlayerInfoByGUID(guid)
		end
		
		if name then
			local color = RAID_CLASS_COLORS[class] or {  r = 157/255, g = 197/255, b = 255/255 }
			frame.sender:SetText(("|cFF%2x%2x%2x%s|r"):format(color.r * 255, color.g * 255, color.b * 255, name))
			if frame.text:GetWidth() < frame.sender:GetWidth() then
				frame.text:SetWidth(frame.sender:GetWidth())
			end
		end
	end
end

-- Find chat bubble with given message
local function FindFrame(msg)
	for i = 1, WorldFrame:GetNumChildren() do
		local frame = select(i, WorldFrame:GetChildren())
		if not frame:GetName() and not frame.inUse then
			for i = 1, select("#", frame:GetRegions()) do
				local region = select(i, frame:GetRegions())
				if region:GetObjectType() == "FontString" and region:GetText() == msg then
					return frame
				end
			end

		end
	end
end

local f = CreateFrame("Frame")
for event, cvar in pairs(events) do f:RegisterEvent(event) end

f:SetScript("OnEvent", function(self, event, msg, sender, _, _, _, _, _, _, _, _, _, guid)
	if GetCVarBool(events[event]) then
		f.elapsed = 0
		f:SetScript("OnUpdate", function(self, elapsed)
			self.elapsed = self.elapsed + elapsed
			local frame = FindFrame(msg)
			if frame or self.elapsed > 0.3 then
				f:SetScript("OnUpdate", nil)
				if frame then UpdateFrame(frame, guid, sender) end
			end
		end)
	end
end)
Thanks again for any help with this.

Coke
  Reply With Quote
07-05-17, 09:04 AM   #4
cokedrivers
A Rage Talon Dragon Guard
 
cokedrivers's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2009
Posts: 325
Ok so I was able to get the chat bubble to be skinned the way I like it but unfortunately I was not able to get the sender working.

Also it is diabled in raids and instances so no error pops up when in them,

Code:
Code:
------------------------------------------------------------------------
--	 Skin the Chat Bubbles
------------------------------------------------------------------------
local select, pairs = select, pairs
local format = string.format
local CreateFrame = CreateFrame
local C_ChatBubbles_GetAllChatBubbles = C_ChatBubbles.GetAllChatBubbles

local function StyleBubble(frame)
	for i=1, frame:GetNumRegions() do
		local region = select(i, frame:GetRegions())
		if region:GetObjectType() == "Texture" then
			region:SetTexture(nil)
		elseif region:GetObjectType() == "FontString" then
			frame.text = region
		end
	end
	
	frame.text:SetFontObject('SystemFont_Small')
	frame.text:SetJustifyH('LEFT')

	frame:ClearAllPoints()
	frame:SetPoint('TOPLEFT', frame.text, -7, 7)
	frame:SetPoint('BOTTOMRIGHT', frame.text, 7, -7)
	frame:SetBackdrop({
		bgFile = 'Interface\\Tooltips\\UI-Tooltip-Background',
		edgeFile = 'Interface\\Tooltips\\UI-Tooltip-Border',
		tile = true,
		tileSize = 16,
		edgeSize = 12,
		insets = {left=3, right=3, top=3, bottom=3},
	})
	frame:SetBackdropColor(0, 0, 0, 1)
	
	local r, g, b = frame.text:GetTextColor()
	frame:SetBackdropBorderColor(r, g, b, .8)
			
	frame.isSkinned = true
end

local frame = CreateFrame('Frame')
frame.lastupdate = -2 -- wait 2 seconds before hooking frames
local numChildren = 0
frame:SetScript('OnUpdate', function(self, elapsed, guid, name)
	self.lastupdate = self.lastupdate + elapsed
	if (self.lastupdate < .1) then return end
	self.lastupdate = 0	
	
	for _, chatBubble in pairs(C_ChatBubbles_GetAllChatBubbles()) do
		if not chatBubble.isSkinned then				
			StyleBubble(chatBubble)
		end
	end
end)
Hope this helps someone.

Coke
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Help with Name Colors


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off