-
Notifications
You must be signed in to change notification settings - Fork 53
/
KMPChatDX.cs
189 lines (157 loc) · 5.96 KB
/
KMPChatDX.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
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace KMP
{
internal class KMPChatDX
{
/* New Chat */
public const int MAX_CHAT_OUT_QUEUE = 4;
public const int MAX_CHAT_LINES = 16;
public const int MAX_CHAT_LINE_LENGTH = 128;
public const float NAME_COLOR_SATURATION_FACTOR = 0.35f;
public static float chatboxWidth = Screen.width / 4.5f;
public static float chatboxHeight = Screen.height / 3.5f;
public static float chatboxX = 0;
public static float chatboxY = 20;
public static bool offsettingEnabled = true;
public static float trackerOffsetX = 200;
public static float trackerOffsetY = 20;
public static float editorOffsetX = 255;
public static float editorOffsetY = 20;
public static float flightOffsetX = 40;
public static float flightOffsetY = 20;
public static GameScenes lastScene = GameScenes.MAINMENU;
public static bool showInput = false;
public static bool draggable = false;
public static bool displayCommands = false;
public static Vector2 scrollPos = Vector2.zero;
public static GUIStyle windowStyle = new GUIStyle();
public static GUIStyle chatStyle = new GUIStyle();
public static GUILayoutOption[] layoutOptions;
public static Rect windowPos = new Rect(chatboxX, chatboxY, chatboxWidth, chatboxHeight);
public static Queue<ChatLine> chatLineQueue = new Queue<ChatLine>();
public static String chatEntryString = String.Empty;
public struct ChatLine
{
public String name;
public String message;
public Color color;
public bool isAdmin;
public ChatLine(String line)
{
this.color = Color.yellow;
this.name = "";
this.message = line;
this.isAdmin = false;
//Check if the message has a name
if (line.Length > 3 && (line.First() == '<' || (line.StartsWith("[" + KMPCommon.ADMIN_MARKER + "]") && line.Contains('<'))))
{
int name_start = line.IndexOf('<');
int name_end = line.IndexOf('>');
int name_length = name_end - name_start - 1;
if (name_length > 0)
{
this.name = line.Substring(name_start + 1, name_length);
this.message = line.Substring(name_end + 1);
if (this.name == "Server")
this.color = Color.magenta;
else if (line.StartsWith("[" + KMPCommon.ADMIN_MARKER + "]"))
{
this.color = Color.red;
this.isAdmin = true;
}
else this.color = KMPVessel.generateActiveColor(name) * NAME_COLOR_SATURATION_FACTOR
+ Color.white * (1.0f - NAME_COLOR_SATURATION_FACTOR);
}
}
}
}
public static Rect getWindowPos()
{
if (offsettingEnabled && !draggable)
{
switch (HighLogic.LoadedScene)
{
case GameScenes.TRACKSTATION:
windowPos.x = chatboxX + trackerOffsetX;
windowPos.y = chatboxY + trackerOffsetY;
return windowPos;
case GameScenes.SPH:
windowPos.x = chatboxX + editorOffsetX;
windowPos.y = chatboxY + editorOffsetY;
return windowPos;
case GameScenes.EDITOR:
windowPos.x = chatboxX + editorOffsetX;
windowPos.y = chatboxY + editorOffsetY;
return windowPos;
case GameScenes.FLIGHT:
windowPos.x = chatboxX + flightOffsetX;
windowPos.y = chatboxY + flightOffsetY;
return windowPos;
default:
windowPos.x = chatboxX;
windowPos.y = chatboxY;
return windowPos;
}
}
else
{
return windowPos;
}
}
public static void enqueueChatLine(String line)
{
Color lineColor = Color.yellow;
bool colorSet = false;
foreach (String singleLine in line.Split('\n'))
{
string choppedLine = "";
foreach (String word in singleLine.Split(' '))
{
//if (GUI.skin.GetStyle("Box").CalcSize(new GUIContent(choppedLine + word)).x > chatboxWidth)
if (choppedLine.Length + word.Length > chatboxWidth / 7) //Kludgy but works relatively reliably
{
ChatLine chat_line = new ChatLine(choppedLine.Substring(0,choppedLine.Length-1));
if (!colorSet)
{
lineColor = chat_line.color;
colorSet = true;
}
else
{
chat_line.color = lineColor;
}
chatLineQueue.Enqueue(chat_line);
choppedLine = word + " ";
}
else
{
choppedLine += word + " ";
}
}
if (choppedLine.Length > 0)
{
ChatLine chat_line = new ChatLine(choppedLine.Substring(0,choppedLine.Length-1));
if (colorSet)
{
chat_line.color = lineColor;
}
chatLineQueue.Enqueue(chat_line);
}
}
while (chatLineQueue.Count > MAX_CHAT_LINES)
chatLineQueue.Dequeue();
//scrollPos.y += 100;
}
public static void setStyle()
{
/* Setup Chat */
chatStyle.fontStyle = FontStyle.Bold;
chatStyle.wordWrap = true;
chatStyle.padding.left = 5;
chatStyle.fixedWidth = chatboxWidth;
}
}
}