-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatGUI.cs
127 lines (110 loc) · 4.16 KB
/
ChatGUI.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
using UnityEngine;
using System.Collections.Generic;
public class ChatGUI : MonoBehaviour
{
public string messages;
private string inputMsg = "";
public GUISkin ChatGUIskin;
private bool playersVisible;
private PlayerController playerController;
private NetworkController networkController;
private Coroutine chatDataCoroutine;
private string playersOnline;
private int playerCount;
private float chatNetTimer;
private Vector2 scrollPosition;
// Use this for initialization
void Start ()
{
playerController = GetComponent<PlayerController>();
}
//! Called once per frame by unity engine.
public void Update()
{
if (PlayerPrefsX.GetPersistentBool("multiplayer") == false || playerController.stateManager.worldLoaded == false)
{
return;
}
if (networkController != null)
{
chatNetTimer += 1 * Time.deltaTime;
if (chatNetTimer >= Random.Range(0.75f, 1.0f))
{
chatDataCoroutine = StartCoroutine(networkController.networkReceive.ReceiveChatData());
chatNetTimer = 0;
}
}
else
{
networkController = playerController.networkController;
}
}
//! Called by unity engine for rendering and handling GUI events.
public void OnGUI()
{
if (PlayerPrefsX.GetPersistentBool("multiplayer") == false || playerController.stateManager.worldLoaded == false || playerController.GuiOpen())
{
return;
}
GUI.skin = ChatGUIskin;
int ScreenHeight = Screen.height;
int ScreenWidth = Screen.width;
Rect textFieldRect = new Rect(0, (ScreenHeight * 0.80f), (ScreenWidth * 0.15f), (ScreenHeight * 0.03f));
scrollPosition = GUILayout.BeginScrollView(scrollPosition,false,false, GUILayout.Width(ScreenWidth * 0.4f), GUILayout.Height(ScreenHeight * 0.65f));
GUILayout.Label(messages);
GUILayout.EndScrollView();
if (playerController.building == false && playerController.machineInSight == null)
{
GUI.SetNextControlName ("textfield");
inputMsg = GUI.TextField (textFieldRect, inputMsg, 300);
if (GUI.GetNameOfFocusedControl() != "textfield")
{
GUI.color = new Color(0.2824f, 0.7882f, 0.9569f);
GUI.Label(textFieldRect, " Press backspace to chat.");
GUI.color = Color.white;
}
Event ev = Event.current;
if (ev.keyCode == KeyCode.Backspace)
{
GUI.FocusControl ("textfield");
}
Event e = Event.current;
if (e.keyCode == KeyCode.Return)
{
if (inputMsg != "")
{
if (inputMsg == "/list" || inputMsg == "/players")
{
NetworkPlayer[] allPlayers = FindObjectsOfType<NetworkPlayer>();
playerCount = allPlayers.Length + 1;
List<string> nameList = new List<string>();
nameList.Add(PlayerPrefs.GetString("UserName"));
foreach (NetworkPlayer player in allPlayers)
{
nameList.Add(player.gameObject.name);
}
playersOnline = string.Join("\n", nameList.ToArray());
playersVisible = true;
}
else
{
networkController.networkSend.SendChatMessage(inputMsg);
}
inputMsg = "";
}
GUIUtility.keyboardControl = 0;
}
if (playersVisible == true)
{
messages += "\n\n"+playerCount+" players online.";
messages += "\n"+playersOnline+"\n\n";
playersVisible = false;
}
}
if (messages.Length >= 500)
{
messages = "";
}
scrollPosition.y += 1000;
}
}