-
Notifications
You must be signed in to change notification settings - Fork 0
/
remglkoutputlayout.cpp
362 lines (334 loc) · 14.8 KB
/
remglkoutputlayout.cpp
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include "remglkoutputlayout.h"
#include "remglktexteditwindow.h"
#include <QDebug>
#include <QTimer>
#include "glk.h"
#include <QMap>
#include "glkwindowdata.h"
RemGlkOutputLayout::RemGlkOutputLayout(QWidget *parent)
: QObject(parent)
{
qDebug() << "~~~! RemGlkOtputLayout constructor";
}
void RemGlkOutputLayout::setOutputWidgets(RemGlkCompleterTextEdit *textEdit, QGridLayout *mainLayout, QLabel *topStatus)
{
this->textEdit = textEdit;
this->mainLayout = mainLayout;
this->topStatus = topStatus;
}
QString RemGlkOutputLayout::toStringJsonObject(const QJsonObject jsonObj) {
QJsonDocument doc(jsonObj);
QString strJson(doc.toJson(QJsonDocument::Compact));
return strJson;
}
void RemGlkOutputLayout::inlineOutputRemGlkDebug(int format, QString debugOutput) {
if (remGlkDebug_JSON_parsing) {
switch (format) {
case 0:
textEdit->insertPlainText(debugOutput);
break;
case 1:
textEdit->insertHtml(debugOutput);
break;
}
}
}
/*
* Typical RemGlk incoming data:
* window "{\"gridheight\":1,\"gridwidth\":80,\"height\":16,\"id\":20,\"left\":0,\"rock\":202,\"top\":0,\"type\":\"grid\",\"width\":720}"
* window "{\"height\":384,\"id\":17,\"left\":0,\"rock\":201,\"top\":16,\"type\":\"buffer\",\"width\":720}"
* */
void RemGlkOutputLayout::stanzaRemGlkWindows(QJsonArray windows) {
if (! windows.isEmpty()) {
inlineOutputRemGlkDebug(0, " windows [");
for(int i=0; i< windows.count(); ++i){
QJsonObject windowObject = windows.at(i).toObject();
int windowId = windowObject["id"].toInt();
GlkWindowData glkWindowData;
if (glkWindows.contains(windowId)) {
glkWindowData = glkWindows.value(windowId);
}
glkWindowData.windowId = windowId;
glkWindows.insert(windowId, glkWindowData);
inlineOutputRemGlkDebug(0, " !! ");
inlineOutputRemGlkDebug(0, toStringJsonObject(windowObject));
qDebug() << "window " << toStringJsonObject(windowObject);
}
inlineOutputRemGlkDebug(0, "] ");
}
qDebug() << "%%%%%% glkWindows " << glkWindows.count();
}
// ToDo: create OutputStyledChunk object and put into window.outputChunks List
void RemGlkOutputLayout::stanzaRemGlkContentStyledChunk(QJsonObject endpointStyledChunk) {
OutputStyledChunk styledChunk;
if (endpointStyledChunk.contains("text")) {
int glkStyle = style_Normal;
QString chunkStyle = "[null]";
if (endpointStyledChunk.contains("style")) {
chunkStyle = endpointStyledChunk["style"].toString();
if (chunkStyle == "input") {
glkStyle = style_Input;
}
if (chunkStyle == "header") {
glkStyle = style_Header;
}
if (chunkStyle == "subheader") {
glkStyle = style_Subheader;
}
}
const QString text(endpointStyledChunk["text"].toString());
switch (glkStyle) {
case style_Normal:
textEdit->insertPlainText(text);
textEdit->insertPlainText(" ");
break;
case style_Input:
// save
fw = textEdit->fontWeight();
tc = textEdit->textColor();
saveFontPointSize = textEdit->fontPointSize();
// append
textEdit->setFontWeight(QFont::DemiBold);
textEdit->setFontPointSize(saveFontPointSize * 1.05);
textEdit->setTextColor(QColor("DarkGreen"));
textEdit->insertPlainText(text);
// restore
textEdit->setFontWeight(fw);
textEdit->setTextColor(tc);
textEdit->setFontPointSize(saveFontPointSize);
break;
case style_Header:
// save
fw = textEdit->fontWeight();
tc = textEdit->textColor();
saveFontPointSize = textEdit->fontPointSize();
// append
textEdit->setFontWeight(QFont::DemiBold);
textEdit->setFontPointSize(saveFontPointSize * 1.3);
textEdit->setTextColor(QColor("red"));
textEdit->insertPlainText(text);
// restore
textEdit->setFontWeight(fw);
textEdit->setTextColor(tc);
textEdit->setFontPointSize(saveFontPointSize);
break;
case style_Subheader:
// save
fw = textEdit->fontWeight();
tc = textEdit->textColor();
saveFontPointSize = textEdit->fontPointSize();
// append
textEdit->setFontWeight(QFont::DemiBold);
textEdit->setFontPointSize(saveFontPointSize * 1.125);
textEdit->setTextColor(QColor("Black"));
textEdit->insertPlainText(text);
// restore
textEdit->setFontWeight(fw);
textEdit->setTextColor(tc);
textEdit->setFontPointSize(saveFontPointSize);
break;
default:
textEdit->insertHtml(" <b>UNEXPECTED_STYLE:</b> ");
textEdit->insertPlainText(chunkStyle);
textEdit->insertPlainText(" ");
textEdit->insertPlainText(text);
textEdit->insertPlainText(" ");
break;
}
styledChunk.glkStyle = glkStyle;
//qDebug() << "content chunk " << toStringJsonObject(endpointStyledChunk);
} else {
//qDebug() << "content chunk NO-text " << toStringJsonObject(endpointStyledChunk);
}
}
QString RemGlkOutputLayout::stanzaRemGlkContentStyledChunkToHTML(QJsonObject endpointStyledChunk) {
QString outHTML = "<b>ERROR: MISSING CHUNK TEXT</b>";
if (endpointStyledChunk.contains("text")) {
int glkStyle = style_Normal;
QString chunkStyle = "[null]";
if (endpointStyledChunk.contains("style")) {
chunkStyle = endpointStyledChunk["style"].toString();
if (chunkStyle == "input") {
glkStyle = style_Input;
}
if (chunkStyle == "header") {
glkStyle = style_Header;
}
if (chunkStyle == "subheader") {
glkStyle = style_Subheader;
}
}
const QString text(endpointStyledChunk["text"].toString());
outHTML = text;
return outHTML;
//****---***** ends here ****---*****
switch (glkStyle) {
case style_Normal:
textEdit->insertPlainText(text);
textEdit->insertPlainText(" ");
break;
case style_Input:
// save
fw = textEdit->fontWeight();
tc = textEdit->textColor();
saveFontPointSize = textEdit->fontPointSize();
// append
textEdit->setFontWeight(QFont::DemiBold);
textEdit->setFontPointSize(saveFontPointSize * 1.05);
textEdit->setTextColor(QColor("DarkGreen"));
textEdit->insertPlainText(text);
// restore
textEdit->setFontWeight(fw);
textEdit->setTextColor(tc);
textEdit->setFontPointSize(saveFontPointSize);
break;
case style_Header:
// save
fw = textEdit->fontWeight();
tc = textEdit->textColor();
saveFontPointSize = textEdit->fontPointSize();
// append
textEdit->setFontWeight(QFont::DemiBold);
textEdit->setFontPointSize(saveFontPointSize * 1.3);
textEdit->setTextColor(QColor("red"));
textEdit->insertPlainText(text);
// restore
textEdit->setFontWeight(fw);
textEdit->setTextColor(tc);
textEdit->setFontPointSize(saveFontPointSize);
break;
case style_Subheader:
// save
fw = textEdit->fontWeight();
tc = textEdit->textColor();
saveFontPointSize = textEdit->fontPointSize();
// append
textEdit->setFontWeight(QFont::DemiBold);
textEdit->setFontPointSize(saveFontPointSize * 1.125);
textEdit->setTextColor(QColor("Black"));
textEdit->insertPlainText(text);
// restore
textEdit->setFontWeight(fw);
textEdit->setTextColor(tc);
textEdit->setFontPointSize(saveFontPointSize);
break;
default:
textEdit->insertHtml(" <b>UNEXPECTED_STYLE:</b> ");
textEdit->insertPlainText(chunkStyle);
textEdit->insertPlainText(" ");
textEdit->insertPlainText(text);
textEdit->insertPlainText(" ");
break;
}
//qDebug() << "content chunk " << toStringJsonObject(endpointStyledChunk);
} else {
//qDebug() << "content chunk NO-text " << toStringJsonObject(endpointStyledChunk);
}
return outHTML;
}
/*
{"id":20, "clear":true, "text": [
{"append":true},
{},
{},
{},
{"content":[{ "style":"normal", "text":"When the seventh day comes and it is time for you to return to the castle in the forest, your sisters cling to your sleeves."}]},
{},
{"content":[{ "style":"normal", "text":"\"Don't go back,\" they say, and \"When will we ever see you again?\" But you imagine they will find consolation somewhere."}]},
{},
{"content":[{ "style":"normal", "text":"Your father hangs back, silent and moody. He has spent the week as far from you as possible, working until late at night. Now he speaks only to ask whether the Beast treated you \"properly.\" Since he obviously has his own ideas about what must have taken place over the past few years, you do not reply beyond a shrug."}]},
{},
{"content":[{ "style":"normal", "text":"You breathe more easily once you're back in the forest, alone."}]},
{},
{},
{},
{"content":[{ "style":"header", "text":"Bronze"}]},
{"content":[{ "style":"normal", "text":"A fractured fairy tale by Emily Short, updated to 6M62 by Shin"}]},
{"content":[{ "style":"normal", "text":"Release 12 / Serial number 161003 / Inform 7 build 6M62 (I6/v6.33 lib 6/12N) "}]},
{},
{"content":[{ "style":"normal", "text":"Have you played interactive fiction before? >"}]}
] }
* */
void RemGlkOutputLayout::stanzaRemGlkContent(QJsonArray content) {
if (! content.isEmpty()) {
inlineOutputRemGlkDebug(0, "content [\n");
for(int i=0; i< content.count(); ++i){
inlineOutputRemGlkDebug(1, " <font color=\"Brown\">!!</font> ");
QJsonObject contentWindow = content.at(i).toObject();
if (contentWindow.contains("text")) {
QJsonArray contentWindowChunks = contentWindow["text"].toArray();
if (! contentWindowChunks.isEmpty()) {
for(int c=0; c< contentWindowChunks.count(); ++c){
// "pileOfChunks" is basically a paragraph of story / book. It can be nothing more than a newline, an empty paragraph.
QJsonObject pileOfChunks = contentWindowChunks.at(c).toObject();
if (pileOfChunks.isEmpty()) {
inlineOutputRemGlkDebug(0, " ~NL~ ");
// Put in Newline, pargraph break.
textEdit->insertPlainText("\n");
qDebug() << "content chunk is newline";
} else {
// One newline per set of StyledChunks, unless append indicates otherwise.
if (! pileOfChunks.contains("append")) {
textEdit->insertPlainText("\n");
}
if (pileOfChunks.contains("content")) {
QJsonArray setStyledChunks = pileOfChunks["content"].toArray();
for(int ssc=0; ssc < setStyledChunks.count(); ++ssc){
stanzaRemGlkContentStyledChunk(setStyledChunks.at(ssc).toObject());
}
}
}
}
}
} else {
if (contentWindow.contains("lines")) {
QString outAllText = "";
int highestLineNumber = 0;
QJsonArray contentWindowLines = contentWindow["lines"].toArray();
for(int line=0; line < contentWindowLines.count(); ++line){
QJsonObject oneLineObject = contentWindowLines.at(line).toObject();
QJsonArray oneLinePileOfChunks = oneLineObject["content"].toArray();
int lineNumber = oneLineObject["line"].toInt();
if (lineNumber > highestLineNumber) {
highestLineNumber = lineNumber;
}
if (line > 0) {
outAllText += "\n";
}
if (glkWindowDebug_TextGrid) {
outAllText += "line " + QString::number(lineNumber) + ": ";
}
for(int ssc=0; ssc < oneLinePileOfChunks.count(); ++ssc){
outAllText += stanzaRemGlkContentStyledChunkToHTML(oneLinePileOfChunks.at(ssc).toObject());
}
}
// ToDo: Study the TextGrid window, and only consolidate if it is one-line "classic" style.
// ToDo: setting to enable or disable.
if (highestLineNumber == 0)
{
// For 5" phone in portrait display mode, the story "She's Actual Size" shows unneeded line wrapping.
// This can consolidate spaces.
// ToDo: know how long our lines are and skip, desktop resized and phone small alike.
if (glkGridWindowRuntimeConsolidate != "") {
// simplified should consolidate whitespaces to a single space.
// outAllText.simplified();
// Replace with interpunct to give visual hint of change
outAllText.replace(QRegExp( "[ \t]{4,}" ), " · ");
// outAllText.replace("···", " · ");
}
}
topStatus->setText(outAllText);
topStatus->setWordWrap(true);
} else {
qDebug() << "#####::: non-text non-lines content " << toStringJsonObject(contentWindow);
/*
* "{\"id\":21,\"lines\":[{\"content\":[{\"style\":\"normal\",\"text\":
* \" \"}]
* ,\"line\":0}]}"
* */
}
}
}
inlineOutputRemGlkDebug(0, "] ");
}
}