Add text highlighting (#31442)

Co-authored-by: ScarKy0 <106310278+ScarKy0@users.noreply.github.com>
Co-authored-by: Hans Larsson <hanandlia@gmail.com>
Co-authored-by: Tobias Berger <toby@tobot.dev>
This commit is contained in:
vitopigno
2025-06-04 12:12:37 +02:00
committed by GitHub
parent 44000552c3
commit f8d0b0cba3
14 changed files with 417 additions and 5 deletions

View File

@@ -0,0 +1,7 @@
<Control xmlns="https://spacestation14.io">
<BoxContainer Orientation="Vertical">
<Label Name="TitleLabel" Access="Public" />
<Label Name="ExampleLabel" Access="Public" />
<ColorSelectorSliders Name="Slider" Access="Public" HorizontalExpand="True" />
</BoxContainer>
</Control>

View File

@@ -0,0 +1,31 @@
using Content.Client.Options.UI;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface;
namespace Content.Client.Options.UI;
/// <summary>
/// Standard UI control used for color sliders in the options menu. Intended for use with <see cref="OptionsTabControlRow"/>.
/// </summary>
/// <seealso cref="OptionsTabControlRow.AddOptionColorSlider"/>
[GenerateTypedNameReferences]
public sealed partial class OptionColorSlider : Control
{
/// <summary>
/// The text describing what this slider affects.
/// </summary>
public string? Title
{
get => TitleLabel.Text;
set => TitleLabel.Text = value;
}
/// <summary>
/// The example text showing the current color of the slider.
/// </summary>
public string? Example
{
get => ExampleLabel.Text;
set => ExampleLabel.Text = value;
}
}

View File

@@ -121,6 +121,19 @@ public sealed partial class OptionsTabControlRow : Control
return AddOption(new OptionSliderFloatCVar(this, _cfg, cVar, slider, min, max, scale, FormatPercent));
}
/// <summary>
/// Add a color slider option, backed by a simple string CVar.
/// </summary>
/// <param name="cVar">The CVar represented by the slider.</param>
/// <param name="slider">The UI control for the option.</param>
/// <returns>The option instance backing the added option.</returns>
public OptionColorSliderCVar AddOptionColorSlider(
CVarDef<string> cVar,
OptionColorSlider slider)
{
return AddOption(new OptionColorSliderCVar(this, _cfg, cVar, slider));
}
/// <summary>
/// Add a slider option, backed by a simple integer CVar.
/// </summary>
@@ -518,6 +531,58 @@ public sealed class OptionSliderFloatCVar : BaseOptionCVar<float>
}
}
/// <summary>
/// Implementation of a CVar option that simply corresponds with a string <see cref="OptionColorSlider"/>.
/// </summary>
/// <seealso cref="OptionsTabControlRow"/>
public sealed class OptionColorSliderCVar : BaseOptionCVar<string>
{
private readonly OptionColorSlider _slider;
protected override string Value
{
get => _slider.Slider.Color.ToHex();
set
{
_slider.Slider.Color = Color.FromHex(value);
UpdateLabelColor();
}
}
/// <summary>
/// Creates a new instance of this type.
/// </summary>
/// <remarks>
/// <para>
/// It is generally more convenient to call overloads on <see cref="OptionsTabControlRow"/>
/// such as <see cref="OptionsTabControlRow.AddOptionPercentSlider"/> instead of instantiating this type directly.
/// </para>
/// </remarks>
/// <param name="controller">The control row that owns this option.</param>
/// <param name="cfg">The configuration manager to get and set values from.</param>
/// <param name="cVar">The CVar that is being controlled by this option.</param>
/// <param name="slider">The UI control for the option.</param>
public OptionColorSliderCVar(
OptionsTabControlRow controller,
IConfigurationManager cfg,
CVarDef<string> cVar,
OptionColorSlider slider) : base(controller, cfg, cVar)
{
_slider = slider;
slider.Slider.OnColorChanged += _ =>
{
ValueChanged();
UpdateLabelColor();
};
}
private void UpdateLabelColor()
{
_slider.ExampleLabel.FontColorOverride = Color.FromHex(Value);
}
}
/// <summary>
/// Implementation of a CVar option that simply corresponds with an integer <see cref="OptionSlider"/>.
/// </summary>

