From da289b8f1c540d2c568636f2828cd0116609cb04 Mon Sep 17 00:00:00 2001 From: Twirlbug Date: Sun, 2 Jul 2023 16:09:07 -0500 Subject: [PATCH] Reushing the file rearangement --- SRMP/Custom UI/ChatUI.cs | 58 +++++++++++++++++++++++++++++++++------- SRMP/SRMP.csproj | 15 +++++------ 2 files changed, 55 insertions(+), 18 deletions(-) diff --git a/SRMP/Custom UI/ChatUI.cs b/SRMP/Custom UI/ChatUI.cs index 9518920..7f10ffa 100644 --- a/SRMP/Custom UI/ChatUI.cs +++ b/SRMP/Custom UI/ChatUI.cs @@ -13,7 +13,11 @@ public class ChatUI : SRSingleton private float fadeTime; private List messages = new List(); private Vector2 chatScroll; + int maxWidth = 290; + /// + /// create a chat message to be displayed + /// public class ChatMessage { public string Text; @@ -27,52 +31,68 @@ public class ChatUI : SRSingleton Time = DateTime.Now; } } - + /// + /// On chat ui update triggered, handle it + /// private void Update() { + //only display chat if in multiplayer mode if (!Globals.IsMultiplayer) { openChat = false; message = ""; return; } - + //if enter is pressed start chat typing mode if (Input.GetKeyUp(KeyCode.Return)) { StartCoroutine(FocusChat()); } } - + /// + /// Crreate the Chat gui + /// private void OnGUI() { + //only mess with the gui in multiplayer mode if (!Globals.IsMultiplayer) return; if (openChat) { - GUILayout.BeginArea(new Rect(20, Screen.height / 2, 500, 300), GUI.skin.box); + //draw chat area + GUILayout.BeginArea(new Rect(20, Screen.height / 2, maxWidth + 10, 300), GUI.skin.box); chatScroll = GUILayout.BeginScrollView(chatScroll); var skin = GUI.skin.box; skin.wordWrap = true; skin.alignment = TextAnchor.MiddleLeft; + + //add each mesage into the chat box scroller foreach (var msg in messages) { - GUILayout.Label(wrapString(msg.Text, 490), skin, GUILayout.MaxWidth(490)); + GUILayout.Label(wrapString(msg.Text, maxWidth), skin, GUILayout.MaxWidth(maxWidth)); } GUILayout.EndScrollView(); + + //add display for text input area GUI.SetNextControlName("ChatInput"); message = GUILayout.TextField(message); GUILayout.EndArea(); + //focus the input GUI.FocusControl("ChatInput"); + //watch for changes to the text input Event e = Event.current; if (e.rawType == EventType.KeyUp && e.keyCode == KeyCode.Return) { + //on send close chat openChat = !openChat; if (!string.IsNullOrWhiteSpace(message)) { + //if server send message to all plauers if (Globals.IsServer) { + //if server send it to the server AddChatMessage(Globals.Username + ": " + message); new PacketPlayerChat() { @@ -81,6 +101,7 @@ public class ChatUI : SRSingleton } else { + //if player send it to the server new PacketPlayerChat() { message = message @@ -90,13 +111,16 @@ public class ChatUI : SRSingleton } } } - else + else //if chat isnt open yet { - GUILayout.BeginArea(new Rect(20, Screen.height / 2, 500, 300)); + //draw chat area + GUILayout.BeginArea(new Rect(20, Screen.height / 2, maxWidth+ 10, 300)); chatScroll = GUILayout.BeginScrollView(chatScroll); var skin = GUI.skin.box; skin.wordWrap = true; skin.alignment = TextAnchor.MiddleLeft; + + //add each mesage into the chat box scroller foreach (var msg in messages) { if (msg.FadeTime > 0f) @@ -105,26 +129,34 @@ public class ChatUI : SRSingleton var c = GUI.color; c.a = (msg.FadeTime / 5f); GUI.color = c; - GUILayout.Label(wrapString(msg.Text, 490), skin, GUILayout.MaxWidth(490)); + GUILayout.Label(wrapString(msg.Text, maxWidth), skin, GUILayout.MaxWidth(maxWidth)); } } GUILayout.EndScrollView(); GUILayout.EndArea(); } } - + /// + /// Add a chat message to be displayed + /// + /// Message to display public void AddChatMessage(string message) { fadeTime = 10f; messages.Add(new ChatMessage(message)); chatScroll = new Vector2(0, 100000); } - + /// + /// Trigger a full clear of the chat + /// public void Clear() { messages.Clear(); } + /// + /// Trigger focus on the chat box for when the uer hits enter + /// private IEnumerator FocusChat() { yield return new WaitForEndOfFrame(); @@ -133,6 +165,12 @@ public class ChatUI : SRSingleton GUI.FocusControl("ChatInput"); } + /// + /// Create the wrapped string to display in the chat box + /// + /// Message to display + /// Width of the chat box + /// string wrapString(string msg, int width) { string[] words = msg.Split(" "[0]); diff --git a/SRMP/SRMP.csproj b/SRMP/SRMP.csproj index 57e1473..3e340f8 100644 --- a/SRMP/SRMP.csproj +++ b/SRMP/SRMP.csproj @@ -160,11 +160,11 @@ - - - + + + @@ -242,7 +242,6 @@ - @@ -476,7 +475,6 @@ - True True @@ -484,9 +482,10 @@ - - - + + + +