-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChatBox.cs
231 lines (176 loc) · 7.36 KB
/
ChatBox.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
using MelonLoader;
using UnityEngine;
using UnityEngine.UI;
using BTD_Mod_Helper.Extensions;
using Assets.Scripts.Unity;
using UnityEngine.EventSystems;
using System;
using In_Game_Chat.Messages;
using System.Linq;
using System.Collections.Generic;
namespace In_Game_Chat
{
public class ChatBox
{
public bool instantiated = false;
private AssetBundle assetBundle;
private GameObject chatBoxCanvasGo;
private GameObject instantiatedChatBox;
private Text messageDisplay;
Image textArea;
ScrollRect textAreaScrollRect;
private InputField messageInput;
private KeyCode SendKey = KeyCode.Return;
private KeyCode SendKeyAlt = KeyCode.KeypadEnter;
private Button closeButton;
private List<Button> dmButtons = new List<Button>();
public bool IsFocused { get { return IsChatFocused(); } }
public bool Visible { get { return instantiatedChatBox.activeSelf; } set { SetVisibility(value); } }
private float timeToIgnoreHotkeys = 0f;
const float timeToIgnoreAfterSendMessage = 1f;
public void InitializeChatBox()
{
if (assetBundle == null)
assetBundle = AssetBundle.LoadFromMemory(Properties.Resources.chatbox_final);
if (chatBoxCanvasGo == null)
chatBoxCanvasGo = assetBundle.LoadAsset("Canvas_Final").Cast<GameObject>();
if (instantiatedChatBox == null)
instantiatedChatBox = GameObject.Instantiate(chatBoxCanvasGo);
textArea = instantiatedChatBox.transform.Find("ChatBox/Image").GetComponent<Image>();
textAreaScrollRect = instantiatedChatBox.transform.Find("ChatBox/Image").GetComponent<ScrollRect>();
messageDisplay = instantiatedChatBox.transform.Find("ChatBox/Image/MessageHistory").GetComponent<Text>();
messageDisplay.text = "";
messageInput = instantiatedChatBox.transform.Find("ChatBox/Input").GetComponent<InputField>();
closeButton = instantiatedChatBox.transform.Find("ChatBox/CloseButton").GetComponent<Button>();
closeButton.onClick.AddListener(new Action(() => { CloseButtonClick(); }));
AddDmButtons();
instantiatedChatBox.SetActive(false);
instantiated = true;
}
private void AddDmButtons()
{
for (int i = 0; i < 4; i++)
{
int buttonNum = i + 1;
var button = instantiatedChatBox.transform.Find($"ChatBox/DM_Buttons/Player {buttonNum}").GetComponent<Button>();
button.onClick.AddListener(new Action(() => { DmButtonPressed(button); }));
dmButtons.Add(button);
}
}
private void DmButtonPressed(Button sender)
{
var text = messageInput.text;
if (IsPrivateMessage(text))
text = text.Replace("/player1 ", "").Replace("/player2 ", "").Replace("/player3 ", "").Replace("/player4 ", "");
var recipient = sender.name.ToLower().Replace(" ", "");
var newMessage = $"/{recipient} {text}";
messageInput.text = newMessage;
FocusOnTextInput();
}
public void Update() => CheckSendMessages();
private void CheckSendMessages()
{
if (messageInput == null || !messageInput.IsActive())
return;
if (Input.GetKeyDown(SendKey) || Input.GetKeyDown(SendKeyAlt))
{
SendMessage();
FocusOnTextInput();
}
}
public void ShowHideChatBox()
{
if (!instantiated)
InitializeChatBox();
bool visibility = (instantiatedChatBox.activeSelf) ? false : true;
instantiatedChatBox.SetActive(visibility);
messageInput.text = "";
if (visibility == true)
FocusOnTextInput();
}
public void SendMessage()
{
string message = messageInput.text;
if (string.IsNullOrEmpty(message))
return;
new Chat_Message(message);
}
public void SendMessage(Chat_Message message)
{
byte? peerID = GetPeerID(message.Message);
if (peerID.HasValue)
message.Message = message.Message.Remove(0, 9);
var json = message.Serialize();
SendMessageToPeers(json, peerID);
messageInput.text = "";
UpdateChatLog(message.Message, "Me");
}
private void SendMessageToPeers(string message, byte? peerId)
{
if (Game.instance?.nkGI != null)
Game.instance.nkGI.SendMessage(message, peerId, code: Chat_Message.chatCoopCode);
}
private byte? GetPeerID(string message)
{
if (!IsPrivateMessage(message))
return null;
Int32.TryParse(message[7].ToString(), out int id);
return (byte?)id;
}
private bool IsPrivateMessage(string message) => message.StartsWith("/player1 ") || message.StartsWith("/player2 ")
|| message.StartsWith("/player3 ") || message.StartsWith("/player4 ");
public void UpdateChatLog(string message, string sender)
{
ShowMessage($"{sender}: {message}");
IncreaseChatlogSize();
textAreaScrollRect.normalizedPosition = new Vector2(0, 0);
timeToIgnoreHotkeys = Time.time + timeToIgnoreAfterSendMessage;
SpawnBloonsIfPossible(message);
}
public void ShowMessage(string message)
{
if (!instantiatedChatBox.activeSelf)
ShowHideChatBox();
var newTest = $"{messageDisplay.text}\n {message}";
messageDisplay.text = newTest;
}
private void IncreaseChatlogSize()
{
var msgDisplaySize = messageDisplay.rectTransform.sizeDelta;
const float bufferSpace = 1.001f;
var spaceToAdd = messageDisplay.fontSize + bufferSpace;
messageDisplay.rectTransform.sizeDelta = new Vector2(msgDisplaySize.x, msgDisplaySize.y + spaceToAdd);
}
public void ClearChatLog()
{
if (messageDisplay != null)
messageDisplay.text = "";
}
private bool IsChatFocused()
{
if (messageInput == null || !messageInput.IsActive())
return false;
bool inputFocused = messageInput.isFocused;
bool ignoreHotkeys = Time.time < timeToIgnoreHotkeys;
return inputFocused || ignoreHotkeys;
}
private void FocusOnTextInput()
{
messageInput.OnPointerClick(new PointerEventData(EventSystem.current));
}
private void SetVisibility(bool visible)
{
if (instantiatedChatBox != null)
instantiatedChatBox.SetActive(visible);
}
private void CloseButtonClick() => ShowHideChatBox();
private void SpawnBloonsIfPossible(string message)
{
var split = message.Split(' ');
var bloonName = split[split.Length - 1].Trim(' ');
var bloonModel = Game.instance.model.bloons.FirstOrDefault(bloon => bloon.name == bloonName);
if (bloonModel != null)
bloonModel.SpawnBloonModel();
}
}
}