View File

@@ -14,6 +14,10 @@
<ui:OptionSlider Name="SpeechBubbleTextOpacitySlider" Title="{Loc 'ui-options-speech-bubble-text-opacity'}" />
<ui:OptionSlider Name="SpeechBubbleSpeakerOpacitySlider" Title="{Loc 'ui-options-speech-bubble-speaker-opacity'}" />
<ui:OptionSlider Name="SpeechBubbleBackgroundOpacitySlider" Title="{Loc 'ui-options-speech-bubble-background-opacity'}" />
<CheckBox Name="AutoFillHighlightsCheckBox" Text="{Loc 'ui-options-auto-fill-highlights'}" />
<ui:OptionColorSlider Name="HighlightsColorSlider"
Title="{Loc 'ui-options-highlights-color'}"
Example="{Loc 'ui-options-highlights-color-example'}"/>
<Label Text="{Loc 'ui-options-accessability-header-content'}"
StyleClasses="LabelKeyText"/>
<CheckBox Name="CensorNudityCheckBox" Text="{Loc 'ui-options-censor-nudity'}" />

View File

@@ -20,6 +20,8 @@ public sealed partial class AccessibilityTab : Control
Control.AddOptionPercentSlider(CCVars.SpeechBubbleTextOpacity, SpeechBubbleTextOpacitySlider);
Control.AddOptionPercentSlider(CCVars.SpeechBubbleSpeakerOpacity, SpeechBubbleSpeakerOpacitySlider);
Control.AddOptionPercentSlider(CCVars.SpeechBubbleBackgroundOpacity, SpeechBubbleBackgroundOpacitySlider);
Control.AddOptionCheckBox(CCVars.ChatAutoFillHighlights, AutoFillHighlightsCheckBox);
Control.AddOptionColorSlider(CCVars.ChatHighlightsColor, HighlightsColorSlider);
Control.AddOptionCheckBox(CCVars.AccessibilityClientCensorNudity, CensorNudityCheckBox);

View File

