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