- add: Abbreviation helper (#435)

This commit is contained in:
Cinkafox
2024-07-11 19:38:35 +03:00
committed by GitHub
parent 8defed4796
commit 7f92c00d3c
6 changed files with 254 additions and 0 deletions

View File

@@ -1,3 +1,4 @@
using Content.Client._White.Chat;
using Content.Client._White.JoinQueue;
using Content.Client._White.Jukebox;
using Content.Client._White.Overlays;
@@ -86,6 +87,7 @@ namespace Content.Client.Entry
[Dependency] private readonly ClientJukeboxSongsSyncManager _jukeboxSyncManager = default!;
[Dependency] private readonly TTSManager _ttsManager = default!;
[Dependency] private readonly ReputationManager _reputationManager = default!;
[Dependency] private readonly IChatAbbreviationManager _chatAbbreviationManager = default!;
//WD-EDIT
public override void Init()
@@ -154,6 +156,7 @@ namespace Content.Client.Entry
//WD-EDIT
_stalinManager.Initialize();
_chatAbbreviationManager.Initialize();
//WD-EDIT
//AUTOSCALING default Setup!

View File

@@ -1,3 +1,4 @@
using Content.Client._White.Chat;
using Content.Client._White.JoinQueue;
using Content.Client._White.Jukebox;
using Content.Client._White.Reputation;
@@ -61,6 +62,7 @@ namespace Content.Client.IoC
IoCManager.Register<TTSManager>();
IoCManager.Register<ITrailLineManager, TrailSplineManager>();
IoCManager.Register<ReputationManager>();
IoCManager.Register<IChatAbbreviationManager,ChatAbbreviationManager>();
//WD-EDIT
}
}

View File

@@ -41,6 +41,8 @@ public partial class ChatBox : UIWidget
_controller = UserInterfaceManager.GetUIController<ChatUIController>();
_controller.MessageAdded += OnMessageAdded;
_controller.RegisterChat(this);
InitializeExtension(); //WD EDIT
}
private void OnTextEntered(LineEditEventArgs args)

View File

@@ -0,0 +1,61 @@
using System.Linq;
using Robust.Client.ResourceManagement;
using Robust.Shared.Serialization.Manager;
using Robust.Shared.Serialization.Markdown;
using Robust.Shared.Serialization.Markdown.Mapping;
using Robust.Shared.Utility;
namespace Content.Client._White.Chat;
public sealed class ChatAbbreviationManager : IChatAbbreviationManager
{
[Dependency] private readonly IResourceCache _resources = default!;
[Dependency] private readonly ISerializationManager _serializationManager = default!;
public readonly string SuggestSeparator = "%";
public List<AbbreviatedWord> Worlds = new List<AbbreviatedWord>();
public void Initialize()
{
LoadWords(new ResPath("/White/ChatFilters/slang.yml"));
}
public void LoadWords(ResPath resPath)
{
try
{
var yaml = _resources.ContentFileReadYaml(resPath);
var node = yaml.Documents[0].RootNode.ToDataNodeCast<MappingDataNode>();
var data = _serializationManager.Read<Dictionary<string, string>>(node, notNullableOverride : false);
Worlds = data.Select(s => new AbbreviatedWord(s.Key, s.Value)).ToList();
}
catch (Exception e)
{
Logger.Error($"Shit happened!: {e}");
}
}
public (List<AbbreviatedWord>, string) GetSuggestWord(string input)
{
var splited = input.Split(SuggestSeparator);
if (splited.Length <= 1)
return (new List<AbbreviatedWord>(), string.Empty);
splited = splited.Last().Split(" ");
if (splited.Length > 1)
return (new List<AbbreviatedWord>(), string.Empty);
var currentAbbreviation = splited[0];
Logger.Debug($"Current shit: {currentAbbreviation}");
return (Worlds.Where(a => a.Short.StartsWith(SuggestSeparator+currentAbbreviation)).ToList(),currentAbbreviation);
}
}
public interface IChatAbbreviationManager
{
public void Initialize();
public void LoadWords(ResPath resPath);
public (List<AbbreviatedWord>, string) GetSuggestWord(string input);
}
public record struct AbbreviatedWord(string Short, string Word);

