index change key
This commit is contained in:
parent
8796d5b604
commit
88ab307384
@ -21,7 +21,7 @@ body {
|
|||||||
|
|
||||||
h1 {
|
h1 {
|
||||||
font-size: 3em;
|
font-size: 3em;
|
||||||
margin: 40px 20px 20px 20px;
|
margin: 20px;
|
||||||
text-shadow: 3px 2px rgb(153, 145, 175);
|
text-shadow: 3px 2px rgb(153, 145, 175);
|
||||||
text-align: center;
|
text-align: center;
|
||||||
}
|
}
|
||||||
@ -35,20 +35,14 @@ button {
|
|||||||
display: grid;
|
display: grid;
|
||||||
grid-template-columns: repeat(4, 1fr);
|
grid-template-columns: repeat(4, 1fr);
|
||||||
grid-gap: 20px;
|
grid-gap: 20px;
|
||||||
margin: 40px auto;
|
margin: 80px auto;
|
||||||
width: 700px;
|
width: 700px;
|
||||||
justify-items: left;
|
justify-items: left;
|
||||||
}
|
}
|
||||||
|
|
||||||
.cookie {
|
|
||||||
margin: 40px 0 0 0;
|
|
||||||
font-size: 0.8em;
|
|
||||||
justify-self: center;
|
|
||||||
width: auto;
|
|
||||||
grid-column: 1 / 5;
|
|
||||||
}
|
|
||||||
|
|
||||||
.play {
|
.play {
|
||||||
grid-column: 2 / 4;
|
grid-column: 2 / 4;
|
||||||
justify-self: center;
|
justify-self: center;
|
||||||
|
width: 70%;
|
||||||
|
margin: 40px;
|
||||||
}
|
}
|
||||||
|
14
index.html
14
index.html
@ -4,23 +4,13 @@
|
|||||||
<meta charset="utf-8" />
|
<meta charset="utf-8" />
|
||||||
<title>Webtris</title>
|
<title>Webtris</title>
|
||||||
<link rel="stylesheet" type="text/css" href="css/index.css" />
|
<link rel="stylesheet" type="text/css" href="css/index.css" />
|
||||||
|
<script type="text/javascript" src="js/index.js"></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>WEBTRIS</h1>
|
<h1>WEBTRIS</h1>
|
||||||
<form action="webtris.html" method="post">
|
<form action="webtris.html" method="post">
|
||||||
<div class="actions">
|
<div class="actions">
|
||||||
<div>GAUCHE</div><button type="button">ArrowLeft</button>
|
<script>loadButtons()</script>
|
||||||
<div>DROITE</div><button type="button">ArrowRight</button>
|
|
||||||
<div>CHUTE LENTE</div><button type="button">ArrowDown</button>
|
|
||||||
<div>CHUTE RAPIDE</div><button type="button">Space</button>
|
|
||||||
<div>ROTATION HORAIRE</div><button type="button">ArrowUp</button>
|
|
||||||
<div>ROTATION INVERSE</div><button type="button">z</button>
|
|
||||||
<div>GARDE</div><button type="button">c</button>
|
|
||||||
<div>PAUSE</div><button type="button">Escape</button>
|
|
||||||
<div class="cookie">
|
|
||||||
<input type="checkbox" id="cookie" name="cookie">
|
|
||||||
<label for="cookie">Utiliser un cookie pour enregistrer les préférences sur cet ordinateur ?</label>
|
|
||||||
</div>
|
|
||||||
<button type="submit" class="play">PLAY</button>
|
<button type="submit" class="play">PLAY</button>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
57
js/index.js
Normal file
57
js/index.js
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
const actionLabel = [
|
||||||
|
{name: "moveLeft", label: "GAUCHE"},
|
||||||
|
{name: "moveRight", label: "DROITE"},
|
||||||
|
{name: "softDrop", label: "CHUTE LENTE"},
|
||||||
|
{name: "hardDrop", label: "CHUTE RAPIDE"},
|
||||||
|
{name: "rotateCW", label: "ROTATION HORAIRE"},
|
||||||
|
{name: "rotateCCW:", label: "ROTATE INVERSE"},
|
||||||
|
{name: "hold", label: "GARDE"},
|
||||||
|
{name: "pause", label: "PAUSE"}
|
||||||
|
]
|
||||||
|
const actionsDefaultKeys = {
|
||||||
|
moveLeft: "ArrowLeft",
|
||||||
|
moveRight: "ArrowRight",
|
||||||
|
softDrop: "ArrowDown",
|
||||||
|
hardDrop: " ",
|
||||||
|
rotateCW: "ArrowUp",
|
||||||
|
rotateCCW: "z",
|
||||||
|
hold: "c",
|
||||||
|
pause: "Escape",
|
||||||
|
}
|
||||||
|
var selectedButton = null
|
||||||
|
var selectedAction = ""
|
||||||
|
|
||||||
|
function getKey(action) {
|
||||||
|
key = localStorage.getItem(action) || actionsDefaultKeys[action]
|
||||||
|
if (key == ' ')
|
||||||
|
return "Space"
|
||||||
|
else
|
||||||
|
return key
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadButtons() {
|
||||||
|
document.open()
|
||||||
|
document.write(
|
||||||
|
actionLabel.map(action => `<div>${action.label}</div>
|
||||||
|
<button type="button" onclick="changeKey(this, '${action.name}')">${getKey(action.name)}</button>
|
||||||
|
`).join("\n")
|
||||||
|
)
|
||||||
|
document.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
function changeKey(button, action) {
|
||||||
|
button.innerHTML = "Touche ?"
|
||||||
|
selectedButton = button
|
||||||
|
selectedAction = action
|
||||||
|
button.blur()
|
||||||
|
}
|
||||||
|
|
||||||
|
function keyUpHandler(e) {
|
||||||
|
if (selectedButton) {
|
||||||
|
localStorage.setItem(selectedAction, e.key)
|
||||||
|
selectedButton.innerHTML = (e.key == " ") ? "Space" : e.key
|
||||||
|
selectedButton = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
addEventListener("keyup", keyUpHandler, false)
|
Reference in New Issue
Block a user