-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeeditorwindow.cpp
More file actions
258 lines (213 loc) · 8.05 KB
/
codeeditorwindow.cpp
File metadata and controls
258 lines (213 loc) · 8.05 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
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
#include "codeeditorwindow.h"
CodeEditorWindow::CodeEditorWindow(Game *g) :
game(g),
do_add_script(false)
{
wrapmode = QTextOption::NoWrap;
textedits.resize(2);
textedits[0] = new QPlainTextEdit();
wordwrap_checkbox = new QCheckBox();
wordwrap_checkbox->setChecked(false);
wordwrap_checkbox->setText("Word Wrap");
connect(wordwrap_checkbox, SIGNAL(clicked(bool)), this, SLOT(SlotToggleWordWrap()));
button_setroomcode = new QPushButton("Save");
connect(button_setroomcode, SIGNAL(clicked(bool)), this, SLOT(SlotSaveChanges()));
button_getroomcode = new QPushButton("Load");
connect(button_getroomcode, SIGNAL(clicked(bool)), this, SLOT(SlotGetRoomCode()));
tabwidget = new QTabWidget();
tabwidget->setStyleSheet("QWidget {color: #FFFFFF; background: #2F363B;}"); //Hover: #3E4D54; Click: #1F2227
connect(tabwidget, SIGNAL(currentChanged(int)), this, SLOT(UpdateTabContents(int)));
QGridLayout * v = new QGridLayout();
v->addWidget(wordwrap_checkbox,0,0);
v->addWidget(button_getroomcode, 1, 0);
v->addWidget(button_setroomcode, 1, 1);
v->addWidget(tabwidget,2,0,1,2);
QFont f("unexistent");
f.setStyleHint(QFont::Monospace);
error_log = new QPlainTextEdit();
error_log->setFont(f);
error_log->setWordWrapMode(QTextOption::WordWrap);
error_log_clear_btn = new QPushButton("Clear");
connect(error_log_clear_btn, SIGNAL(clicked(bool)), this, SLOT(ClearErrorLog()));
v->addWidget(new QLabel("Log:"),3,0,1,1);
v->addWidget(error_log_clear_btn,3,1,1,1);
v->addWidget(error_log,4,0,1,2);
v->setAlignment(Qt::AlignTop);
// v->setSpacing(0);
// v->setMargin(1);
setLayout(v);
}
void CodeEditorWindow::ClearErrorLog()
{
error_log->setPlainText("");
}
void CodeEditorWindow::AddError(const QString s)
{
//59.6 - error log only contains last 2000 characters
const unsigned int max_error_log_length = 2000;
error_log->setPlainText(error_log->toPlainText().right(max_error_log_length) + s + "\n");
error_log->verticalScrollBar()->setValue(error_log->verticalScrollBar()->maximum());
}
void CodeEditorWindow::UpdateTabContents(int i)
{
if (i > 0 && i == tabwidget->count()-1) {
do_add_script = true;
}
cur_room.clear();
}
void CodeEditorWindow::Update()
{
QPointer <Room> r = game->GetEnvironment()->GetCurRoom();
if (r.isNull()) {
return;
}
if (do_add_script) {
//59.3 - create new script
r->AddNewAssetScript();
do_add_script = false;
}
//update if empty and visible, or we changed rooms
if (r->GetLoaded() && cur_room != r) {
//temporarily disconnect index changed signal
const int last_index = tabwidget->currentIndex();
disconnect(tabwidget, SIGNAL(currentChanged(int)), 0, 0);
cur_room = r;
//obtain list of AssetScripts
QFont f("unexistent");
f.setStyleHint(QFont::Monospace);
QString room_code;
QTextStream ofs(&room_code);
r->SaveXML(ofs);
QHash <QString, QPointer <AssetScript> > scripts = r->GetAssetScripts();
//delete existing textedits, and resize vector
for (int i=0; i<textedits.size(); ++i) {
if (textedits[i]) {
delete textedits[i];
}
}
textedits.resize(scripts.size()+2);
tabwidget->clear();
textedits[0] = new QPlainTextEdit();
textedits[0]->setFont(f);
textedits[0]->setWordWrapMode(wrapmode);
textedits[0]->setPlainText(room_code);
tabwidget->addTab(textedits[0], "JML");
int i = 0;
for (QPointer <AssetScript> & a : scripts) {
++i;
if (a) {
textedits[i] = new QPlainTextEdit();
textedits[i]->setFont(f);
textedits[i]->setWordWrapMode(wrapmode);
if (a->GetFinished() && !a->GetError()) {
textedits[i]->setPlainText(a->GetJSCode());
}
else if (a->GetError()) {
textedits[i]->setPlainText(QString("Error: ") + a->GetErrorString());
}
else {
textedits[i]->setPlainText("Loading...");
}
tabwidget->addTab(textedits[i], a->GetS("src"));
}
}
//59.3 - add "plus" tab, to create new assetscripts
textedits[textedits.size()-1] = new QPlainTextEdit();
tabwidget->addTab(textedits[textedits.size()-1], "+");
if (last_index < tabwidget->count()) {
tabwidget->setCurrentIndex(last_index);
}
//reconnect index changed signal
connect(tabwidget, SIGNAL(currentChanged(int)), this, SLOT(UpdateTabContents(int)));
}
//update error log
if (game) {
// qDebug() << "errors" << MathUtil::GetErrorLogTemp().size() << AssetScript::GetErrors().size();
const QStringList accum_errs = MathUtil::GetErrorLogTemp();
for (int i=0; i<accum_errs.size(); ++i) {
AddError(accum_errs[i]);
}
MathUtil::ClearErrorLogTemp();
}
}
bool CodeEditorWindow::GetHasFocus()
{
for (int i=0;i<textedits.size(); ++i) {
if (textedits[i] && textedits[i]->hasFocus()) {
return true;
}
}
return false;
}
void CodeEditorWindow::SlotSaveChanges()
{
const QString room_filename = game->GetEnvironment()->GetCurRoom()->GetSaveFilename();
const bool visible = isVisible();
if (!visible) { //not visible, just save as usual
game->SaveRoom(room_filename);
}
else { //visible, save directly what's in the text, and update the room
QFile file(room_filename);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
qDebug() << "CodeEditorWindow::SlotSaveChanges(): File " << room_filename << " can't be saved";
return;
}
//save out the data
QTextStream ofs(&file);
ofs << textedits[0]->toPlainText();
//close file, report saving ok
file.close();
QPointer <Room> r = game->GetEnvironment()->GetCurRoom();
QHash <QString, QPointer <AssetScript> > scripts = r->GetAssetScripts();
int i = 0;
for (QPointer <AssetScript> & a : scripts) {
++i;
if (a) {
//59.4 - do not save if script had not finished loading, or Loading... message is shown
QString js_code = textedits[i]->toPlainText();
if (!a->GetFinished() || js_code == "Loading...") {
continue;
}
a->SetJSCode(textedits[i]->toPlainText());
const QString js_filename = QUrl(a->GetS("_src_url")).toLocalFile();
// qDebug() << "a->GetFullURL();" << a->GetFullURL() << js_filename;
QFile file(js_filename);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
qDebug() << "CodeEditorWindow::SlotSaveChanges(): JS File " << js_filename << " can't be saved";
return;
}
//save out the data
QTextStream ofs(&file);
ofs << textedits[i]->toPlainText();
//close file, report saving ok
file.close();
}
}
if (!textedits.isEmpty()) {
const QString code = textedits[0]->toPlainText();
game->GetEnvironment()->UpdateRoomCode(code);
}
}
}
void CodeEditorWindow::SlotToggleWordWrap()
{
if (wordwrap_checkbox->isChecked()) {
wrapmode = QTextOption::WordWrap;
}
else {
wrapmode = QTextOption::NoWrap;
}
for (int i=0;i<textedits.size(); ++i) {
textedits[i]->setWordWrapMode(wrapmode);
}
}
void CodeEditorWindow::SlotGetRoomCode()
{
QString room_code;
QTextStream ofs(&room_code);
game->GetEnvironment()->GetCurRoom()->SaveXML(ofs);
if (!textedits.isEmpty()) {
// qDebug() << "CodeEditorWindow::SlotGetRoomCode()" << room_code;
textedits[0]->setPlainText(room_code);
}
}