-
Notifications
You must be signed in to change notification settings - Fork 371
Commit
* Initial chat commit * Disable input when chat is opened * Maximum messages limit * Fading * Final touches
- Loading branch information
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,167 @@ | ||
using System.Collections.Generic; | ||
using SanAndreasUnity.Utilities; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using TMPro; | ||
using UnityEngine; | ||
using SanAndreasUnity.Utilities; | ||
using UnityEngine.UI; | ||
using System.Linq; | ||
|
||
|
||
namespace SanAndreasUnity.UI | ||
{ | ||
|
||
public class ChatDisplay : MonoBehaviour | ||
{ | ||
public static ChatDisplay Instance { get; private set; } | ||
|
||
Queue<Chat.ChatMessage> m_chatMessages = new Queue<Chat.ChatMessage>(); | ||
public TMP_InputField inputField; | ||
public TMP_Text text; | ||
|
||
CustomInput customInput; | ||
|
||
static UnityEngine.EventSystems.EventSystem eventSystemComponent; | ||
|
||
Queue<Chat.ChatMessage> m_chatMessages = new Queue<Chat.ChatMessage>(); | ||
public int maxNumChatMessages = 5; | ||
public float timeToRemoveMessage = 3f; | ||
|
||
bool currentlyFading = false; | ||
|
||
public float timeToHideChat = 5f; | ||
|
||
void Awake() | ||
{ | ||
Instance = this; | ||
} | ||
|
||
void Start() | ||
{ | ||
eventSystemComponent = UnityEngine.EventSystems.EventSystem.current; | ||
|
||
Chat.ChatManager.onChatMessage += OnChatMsg; | ||
inputField.onSubmit.AddListener((s) => SendChatMessage(s)); | ||
|
||
customInput = CustomInput.Instance; | ||
} | ||
|
||
void OnChatMsg(Chat.ChatMessage chatMsg) | ||
{ | ||
if (m_chatMessages.Count >= this.maxNumChatMessages) | ||
m_chatMessages.Dequeue(); | ||
|
||
m_chatMessages.Enqueue(chatMsg); | ||
static bool lastFrameIsOpened = false; | ||
|
||
if (!this.IsInvoking(nameof(RemoveMessage))) | ||
this.Invoke(nameof(RemoveMessage), this.timeToRemoveMessage); | ||
public static bool IsOpened | ||
{ | ||
get { | ||
if (Instance != null) | ||
{ | ||
bool condition = eventSystemComponent.currentSelectedGameObject == Instance.inputField.gameObject; | ||
bool output = condition || lastFrameIsOpened; | ||
lastFrameIsOpened = condition; | ||
return output; | ||
} | ||
else | ||
{ | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
this.UpdateUI(); | ||
} | ||
void Update() | ||
{ | ||
if ( customInput != null) | ||
{ | ||
if (customInput.GetKeyDown(KeyCode.T)) | ||
{ | ||
inputField.gameObject.SetActive(true); | ||
inputField.Select(); | ||
UnFade(); | ||
} | ||
} | ||
} | ||
|
||
string GetDisplayTextForChatMessage(Chat.ChatMessage chatMessage) | ||
{ | ||
return "<color=blue>" + chatMessage.sender + "</color> : " + chatMessage.msg; | ||
} | ||
|
||
void OnChatMsg(Chat.ChatMessage chatMsg) | ||
{ | ||
AddMessage(chatMsg); | ||
inputField.text = ""; | ||
} | ||
|
||
void RemoveMessage() | ||
void AddMessage(Chat.ChatMessage chatMsg) | ||
{ | ||
if (m_chatMessages.Count > 0) | ||
if (m_chatMessages.Count >= this.maxNumChatMessages) | ||
m_chatMessages.Dequeue(); | ||
|
||
// invoke again if there are more messages | ||
if (m_chatMessages.Count > 0) | ||
this.Invoke(nameof(RemoveMessage), this.timeToRemoveMessage); | ||
|
||
m_chatMessages.Enqueue(chatMsg); | ||
|
||
this.UpdateUI(); | ||
} | ||
|
||
void UpdateUI() | ||
{ | ||
Text[] texts = this.gameObject.GetFirstLevelChildrenComponents<Text>().ToArray(); | ||
Chat.ChatMessage[] chatMessages = m_chatMessages.ToArray(); | ||
|
||
for (int i = 0; i < texts.Length; i++) | ||
string textStr = ""; | ||
|
||
for ( int i = 0; i < chatMessages.Length; i++) | ||
{ | ||
if (i < chatMessages.Length) | ||
texts[i].text = GetDisplayTextForChatMessage(chatMessages[i]); | ||
else | ||
texts[i].text = ""; | ||
textStr += GetDisplayTextForChatMessage(chatMessages[i]) + "\n"; | ||
} | ||
|
||
text.text = textStr; | ||
|
||
UnFade(); | ||
SetFadeTimeout(); | ||
} | ||
|
||
string GetDisplayTextForChatMessage(Chat.ChatMessage chatMessage) | ||
IEnumerator FadeChat() | ||
{ | ||
return "<color=blue>" + chatMessage.sender + "</color> : " + chatMessage.msg; | ||
// fade from opaque to transparent | ||
// loop over 1 second backwards | ||
for (float i = 1; i >= 0; i -= Time.deltaTime) | ||
{ | ||
if (!currentlyFading) | ||
yield break; | ||
|
||
// set color with i as alpha | ||
text.color = new Color(1, 1, 1, i); | ||
yield return null; | ||
} | ||
} | ||
|
||
} | ||
void StartFade() | ||
{ | ||
if ( currentlyFading == false) | ||
{ | ||
currentlyFading = true; | ||
StartCoroutine(FadeChat()); | ||
} | ||
} | ||
|
||
} | ||
void SetFadeTimeout() | ||
{ | ||
if (!this.IsInvoking(nameof(StartFade))) | ||
this.Invoke(nameof(StartFade), this.timeToHideChat); | ||
} | ||
|
||
void UnFade() | ||
{ | ||
currentlyFading = false; | ||
text.color = new Color(1, 1, 1, 1); | ||
} | ||
|
||
void SendChatMessage(string msg) | ||
{ | ||
eventSystemComponent.SetSelectedGameObject(null); | ||
SetFadeTimeout(); | ||
|
||
inputField.text = ""; | ||
|
||
inputField.gameObject.SetActive(false); | ||
|
||
if (!this.IsInvoking(nameof(StartFade))) | ||
this.Invoke(nameof(StartFade), 5f); | ||
|
||
if (string.IsNullOrWhiteSpace(msg)) | ||
return; | ||
|
||
Chat.ChatManager.SendChatMessageToAllPlayersAsLocalPlayer(msg); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
%YAML 1.1 | ||
%TAG !u! tag:unity3d.com,2011: | ||
--- !u!21 &2100000 | ||
Material: | ||
serializedVersion: 6 | ||
m_ObjectHideFlags: 0 | ||
m_PrefabParentObject: {fileID: 0} | ||
m_PrefabInternal: {fileID: 0} | ||
m_Name: LiberationSans SDF - Drop Shadow | ||
m_Shader: {fileID: 4800000, guid: fe393ace9b354375a9cb14cdbbc28be4, type: 3} | ||
m_ShaderKeywords: OUTLINE_ON UNDERLAY_ON | ||
m_LightmapFlags: 5 | ||
m_EnableInstancingVariants: 0 | ||
m_DoubleSidedGI: 0 | ||
m_CustomRenderQueue: -1 | ||
stringTagMap: {} | ||
disabledShaderPasses: [] | ||
m_SavedProperties: | ||
serializedVersion: 3 | ||
m_TexEnvs: | ||
- _BumpMap: | ||
m_Texture: {fileID: 0} | ||
m_Scale: {x: 1, y: 1} | ||
m_Offset: {x: 0, y: 0} | ||
- _Cube: | ||
m_Texture: {fileID: 0} | ||
m_Scale: {x: 1, y: 1} | ||
m_Offset: {x: 0, y: 0} | ||
- _FaceTex: | ||
m_Texture: {fileID: 0} | ||
m_Scale: {x: 1, y: 1} | ||
m_Offset: {x: 0, y: 0} | ||
- _MainTex: | ||
m_Texture: {fileID: 2846298, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2} | ||
m_Scale: {x: 1, y: 1} | ||
m_Offset: {x: 0, y: 0} | ||
- _OutlineTex: | ||
m_Texture: {fileID: 0} | ||
m_Scale: {x: 1, y: 1} | ||
m_Offset: {x: 0, y: 0} | ||
m_Floats: | ||
- _Ambient: 0.5 | ||
- _Bevel: 0.5 | ||
- _BevelClamp: 0 | ||
- _BevelOffset: 0 | ||
- _BevelRoundness: 0 | ||
- _BevelWidth: 0 | ||
- _BumpFace: 0 | ||
- _BumpOutline: 0 | ||
- _ColorMask: 15 | ||
- _Diffuse: 0.5 | ||
- _DiffusePower: 1 | ||
- _FaceDilate: 0.1 | ||
- _FaceUVSpeedX: 0 | ||
- _FaceUVSpeedY: 0 | ||
- _GlowInner: 0.05 | ||
- _GlowOffset: 0 | ||
- _GlowOuter: 0.05 | ||
- _GlowPower: 0.75 | ||
- _GradientScale: 10 | ||
- _LightAngle: 3.1416 | ||
- _MaskSoftnessX: 0 | ||
- _MaskSoftnessY: 0 | ||
- _OutlineSoftness: 0 | ||
- _OutlineUVSpeedX: 0 | ||
- _OutlineUVSpeedY: 0 | ||
- _OutlineWidth: 0.1 | ||
- _PerspectiveFilter: 0.875 | ||
- _Reflectivity: 10 | ||
- _ScaleRatioA: 0.9 | ||
- _ScaleRatioB: 0.73125 | ||
- _ScaleRatioC: 0.64125 | ||
- _ScaleX: 1 | ||
- _ScaleY: 1 | ||
- _ShaderFlags: 0 | ||
- _SpecularPower: 2 | ||
- _Stencil: 0 | ||
- _StencilComp: 8 | ||
- _StencilOp: 0 | ||
- _StencilReadMask: 255 | ||
- _StencilWriteMask: 255 | ||
- _TextureHeight: 1024 | ||
- _TextureWidth: 1024 | ||
- _UnderlayDilate: 0 | ||
- _UnderlayOffsetX: 0.5 | ||
- _UnderlayOffsetY: -0.5 | ||
- _UnderlaySoftness: 0.05 | ||
- _VertexOffsetX: 0 | ||
- _VertexOffsetY: 0 | ||
- _WeightBold: 0.75 | ||
- _WeightNormal: 0 | ||
m_Colors: | ||
- _ClipRect: {r: -32767, g: -32767, b: 32767, a: 32767} | ||
- _Color: {r: 1, g: 1, b: 1, a: 1} | ||
- _EnvMatrixRotation: {r: 0, g: 0, b: 0, a: 0} | ||
- _FaceColor: {r: 1, g: 1, b: 1, a: 1} | ||
- _GlowColor: {r: 0, g: 1, b: 0, a: 0.5} | ||
- _MaskCoord: {r: 0, g: 0, b: 32767, a: 32767} | ||
- _OutlineColor: {r: 0, g: 0, b: 0, a: 1} | ||
- _ReflectFaceColor: {r: 0, g: 0, b: 0, a: 1} | ||
- _ReflectOutlineColor: {r: 0, g: 0, b: 0, a: 1} | ||
- _SpecularColor: {r: 1, g: 1, b: 1, a: 1} | ||
- _UnderlayColor: {r: 0, g: 0, b: 0, a: 0.5} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.