@@ -0,0 +1,156 @@
using System.Linq;
using System.Text.RegularExpressions;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controllers;
using Content.Shared.CCVar;
using Content.Client.CharacterInfo;
using static Content.Client.CharacterInfo.CharacterInfoSystem;
namespace Content.Client.UserInterface.Systems.Chat;
/// <summary>
/// A partial class of ChatUIController that handles the saving and loading of highlights for the chatbox.
/// It also makes use of the CharacterInfoSystem to optionally generate highlights based on the character's info.
/// </summary>
public sealed partial class ChatUIController : IOnSystemChanged<CharacterInfoSystem>
{
[UISystemDependency] private readonly CharacterInfoSystem _characterInfo = default!;
/// <summary>
/// The list of words to be highlighted in the chatbox.
/// </summary>
private List<string> _highlights = new();
/// <summary>
/// The string holding the hex color used to highlight words.
/// </summary>
private string? _highlightsColor;
private bool _autoFillHighlightsEnabled;
/// <summary>
/// The boolean that keeps track of the 'OnCharacterUpdated' event, whenever it's a player attaching or opening the character info panel.
/// </summary>
private bool _charInfoIsAttach = false;
public event Action<string>? HighlightsUpdated;
private void InitializeHighlights()
{
_config.OnValueChanged(CCVars.ChatAutoFillHighlights, (value) => { _autoFillHighlightsEnabled = value; }, true);
_config.OnValueChanged(CCVars.ChatHighlightsColor, (value) => { _highlightsColor = value; }, true);
// Load highlights if any were saved.
string highlights = _config.GetCVar(CCVars.ChatHighlights);
if (!string.IsNullOrEmpty(highlights))
{
UpdateHighlights(highlights, true);
}
}
public void OnSystemLoaded(CharacterInfoSystem system)
{
system.OnCharacterUpdate += OnCharacterUpdated;
}
public void OnSystemUnloaded(CharacterInfoSystem system)
{
system.OnCharacterUpdate -= OnCharacterUpdated;
}
private void UpdateAutoFillHighlights()
{
if (!_autoFillHighlightsEnabled)
return;
// If auto highlights are enabled generate a request for new character info
// that will be used to determine the highlights.
_charInfoIsAttach = true;
_characterInfo.RequestCharacterInfo();
}
public void UpdateHighlights(string newHighlights, bool firstLoad = false)
{
// Do nothing if the provided highlights are the same as the old ones and it is not the first time.
if (!firstLoad && _config.GetCVar(CCVars.ChatHighlights).Equals(newHighlights, StringComparison.CurrentCultureIgnoreCase))
return;
_config.SetCVar(CCVars.ChatHighlights, newHighlights);
_config.SaveToFile();
_highlights.Clear();
// We first subdivide the highlights based on newlines to prevent replacing
// a valid "\n" tag and adding it to the final regex.
string[] splittedHighlights = newHighlights.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
for (int i = 0; i < splittedHighlights.Length; i++)
{
// Replace every "\" character with a "\\" to prevent "\n", "\0", etc...
string keyword = splittedHighlights[i].Replace(@"\", @"\\");
// Escape the keyword to prevent special characters like "(" and ")" to be considered valid regex.
keyword = Regex.Escape(keyword);
// 1. Since the "["s in WrappedMessage are already sanitized, add 2 extra "\"s
// to make sure it matches the literal "\" before the square bracket.
keyword = keyword.Replace(@"\[", @"\\\[");
// If present, replace the double quotes at the edges with tags
// that make sure the words to match are separated by spaces or punctuation.
// NOTE: The reason why we don't use \b tags is that \b doesn't match reverse slash characters "\" so
// a pre-sanitized (see 1.) string like "\[test]" wouldn't get picked up by the \b.
if (keyword.Count(c => (c == '"')) > 0)
{
// Matches the last double quote character.
keyword = Regex.Replace(keyword, "\"$", "(?!\\w)");
// When matching for the first double quote character we also consider the possibility
// of the double quote being preceded by a @ character.
keyword = Regex.Replace(keyword, "^\"|(?<=^@)\"", "(?<!\\w)");
}
// Make sure any name tagged as ours gets highlighted only when others say it.
keyword = Regex.Replace(keyword, "^@", "(?<=(?<=/name.*)|(?<=,.*\"\".*))");
_highlights.Add(keyword);
}
// Arrange the list of highlights in descending order so that when highlighting,
// the full word (eg. "Security") gets picked before the abbreviation (eg. "Sec").
_highlights.Sort((x, y) => y.Length.CompareTo(x.Length));
}
private void OnCharacterUpdated(CharacterData data)
{
// If _charInfoIsAttach is false then the opening of the character panel was the one
// to generate the event, dismiss it.
if (!_charInfoIsAttach)
return;
var (_, job, _, _, entityName) = data;
// Mark this entity's name as our character name for the "UpdateHighlights" function.
string newHighlights = "@" + entityName;
// Subdivide the character's name based on spaces or hyphens so that every word gets highlighted.
if (newHighlights.Count(c => (c == ' ' || c == '-')) == 1)
newHighlights = newHighlights.Replace("-", "\n@").Replace(" ", "\n@");
// If the character has a name with more than one hyphen assume it is a lizard name and extract the first and
// last name eg. "Eats-The-Food" -> "@Eats" "@Food"
if (newHighlights.Count(c => c == '-') > 1)
newHighlights = newHighlights.Split('-')[0] + "\n@" + newHighlights.Split('-')[^1];
// Convert the job title to kebab-case and use it as a key for the loc file.
string jobKey = job.Replace(' ', '-').ToLower();
if (Loc.TryGetString($"highlights-{jobKey}", out var jobMatches))
newHighlights += '\n' + jobMatches.Replace(", ", "\n");
UpdateHighlights(newHighlights);
HighlightsUpdated?.Invoke(newHighlights);
_charInfoIsAttach = false;
}
}

View File

@@ -41,9 +41,10 @@ using Robust.Shared.Replays;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
namespace Content.Client.UserInterface.Systems.Chat;
public sealed class ChatUIController : UIController
public sealed partial class ChatUIController : UIController
{
[Dependency] private readonly IClientAdminManager _admin = default!;
[Dependency] private readonly IChatManager _manager = default!;
@@ -240,6 +241,7 @@ public sealed class ChatUIController : UIController
_config.OnValueChanged(CCVars.ChatWindowOpacity, OnChatWindowOpacityChanged);
InitializeHighlights();
}
public void OnScreenLoad()
@@ -426,6 +428,8 @@ public sealed class ChatUIController : UIController
private void OnAttachedChanged(EntityUid uid)
{
UpdateChannelPermissions();
UpdateAutoFillHighlights();
}
private void AddSpeechBubble(ChatMessage msg, SpeechBubble.SpeechType speechType)
@@ -825,6 +829,12 @@ public sealed class ChatUIController : UIController
msg.WrappedMessage = SharedChatSystem.InjectTagInsideTag(msg, "Name", "color", GetNameColor(SharedChatSystem.GetStringInsideTag(msg, "Name")));
}
// Color any words chosen by the client.
foreach (var highlight in _highlights)
{
msg.WrappedMessage = SharedChatSystem.InjectTagAroundString(msg, highlight, "color", _highlightsColor);
}
// Color any codewords for minds that have roles that use them
if (_player.LocalUser != null && _mindSystem != null && _roleCodewordSystem != null)
{

View File

@@ -1,10 +1,22 @@
<controls:ChannelFilterPopup
xmlns="https://spacestation14.io"
xmlns:gfx="clr-namespace:Robust.Client.Graphics;assembly=Robust.Client"
xmlns:controls="clr-namespace:Content.Client.UserInterface.Systems.Chat.Controls">
<PanelContainer Name="FilterPopupPanel" StyleClasses="BorderedWindowPanel">
<BoxContainer Orientation="Horizontal">
<Control MinSize="4 0"/>
<BoxContainer Name="FilterVBox" MinWidth="110" Margin="0 10" Orientation="Vertical" SeparationOverride="4"/>
<BoxContainer Orientation="Horizontal" SeparationOverride="8" Margin="10 0">
<BoxContainer Name="FilterVBox" MinWidth="105" Margin="0 10" Orientation="Vertical" SeparationOverride="4"/>
<BoxContainer Name="HighlightsVBox" MinWidth="120" Margin="0 10" Orientation="Vertical" SeparationOverride="4">
<Label Text="{Loc 'hud-chatbox-highlights'}"/>
<PanelContainer>
<!-- Begin custom background for TextEdit -->
<PanelContainer.PanelOverride>
<gfx:StyleBoxFlat BackgroundColor="#323446"/>
</PanelContainer.PanelOverride>
<!-- End custom background -->
<TextEdit Name="HighlightEdit" MinHeight="150" Margin="5 5"/>
</PanelContainer>
<Button Name="HighlightButton" Text="{Loc 'hud-chatbox-highlights-button'}" ToolTip="{Loc 'hud-chatbox-highlights-tooltip'}"/>
</BoxContainer>
</BoxContainer>
</PanelContainer>
</controls:ChannelFilterPopup>

View File

@@ -1,4 +1,7 @@
using Content.Shared.Chat;
using Content.Shared.CCVar;
using Robust.Shared.Utility;
using Robust.Shared.Configuration;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.XAML;
@@ -29,10 +32,24 @@ public sealed partial class ChannelFilterPopup : Popup
private readonly Dictionary<ChatChannel, ChannelFilterCheckbox> _filterStates = new();
public event Action<ChatChannel, bool>? OnChannelFilter;
public event Action<string>? OnNewHighlights;
public ChannelFilterPopup()
{
RobustXamlLoader.Load(this);
HighlightButton.OnPressed += HighlightsEntered;
// Add a placeholder text to the highlights TextEdit.
HighlightEdit.Placeholder = new Rope.Leaf(Loc.GetString("hud-chatbox-highlights-placeholder"));
// Load highlights if any were saved.
var cfg = IoCManager.Resolve<IConfigurationManager>();
string highlights = cfg.GetCVar(CCVars.ChatHighlights);
if (!string.IsNullOrEmpty(highlights))
{
UpdateHighlights(highlights);
}
}
public bool IsActive(ChatChannel channel)
@@ -92,12 +109,22 @@ public sealed partial class ChannelFilterPopup : Popup
}
}
public void UpdateHighlights(string highlights)
{
HighlightEdit.TextRope = new Rope.Leaf(highlights);
}
private void CheckboxPressed(ButtonEventArgs args)
{
var checkbox = (ChannelFilterCheckbox) args.Button;
OnChannelFilter?.Invoke(checkbox.Channel, checkbox.Pressed);
}
private void HighlightsEntered(ButtonEventArgs _args)
{
OnNewHighlights?.Invoke(Rope.Collapse(HighlightEdit.TextRope));
}
public void UpdateUnread(ChatChannel channel, int? unread)
{
if (_filterStates.TryGetValue(channel, out var checkbox))

View File

@@ -38,9 +38,10 @@ public partial class ChatBox : UIWidget
ChatInput.Input.OnFocusExit += OnFocusExit;
ChatInput.ChannelSelector.OnChannelSelect += OnChannelSelect;
ChatInput.FilterButton.Popup.OnChannelFilter += OnChannelFilter;
ChatInput.FilterButton.Popup.OnNewHighlights += OnNewHighlights;
_controller = UserInterfaceManager.GetUIController<ChatUIController>();
_controller.MessageAdded += OnMessageAdded;
_controller.HighlightsUpdated += OnHighlightsUpdated;
_controller.RegisterChat(this);
}
@@ -67,6 +68,11 @@ public partial class ChatBox : UIWidget
AddLine(msg.WrappedMessage, color);
}
private void OnHighlightsUpdated(string highlights)
{
ChatInput.FilterButton.Popup.UpdateHighlights(highlights);
}
private void OnChannelSelect(ChatSelectChannel channel)
{
_controller.UpdateSelectedChannel(this);
@@ -97,6 +103,11 @@ public partial class ChatBox : UIWidget
}
}
private void OnNewHighlights(string highlighs)
{
_controller.UpdateHighlights(highlighs);
}
public void AddLine(string message, Color color)
{
var formatted = new FormattedMessage(3);

View File

@@ -65,4 +65,22 @@ public sealed partial class CCVars
"",
CVar.SERVER | CVar.SERVERONLY | CVar.ARCHIVE,
"A message broadcast to each player that joins the lobby.");
/// <summary>
/// A string containing a list of newline-separated words to be highlighted in the chat.
/// </summary>
public static readonly CVarDef<string> ChatHighlights =
CVarDef.Create("chat.highlights", "", CVar.CLIENTONLY | CVar.ARCHIVE, "A list of newline-separated words to be highlighted in the chat.");
/// <summary>
/// An option to toggle the automatic filling of the highlights with the character's info, if available.
/// </summary>
public static readonly CVarDef<bool> ChatAutoFillHighlights =
CVarDef.Create("chat.auto_fill_highlights", false, CVar.CLIENTONLY | CVar.ARCHIVE, "Toggles automatically filling the highlights with the character's information.");
/// <summary>
/// The color in which the highlights will be displayed.
/// </summary>
public static readonly CVarDef<string> ChatHighlightsColor =
CVarDef.Create("chat.highlights_color", "#17FFC1FF", CVar.CLIENTONLY | CVar.ARCHIVE, "The color in which the highlights will be displayed.");
}

