Merge into master #5

Merged
Arduinum merged 2 commits from arduinum/mvp_1 into master 2025-11-17 14:24:17 +03:00

View File

@@ -63,41 +63,88 @@ document.addEventListener("DOMContentLoaded", () => {
} }
} }
const commandStop = {"command": "stop"}; // Команда остановки
const commandStop = { "command": "stop" };
// Назначение обработчиков событий для кнопки "Вперёд" let command = null;
const forwardButton = document.getElementById("forward-button"); let currentCommand = null;
forwardButton.addEventListener("mousedown", () => startSendingCommand({"command": "forward"})); // Начало отправки команды
forwardButton.addEventListener("mouseup", () => {
sendCommand(JSON.stringify(commandStop));
stopSendingCommand();
}); // отправка команды forwardOff и остановка отправки при отпускании кнопки
forwardButton.addEventListener("mouseleave", stopSendingCommand); // Остановка отправки, если курсор уходит с кнопки
// Назначение обработчиков событий для кнопки "Влево" // Управление кнопками мыши
const leftButton = document.getElementById("left-button"); function robotControlButton(buttonId, commandName) {
leftButton.addEventListener("mousedown", () => startSendingCommand({"command": "left"})); const button = document.getElementById(buttonId);
leftButton.addEventListener("mouseup", () => { if (!button) return;
sendCommand(JSON.stringify(commandStop));
stopSendingCommand(); button.addEventListener("mousedown", () => {
if (currentCommand !== commandName) {
currentCommand = commandName;
startSendingCommand({command: commandName});
}
});
const stopHandler = () => {
if (currentCommand === commandName) {
sendCommand(JSON.stringify(commandStop));
stopSendingCommand();
currentCommand = null;
}
};
button.addEventListener("mouseup", stopHandler);
button.addEventListener("mouseleave", stopHandler);
}
// Инициализация кнопок
robotControlButton("forward-button", "forward");
robotControlButton("backward-button", "backward");
robotControlButton("left-button", "left");
robotControlButton("right-button", "right");
const keyMap = {
forwardKeys: ["w", "arrowup", "8"],
backwardKeys: ["s", "arrowdown", "2"],
leftKeys: ["a", "arrowleft", "4"],
rightKeys: ["d", "arrowright", "6"]
};
let activeKey = null; // конкретная активная клавиша
document.addEventListener("keydown", (event) => {
const key = event.key.toLowerCase();
// если уже нажата какая-то клавиша — игнорируем новую
if (activeKey) return;
if (keyMap.forwardKeys.includes(key)) {
command = "forward";
} else if (keyMap.backwardKeys.includes(key)) {
command = "backward";
} else if (keyMap.leftKeys.includes(key)) {
command = "left";
} else if (keyMap.rightKeys.includes(key)) {
command = "right";
} else {
return; // неуправляющая клавиша
}
if (command !== currentCommand) {
activeKey = key; // запоминаем, какая клавиша активна
currentCommand = command;
startSendingCommand({ "command": command });
}
}); });
leftButton.addEventListener("mouseleave", stopSendingCommand);
// Назначение обработчиков событий для кнопки "Вправо" document.addEventListener("keyup", (event) => {
const rightButton = document.getElementById("right-button"); const key = event.key.toLowerCase();
rightButton.addEventListener("mousedown", () => startSendingCommand({"command": "right"}));
rightButton.addEventListener("mouseup", () => {
sendCommand(JSON.stringify(commandStop));
stopSendingCommand();
});
rightButton.addEventListener("mouseleave", stopSendingCommand);
// Назначение обработчиков событий для кнопки "Назад" // сработает стоп только если отпущена именно активная клавиша
const backwardButton = document.getElementById("backward-button"); if (key === activeKey) {
backwardButton.addEventListener("mousedown", () => startSendingCommand({"command": "backward"})); sendCommand(JSON.stringify(commandStop));
backwardButton.addEventListener("mouseup", () => { stopSendingCommand();
sendCommand(JSON.stringify(commandStop));
stopSendingCommand(); activeKey = null;
command = null;
currentCommand = null;
}
}); });
backwardButton.addEventListener("mouseleave", stopSendingCommand);
}); });