This repository has been archived by the owner on Dec 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathzztdialog.js
208 lines (171 loc) · 6.16 KB
/
zztdialog.js
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
/*
Dialog.
The ZZT dialog is rendered like:
|=========================|
| Title |
|=======================|
| |
| Some text |
|> Text <|
| More text |
| |
|=========================|
(It actually uses linedrawing characters, but I try to stick to ASCII
encoded text for comments.)
The .x, .y, .width, and .height are specified in terms of character cells.
The selected line is always in the center.
Text lines can come in three forms:
'text' - Regular text. Yellow-on-blue.
'!msg;text' - Label selection. Solid purple arrow, rest of text is white.
Sends 'msg' to the object on selection.
'$text' - Header. Centered, white-on-blue.
*/
function _ZZTDialog_parseLine(line)
{
var parsed = {};
if (line.charAt(0) == "!")
{
// !msg;text
var delim = line.slice(1).split(";");
parsed.message = delim[0];
parsed.text = delim[1];
}
else if (line.charAt(0) == "$")
{
// $text
parsed.text = line.slice(1);
parsed.centered = true;
}
else
{
parsed.text = line;
}
return parsed;
}
function ZZTDialog(title, lines)
{
/* x/y/w/h includes the borders. */
this.x = 5;
this.y = 3;
this.width = 49;
this.height = 19;
this.title = title;
this.lines = [];
for (var i = 0; i < lines.length; ++i)
{
this.lines.push(_ZZTDialog_parseLine(lines[i]))
}
this.selectedLine = 0;
this.done = null;
}
ZZTDialog.prototype.keydown = function(event)
{
if (event.keyCode == 40) /* down */
{
if (this.selectedLine < this.lines.length-2);
this.selectedLine++;
}
else if (event.keyCode == 38) /* up */
{
if (this.selectedLine > 0)
this.selectedLine--;
}
else if (event.keyCode == 13) /* enter */
{
this.done = { dialog: this, line: this.selectedLine };
}
else if (event.keyCode == 27) /* escape */
{
this.done = { dialog: this, cancelled: true };
}
}
ZZTDialog.prototype.draw = function(textconsole)
{
/* top row */
textconsole.set(this.x, this.y, 198, VGA.ATTR_FG_WHITE);
textconsole.set(this.x+1, this.y, 209, VGA.ATTR_FG_WHITE);
for (var x = this.x+2; x < this.x+this.width-2; ++x)
textconsole.set(x, this.y, 205, VGA.ATTR_FG_WHITE);
textconsole.set(this.x+this.width-2, this.y, 209, VGA.ATTR_FG_WHITE);
textconsole.set(this.x+this.width-1, this.y, 181, VGA.ATTR_FG_WHITE);
/* title row */
textconsole.set(this.x, this.y+1, 32, 0);
textconsole.set(this.x+1, this.y+1, 179, VGA.ATTR_FG_WHITE);
for (var x = this.x+2; x < this.x+this.width-2; ++x)
textconsole.set(x, this.y+1, 32, VGA.ATTR_BG_BLUE);
textconsole.setString(
Math.floor((60 - this.title.length) / 2),
this.y+1,
this.title,
VGA.ATTR_BG_BLUE|VGA.ATTR_FG_YELLOW);
textconsole.set(this.x+this.width-2, this.y+1, 179, VGA.ATTR_FG_WHITE);
textconsole.set(this.x+this.width-1, this.y+1, 32, 0);
/* titlebar dividing row */
textconsole.set(this.x, this.y+2, 32, 0);
textconsole.set(this.x+1, this.y+2, 198, VGA.ATTR_FG_WHITE);
for (var x = this.x+2; x < this.x+this.width-2; ++x)
textconsole.set(x, this.y+2, 205, VGA.ATTR_FG_WHITE);
textconsole.set(this.x+this.width-2, this.y+2, 181, VGA.ATTR_FG_WHITE);
textconsole.set(this.x+this.width-1, this.y+2, 32, 0);
/* now, we do the text portion */
var viewportHeight = this.height-4;
var centerLineInViewport = Math.floor(viewportHeight/2);
for (var l = 0; l < viewportHeight; ++l)
{
var y = this.y+3+l;
textconsole.set(this.x, y, 32, 0);
textconsole.set(this.x+1, y, 179, VGA.ATTR_FG_WHITE);
for (var x = this.x+2; x < this.x+this.width-2; ++x)
textconsole.set(x, y, 32, VGA.ATTR_BG_BLUE);
if (l == centerLineInViewport)
{
textconsole.set(this.x+2, y, 175, VGA.ATTR_BG_BLUE|VGA.ATTR_FG_BRED);
textconsole.set(this.x+this.width-3, y, 174, VGA.ATTR_BG_BLUE|VGA.ATTR_FG_BRED);
}
var textLineIndex = this.selectedLine + (l - centerLineInViewport);
if (textLineIndex < 0 || textLineIndex >= this.lines.length)
{
/* off the page */
for (var x = this.x+2; x < this.x+this.width-2; ++x)
textconsole.set(x, y, 32, VGA.ATTR_BG_BLUE);
}
else
{
if (l == centerLineInViewport)
textconsole.set(this.x+2, y, 175, VGA.ATTR_BG_BLUE|VGA.ATTR_FG_BRED);
else
textconsole.set(this.x+2, y, 32, VGA.ATTR_BG_BLUE);
textconsole.set(this.x+3, y, 32, VGA.ATTR_BG_BLUE);
var line = this.lines[textLineIndex];
if (line.message)
{
textconsole.set(this.x+6, y, 16, VGA.ATTR_BG_BLUE|VGA.ATTR_FG_BMAGENTA);
textconsole.setString(this.x+9, y, line.text, VGA.ATTR_BG_BLUE|VGA.ATTR_FG_WHITE);
}
else if (line.centered)
{
textconsole.setString(
Math.floor((60 - this.title.length) / 2), y,
line.text, VGA.ATTR_BG_BLUE|VGA.ATTR_FG_WHITE);
}
else
{
textconsole.setString(this.x+4, y, this.lines[textLineIndex].text, VGA.ATTR_BG_BLUE|VGA.ATTR_FG_YELLOW);
}
textconsole.set(this.x+this.width-4, y, 32, VGA.ATTR_BG_BLUE);
if (l == centerLineInViewport)
textconsole.set(this.x+this.width-3, y, 174, VGA.ATTR_BG_BLUE|VGA.ATTR_FG_BRED);
else
textconsole.set(this.x+this.width-3, y, 32, VGA.ATTR_BG_BLUE);
}
textconsole.set(this.x+this.width-2, y, 179, VGA.ATTR_FG_WHITE);
textconsole.set(this.x+this.width-1, y, 32, 0);
}
/* bottom row */
textconsole.set(this.x, this.y+this.height-1, 198, VGA.ATTR_FG_WHITE);
textconsole.set(this.x+1, this.y+this.height-1, 207, VGA.ATTR_FG_WHITE);
for (var x = this.x+2; x < this.x+this.width-2; ++x)
textconsole.set(x, this.y+this.height-1, 205, VGA.ATTR_FG_WHITE);
textconsole.set(this.x+this.width-2, this.y+this.height-1, 207, VGA.ATTR_FG_WHITE);
textconsole.set(this.x+this.width-1, this.y+this.height-1, 181, VGA.ATTR_FG_WHITE);
}