Re-implement chat in content. (#198)
* OOC is a word. * Re-implement chat in content.
This commit is contained in:
committed by
GitHub
parent
51caae7ebe
commit
52af7d27da
157
Content.Client/Chat/ChatBox.cs
Normal file
157
Content.Client/Chat/ChatBox.cs
Normal file
@@ -0,0 +1,157 @@
|
||||
using System.Collections.Generic;
|
||||
using Content.Shared.Chat;
|
||||
using SS14.Client.Console;
|
||||
using SS14.Client.Graphics.Drawing;
|
||||
using SS14.Client.Input;
|
||||
using SS14.Client.UserInterface;
|
||||
using SS14.Client.UserInterface.Controls;
|
||||
using SS14.Shared.Maths;
|
||||
using SS14.Shared.Utility;
|
||||
|
||||
namespace Content.Client.Chat
|
||||
{
|
||||
public class ChatBox : PanelContainer
|
||||
{
|
||||
protected override ResourcePath ScenePath => new ResourcePath("/Scenes/ChatBox/ChatBox.tscn");
|
||||
|
||||
public delegate void TextSubmitHandler(ChatBox chatBox, string text);
|
||||
|
||||
private const int MaxLinePixelLength = 500;
|
||||
|
||||
private readonly IList<string> _inputHistory = new List<string>();
|
||||
|
||||
public LineEdit Input { get; private set; }
|
||||
private OutputPanel contents;
|
||||
|
||||
/// <summary>
|
||||
/// Index while cycling through the input history. -1 means not going through history.
|
||||
/// </summary>
|
||||
private int _inputIndex = -1;
|
||||
|
||||
/// <summary>
|
||||
/// Message that WAS being input before going through history began.
|
||||
/// </summary>
|
||||
private string _inputTemp;
|
||||
|
||||
/// <summary>
|
||||
/// Default formatting string for the ClientChatConsole.
|
||||
/// </summary>
|
||||
public string DefaultChatFormat { get; set; }
|
||||
|
||||
protected override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
Input = GetChild<LineEdit>("VBoxContainer/Input");
|
||||
Input.OnKeyDown += InputKeyDown;
|
||||
Input.OnTextEntered += Input_OnTextEntered;
|
||||
GetChild<Control>("VBoxContainer/Contents").Dispose();
|
||||
|
||||
contents = new OutputPanel
|
||||
{
|
||||
SizeFlagsVertical = SizeFlags.FillExpand,
|
||||
};
|
||||
GetChild("VBoxContainer").AddChild(contents);
|
||||
contents.SetPositionInParent(0);
|
||||
|
||||
PanelOverride = new StyleBoxFlat {BackgroundColor = Color.Gray.WithAlpha(0.5f)};
|
||||
}
|
||||
|
||||
protected override void MouseDown(GUIMouseButtonEventArgs e)
|
||||
{
|
||||
base.MouseDown(e);
|
||||
|
||||
Input.GrabKeyboardFocus();
|
||||
}
|
||||
|
||||
private void InputKeyDown(GUIKeyEventArgs e)
|
||||
{
|
||||
if (e.Key == Keyboard.Key.Escape)
|
||||
{
|
||||
Input.ReleaseKeyboardFocus();
|
||||
e.Handle();
|
||||
return;
|
||||
}
|
||||
|
||||
if (e.Key == Keyboard.Key.Up)
|
||||
{
|
||||
if (_inputIndex == -1 && _inputHistory.Count != 0)
|
||||
{
|
||||
_inputTemp = Input.Text;
|
||||
_inputIndex++;
|
||||
}
|
||||
else if (_inputIndex + 1 < _inputHistory.Count)
|
||||
{
|
||||
_inputIndex++;
|
||||
}
|
||||
|
||||
if (_inputIndex != -1)
|
||||
{
|
||||
Input.Text = _inputHistory[_inputIndex];
|
||||
}
|
||||
|
||||
e.Handle();
|
||||
return;
|
||||
}
|
||||
|
||||
if (e.Key == Keyboard.Key.Down)
|
||||
{
|
||||
if (_inputIndex == 0)
|
||||
{
|
||||
Input.Text = _inputTemp;
|
||||
_inputTemp = "";
|
||||
_inputIndex--;
|
||||
}
|
||||
else if (_inputIndex != -1)
|
||||
{
|
||||
_inputIndex--;
|
||||
Input.Text = _inputHistory[_inputIndex];
|
||||
}
|
||||
|
||||
e.Handle();
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
base.Dispose(disposing);
|
||||
|
||||
if (disposing)
|
||||
{
|
||||
TextSubmitted = null;
|
||||
Input = null;
|
||||
contents = null;
|
||||
}
|
||||
}
|
||||
|
||||
public event TextSubmitHandler TextSubmitted;
|
||||
|
||||
public void AddLine(string message, ChatChannel channel, Color color)
|
||||
{
|
||||
if (Disposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var formatted = new FormattedMessage(3);
|
||||
formatted.PushColor(color);
|
||||
formatted.AddText(message);
|
||||
formatted.Pop();
|
||||
contents.AddMessage(formatted);
|
||||
}
|
||||
|
||||
private void Input_OnTextEntered(LineEdit.LineEditEventArgs args)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(args.Text))
|
||||
{
|
||||
TextSubmitted?.Invoke(this, args.Text);
|
||||
_inputHistory.Insert(0, args.Text);
|
||||
}
|
||||
|
||||
_inputIndex = -1;
|
||||
|
||||
Input.Clear();
|
||||
Input.ReleaseKeyboardFocus();
|
||||
}
|
||||
}
|
||||
}
|
||||
110
Content.Client/Chat/ChatManager.cs
Normal file
110
Content.Client/Chat/ChatManager.cs
Normal file
@@ -0,0 +1,110 @@
|
||||
using System;
|
||||
using Content.Client.Interfaces.Chat;
|
||||
using Content.Shared.Chat;
|
||||
using SS14.Client.Console;
|
||||
using SS14.Shared.Interfaces.GameObjects;
|
||||
using SS14.Shared.Interfaces.Network;
|
||||
using SS14.Shared.IoC;
|
||||
using SS14.Shared.Log;
|
||||
using SS14.Shared.Maths;
|
||||
using SS14.Shared.Utility;
|
||||
|
||||
namespace Content.Client.Chat
|
||||
{
|
||||
internal sealed class ChatManager : IChatManager
|
||||
{
|
||||
private const char ConCmdSlash = '/';
|
||||
private const char OOCAlias = '[';
|
||||
private const char MeAlias = '@';
|
||||
|
||||
#pragma warning disable 649
|
||||
[Dependency] private readonly IClientNetManager _netManager;
|
||||
[Dependency] private readonly IClientConsole _console;
|
||||
[Dependency] private readonly IEntityManager _entityManager;
|
||||
#pragma warning restore 649
|
||||
|
||||
private ChatBox _currentChatBox;
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
_netManager.RegisterNetMessage<MsgChatMessage>(MsgChatMessage.NAME, _onChatMessage);
|
||||
}
|
||||
|
||||
public void SetChatBox(ChatBox chatBox)
|
||||
{
|
||||
if (_currentChatBox != null)
|
||||
{
|
||||
_currentChatBox.TextSubmitted -= _onChatBoxTextSubmitted;
|
||||
}
|
||||
|
||||
_currentChatBox = chatBox;
|
||||
if (_currentChatBox != null)
|
||||
{
|
||||
_currentChatBox.TextSubmitted += _onChatBoxTextSubmitted;
|
||||
}
|
||||
}
|
||||
|
||||
private void _onChatMessage(MsgChatMessage message)
|
||||
{
|
||||
Logger.Debug($"{message.Channel}: {message.Message}");
|
||||
|
||||
var color = Color.DarkGray;
|
||||
var messageText = message.Message;
|
||||
if (!string.IsNullOrEmpty(message.MessageWrap))
|
||||
{
|
||||
messageText = string.Format(message.MessageWrap, messageText);
|
||||
}
|
||||
|
||||
switch (message.Channel)
|
||||
{
|
||||
case ChatChannel.Server:
|
||||
color = Color.Orange;
|
||||
break;
|
||||
case ChatChannel.OOC:
|
||||
color = Color.LightSkyBlue;
|
||||
break;
|
||||
}
|
||||
|
||||
_currentChatBox?.AddLine(messageText, message.Channel, color);
|
||||
}
|
||||
|
||||
private void _onChatBoxTextSubmitted(ChatBox chatBox, string text)
|
||||
{
|
||||
DebugTools.Assert(chatBox == _currentChatBox);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(text))
|
||||
return;
|
||||
|
||||
switch (text[0])
|
||||
{
|
||||
case ConCmdSlash:
|
||||
{
|
||||
// run locally
|
||||
var conInput = text.Substring(1);
|
||||
_console.ProcessCommand(conInput);
|
||||
break;
|
||||
}
|
||||
case OOCAlias:
|
||||
{
|
||||
var conInput = text.Substring(1);
|
||||
_console.ProcessCommand($"ooc \"{conInput}\"");
|
||||
break;
|
||||
}
|
||||
case MeAlias:
|
||||
{
|
||||
var conInput = text.Substring(1);
|
||||
_console.ProcessCommand($"me \"{conInput}\"");
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
var conInput = _currentChatBox.DefaultChatFormat != null
|
||||
? string.Format(_currentChatBox.DefaultChatFormat, text)
|
||||
: text;
|
||||
_console.ProcessCommand(conInput);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user