Skip to content

Commit

Permalink
Added server command handling back to in-game chat (#1044)
Browse files Browse the repository at this point in the history
* Added server command handling back to in-game chat

* Removed redundant check
  • Loading branch information
Measurity authored Apr 18, 2020
1 parent b73d55d commit 8ec8989
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
23 changes: 19 additions & 4 deletions NitroxClient/GameLogic/ChatUI/PlayerChatManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public PlayerChatManager(IMultiplayerSession multiplayerSession)
private PlayerChat playerChat;
public Transform PlayerChaTransform => playerChat.transform;
private readonly IMultiplayerSession multiplayerSession;
private const char SERVER_COMMAND_PREFIX = '/';

public void ShowChat() => Player.main.StartCoroutine(ShowChatAsync());
private IEnumerator ShowChatAsync()
Expand Down Expand Up @@ -51,12 +52,26 @@ private IEnumerator AddMessageAsync(string playerName, string message, Color col

public void SendMessage()
{
if (playerChat.inputText.Trim() != "")
if (string.IsNullOrWhiteSpace(playerChat.InputText))
{
multiplayerSession.Send(new ChatMessage(multiplayerSession.Reservation.PlayerId, playerChat.inputText));
playerChat.WriteLogEntry(multiplayerSession.AuthenticationContext.Username, playerChat.inputText, multiplayerSession.PlayerSettings.PlayerColor);
playerChat.inputText = "";
playerChat.Select();
return;
}

string trimmedInput = playerChat.InputText.Trim();
if (trimmedInput[0] == SERVER_COMMAND_PREFIX)
{
// Server command
multiplayerSession.Send(new ServerCommand(trimmedInput.Substring(1)));
playerChat.InputText = "";
playerChat.Select();
return;
}

// Chat message
multiplayerSession.Send(new ChatMessage(multiplayerSession.Reservation.PlayerId, trimmedInput));
playerChat.WriteLogEntry(multiplayerSession.AuthenticationContext.Username, playerChat.InputText, multiplayerSession.PlayerSettings.PlayerColor);
playerChat.InputText = "";
playerChat.Select();
}

Expand Down
27 changes: 12 additions & 15 deletions NitroxClient/MonoBehaviours/Gui/Chat/PlayerChat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using NitroxClient.GameLogic.ChatUI;
using NitroxModel.Core;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace NitroxClient.MonoBehaviours.Gui.Chat
Expand All @@ -16,29 +15,29 @@ public class PlayerChat : uGUI_InputGroup
private const float TOGGLED_TRANSPARENCY = 0.4f;
public const float CHAT_VISIBILITY_TIME_LENGTH = 10f;

public static bool IsReady { get; private set; }

private PlayerChatManager playerChatManager;
private static readonly Queue<ChatLogEntry> entries = new Queue<ChatLogEntry>();
private Image[] backgroundImages;
private CanvasGroup canvasGroup;
private HorizontalOrVerticalLayoutGroup[] layoutGroups;
private InputField inputField;
private GameObject logEntryPrefab;
private Image[] backgroundImages;

private PlayerChatManager playerChatManager;
private bool transparent;
private InputField inputField;
public string inputText

public static bool IsReady { get; private set; }

public string InputText
{
get { return inputField.text; }
set { inputField.text = value; }
}

private static readonly Queue<ChatLogEntry> entries = new Queue<ChatLogEntry>();

public IEnumerator SetupChatComponents()
{
playerChatManager = NitroxServiceLocator.LocateService<PlayerChatManager>();

canvasGroup = GetComponent<CanvasGroup>();
layoutGroups = GetComponentsInChildren<HorizontalOrVerticalLayoutGroup>();
GetComponentsInChildren<HorizontalOrVerticalLayoutGroup>();

logEntryPrefab = GameObject.Find("ChatLogEntryPrefab");
logEntryPrefab.AddComponent<PlayerChatLogItem>();
Expand All @@ -51,10 +50,7 @@ public IEnumerator SetupChatComponents()
inputField.gameObject.AddComponent<PlayerChatInputField>().InputField = inputField;
inputField.GetComponentInChildren<Button>().onClick.AddListener(playerChatManager.SendMessage);

backgroundImages = new[]
{
transform.GetChild(0).GetComponent<Image>(), transform.GetChild(1).GetComponent<Image>(), transform.GetChild(3).GetComponent<Image>()
};
backgroundImages = new[] { transform.GetChild(0).GetComponent<Image>(), transform.GetChild(1).GetComponent<Image>(), transform.GetChild(3).GetComponent<Image>() };

yield return new WaitForEndOfFrame(); //Needed so Select() works on initialization
IsReady = true;
Expand Down Expand Up @@ -92,6 +88,7 @@ public void Show()
PlayerChatInputField.ResetTimer();
StartCoroutine(ToggleChatFade(true));
}

public void Hide()
{
StartCoroutine(ToggleChatFade(false));
Expand Down

0 comments on commit 8ec8989

Please sign in to comment.