View File

@@ -0,0 +1,131 @@
using System.Linq;
using System.Numerics;
using System.Text;
using Content.Client._White.Chat;
using Robust.Client.Graphics;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Shared.Utility;
namespace Content.Client.UserInterface.Systems.Chat.Widgets; // НЕ ТРОГАТЬ НАХУЙ! ФИЧА БЛЯТЬ!
#pragma warning disable RA0003
public partial class ChatBox
#pragma warning restore RA0003
{
[Dependency] private readonly IChatAbbreviationManager _chatAbbreviationManager = default!;
private readonly RichTextLabel _currentLabel = new();
private readonly FormattedMessage _currentMessage = new();
private readonly RichTextLabel _fullLabel = new();
private readonly FormattedMessage _fullMessage = new();
public void InitializeExtension()
{
ChatInput.Input.OnTextChanged += InputTextChanged;
ChatInput.Input.OnFocusEnter += OnEnter;
ChatInput.Input.OnFocusExit += OnExit;
ChatInput.Input.OnTextEntered += InputTextEntered;
_currentLabel.SetMessage(_currentMessage);
_fullLabel.SetMessage(_fullMessage);
ChatInput.AddChild(_currentLabel);
ChatInput.AddChild(_fullLabel);
}
private void InputTextEntered(LineEdit.LineEditEventArgs obj)
{
_currentMessage.Clear();
_fullMessage.Clear();
}
private void OnEnter(LineEdit.LineEditEventArgs obj)
{
_currentLabel.Visible = true;
_fullLabel.Visible = true;
}
private void OnExit(LineEdit.LineEditEventArgs obj)
{
_currentLabel.Visible = false;
_fullLabel.Visible = false;
}
private void InputTextChanged(LineEdit.LineEditEventArgs args)
{
_currentMessage.Clear();
_fullMessage.Clear();
_currentLabel.Visible = false;
_fullLabel.Visible = false;
var font = GetFont(args.Control);
var cursorPosShift = UIScale * 0f;
var runes = args.Text.EnumerateRunes().ToArray();
var (words,suggest) = _chatAbbreviationManager.GetSuggestWord(args.Text);
if (words.Count == 0)
return;
var count = 0;
var posX = 0;
foreach (var rune in runes)
{
if (!font.TryGetCharMetrics(rune, UIScale, out var metrics))
{
count += 1;
continue;
}
posX += metrics.Advance;
count += rune.Utf16SequenceLength;
if (count == args.Control.CursorPosition)
{
cursorPosShift = posX;
}
}
_currentLabel.Margin = new Thickness(ChatInput.Input.Position.X + cursorPosShift,0,0,0);
_currentLabel.InvalidateMeasure();
_fullLabel.Margin = new Thickness(ChatInput.Input.Position.X + cursorPosShift,0,0,0);
_fullLabel.InvalidateMeasure();
var limit = 0;
foreach (var word in words)
{
limit++;
if(limit > 4) continue;
if (!_currentLabel.Visible)
_currentLabel.Visible = true;
if (!_fullLabel.Visible)
_fullLabel.Visible = true;
_currentMessage.AddMarkup($"[color=#aaaaaa]{word.Short.Substring(suggest.Length+1)}[/color]\r");
}
if (words.Count > 0)
{
_fullMessage.PushNewline();
_fullMessage.AddMarkup($"[font size=8][color=#aaaaaa]{words[0].Word}[/color][/font]\r");
}
}
private Font GetFont(Control element)
{
if (element.TryGetStyleProperty<Font>("font", out var font))
{
return font;
}
return UserInterfaceManager.ThemeDefaults.DefaultFont;
}
}