Reply
 
Thread Tools Display Modes
Old 02-14-14, 06:35 PM   #21
Phanx
A Pyroguard Emberseer
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 3,945
Originally Posted by Aanson View Post
But that points to CF_MEH which is where 'self' is coming from. I know that MessageEventHandler is passed with chat frame as it's 1st arg. Filters don't. If you check out ChatFrame_AddMessageEventFilter (event, filter) line 3193 of ChatFrame.lua, you'll see what I'm talking about.
Look again. The specific line of code Lombra linked clearly shows that self -- which in that scope is a reference to the chat frame handling the event -- is the first argument passed to every chat filter function.

The lines you mentioned handle adding new filter functions -- that part of the code has nothing to do with calling filter functions.

Originally Posted by Aanson View Post
That way I have a guarantee that the message will actually be displayed. It should be reliable because self.lastEvent will always be obtained immediately before AddMessage gets called.
That's not reliable at all. The default UI calls AddMessage from inside its OnEvent script, and HookScript is the equivalent of hooksecurefunc -- it runs your script after the original, so your event handler hook will run after the default UI has already called AddMessage, and your AddMessage hook will be seeing the previous event. In many cases (eg. Trade chat in a city) this will be the same event, but in many other cases it will not be.
__________________
Author/maintainer of Grid, PhanxChat, ShieldsUp, and many more.
Troubleshoot an addonTurn any code into an addonMore addon resources
Need help with your code? Post all of your actual code! Attach or paste your files.
Please don’t PM me about addon bugs or code questions. Post a comment or forum thread instead!

Last edited by Phanx : 02-14-14 at 06:52 PM.
Phanx is offline   Reply With Quote
Old 02-14-14, 07:34 PM   #22
pelf
Sentient Plasmoid
 
pelf's Avatar
Premium Member
Join Date: May 2008
Posts: 128
Alright, I see the reason for both, now. I just got thrown off by the talk about "the color used by the last message" instead of just looking up the color once you have the chat type.

Incidentally, what is the chat type of a message that gets added like this?
Code:
DEFAULT_CHAT_FRAME:AddMessage("Hello, world.")
Does is it end up as "SAY", even though it isn't?

@Phanx
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.
pelf is offline   Reply With Quote
Old 02-14-14, 07:43 PM   #23
Lombra
A Chromatic Dragonspawn
 
Lombra's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 172
Originally Posted by Aanson View Post
But that points to CF_MEH which is where 'self' is coming from. I know that MessageEventHandler is passed with chat frame as it's 1st arg. Filters don't. If you check out ChatFrame_AddMessageEventFilter (event, filter) line 3193 of ChatFrame.lua, you'll see what I'm talking about.
If you check the exact line that I linked (highlighted), you will see that self is passed to filterFunc, which is the function that you register using ChatFrame_AddMessageEventFilter. ChatFrame_AddMessageEventFilter itself doesn't take a frame because the filter is applied to all chat frames.

Edit: Didn't see new posts.
__________________
Grab your sword and fight the Horde!
Lombra is offline   Reply With Quote
Old 02-14-14, 07:59 PM   #24
Aanson
A Flamescale Wyrmkin
Join Date: Aug 2009
Posts: 123
Oooof.

I see your point about not getting the event until all of the ChatFrame's OnEvent code has already run. Should be easily fixed though ty.
__________________
__________________
Aanson is offline   Reply With Quote
Old 02-14-14, 08:04 PM   #25
Aanson
A Flamescale Wyrmkin
Join Date: Aug 2009
Posts: 123
Originally Posted by pelf View Post
Alright, I see the reason for both, now. I just got thrown off by the talk about "the color used by the last message" instead of just looking up the color once you have the chat type.

Incidentally, what is the chat type of a message that gets added like this?
Code:
DEFAULT_CHAT_FRAME:AddMessage("Hello, world.")
Does is it end up as "SAY", even though it isn't?

@Phanx
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.
I'm not sure what it would default to actually.

I'd just assumed that Phanx had written it that way to avoid global table lookups all the time.
__________________
__________________
Aanson is offline   Reply With Quote
Old 02-14-14, 08:12 PM   #26
Aanson
A Flamescale Wyrmkin
Join Date: Aug 2009
Posts: 123
Originally Posted by Lombra View Post
If you check the exact line that I linked (highlighted), you will see that self is passed to filterFunc, which is the function that you register using ChatFrame_AddMessageEventFilter. ChatFrame_AddMessageEventFilter itself doesn't take a frame because the filter is applied to all chat frames.

Edit: Didn't see new posts.
Gotcha, sorry.

Lua Code:
  1. local filterFunc = function(self, event, ...)
  2.   -- do stuff;
  3. end
  4. ChatFrame_AddMessageEventFilter("AN_EVENT", filterFunc);

Back to the drawing board for me, I guess.
__________________
__________________
Aanson is offline   Reply With Quote
Old 02-15-14, 07:00 AM   #27
Phanx
A Pyroguard Emberseer
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 3,945
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.
__________________
Author/maintainer of Grid, PhanxChat, ShieldsUp, and many more.
Troubleshoot an addonTurn any code into an addonMore addon resources
Need help with your code? Post all of your actual code! Attach or paste your files.
Please don’t PM me about addon bugs or code questions. Post a comment or forum thread instead!
Phanx is offline   Reply With Quote
Old 02-17-14, 03:28 PM   #28
Aanson
A Flamescale Wyrmkin
Join Date: Aug 2009
Posts: 123
Originally Posted by Phanx View Post
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.
Yeah, that was my concern when I initially dealt with it via filters. My other concern was that filters are checked quite early on in CF_MEH and the message could potentially (albeit a slim chance) get filtered out later on during the function.

Thanks for that example code and for the advice Phanx. I'll get stuck into it now.
__________________
__________________
Aanson is offline   Reply With Quote
Reply

Go BackWoWInterface » Developer Discussions » General Authoring Discussion » Chat message colours

Thread Tools
Display Modes

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