Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
259 lines (205 loc) · 8.29 KB
/
Copy pathscript.js
File metadata and controls
259 lines (205 loc) · 8.29 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
259
document.getElementById('add-parent-node').addEventListener('click', addParentNode);
document.getElementById('generate-prompt').addEventListener('click', generatePrompt);
document.getElementById('script-uploads').addEventListener('change', handleScriptUploads);
let selectedNode = null;
function addParentNode() {
const parentNode = document.createElement('li');
const nodeName = prompt('Enter parent node name:');
if (!nodeName) return;
parentNode.onmouseenter = handleNodeMouseEnter;
parentNode.onmouseleave = handleNodeMouseLeave;
parentNode.onclick = handleNodeClick;
const childrenList = document.createElement('ul');
parentNode.appendChild(childrenList);
const addChildrenText = document.createElement('button');
addChildrenText.type = 'button';
addChildrenText.textContent = 'Add Child Node';
addChildrenText.onclick = function () {
addChildNode(childrenList);
};
parentNode.appendChild(addChildrenText);
const nodeNameContainer = document.createElement('div');
nodeNameContainer.className = 'nodeNameContainer';
const nodeNameSpan = document.createElement('span');
nodeNameSpan.textContent = nodeName;
nodeNameContainer.appendChild(nodeNameSpan);
parentNode.insertBefore(nodeNameContainer, parentNode.firstChild);
document.getElementById('node-tree-list').appendChild(parentNode);
}
function addChildNode(parentList) {
const childNode = document.createElement('li');
const nodeName = prompt('Enter child node name:');
if (!nodeName) return;
childNode.onmouseenter = handleNodeMouseEnter;
childNode.onmouseleave = handleNodeMouseLeave;
childNode.onclick = handleNodeClick;
parentList.appendChild(childNode);
const nodeNameContainer = document.createElement('div');
nodeNameContainer.className = 'nodeNameContainer';
const nodeNameSpan = document.createElement('span');
nodeNameSpan.textContent = nodeName;
nodeNameContainer.appendChild(nodeNameSpan);
childNode.insertBefore(nodeNameContainer, childNode.firstChild);
}
function handleNodeMouseEnter() {
const nodeNameContainer = this.querySelector('.nodeNameContainer');
nodeNameContainer.style.backgroundColor = 'lightgray';
}
function handleNodeMouseLeave() {
const nodeNameContainer = this.querySelector('.nodeNameContainer');
nodeNameContainer.style.backgroundColor = selectedNode === this ? 'gray' : '';
}
function handleNodeClick(event) {
if (event.target.tagName === 'BUTTON') return;
if (selectedNode) {
const previousNodeNameContainer = selectedNode.querySelector('.nodeNameContainer');
previousNodeNameContainer.style.backgroundColor = '';
}
const nodeNameContainer = event.target.classList.contains('nodeNameContainer')
? event.target
: event.target.querySelector('.nodeNameContainer');
selectedNode = nodeNameContainer.parentNode;
nodeNameContainer.style.backgroundColor = 'gray';
showAddScriptLink();
if (selectedNode.getAttribute('data-script')) {
showDeleteScriptLink();
} else {
hideDeleteScriptLink();
}
}
function showAddScriptLink() {
const addScriptLink = document.getElementById('add-script');
if (!addScriptLink) {
const link = document.createElement('button');
link.id = 'add-script';
link.textContent = 'Add Script';
link.onclick = addScriptToSelectedNode;
document.getElementById('node-tree-list').parentNode.appendChild(link);
} else {
addScriptLink.style.display = 'inline-block';
}
}
function hideAddScriptLink() {
const addScriptLink = document.getElementById('add-script');
addScriptLink.style.display = 'none';
}
function showDeleteScriptLink() {
const deleteScriptLink = document.getElementById('delete-script');
if (!deleteScriptLink) {
const link = document.createElement('button');
link.id = 'delete-script';
link.textContent = 'Delete Script';
link.onclick = function () {
deleteScriptFromSelectedNode();
return false;
};
link.style.display = 'inline-block';
document.getElementById('node-tree-list').parentNode.appendChild(link);
} else {
deleteScriptLink.style.display = 'inline-block';
}
}
function hideDeleteScriptLink() {
const deleteScriptLink = document.getElementById('delete-script');
deleteScriptLink.style.display = 'none';
}
function addScriptToSelectedNode(event) {
event.preventDefault();
if (!selectedNode) return;
const scriptName = document.querySelector('.script-link.selected');
if (!scriptName) return;
selectedNode.setAttribute('data-script', `${scriptName.textContent}`);
const scriptIndicator = document.createElement('span');
scriptIndicator.textContent = ` (script: ${scriptName.textContent})`;
const nodeNameContainer = selectedNode.querySelector('.nodeNameContainer');
nodeNameContainer.appendChild(scriptIndicator);
showDeleteScriptLink();
}
function deleteScriptFromSelectedNode() {
if (!selectedNode) return;
const nodeNameContainer = selectedNode.querySelector('.nodeNameContainer');
const scriptIndicator = nodeNameContainer.querySelector('span:last-child');
nodeNameContainer.removeChild(scriptIndicator);
selectedNode.removeAttribute('data-script');
hideDeleteScriptLink();
}
function handleScriptUploads(e) {
const files = e.target.files;
let filesLength = files.length;
const reader = new FileReader();
let fileContent = document.getElementById('script-content').value;
reader.onload = function (e) {
const currentFile = files[filesLength - 1];
fileContent += `[${currentFile.name}]\n` + e.target.result + '\n\n';
if (filesLength > 1) {
const nextFile = files[filesLength - 2];
filesLength--;
reader.readAsText(nextFile);
} else {
document.getElementById('script-content').value = fileContent;
}
createScriptLink(currentFile.name);
}
if (filesLength) {
reader.readAsText(files[0]);
}
}
function createScriptLink(scriptName) {
const scriptLink = document.createElement('button');
scriptLink.type = 'button';
scriptLink.textContent = scriptName;
scriptLink.className = 'script-link';
scriptLink.addEventListener('click', handleScriptLinkClick);
document.getElementById('script-content').parentNode.insertBefore(scriptLink, document.getElementById('script-content'));
}
function handleScriptLinkClick(e) {
const scriptLinks = document.querySelectorAll('.script-link');
scriptLinks.forEach(scriptLink => {
scriptLink.classList.remove('selected');
});
e.target.classList.add('selected');
}
function generatePrompt() {
const nodeTreeList = getNodeTree(document.getElementById('node-tree-list'));
const scriptContent = document.getElementById('script-content').value;
const issues = document.getElementById('issues').value;
const expectedBehavior = document.getElementById('expected-behavior').value;
const outputText = `
Game issue in Godot 3:
${issues}
Expected behavior:
${expectedBehavior}
Encountered errors (if any):
[List any error messages or unexpected behaviors you have encountered]
Current GDScript(s) involved:
${scriptContent}
Node tree structure:
${nodeTreeList}
`;
const filename = 'Godot_Game_Issues.txt';
const filetype = 'text/plain;charset=utf-8';
const file = new Blob([outputText], {type: filetype});
const fileURL = URL.createObjectURL(file);
const link = document.createElement('a');
link.href = fileURL;
link.download = filename;
link.click();
URL.revokeObjectURL(fileURL);
}
function getNodeTree(nodeList, depth = 0) {
let structure = '';
const indentation = ' '.repeat(depth);
for (const listItem of nodeList.children) {
if (listItem.tagName === 'LI') {
const nodeText = listItem.childNodes[0].childNodes[0].textContent;
const scriptId = listItem.getAttribute('data-script') ? ` (script: ${listItem.getAttribute('data-script')})` : '';
const relationshipSymbol = depth > 0 ? '->' : '';
structure += `\n${indentation}${relationshipSymbol}${nodeText}${scriptId}`;
const childNodeList = listItem.getElementsByTagName('ul')[0];
if (childNodeList) {
structure += getNodeTree(childNodeList, depth + 1);
}
}
}
return structure;
}