| | 1 | | using System.Collections; |
| | 2 | | using DCL.Interface; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.EventSystems; |
| | 5 | | using UnityEngine.UI; |
| | 6 | | using UnityEngine.Events; |
| | 7 | |
|
| | 8 | | public class WorldChatWindowHUDView : MonoBehaviour, IPointerClickHandler |
| | 9 | | { |
| | 10 | | const string VIEW_PATH = "World Chat Window"; |
| | 11 | |
|
| | 12 | | public Button closeButton; |
| | 13 | |
|
| | 14 | | public ChatHUDView chatHudView; |
| | 15 | |
|
| | 16 | | public CanvasGroup group; |
| | 17 | | public WorldChatWindowHUDController controller; |
| 0 | 18 | | public bool isInPreview { get; private set; } |
| | 19 | |
|
| | 20 | | public event UnityAction OnDeactivatePreview; |
| | 21 | | public event UnityAction OnActivatePreview; |
| | 22 | | public event UnityAction OnClose; |
| | 23 | | public UnityAction<ChatMessage> OnSendMessage; |
| | 24 | |
|
| | 25 | | ChatMessage lastWhisperMessageSent; |
| 20 | 26 | | string lastInputText = string.Empty; |
| | 27 | |
|
| | 28 | | public static WorldChatWindowHUDView Create() |
| | 29 | | { |
| 10 | 30 | | var view = Instantiate(Resources.Load<GameObject>(VIEW_PATH)).GetComponent<WorldChatWindowHUDView>(); |
| 10 | 31 | | view.Initialize(); |
| 10 | 32 | | return view; |
| | 33 | | } |
| | 34 | |
|
| | 35 | | void Awake() |
| | 36 | | { |
| 10 | 37 | | chatHudView.OnSendMessage += ChatHUDView_OnSendMessage; |
| 10 | 38 | | chatHudView.inputField.onValueChanged.AddListener(OnTextInputValueChanged); |
| 10 | 39 | | } |
| | 40 | |
|
| 20 | 41 | | private void Initialize() { this.closeButton.onClick.AddListener(OnCloseButtonPressed); } |
| | 42 | |
|
| | 43 | | public void OnCloseButtonPressed() |
| | 44 | | { |
| 1 | 45 | | controller.SetVisibility(false); |
| 1 | 46 | | OnClose?.Invoke(); |
| 0 | 47 | | } |
| | 48 | |
|
| | 49 | | public void DeactivatePreview() |
| | 50 | | { |
| 5 | 51 | | chatHudView.scrollRect.enabled = true; |
| 5 | 52 | | group.alpha = 1; |
| 5 | 53 | | isInPreview = false; |
| 5 | 54 | | chatHudView.SetFadeoutMode(false); |
| 5 | 55 | | OnDeactivatePreview?.Invoke(); |
| 0 | 56 | | } |
| | 57 | |
|
| | 58 | | public void ActivatePreview() |
| | 59 | | { |
| 3 | 60 | | chatHudView.scrollRect.enabled = false; |
| 3 | 61 | | group.alpha = 0; |
| 3 | 62 | | isInPreview = true; |
| 3 | 63 | | chatHudView.SetFadeoutMode(true); |
| 3 | 64 | | OnActivatePreview?.Invoke(); |
| 0 | 65 | | } |
| | 66 | |
|
| 0 | 67 | | public void OnPointerClick(PointerEventData eventData) { DeactivatePreview(); } |
| | 68 | |
|
| | 69 | | public void OnTextInputValueChanged(string text) |
| | 70 | | { |
| 6 | 71 | | if (isInPreview) |
| 0 | 72 | | chatHudView.inputField.text = lastInputText; |
| | 73 | | else |
| 6 | 74 | | lastInputText = chatHudView.inputField.text; |
| | 75 | |
|
| | 76 | |
|
| 6 | 77 | | if (!string.IsNullOrEmpty(controller.lastPrivateMessageReceivedSender) && text == "/r ") |
| | 78 | | { |
| 1 | 79 | | chatHudView.inputField.text = $"/w {controller.lastPrivateMessageReceivedSender} "; |
| 1 | 80 | | chatHudView.inputField.MoveTextEnd(false); |
| | 81 | | } |
| 6 | 82 | | } |
| | 83 | |
|
| | 84 | | public void ChatHUDView_OnSendMessage(ChatMessage message) |
| | 85 | | { |
| 2 | 86 | | if (message.messageType == ChatMessage.Type.PRIVATE && !string.IsNullOrEmpty(message.body)) |
| 2 | 87 | | lastWhisperMessageSent = message; |
| | 88 | | else |
| 0 | 89 | | lastWhisperMessageSent = null; |
| | 90 | |
|
| 2 | 91 | | if (lastWhisperMessageSent != null) |
| 2 | 92 | | StartCoroutine(WaitAndUpdateInputText($"/w {lastWhisperMessageSent.recipient} ")); |
| | 93 | | else |
| 0 | 94 | | StartCoroutine(WaitAndUpdateInputText(string.Empty)); |
| | 95 | |
|
| 2 | 96 | | OnSendMessage?.Invoke(message); |
| 2 | 97 | | } |
| | 98 | |
|
| | 99 | | IEnumerator WaitAndUpdateInputText(string newText) |
| | 100 | | { |
| 2 | 101 | | yield return null; |
| | 102 | |
|
| 2 | 103 | | chatHudView.inputField.text = newText; |
| 2 | 104 | | chatHudView.inputField.caretPosition = newText.Length; |
| 2 | 105 | | } |
| | 106 | | } |