-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.html
406 lines (349 loc) · 13 KB
/
index.html
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="description" content="Online tool to artfully visualize Factorio Blueprints.">
<title>Factorio Blueprint Visualizer</title>
<link rel='shortcut icon' type='image/x-icon' href='website/favicon.ico'/>
<link rel="stylesheet" href="website/index.css">
<script defer data-domain="piebro.github.io/factorio-blueprint-visualizer" src="https://plausible.io/js/plausible.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pako/2.1.0/pako.min.js"></script> <!-- for deflating the blueprint string -->
<script src="website/startExampleBlueprintString.js"></script>
<script src="website/entityProperties.js"></script>
<script src="website/drawingSettings.js"></script>
<script src="website/blueprintVisualizer.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/codemirror.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/theme/monokai.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/codemirror.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/mode/javascript/javascript.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/addon/lint/lint.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/addon/lint/lint.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/addon/lint/json-lint.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jsonlint/1.6.0/jsonlint.min.js"></script>
</head>
<script>
function newBlueprintString(blueprintString){
[currentBlueprintNames, currentBlueprintJsons] = getBlueprintList(blueprintString);
currentBlueprint = processBlueprint(currentBlueprintJsons[0])
getCurrentBlueprintEntityCount(currentBlueprint)
const selection = document.getElementById('blueprintSelection');
selection.innerHTML = ''; // Clear existing options
for (i in currentBlueprintNames){
option = document.createElement("option")
option.setAttribute("value", i)
option.innerHTML = currentBlueprintNames[i] || "unnamed"
selection.appendChild(option)
}
}
async function setup(){
// Add keyboard event listener for arrow keys
document.addEventListener('keydown', (event) => {
// Check if the active element is within a CodeMirror instance
const isInCodeMirror = event.target.closest('.CodeMirror');
if (isInCodeMirror) return;
switch(event.key) {
case 'ArrowLeft':
previousSetting();
break;
case 'ArrowRight':
nextSetting();
break;
case 'ArrowUp':
const selectUp = document.getElementById('blueprintSelection');
if (selectUp.selectedIndex > 0) {
selectUp.selectedIndex--;
blueprintSelectionOnChange(selectUp);
}
break;
case 'ArrowDown':
const selectDown = document.getElementById('blueprintSelection');
if (selectDown.selectedIndex < selectDown.options.length - 1) {
selectDown.selectedIndex++;
blueprintSelectionOnChange(selectDown);
}
break;
}
});
// Create window controllers after DOM is loaded
settingsWindow = createCodeEditorWindow({
windowId: "settingWindow",
buttonId: "settingsBtn",
buttonTextShow: "Modify Drawing Settings",
buttonTextHide: "Hide Drawing Settings",
editorOptions: {
mode: { name: "javascript", json: true },
lint: true,
gutters: ["CodeMirror-lint-markers"],
autoCloseBrackets: true,
matchBrackets: true,
indentUnit: 2,
tabSize: 2
},
onOpen: (editor) => {
editor.setValue(JSON.stringify(settingsList[currentSettingIndex], null, 2));
editor.on("change", validateAndApplySettings);
}
});
entityCountWindow = createCodeEditorWindow({
windowId: "entityCountWindow",
buttonId: "entityCountBtn",
buttonTextShow: "Show Entity Count",
buttonTextHide: "Hide Entity Count",
editorOptions: {
readOnly: true
},
initialPosition: { top: "18vh", left: "20px" },
heightOffset: 30,
onOpen: (editor) => {
const entityCounts = getCurrentBlueprintEntityCount(currentBlueprint);
const formattedCounts = entityCounts.map(([entity, count]) =>
`${entity}: ${count}`
).join('\n');
editor.setValue(formattedCounts);
}
});
newBlueprintString(START_EXAMPLE_BLUEPRINT);
showBlueprint();
// Add click handler for random color palette button
document.getElementById("randomColorPalletBtn").onclick = () => {
settingsList[currentSettingIndex] = settingsChangeColors(settingsList[currentSettingIndex], null, true);
showBlueprint();
};
}
function showBlueprint(){
updateWindows()
document.getElementById("svg-container").innerHTML = drawBlueprint(currentBlueprint, settingsList[currentSettingIndex], svgWidthInMm=300)
}
async function saveBtnClick(){
svg_str = document.getElementById("svg-container").innerHTML
const element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(svg_str));
element.setAttribute('download', "blueprint.svg");
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
function blueprintSelectionOnChange(selectObject){
currentBlueprint = processBlueprint(currentBlueprintJsons[selectObject.value])
showBlueprint()
}
function uploadBtnClick(){
input = window.prompt("Export your blueprint as text in Factorio and paste it here:")
if (input != null && input != ""){
newBlueprintString(input)
showBlueprint()
}
}
function nextSetting(){
currentSettingIndex += 1;
if (settingsList.length == currentSettingIndex) {
settingsList.push(getRandomSettings());
}
showBlueprint();
}
function previousSetting(){
if (currentSettingIndex > 0) {
currentSettingIndex -= 1;
showBlueprint();
}
}
function dragElement(elmnt) {
var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
// Move the element from anywhere inside it
elmnt.onmousedown = function(e) {
// Don't initiate drag if clicking on textarea, buttons, or the resize handle
if (e.target.tagName === 'TEXTAREA' ||
e.target.tagName === 'BUTTON' ||
e.offsetX > e.target.clientWidth - 20 && e.offsetY > e.target.clientHeight - 20) {
return;
}
dragMouseDown(e);
};
function dragMouseDown(e) {
e = e || window.event;
e.preventDefault();
// get the mouse cursor position at startup
pos3 = e.clientX;
pos4 = e.clientY;
document.onmouseup = closeDragElement;
document.onmousemove = elementDrag;
}
function elementDrag(e) {
e = e || window.event;
e.preventDefault();
// calculate the new cursor position
pos1 = pos3 - e.clientX;
pos2 = pos4 - e.clientY;
pos3 = e.clientX;
pos4 = e.clientY;
// set the element's new position
elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
}
function closeDragElement() {
// stop moving when mouse button is released
document.onmouseup = null;
document.onmousemove = null;
}
}
function createCodeEditorWindow(options) {
const {
windowId,
buttonId,
buttonTextShow = "Show Window",
buttonTextHide = "Hide Window",
editorOptions = {},
initialPosition = { top: "18vh", right: "20px" },
onOpen = () => {},
heightOffset = 60
} = options;
let editor;
document.getElementById(buttonId).onclick = () => toggle();
const window = document.getElementById(windowId);
const closeBtn = window.querySelector(".close");
closeBtn.onclick = () => {
close();
document.getElementById(buttonId).textContent = buttonTextShow;
};
function toggle() {
const window = document.getElementById(windowId);
const button = document.getElementById(buttonId);
if (window.style.display === "block") {
close();
button.textContent = buttonTextShow;
} else {
open();
button.textContent = buttonTextHide;
}
}
function open() {
const window = document.getElementById(windowId);
const windowContent = window.querySelector(".setting-window-content");
const textArea = document.getElementById(`${windowId}Text`);
if (!editor) {
editor = CodeMirror.fromTextArea(textArea, {
theme: "monokai",
lineNumbers: true,
lineWrapping: true,
...editorOptions
});
}
window.style.display = "block";
// Set initial position if not already positioned
if (!windowContent.style.top) {
Object.assign(windowContent.style, initialPosition);
}
// Make draggable
dragElement(windowContent);
// Refresh editor after modal is visible
editor.refresh();
// Add resize observer
const resizeObserver = new ResizeObserver(() => {
if (editor) {
editor.refresh();
editor.setSize(null, windowContent.clientHeight - heightOffset);
}
});
resizeObserver.observe(windowContent);
onOpen(editor);
}
function close() {
const window = document.getElementById(windowId);
window.style.display = "none";
// Clean up resize observer
const windowContent = window.querySelector(".setting-window-content");
const resizeObserver = new ResizeObserver(() => {});
resizeObserver.unobserve(windowContent);
}
return {
toggle,
open,
close,
getEditor: () => editor
};
}
function validateAndApplySettings() {
try {
const editor = window.settingsWindow.getEditor();
const newSettings = JSON.parse(editor.getValue());
settingsList[currentSettingIndex] = newSettings;
// Set flag to prevent recursive update
editor.isUpdatingFromCode = true;
showBlueprint();
editor.isUpdatingFromCode = false;
} catch (e) {
// Invalid JSON - silently fail
}
}
function updateWindows() {
// Update settings window
const settingsEditor = window.settingsWindow.getEditor();
if (settingsEditor && document.getElementById("settingWindow").style.display === "block") {
// Only update if not triggered by validateAndApplySettings
if (!settingsEditor.isUpdatingFromCode) {
settingsEditor.setValue(JSON.stringify(settingsList[currentSettingIndex], null, 2));
}
}
// Update entity count window
const entityCountEditor = window.entityCountWindow.getEditor();
if (entityCountEditor && document.getElementById("entityCountWindow").style.display === "block") {
const entityCounts = getCurrentBlueprintEntityCount(currentBlueprint);
const formattedCounts = entityCounts.map(([entity, count]) =>
`${entity}: ${count}`
).join('\n');
entityCountEditor.setValue(formattedCounts);
}
}
let settingsList = []
let currentSettingIndex = 0
let currentBlueprint = null
let currentBlueprintJsons = null
let currentBlueprintNames = null
settingsList.push(EXAMPLE_SETTINGS)
</script>
<body onload="setup()">
<div class="header-links">
<a href="https://github.com/piebro/factorio-blueprint-visualizer?ref=piebro.github.io/factorio-blueprint-visualizer">About</a>
<a href="https://github.com/piebro/factorio-blueprint-visualizer/blob/master/drawing_settings_documentation.md?ref=piebro.github.io/factorio-blueprint-visualizer">How to use</a>
<a href="https://piebro.github.io?ref=piebro.github.io/factorio-blueprint-visualizer">Other Projects</a>
</div>
<h1 style="text-align: center; color: #04AA6D; margin: 5px 0;">Factorio Blueprint Visualizer</h1>
<div class="button-container" id="button-container">
<button class="btn" id="uploadBtn" onclick="uploadBtnClick()">Upload Blueprint</button>
<button class="btn tooltip" id="nextSettingBtn" onclick="nextSetting()">
Random Drawing Setting
<span class="tooltiptext">You can also use the arrow keys</span>
</button>
<select class="btn" id="blueprintSelection" style="width: 160px;" onChange="blueprintSelectionOnChange(this)">
</select>
</div>
<div class="svg-container" id="svg-container"></div>
<div class="button-container">
<button class="btn tooltip" onclick="previousSetting()">
←
<span class="tooltiptext">You can also use the arrow keys</span>
</button>
<button class="btn" id="entityCountBtn">Show Entity Count</button>
<button class="btn" id="settingsBtn">Modify Drawing Settings</button>
<button class="btn" id="randomColorPalletBtn">Random Color Pallet</button>
<button class="btn" id="saveBtn" onclick="saveBtnClick()">Save</button>
<button class="btn tooltip" onclick="nextSetting()">
→
<span class="tooltiptext">You can also use the arrow keys</span>
</button>
</div>
<div id="settingWindow" class="setting-window">
<div class="setting-window-content">
<span class="close">×</span>
<textarea id="settingWindowText"></textarea>
</div>
</div>
<div id="entityCountWindow" class="setting-window">
<div class="setting-window-content">
<span class="close">×</span>
<textarea id="entityCountWindowText"></textarea>
</div>
</div>
</body>
</html>