View Single Post
02-15-14, 08:00 AM   #27
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by pelf View Post
Incidentally, what is the chat type of a message that gets added like this?
Code:
DEFAULT_CHAT_FRAME:AddMessage("Hello, world.")
It doesn't have a type. Adding a message directly does not trigger any event handlers.

Originally Posted by pelf View Post
After ChatTypeGroup is defined, a reverse lookup table is created called ChatTypeGroupInverted. Couldn't that be used instead of the one you created in your code? The output is the key into ChatTypeInfo, so I assume it could be.
Ah, I missed that. Yes, that will work.

Also, I've realized that using only a filter could "alert" on messages that aren't actually added to the frame (due to being removed by another filter). If you want to account for that possibility, you could use an AddMessage hook in conjunction with a filter, but you'd want to be a little smarter about it than just assuming the last event seen by the filter was related to the current message.

Code:
local lastGroup = {}

local ChatTypeGroupInverted = ChatTypeGroupInverted
ChatFrame_AddMessageEventFilter(function(frame, event, ...)
     lastGroup[frame] = ChatTypeGroupInverted[event]
end)

local function AddMessage(frame, message, r, g, b)
     local group = lastGroup[frame]
     lastGroup[frame] = nil
     if not group or not MONITORED_EVENTS[group] or not frame.isDocked then return end

     local color = ChatTypeInfo[group]
     if r == color.r and g == color.g and b == color.b then
          -- check group against your settings and flash if desired
     end
end

for i = 1, 10 do
     local frame = _G["ChatFrame"..i]
     hooksecurefunc(frame, "AddMessage", AddMessage)
end
Still not foolproof, but better than no checking at all. Also note that your MONITORED_EVENTS table would need to be changed to hold chat groups (eg. the keys in ChatTypeInfo) rather than all the individual events... if you actually want to let users pick and choose individual events instead of whole groups, just set event as the value in the filter, and do the ChatTypeGroupInverted lookup to find the group (to find the color) in the AddMessage hook after the return check.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote