| | 1 | | using System; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Interface; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.Events; |
| | 6 | | using Object = UnityEngine.Object; |
| | 7 | |
|
| | 8 | | public class ChatHUDController : IDisposable |
| | 9 | | { |
| 1 | 10 | | public static int MAX_CHAT_ENTRIES { internal set; get; } = 30; |
| | 11 | |
|
| | 12 | | public ChatHUDView view; |
| | 13 | |
|
| | 14 | | public event UnityAction<string> OnPressPrivateMessage; |
| | 15 | |
|
| | 16 | | private readonly DataStore dataStore; |
| | 17 | | private readonly RegexProfanityFilter profanityFilter; |
| | 18 | | private InputAction_Trigger closeWindowTrigger; |
| | 19 | |
|
| 32 | 20 | | public ChatHUDController(DataStore dataStore, RegexProfanityFilter profanityFilter = null) |
| | 21 | | { |
| 32 | 22 | | this.dataStore = dataStore; |
| 32 | 23 | | this.profanityFilter = profanityFilter; |
| 32 | 24 | | } |
| | 25 | |
|
| | 26 | | public void Initialize(ChatHUDView view = null, UnityAction<ChatMessage> onSendMessage = null) |
| | 27 | | { |
| 32 | 28 | | this.view = view ?? ChatHUDView.Create(); |
| | 29 | |
|
| 32 | 30 | | this.view.Initialize(this, onSendMessage); |
| | 31 | |
|
| 32 | 32 | | this.view.OnPressPrivateMessage -= View_OnPressPrivateMessage; |
| 32 | 33 | | this.view.OnPressPrivateMessage += View_OnPressPrivateMessage; |
| | 34 | |
|
| 32 | 35 | | if (this.view.contextMenu != null) |
| | 36 | | { |
| 24 | 37 | | this.view.contextMenu.OnShowMenu -= ContextMenu_OnShowMenu; |
| 24 | 38 | | this.view.contextMenu.OnShowMenu += ContextMenu_OnShowMenu; |
| | 39 | | } |
| | 40 | |
|
| 32 | 41 | | closeWindowTrigger = Resources.Load<InputAction_Trigger>("CloseWindow"); |
| 32 | 42 | | closeWindowTrigger.OnTriggered -= OnCloseButtonPressed; |
| 32 | 43 | | closeWindowTrigger.OnTriggered += OnCloseButtonPressed; |
| 32 | 44 | | } |
| | 45 | |
|
| 0 | 46 | | void View_OnPressPrivateMessage(string friendUserId) { OnPressPrivateMessage?.Invoke(friendUserId); } |
| | 47 | |
|
| 0 | 48 | | private void ContextMenu_OnShowMenu() { view.OnMessageCancelHover(); } |
| | 49 | |
|
| | 50 | | private void OnCloseButtonPressed(DCLAction_Trigger action) |
| | 51 | | { |
| 0 | 52 | | if (view.contextMenu != null) |
| | 53 | | { |
| 0 | 54 | | view.contextMenu.Hide(); |
| 0 | 55 | | view.confirmationDialog.Hide(); |
| | 56 | | } |
| 0 | 57 | | } |
| | 58 | |
|
| | 59 | | public void AddChatMessage(ChatEntry.Model chatEntryModel, bool setScrollPositionToBottom = false) |
| | 60 | | { |
| 38 | 61 | | if (IsProfanityFilteringEnabled() && chatEntryModel.messageType != ChatMessage.Type.PRIVATE) |
| | 62 | | { |
| 24 | 63 | | chatEntryModel.bodyText = profanityFilter.Filter(chatEntryModel.bodyText); |
| 24 | 64 | | if (!string.IsNullOrEmpty(chatEntryModel.senderName)) |
| 24 | 65 | | chatEntryModel.senderName = profanityFilter.Filter(chatEntryModel.senderName); |
| 24 | 66 | | if (!string.IsNullOrEmpty(chatEntryModel.recipientName)) |
| 2 | 67 | | chatEntryModel.recipientName = profanityFilter.Filter(chatEntryModel.recipientName); |
| | 68 | | } |
| | 69 | |
|
| 38 | 70 | | view.AddEntry(chatEntryModel, setScrollPositionToBottom); |
| | 71 | |
|
| 38 | 72 | | if (view.entries.Count > MAX_CHAT_ENTRIES) |
| | 73 | | { |
| 5 | 74 | | Object.Destroy(view.entries[0].gameObject); |
| 5 | 75 | | view.entries.Remove(view.entries[0]); |
| | 76 | | } |
| 38 | 77 | | } |
| | 78 | |
|
| | 79 | | public void Dispose() |
| | 80 | | { |
| 14 | 81 | | view.OnPressPrivateMessage -= View_OnPressPrivateMessage; |
| 14 | 82 | | if (view.contextMenu != null) |
| | 83 | | { |
| 14 | 84 | | view.contextMenu.OnShowMenu -= ContextMenu_OnShowMenu; |
| | 85 | | } |
| 14 | 86 | | closeWindowTrigger.OnTriggered -= OnCloseButtonPressed; |
| 14 | 87 | | Object.Destroy(view.gameObject); |
| 14 | 88 | | } |
| | 89 | |
|
| | 90 | | public static ChatEntry.Model ChatMessageToChatEntry(ChatMessage message) |
| | 91 | | { |
| 13 | 92 | | ChatEntry.Model model = new ChatEntry.Model(); |
| | 93 | |
|
| 13 | 94 | | var ownProfile = UserProfile.GetOwnUserProfile(); |
| | 95 | |
|
| 13 | 96 | | model.messageType = message.messageType; |
| 13 | 97 | | model.bodyText = message.body; |
| 13 | 98 | | model.timestamp = message.timestamp; |
| | 99 | |
|
| 13 | 100 | | if (message.recipient != null) |
| | 101 | | { |
| 5 | 102 | | var recipientProfile = UserProfileController.userProfilesCatalog.Get(message.recipient); |
| 5 | 103 | | model.recipientName = recipientProfile != null ? recipientProfile.userName : message.recipient; |
| | 104 | | } |
| | 105 | |
|
| 13 | 106 | | if (message.sender != null) |
| | 107 | | { |
| 13 | 108 | | var senderProfile = UserProfileController.userProfilesCatalog.Get(message.sender); |
| 13 | 109 | | model.senderName = senderProfile != null ? senderProfile.userName : message.sender; |
| 13 | 110 | | model.senderId = message.sender; |
| | 111 | | } |
| | 112 | |
|
| 13 | 113 | | if (model.messageType == ChatMessage.Type.PRIVATE) |
| | 114 | | { |
| 11 | 115 | | if (message.recipient == ownProfile.userId) |
| | 116 | | { |
| 4 | 117 | | model.subType = ChatEntry.Model.SubType.PRIVATE_FROM; |
| 4 | 118 | | model.otherUserId = message.sender; |
| 4 | 119 | | } |
| 7 | 120 | | else if (message.sender == ownProfile.userId) |
| | 121 | | { |
| 1 | 122 | | model.subType = ChatEntry.Model.SubType.PRIVATE_TO; |
| 1 | 123 | | model.otherUserId = message.recipient; |
| 1 | 124 | | } |
| | 125 | | else |
| | 126 | | { |
| 6 | 127 | | model.subType = ChatEntry.Model.SubType.NONE; |
| | 128 | | } |
| | 129 | | } |
| | 130 | |
|
| 13 | 131 | | return model; |
| | 132 | | } |
| | 133 | |
|
| | 134 | | private bool IsProfanityFilteringEnabled() |
| | 135 | | { |
| 38 | 136 | | return dataStore.settings.profanityChatFilteringEnabled.Get() |
| | 137 | | && profanityFilter != null; |
| | 138 | | } |
| | 139 | | } |