From 4ab15e74e6c3fc71c5d53799ddb36e781aea4f74 Mon Sep 17 00:00:00 2001 From: _nebula <41904486+misternebula@users.noreply.github.com> Date: Sun, 9 Jul 2023 21:19:52 +0100 Subject: [PATCH] add command intepreter and message scrolling --- QSB/HUD/MultiplayerHUDManager.cs | 40 ++++++++++++++++- QSB/Utility/CommandInterpreter.cs | 74 +++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 QSB/Utility/CommandInterpreter.cs diff --git a/QSB/HUD/MultiplayerHUDManager.cs b/QSB/HUD/MultiplayerHUDManager.cs index 2970b741..fe698ba1 100644 --- a/QSB/HUD/MultiplayerHUDManager.cs +++ b/QSB/HUD/MultiplayerHUDManager.cs @@ -9,6 +9,7 @@ using QSB.WorldSync; using System; using System.Linq; using UnityEngine; +using UnityEngine.InputSystem; using UnityEngine.SceneManagement; using UnityEngine.UI; @@ -157,6 +158,8 @@ internal class MultiplayerHUDManager : MonoBehaviour, IAddComponentOnStart _textChat.GetComponent().alpha = 1; } + ListStack previousMessages = new(true); + private void Update() { if (!QSBWorldSync.AllObjectsReady || _playerList == null) @@ -168,12 +171,39 @@ internal class MultiplayerHUDManager : MonoBehaviour, IAddComponentOnStart var inSuit = Locator.GetPlayerSuit().IsWearingHelmet(); - if (OWInput.IsNewlyPressed(InputLibrary.enter, InputMode.Character) && !_writingMessage && inSuit && QSBCore.TextChatInput) + if ((OWInput.IsNewlyPressed(InputLibrary.enter, InputMode.Character) || (Keyboard.current[Key.Slash].wasPressedThisFrame && OWInput.IsInputMode(InputMode.Character))) + && !_writingMessage && inSuit && QSBCore.TextChatInput) { OWInput.ChangeInputMode(InputMode.KeyboardInput); _writingMessage = true; _inputField.ActivateInputField(); _textChat.GetComponent().alpha = 1; + + if (Keyboard.current[Key.Slash].wasPressedThisFrame) + { + Delay.RunNextFrame(() => _inputField.text = "/"); + } + } + + if (Keyboard.current[Key.UpArrow].wasPressedThisFrame && _writingMessage) + { + var currentText = _inputField.text; + + if (previousMessages.Contains(currentText)) + { + var index = previousMessages.IndexOf(currentText); + + if (index == 0) + { + return; + } + + _inputField.text = previousMessages[index - 1]; + } + else + { + _inputField.text = previousMessages.Last(); + } } if (OWInput.IsNewlyPressed(InputLibrary.enter, InputMode.KeyboardInput) && _writingMessage) @@ -185,6 +215,14 @@ internal class MultiplayerHUDManager : MonoBehaviour, IAddComponentOnStart var message = _inputField.text; _inputField.text = ""; message = message.Replace("\n", "").Replace("\r", ""); + + previousMessages.Push(message); + + if (QSBCore.DebugSettings.DebugMode && CommandInterpreter.InterpretCommand(message)) + { + return; + } + message = $"{QSBPlayerManager.LocalPlayer.Name}: {message}"; new ChatMessage(message, Color.white).Send(); } diff --git a/QSB/Utility/CommandInterpreter.cs b/QSB/Utility/CommandInterpreter.cs new file mode 100644 index 00000000..f402d559 --- /dev/null +++ b/QSB/Utility/CommandInterpreter.cs @@ -0,0 +1,74 @@ +using QSB.HUD; +using QSB.Messaging; +using QSB.ShipSync; +using QSB.ShipSync.Messages; +using QSB.WorldSync; +using System.Linq; +using UnityEngine; + +namespace QSB.Utility; + +public class CommandInterpreter : MonoBehaviour, IAddComponentOnStart +{ + public static bool InterpretCommand(string message) + { + if (message[0] != '/') + { + return false; + } + + var commandParts = message.ToLower().Substring(1).Split(' '); + var command = commandParts[0]; + + switch (command) + { + case "ship": + ShipCommand(commandParts.Skip(1).ToArray()); + break; + default: + MultiplayerHUDManager.Instance.WriteMessage($"Unknown command \"{command}\".", Color.red); + break; + } + + return true; + } + + public static void ShipCommand(string[] arguments) + { + var command = arguments[0]; + + switch (command) + { + case "explode": + MultiplayerHUDManager.Instance.WriteMessage($"Blowing up the ship.", Color.green); + var shipDamageController = Locator.GetShipTransform().GetComponentInChildren(); + shipDamageController.Explode(); + break; + case "repair": + case "damage": + var damage = command == "damage"; + switch (arguments[1]) + { + case "headlight": + var headlight = QSBWorldSync.GetUnityObject(); + headlight.SetDamaged(damage); + break; + default: + break; + } + MultiplayerHUDManager.Instance.WriteMessage($"{(damage ? "Damaging" : "Repairing")} the {arguments[1]}.", Color.green); + break; + case "open-hatch": + QSBWorldSync.GetUnityObject().OpenHatch(); + new HatchMessage(true).Send(); + break; + case "close-hatch": + QSBWorldSync.GetUnityObject().CloseHatch(); + new HatchMessage(false).Send(); + break; + default: + MultiplayerHUDManager.Instance.WriteMessage($"Unknown ship command \"{command}\".", Color.red); + break; + } + } +}