-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
114 lines (95 loc) · 3.72 KB
/
script.js
File metadata and controls
114 lines (95 loc) · 3.72 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
document.addEventListener('DOMContentLoaded', () => {
const workspace = document.getElementById('workspace');
document.querySelectorAll('.block-palette .block').forEach(block => {
block.addEventListener('dragstart', e => {
e.dataTransfer.setData('text/plain', block.dataset.type);
});
});
workspace.addEventListener('dragover', e => {
e.preventDefault();
});
workspace.addEventListener('drop', e => {
e.preventDefault();
const type = e.dataTransfer.getData('text/plain');
const newBlock = document.createElement('div');
newBlock.className = `block ${type}-block`;
newBlock.innerHTML = `
<span class="delete-btn">Remove</span>
${getBlockContent(type)}
`;
workspace.appendChild(newBlock);
addDeleteListener(newBlock);
});
});
function getBlockContent(type) {
const templates = {
straight: `Straight (
<input type="number" placeholder="Distance" class="param">
) (
<input type="number" placeholder="Power" class="param">
)`,
strafe: `Strafe (
<input type="number" placeholder="Distance" class="param">
) (
<input type="number" placeholder="Power" class="param">
)`,
turn: `Turn (
<input type="number" placeholder="Degrees" class="param">
)`
};
return templates[type] || '';
}
function addDeleteListener(block) {
block.querySelector('.delete-btn').addEventListener('click', () => {
block.remove();
});
}
function convertBlocks() {
const blocks = Array.from(document.querySelectorAll('#workspace .block'));
let output = [];
blocks.forEach((block, index) => {
if (index === 0 && block.classList.contains('start-block')) return;
const inputs = block.getElementsByTagName('input');
if (block.classList.contains('straight-block')) {
output.push(`addCommands(new Drivetrain_GyroStraight(${inputs[0].value || 0}, ${inputs[1].value || 0}));`);
}
else if (block.classList.contains('strafe-block')) {
output.push(`addCommands(new Drivetrain_GyroStrafe(${inputs[0].value || 0}, ${inputs[1].value || 0}));`);
}
else if (block.classList.contains('turn-block')) {
output.push(`addCommands(new Drivetrain_GyroTurn(${inputs[0].value || 0}));`);
}
});
output.unshift("// Generated by Tobin's Auto Coder\n");
showOutput(output.join('\n'));
}
function showOutput(code) {
const modal = document.getElementById('outputModal');
modal.style.display = 'block';
document.getElementById('convertedCode').innerHTML = Prism.highlight(code, Prism.languages.javascript, 'javascript');
document.getElementById('outputModal').style.display = 'block';
document.querySelectorAll('.param').forEach(input => {
input.addEventListener('dblclick', function() {
this.focus();
this.select();
});
});
window.onclick = function(event) {
const modal = document.getElementById('outputModal');
if (event.target == modal) {
closeModal();
}
}
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape') closeModal();
});
}
function copyCode() {
const code = document.getElementById('convertedCode').textContent;
navigator.clipboard.writeText(code).then(() => {
alert('Code copied to clipboard!');
});
}
function closeModal() {
document.getElementById('outputModal').style.display = 'none';
}