feat(client): add touch ui and handlers for controls

This commit is contained in:
Julien Oculi 2024-06-25 14:02:59 +02:00
parent ff6604dfa1
commit a941446dd6

View file

@ -32,7 +32,7 @@
</label> </label>
<button>Se connecter</button> <button>Se connecter</button>
</form> </form>
<div class="controls"> <div class="input-controls">
<label> <label>
<span>Rotation en degrés</span> <span>Rotation en degrés</span>
<input <input
@ -58,6 +58,31 @@
/> />
</label> </label>
</div> </div>
<div class="touch-controls" id="touch-controls">
<span class="placeholder"></span>
<button title="Avancer" data-command="forward" data-value="2">
⬆️
</button>
<span class="placeholder"></span>
<button title="Tourner à gauche" data-command="left" data-value="5">
⬅️
</button>
<button title="Arrêter" data-command="stop" data-value="0">
⏸️
</button>
<button
title="Tourner à droite"
data-command="right"
data-value="5"
>
➡️
</button>
<span class="placeholder"></span>
<button title="Reculer" data-command="backward" data-value="2">
⬇️
</button>
<span class="placeholder"></span>
</div>
<h2>Contrôle</h2> <h2>Contrôle</h2>
<p> <p>
Amusez vous ! Pour contrôler le robot, utilisez les flèches Amusez vous ! Pour contrôler le robot, utilisez les flèches
@ -77,6 +102,51 @@
assignCommands('left', 'right', event) assignCommands('left', 'right', event)
) )
// Buttons handlers
for (const button of document
.querySelector('#touch-controls')
.querySelectorAll('button')) {
button.addEventListener('click', () => {
const { command, value } = button.dataset
// Check datas are set for button
if (command === undefined || value === undefined) {
alert(
`Pas de command ou de valeur assigné au bouton "${button.title}"`
)
throw new Error(
`no command or value assigned to ${button.title}`
)
}
// Check command is valid
if (
!['forward', 'backward', 'left', 'right', 'stop'].includes(
command
)
) {
alert(
`La command "${command}" n'est pas valide en tant que ('forward', 'backward', 'left', 'right', 'stop')`
)
throw new Error(
`specified command "${command}" is not in ['forward', 'backward', 'left', 'right', 'stop']`
)
}
// Check value is valid finite number
if (!Number.isFinite(Number(value))) {
alert(
`La valeur "${value}" n'est convertible en entier fini`
)
throw new Error(
`value "${value}" cannot be casted to finite integer`
)
}
sendCommand(command, Number(value))
})
}
// Keyboard handler // Keyboard handler
document.addEventListener('keydown', ({ code }) => { document.addEventListener('keydown', ({ code }) => {
if (code === 'ArrowUp') { if (code === 'ArrowUp') {
@ -208,4 +278,10 @@
return sendCommand(positiveCommand, value) return sendCommand(positiveCommand, value)
} }
</script> </script>
<style>
.touch-controls {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
</style>
</html> </html>