View File

@@ -0,0 +1,57 @@
# Command
highlights-captain = Captain, "Cap", Bridge, Command
highlights-head-of-personnel = Head Of Personnel, "HoP", Service, Bridge, Command
highlights-chief-engineer = Chief Engineer, "CE", Engineering, Engineer, "Engi", Bridge, Command
highlights-chief-medical-officer = Chief Medical Officer, "CMO", MedBay, "Med", Bridge, Command
highlights-head-of-security = Head of Security, "HoS", Security, "Sec", Bridge, Command
highlights-quartermaster = Quartermaster, "QM", Cargo, Bridge, Command
highlights-research-director = Research Director, "RD", Science, "Sci", Bridge, Command
# Security
highlights-detective = Detective, "Det", Security, "Sec"
highlights-security-cadet = Security Cadet, Secoff, Cadet, Security, "Sec"
highlights-security-officer = Security Officer, Secoff, Officer, Security, "Sec"
highlights-warden = Warden, "Ward", Security, "Sec"
# Cargo
highlights-cargo-technician = Cargo Technician, Cargo Tech, "Cargo"
highlights-salvage-specialist = Salvage Specialist, Salvager, Salvage, "Salv", "Cargo", Miner
# Engineering
highlights-atmospheric-technician = Atmospheric Technician, Atmos tech, Atmospheric, Engineering, "Atmos", "Engi"
highlights-station-engineer = Station Engineer, Engineering, Engineer, "Engi"
highlights-technical-assistant = Technical Assistant, Tech Assistant, Engineering, Engineer, "Engi"
# Medical
highlights-chemist = Chemist, Chemistry, "Chem", MedBay, "Med"
highlights-medical-doctor = Medical Doctor, Doctor, "Doc", MedBay, "Med"
highlights-medical-intern = Medical Intern, "Doc", Intern, MedBay, "Med"
highlights-paramedic = Paramedic, "Para", MedBay, "Med"
# Science
highlights-scientist = Scientist, Science, "Sci"
highlights-research-assistant = Research Assistant, Science, "Sci"
# Civilian
highlights-bartender = Bartender, Barkeeper, Barkeep, "Bar"
highlights-botanist = Botanist, Botany, Hydroponics
highlights-chaplain = Chaplain, "Chap", Chapel
highlights-chef = Chef, "Cook", Kitchen
highlights-clown = Clown, Jester
highlights-janitor = Janitor, "Jani"
highlights-lawyer = Lawyer, Attorney
highlights-librarian = Librarian, Library
highlights-mime = Mime
highlights-passenger = Passenger, Greytider, "Tider"
highlights-service-worker = Service Worker
# Station-specific
highlights-boxer = Boxer
highlights-reporter = Reporter, Journalist
highlights-zookeeper = Zookeeper
highlights-psychologist = Psychologist, Psychology
# Silicon
highlights-personal-ai = Personal AI, "pAI"
highlights-cyborg = Cyborg, Silicon, Borg
highlights-station-ai = Station AI, Silicon, "AI", "sAI"

View File

@@ -31,3 +31,12 @@ hud-chatbox-channel-Server = Server
hud-chatbox-channel-Visual = Actions
hud-chatbox-channel-Damage = Damage
hud-chatbox-channel-Unspecified = Unspecified
hud-chatbox-highlights = Highlights:
hud-chatbox-highlights-button = Submit
hud-chatbox-highlights-tooltip = The words need to be separated by a newline,
if wrapped around " they will be highlighted
only if separated by spaces or punctuation.
hud-chatbox-highlights-placeholder = McHands
"Judge"
Medical

View File

@@ -49,6 +49,9 @@ ui-options-misc-label = Misc
ui-options-interface-label = Interface
ui-options-auto-fill-highlights = Auto-fill the highlights with the character's information
ui-options-highlights-color = Highlighs color:
ui-options-highlights-color-example = This is an highlighted text!
ui-options-show-held-item = Show held item next to cursor
ui-options-show-combat-mode-indicators = Show combat mode indicators with cursor
ui-options-opaque-storage-window = Opaque storage window