chatbox library
Summary
Functions
/ or /? and is not OOC).//, .// or [[).chatbox.clientMode, which marks that the message being built was requested by a client.Functions
chatbox.AddBBCode(id, callback, requireRich)
client gamemodes/catwork/gamemode/core/libraries/cl_chatbox.lua:374Registers a BB-code tag that can be used in chat messages.
The callback receives the value after = in the opening tag and the text up to
the next tag, and returns the object inserted into the parsed line in place of
the tag (usually a Color). It may return a replacement for the text as a second value.
chatbox.AddBBCode('red', function(value, text)
return Color(255, 0, 0)
end)
Parameters
- id String Name of the tag, such as
'color'for[color=...] - callback Function Called with the tag value and the following text
- requireRich Boolean optional, defaults to
trueWhether the tag is only parsed in rich messages
On the server
Registers a chat filter that decides who receives messages of a type.
Messages pick their filter through msgData.filter ('ic', 'ooc',
'looc', 'admin', 'command' and so on). Registering an existing ID
replaces it. Does nothing if the ID is empty.
chatbox.AddFilter('radio', function(listener, msgData)
return listener:HasItemByID('handheld_radio')
end)
Parameters
- id String Name of the filter
- callback Function Called as
callback(listener, msgData); returntrueto send the message tolistener
See also
On the client
Registers a chat filter.
A filter is called with the message data before it is parsed and sets the
message's display options (drawAvatar, drawTime, icon, rich, translate,
type...). Does nothing when id is empty.
chatbox.AddFilter('radio', function(messageData)
messageData.isPlayerMessage = true
messageData.type = 'radio'
end)
Parameters
- id String Unique ID of the filter, set as
filteron messages - callback Function Called with the message data
Mapto change
See also
Defined in
chatbox.AddPrefix(prefix, callback)
server gamemodes/catwork/gamemode/core/libraries/sv_chatbox.lua:37Registers a chat prefix that is checked against messages players type.
The callback receives the message table and inspects msgData.text. When it
returns a truthy value, the prefix is stripped from the text afterwards (for
/? only the first character is stripped). The callback usually sets
msgData.filter and msgData.radius. Does nothing if the prefix is empty.
chatbox.AddPrefix('!', function(msgData)
if msgData.text:StartsWith('!') then
msgData.filter = 'looc'
msgData.radius = config.GetVal('talk_radius')
return true
end
end)
Parameters
- prefix String The text messages must start with, such as
'//' - callback Function Called as
callback(msgData); returntrueonce the message has been handled
See also
On the server
Builds a chat message from its arguments and sends it to the listeners that pass its filter.
Strings are appended to the text, Colors wrap the following text in
[color=r,g,b,a] tags, players append their name and become the message's
position and players entry, and tables are merged into the message to set
fields such as filter, icon, radius, sender or textColor. The
message defaults to the 'default' filter and the information icon.
Fires ChatAddText(listeners, message), then ChatboxAdjustMessageInfo(message, listeners) (returning false there cancels the message), sends the message
over the ChatboxAddText Cable message to the listeners that pass its filter (an
unknown filter counts as 'default') and finally fires
ChatboxMessageSent(message).
chatbox.AddText(nil, Color(255, 100, 100), 'The server restarts in five minutes.', {
filter = 'events',
icon = 'icon16/error.png'
})
Parameters
- listeners Player optional, defaults to
nilA player or a list of players to send to;nilsends to everyone - ... Any Strings, colors, players and option tables making up the message
Returns
- Map The message table with a
listenerskey added (the list of players it was sent to), ornilif a hook cancelled it or the only listener is no longer valid
See also
On the client
Asks the server to add a message to the local player's chat box.
The arguments are sent to the server, which builds the message with its own
chatbox.AddText and sends it back to this player only.
chatbox.AddText(Color(255, 100, 100), 'You cannot do that.')
Parameters
- ... Any Strings,
Colors, players and message option tables
Defined in
Registers a chat message type.
A type is called with the message data after its filter and sets its colors
and prefix (textColor, prefix, prefixColor, nameColorOverride...).
Does nothing when id is empty.
Parameters
- id String Unique ID of the type, set as
typeon messages - callback Function Called with the message data
Mapto change
See also
chatbox.CanHear(listener, position, radius)
server gamemodes/catwork/gamemode/core/libraries/sv_chatbox.lua:106Returns whether a player is within hearing range of a position.
The player hears it when the position is within radius of them, or within
half the radius of the point they are looking at. Players that have not
initialized never hear anything.
Parameters
- listener Player The player who would hear the message
- position Vector Where the message comes from; without one only a radius of
0is heard - radius Number Hearing radius;
0means everyone hears it, a negative value or a non-number means nobody does
Returns
- Boolean Whether the listener can hear the message
Creates the chat box panel, replacing the existing one.
Creates the chat box panel and its text entry.
Creates the chat text entry inside the chat box, replacing the existing one.
Returns the callback of a BB-code tag.
Parameters
- id String Name of the tag
Returns
- Function The tag's callback, or
nilif it is not registered
Returns the text the local player is typing.
Returns
- String The text in the entry, or
''if the chat box is closed
On the server
Returns the callback of a chat filter.
Parameters
- id String Name of the filter, as passed to
chatbox.AddFilter
Returns
- Function The filter callback, or
nilif no filter has that name
On the client
Returns the callback of a chat filter.
Parameters
- id String Unique ID of the filter
Returns
- Function The filter, the
defaultfilter ifidis unknown, ornilifidis empty
Defined in
Returns the number of lines in the chat history, counting wrapped lines.
Returns
- Number Total line count of the stored messages
Returns the data registered for a chat prefix.
Parameters
- prefix String The prefix as passed to
chatbox.AddPrefix
Returns
- Map The prefix data with
Callbackandlengthkeys, ornilif it is not registered
Returns the callback of a chat message type.
Parameters
- id String Unique ID of the type
Returns
- Function The type, or
nilif it is unknown oridis empty
Closes the chat box.
Runs the ChatBoxClosed hook with the typed text and then FinishChat. When
Escape closed it, keeps the pause menu from opening.
See also
Returns whether the chat box is open.
Returns
- Boolean Whether the chat box is open, or
nilif it has not been created
Returns whether the typed text is a command (starts with / or /? and is not OOC).
Returns
- Boolean Whether the player is typing a command
Returns whether the typed text starts with an OOC prefix (//, .// or [[).
Returns
- Boolean Whether the player is typing an OOC message
chatbox.ParseBBCodes(line, rich)
client gamemodes/catwork/gamemode/core/libraries/cl_chatbox.lua:400Replaces the BB-code tags in a line of text with the objects their callbacks return.
Changes line in place, splitting each string around its tags; tags that are not registered stay in
the text. The result of the last tag is stored in chatbox.LastBBCode and inserted at the start of
the next parsed line, so a color carries over wrapped lines.
Parameters
- line List Strings and other objects making up a line
- rich Boolean Whether tags that require rich messages are parsed
Turns a message into the lines drawn by the chat box.
Applies the message's filter ('ooc' when unset) and type, then builds the time,
icon, avatar, prefix and sender name parts and wraps the text. Runs the
ChatboxPreProcess and PreChatboxParse hooks with the message data and
PostChatboxParse with the result. A filter or type that is not registered falls
back to 'default'.
Parameters
- messageData Map The message data received from the server
Returns
- List<List> The lines; each starts with its vertical offset, followed by the sender,
Colors, strings and marker strings such as'[SenderAvatar]'. The positions of the marker strings are the keys of the line'smarkersfield
chatbox.PlayerCanHear(listener, messageData)
server gamemodes/catwork/gamemode/core/libraries/sv_chatbox.lua:282Returns whether a listener receives a message, according to the message's filter.
An invalid listener (such as the server console) receives everything except in-character messages.
Parameters
- listener Player The player to check
- messageData Map The message table;
messageData.filternames the filter and defaults to'default'
Returns
- Boolean Whether the listener receives the message
See also
Removes the chat box panels and creates them again, closed.
Bound to the cw_resetchat console command.
Moves the chat box back to its default position in the bottom left corner.
See also
chatbox.SayAsPlayer(player, radius, text)
server gamemodes/catwork/gamemode/core/libraries/sv_chatbox.lua:422Makes a player say something in character, as if they had typed it.
The text is quoted and sent through chatbox.AddText with the 'ic'
filter, so only players within the radius receive it.
Parameters
- player Player The player who speaks
- radius Number optional, defaults to
nilHearing radius;niluses thetalk_radiusconfig - text String What the player says
See also
Sets chatbox.clientMode, which marks that the message being built was requested by a client.
The ChatboxAddText Cable receiver turns it on around its call to
chatbox.AddText.
Parameters
- isclient Boolean Whether client mode is on
chatbox.SetCustomPos(x, y, duration)
client gamemodes/catwork/gamemode/core/libraries/cl_chatbox.lua:109Moves the chat box to a position on the screen.
The panel slides to the new position when it exists; otherwise the position is used when it is created.
Parameters
- x Number Horizontal position
- y Number Vertical position
- duration Number optional, defaults to
0.25Length of the move animation in seconds; defaults tochatbox.moveDuration
See also
chatbox.SetCustomSize(w, h, duration)
client gamemodes/catwork/gamemode/core/libraries/cl_chatbox.lua:125Resizes the chat box.
The panel animates to the new size when it exists; otherwise the size is used when it is created.
Parameters
- w Number New width
- h Number New height
- duration Number optional, defaults to
0.25Length of the resize animation in seconds; defaults tochatbox.moveDuration
Opens the chat box and focuses its text entry.
Creates the panels first if needed.
Parameters
- panel Panel optional, defaults to
nilPanel to parent the chat box to; whennilthe chat box becomes a popup
See also
Rebuilds the drawn lines from the chat history.
Parses up to 19 of the newest messages, skipping as many as the panel is scrolled, lays them out from the bottom up and prepares them for drawing. Called whenever a message arrives or the history is scrolled.
chatbox.WrapText(msgData, maxWidth, initWidth)
client gamemodes/catwork/gamemode/core/libraries/cl_chatbox.lua:512Splits a message's text into pieces that fit the width of the chat box.
Translates the text first when the message has translate set and parses its
BB-codes. Words longer than a line are broken with a dash.
Parameters
- msgData Map The message data; reads
text,translate,richanddata.sizeMultiplier - maxWidth Number Width of a line in pixels
- initWidth Number optional, defaults to
0Width already taken on the first line by the time, icon and name
Returns
- List Strings and BB-code objects, with
'std::endl'marking each line break