-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageWindow.js
More file actions
175 lines (147 loc) · 5.11 KB
/
Copy pathMessageWindow.js
File metadata and controls
175 lines (147 loc) · 5.11 KB
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
var Window = load({}, "window.js");
function MessageWindow(width, height, x, y) {
Window.call(this, width, height, x, y);
this.messages = [];
this.typeToggled = false;
}
MessageWindow.prototype = Object.create(Window.prototype);
MessageWindow.prototype.constructor = MessageWindow;
MessageWindow.prototype.typeToggle = function() {
this.typeToggled = !this.typeToggled;
};
MessageWindow.prototype.setTypeToggled = function(newToggle) {
this.typeToggled = newToggle;
};
MessageWindow.prototype.addMessage = function(user, message) {
unixTime = time();
this.messages.push({ text: message, author: user, date: unixTime});
}
MessageWindow.prototype.drawMessages = function(messageAdjust) {
if (messageAdjust === undefined) {
messageAdjust = 0;
}
var maxWidth = this.width - 2;
var maxMessages = this.height - 3; // Adjust for borders and title
maxMessages = maxMessages - messageAdjust; // Adjust for message entry section.
// Clear the chat area
for (var y = 2; y < this.height - 1; y++) {
console.gotoxy(this.x + 1, this.y + y);
for (var x = 1; x < this.width - 1; x++) {
console.print(' ');
}
}
var allLines = [];
// TODO: Sort messages by date in descending order
// Process messages into lines
for (var m = 0; m < this.messages.length; m++) {
var message = this.messages[m];
var user = message.author;
var text = message.text;
var formattedMessage = user + ": " + text;
var words = formattedMessage.split(' ');
var lines = [];
var line = '';
for (var i = 0; i < words.length; i++) {
var word = words[i];
while (word.length > maxWidth) {
// Split the word if it's too long
var part = word.substring(0, maxWidth);
word = word.substring(maxWidth);
if (line.length > 0) {
lines.push(line);
line = '';
}
lines.push(part);
}
if (line.length + word.length + (line.length > 0 ? 1 : 0) > maxWidth) {
lines.push(line);
line = word;
} else {
if (line.length > 0) line += ' ';
line += word;
}
}
if (line.length > 0) lines.push(line);
allLines = allLines.concat(lines);
}
// Display the most recent messages
var startLine = Math.max(0, allLines.length - maxMessages);
var yPosition = 2;
for (var i = startLine; i < allLines.length && yPosition < this.height - 1; i++) {
console.gotoxy(this.x + 1, this.y + yPosition);
console.print(allLines[i]);
yPosition++;
}
};
MessageWindow.prototype.calculateMessageLines = function(user, message) {
var maxWidth = this.width - 2;
var formattedMessage = user + ": " + message;
var words = formattedMessage.split(' ');
var lines = [];
var line = '';
for (var i = 0; i < words.length; i++) {
var word = words[i];
while (word.length > maxWidth) {
var part = word.substring(0, maxWidth);
word = word.substring(maxWidth);
if (line.length > 0) {
lines.push(line);
line = '';
}
lines.push(part);
}
if (line.length + word.length + (line.length > 0 ? 1 : 0) > maxWidth) {
lines.push(line);
line = word;
} else {
if (line.length > 0) line += ' ';
line += word;
}
}
if (line.length > 0) lines.push(line);
return lines.length;
};
MessageWindow.prototype.drawTypedMessage = function(user, message) {
var maxWidth = this.width - 2;
var formattedMessage = user + ": " + message;
var words = formattedMessage.split(' ');
var lines = [];
var line = '';
for (var i = 0; i < words.length; i++) {
var word = words[i];
while (word.length > maxWidth) {
var part = word.substring(0, maxWidth);
word = word.substring(maxWidth);
if (line.length > 0) {
lines.push(line);
line = '';
}
lines.push(part);
}
if (line.length + word.length + (line.length > 0 ? 1 : 0) > maxWidth) {
lines.push(line);
line = word;
} else {
if (line.length > 0) line += ' ';
line += word;
}
}
if (line.length > 0) lines.push(line);
var startLine = this.y + this.height - 1 - lines.length;
for (var i = 0; i < lines.length; i++) {
console.gotoxy(this.x + 1, startLine + i);
for (var x = 1; x < this.width - 1; x++) {
console.print(' ');
}
console.gotoxy(this.x + 1, startLine + i);
console.print(lines[i]);
}
var yPosition = this.y + this.height - lines.length - 2;
console.gotoxy(this.x + 1, yPosition);
for (var x = 1; x < this.width - 1; x++) {
console.print('-');
}
return lines.length;
};
/* Leave as last line for convenient load() usage: */
MessageWindow;