Custom configuration now functions

This commit is contained in:
2025-10-28 14:35:03 -04:00
parent f1117bf925
commit 94fd41e424
2 changed files with 68 additions and 0 deletions

View File

@ -78,6 +78,7 @@
</div>
<button id="generate-inputs-btn" type="button">Generate Position Inputs</button>
<button id="delete-inputs-btn" type="button">Remove Position Inputs</button>
<div id="module-position-inputs" class="position-inputs">
<!-- Dynamically generated position inputs will appear here -->

View File

@ -171,6 +171,7 @@ const maxSpeedOutput = document.getElementById('max-speed-value');
// Get button elements
const resetBtn = document.getElementById('reset-btn');
const generateInputsBtn = document.getElementById('generate-inputs-btn');
const clearInputsBtn = document.getElementById('delete-inputs-btn');
const applyCustomBtn = document.getElementById('apply-custom-btn');
// Preset buttons
@ -266,11 +267,77 @@ preset8WheelBtn.addEventListener('click', () => {
updateModuleDisplays(robot);
});
generateInputsBtn.addEventListener('click', () => {
const count = parseInt(moduleCountInput.value);
if (isNaN(count) || count < 2) {
alert('Please enter a valid number of modules between 2 and 12.');
return;
}
generateModuleInputs(count);
applyCustomBtn.style.display = 'block';
});
clearInputsBtn.addEventListener('click', () => {
generateModuleInputs(0);
applyCustomBtn.style.display = 'none';
});
applyCustomBtn.addEventListener('click', () => {
const container = document.getElementById('module-position-inputs');
const moduleElements = container.childNodes;
const customModules = [];
for (let i = 0; i < moduleElements.length; i++) {
const xInput = document.getElementById(`module-${i}-x`);
const yInput = document.getElementById(`module-${i}-y`);
const nameInput = document.getElementById(`module-${i}-name`);
const x = parseFloat(xInput.value);
const y = parseFloat(yInput.value);
const name = nameInput.value.trim();
customModules.push({ x, y, name });
}
robot.setModules(customModules);
robot.setName("Custom Configuration");
createModuleDisplays(robot);
updateModuleDisplays(robot);
});
/*
* END LISTENER CODE
* BEGIN DYNAMIC DOM FUNCTIONS
*/
function generateModuleInputs(count) {
const container = document.getElementById('module-position-inputs');
container.innerHTML = ''; // Clear existing inputs
for (let i = 0; i < count; i++) {
const moduleFieldset = document.createElement('fieldset');
moduleFieldset.className = 'module-input-group';
moduleFieldset.innerHTML = `
<legend>Module ${i + 1}</legend>
<div class="control-group">
<label for="module-${i}-name">Module Name</label>
<input type="text" id="module-${i}-name" value="Module ${i + 1}" required>
</div>
<div class="control-group">
<label for="module-${i}-x">X Position (pixels)</label>
<input type="number" id="module-${i}-x" step="1" value="0" required>
</div>
<div class="control-group">
<label for="module-${i}-y">Y Position (pixels)</label>
<input type="number" id="module-${i}-y" step="0.1" value="0" required>
</div>
`;
container.appendChild(moduleFieldset);
}
}
function createModuleDisplays(robot) {
const grid = document.getElementById('module-grid');
grid.innerHTML = ''; // Delete any pre-existing elements before creating new ones