Skip to content

Commit

Permalink
Merge pull request #1029 from ZarothYe/change-chat-ping-color
Browse files Browse the repository at this point in the history
Change chat mentions to only highlight player's name instead of entire message
  • Loading branch information
Hoikas committed Dec 7, 2021
2 parents c5d2e02 + 9c9fc61 commit 2ea1163
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
5 changes: 4 additions & 1 deletion Scripts/Python/ki/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1342,6 +1342,9 @@ def SetupKI(self):
chatArea.moveCursor(PtGUIMultiLineDirection.kBufferStart)
chatArea.getOwnerDialog().refreshAllControls()

# Setup the chat mention regex.
self.chatMgr.playerName = PtGetClientName()

# Remove unneeded kFontShadowed flags (as long as we can't do that directly in the PRPs)
for dialogAttr in (BigKI, KIListModeDialog, KIJournalExpanded, KIPictureExpanded, KIPlayerExpanded, KIAgeOwnerExpanded, KISettings, KIMarkerFolderExpanded, KICreateMarkerGameGUI):
for i in range(dialogAttr.dialog.getNumControls()):
Expand Down Expand Up @@ -2484,7 +2487,7 @@ def StartFadeTimer(self):

# Never start the fade timer if the user is currently in chat edit mode
if self.chatMgr.isChatting:
return
return

if not BigKI.dialog.isEnabled():
if self.chatMgr.fadeMode in (kChat.FadeNotActive, kChat.FadeDone):
Expand Down
54 changes: 49 additions & 5 deletions Scripts/Python/ki/xKIChat.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ def __init__(self, StartFadeTimer, ResetFadeState, FadeCompletely, GetCensorLeve
# Add the commands processor.
self.commandsProcessor = CommandsProcessor(self)

# Set a fake player name to avoid errors.
self.playerName = None

# Message History
self.MessageHistoryIs = -1 # Current position in message history (up/down key)
self.MessageHistoryList = [] # Contains our message history
Expand Down Expand Up @@ -138,6 +141,20 @@ def IsFaded(self):
# Chatting #
############

# Update the current player name.
def _setPlayerName(self, value):
value = re.escape(value if value else "Anonymous Coward")

# (?:^|[\s\W](?<!\/\/|\w\.)) - non-capturing group: line start or whitespace or non-word character
# with lookbehind that excludes matches preceded by // or word character and .
# trying to prevent mentions that are part of a URL
# (?P<mention>({value}\s?)+) - named capture group: one or more occurrence of name, optionally split by a space
# (?=$|[\s\W]) - lookahead ensures match is followed by line end or whitespace or non-word character
regex = rf"(?:^|[\s\W](?<!\/\/|\w\.))(?P<mention>({value}\s?)+)(?=$|[\s\W])"
PtDebugPrint(f"xKIChat: The chat mention regex is now `{regex}`", level = kWarningLevel)
self._chatMentionRegex = re.compile(regex, re.IGNORECASE)
playerName = property(None, _setPlayerName)

## Make the player enter or exit chat mode.
# Chat mode means the player's keyboard input is being sent to the chat.
def ToggleChatMode(self, entering, firstChar=None):
Expand Down Expand Up @@ -416,6 +433,9 @@ def AddChatLine(self, player, message, cFlags, forceKI=True):
pretext = ""
headerColor = kColors.ChatHeaderBroadcast
bodyColor = kColors.ChatMessage
mentionColor = kColors.ChatMessageMention
censorLevel = self.GetCensorLevel()
hasMention = False

# Is it an object to represent the flags?
if isinstance(cFlags, ChatFlags):
Expand All @@ -438,6 +458,7 @@ def AddChatLine(self, player, message, cFlags, forceKI=True):
headerColor = kColors.ChatHeaderNeighbors
else:
headerColor = kColors.ChatHeaderBuddies

if cFlags.toSelf:
pretext = PtGetLocalizedString("KI.Chat.InterAgeSendTo")
if message[:2] == "<<":
Expand Down Expand Up @@ -471,8 +492,8 @@ def AddChatLine(self, player, message, cFlags, forceKI=True):
self.lastPrivatePlayerID = (player.getPlayerName(), player.getPlayerID(), 1)
PtFlashWindow()
# Are we mentioned in the message?
elif message.casefold().find(PtGetLocalPlayer().getPlayerName().casefold()) >= 0:
bodyColor = kColors.ChatMessageMention
elif self._chatMentionRegex.search(message) is not None:
hasMention = True
PtFlashWindow()

# Is it a ccr broadcast?
Expand Down Expand Up @@ -514,8 +535,8 @@ def AddChatLine(self, player, message, cFlags, forceKI=True):
self.AddPlayerToRecents(player.getPlayerID())

# Are we mentioned in the message?
if message.casefold().find(PtGetClientName().casefold()) >= 0:
bodyColor = kColors.ChatMessageMention
if self._chatMentionRegex.search(message) is not None:
hasMention = True
forceKI = True
PtFlashWindow()

Expand Down Expand Up @@ -560,6 +581,11 @@ def AddChatLine(self, player, message, cFlags, forceKI=True):
else:
chatMessageFormatted = " {}".format(message)

if hasMention:
chatMentions = [(i.start("mention"), i.end("mention"), i.group("mention")) for i in self._chatMentionRegex.finditer(chatMessageFormatted)]
else:
chatMentions = []

for chatArea in (self.miniChatArea, self.microChatArea):
with PtBeginGUIUpdate(chatArea):
savedPosition = chatArea.getScrollPosition()
Expand All @@ -570,7 +596,25 @@ def AddChatLine(self, player, message, cFlags, forceKI=True):
# Added unicode support here.
chatArea.insertStringW("\n{}".format(chatHeaderFormatted))
chatArea.insertColor(bodyColor)
chatArea.insertStringW(chatMessageFormatted, censorLevel=self.GetCensorLevel())

lastInsert = 0

# If we have player name mentions, we change text colors mid-message
for start, end, mention in chatMentions:
if start > lastInsert:
# Insert normal text up to the current name mention position
chatArea.insertStringW(chatMessageFormatted[lastInsert:start], censorLevel=censorLevel)

lastInsert = end
chatArea.insertColor(mentionColor)
chatArea.insertStringW(mention, censorLevel=censorLevel, urlDetection=False)
chatArea.insertColor(bodyColor)

# If there is remaining text to display after last mention, write it
# Or if it was just a plain message with no mention of player's name
if lastInsert != len(chatMessageFormatted):
chatArea.insertStringW(chatMessageFormatted[lastInsert:], censorLevel=censorLevel)

chatArea.moveCursor(PtGUIMultiLineDirection.kBufferEnd)

# Write to the log file.
Expand Down
1 change: 1 addition & 0 deletions Scripts/Python/ki/xKIConstants.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ class kColors:
DniYellowLt = ptColor(1.0, 1.0, 0.6, 1.0)
DniCyan = ptColor(0.576, 0.867, 0.851, 1.0)
DniBlue = ptColor(0.780, 0.706, 0.870, 1.0)
DniBlueDk = ptColor(0.388, 0.577, 0.676, 1.0)
DniRed = ptColor(1.0, 0.216, 0.380, 1.0)
DniGreen = ptColor(0.698, 0.878, 0.761, 1.0)
DniGreenDk = ptColor(0.0, 0.596, 0.211, 1.0)
Expand Down

0 comments on commit 2ea1163

Please sign in to comment.