refactor: create keyPosition.ts, keyLayouts.ts

This commit is contained in:
Nebel 2020-12-29 11:17:12 +09:00
parent 21dfc3c19a
commit 24b6516d39
3 changed files with 39 additions and 28 deletions

17
src/config/keyLayouts.ts Normal file
View file

@ -0,0 +1,17 @@
/** キーボード配列。プロパティ `KeyboardEvent.key` 相当の空白 ` ` 区切りの文字列の配列 */
const keyLayouts = {
default: [
`\` 1 2 3 4 5 6 7 8 9 0 - = Backspace`,
`Tab q w e r t y u i o p [ ] \\`,
`CapsLock a s d f g h j k l ; ' Enter`,
`Shift z x c v b n m , . /`,
],
shift: [
`~ ! @ # $ % ^ & * ( ) _ + Backspace`,
`Tab Q W E R T Y U I O P { } |`,
`CapsLock A S D F G H J K L : " Enter`,
`Shift Z X C V B N M < > ?`,
],
} as const;
export default keyLayouts;

18
src/keyPosition.ts Normal file
View file

@ -0,0 +1,18 @@
/**
*
* @param layout ` `
* @param key `KeyboardEvent.key`
* @return `[x, y]` `[]`
*/
function keyPosition(
layout: readonly string[],
key: string
): [number, number] | [] {
const [x, y] = layout
.map((row) => row.split(" ").indexOf(key))
.flatMap((x, y) => (x >= 0 ? [x, y] : []));
return x >= 0 ? [x, y] : [];
}
export default keyPosition;

View file

@ -10,6 +10,8 @@ import {
WebGLRenderer, WebGLRenderer,
} from "three"; } from "three";
import randomInt from "./randomInt"; import randomInt from "./randomInt";
import keyPosition from "./keyPosition";
import keyLayouts from "./config/keyLayouts";
const root = document.body; const root = document.body;
const scene = new Scene(); const scene = new Scene();
@ -65,32 +67,6 @@ function initPanels(panels: Mesh<PlaneGeometry, MeshBasicMaterial>[]) {
} }
} }
const layouts = {
default: [
`\` 1 2 3 4 5 6 7 8 9 0 - = Backspace`,
`Tab q w e r t y u i o p [ ] \\`,
`CapsLock a s d f g h j k l ; ' Enter`,
`Shift z x c v b n m , . /`,
],
shift: [
`~ ! @ # $ % ^ & * ( ) _ + Backspace`,
`Tab Q W E R T Y U I O P { } |`,
`CapsLock A S D F G H J K L : " Enter`,
`Shift Z X C V B N M < > ?`,
],
} as const;
function position(
layout: readonly string[],
key: string
): [number, number] | [] {
const [x, y] = layout
.map((row) => row.split(" ").indexOf(key))
.flatMap((x, y) => (x >= 0 ? [x, y] : []));
return x >= 0 ? [x, y] : [];
}
function handleKeydown({ key }: KeyboardEvent) { function handleKeydown({ key }: KeyboardEvent) {
if (key === " ") { if (key === " ") {
scene.clear(); scene.clear();
@ -99,8 +75,8 @@ function handleKeydown({ key }: KeyboardEvent) {
} }
const [x = randomInt(grid.colum - 1), y = randomInt(grid.row - 1)] = [ const [x = randomInt(grid.colum - 1), y = randomInt(grid.row - 1)] = [
...position(layouts.default, key), ...keyPosition(keyLayouts.default, key),
...position(layouts.shift, key), ...keyPosition(keyLayouts.shift, key),
]; ];
const offset = () => Math.random() - 0.5; const offset = () => Math.random() - 0.5;