Skip to content

Commit

Permalink
change input
Browse files Browse the repository at this point in the history
  • Loading branch information
neolao committed Jan 28, 2024
1 parent b5d7973 commit 164a01f
Show file tree
Hide file tree
Showing 25 changed files with 197 additions and 168 deletions.
49 changes: 23 additions & 26 deletions dev-env/quick-versus.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"frame": true,
"characterColumns": 3,
"ikemenExecutablePath": "mugen.exe",
"sound": {
"volume": 30,
"background": "sound/title.mp3",
Expand Down Expand Up @@ -235,14 +236,13 @@
"right": "ArrowRight",
"up": "ArrowUp",
"down": "ArrowDown",
"a": "w",
"b": "x",
"c": "c",
"x": "q",
"y": "s",
"z": "d",
"escape": "Escape",
"enter": "Enter"
"a": "x",
"b": "w",
"x": "s",
"y": "q",
"l1": "d",
"r1": "c",
"quit": "i"
},
"gamepad": {
"left": "A17",
Expand All @@ -251,12 +251,11 @@
"down": "A20",
"a": "A0",
"b": "A1",
"c": "A7",
"x": "A2",
"y": "A3",
"z": "A6",
"escape": "A8",
"enter": "A9"
"x": "A7",
"y": "A2",
"l1": "A3",
"r1": "A6",
"quit": "A9"
}
},
"playerTwo": {
Expand All @@ -266,13 +265,12 @@
"up": "o",
"down": "l",
"a": "g",
"b": "h",
"c": "j",
"b": "f",
"x": "t",
"y": "y",
"z": "u",
"escape": "i",
"enter": "p"
"y": "r",
"l1": "y",
"r1": "h",
"quit": "o"
},
"gamepad": {
"left": "B17",
Expand All @@ -281,12 +279,11 @@
"down": "B20",
"a": "B0",
"b": "B1",
"c": "B7",
"x": "B2",
"y": "B3",
"z": "B6",
"escape": "B8",
"enter": "B9"
"x": "B7",
"y": "B2",
"l1": "B3",
"r1": "B6",
"quit": "B9"
}
}
}
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,6 @@ async function start() {
const window = createWindow();
ipcMain.handle("minimize", () => window.minimize());
ipcMain.handle("restore", () => window.restore());
ipcMain.handle("quit", () => window.close());
}
start();
3 changes: 2 additions & 1 deletion preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ contextBridge.exposeInMainWorld("mainAPI", {
configYaml: (filePath) => ipcRenderer.sendSync("configYaml", filePath),
getCurrentDirectory: () => { return ipcRenderer.sendSync("getCurrentDirectory"); },
minimize: () => ipcRenderer.invoke("minimize"),
restore: () => ipcRenderer.invoke("restore")
restore: () => ipcRenderer.invoke("restore"),
quit: () => ipcRenderer.invoke("quit")
});
9 changes: 5 additions & 4 deletions src/category/useCategoryIndex.hook.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useState, useEffect } from "react";
import { L1, R1 } from "../input/event";

export default function useCategoryIndex(categories, input, initialIndex = 0) {
const [selectedIndex, selectIndex] = useState(initialIndex);
Expand All @@ -15,12 +16,12 @@ export default function useCategoryIndex(categories, input, initialIndex = 0) {
selectIndex((selectedIndex + 1) % categories.length);
};

input.addEventListener("x", decreaseIndex);
input.addEventListener("y", increaseIndex);
input.addEventListener(L1, decreaseIndex);
input.addEventListener(R1, increaseIndex);

return () => {
input.removeEventListener("x", decreaseIndex);
input.removeEventListener("y", increaseIndex);
input.removeEventListener(L1, decreaseIndex);
input.removeEventListener(R1, increaseIndex);
};
}, [input, categories, selectedIndex]);

Expand Down
9 changes: 5 additions & 4 deletions src/character/useCharacterAILevel.hook.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useState, useEffect } from "react";
import useMoveCursorSound from "../configuration/useMoveCursorSound.hook";
import { DOWN, UP } from "../input/event";

export default function useCharacterAILevel(input, initialLevel = 0) {
const moveCursorSound = useMoveCursorSound();
Expand All @@ -19,12 +20,12 @@ export default function useCharacterAILevel(input, initialLevel = 0) {
moveCursorSound.play();
};

input.addEventListener("up", decreaseLevel);
input.addEventListener("down", increaseLevel);
input.addEventListener(UP, decreaseLevel);
input.addEventListener(DOWN, increaseLevel);

return () => {
input.removeEventListener("up", decreaseLevel);
input.removeEventListener("down", increaseLevel);
input.removeEventListener(UP, decreaseLevel);
input.removeEventListener(DOWN, increaseLevel);
};
}, [input, level, moveCursorSound]);

Expand Down
9 changes: 5 additions & 4 deletions src/character/useCharacterColorIndex.hook.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useState, useEffect } from "react";
import useMoveCursorSound from "../configuration/useMoveCursorSound.hook";
import { DOWN, UP } from "../input/event";

export default function useCharacterColorIndex(input, total, initialIndex = 1) {
const moveCursorSound = useMoveCursorSound();
Expand All @@ -24,12 +25,12 @@ export default function useCharacterColorIndex(input, total, initialIndex = 1) {
moveCursorSound.play();
};

input.addEventListener("up", decreaseIndex);
input.addEventListener("down", increaseIndex);
input.addEventListener(UP, decreaseIndex);
input.addEventListener(DOWN, increaseIndex);

return () => {
input.removeEventListener("up", decreaseIndex);
input.removeEventListener("down", increaseIndex);
input.removeEventListener(UP, decreaseIndex);
input.removeEventListener(DOWN, increaseIndex);
};
}, [input, total, currentIndex, moveCursorSound]);

Expand Down
17 changes: 9 additions & 8 deletions src/character/useCharacterIndex.hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useState, useEffect } from "react";
import getCharactersMatrix from "./getCharactersMatrix";
import useCharacterColumns from "../configuration/useCharacterColumns.hook";
import useMoveCursorSound from "../configuration/useMoveCursorSound.hook";
import { DOWN, UP, LEFT, RIGHT } from "../input/event";

export default function useCharacterIndex(characters, input, initialIndex = 0) {
const moveCursorSound = useMoveCursorSound();
Expand Down Expand Up @@ -79,16 +80,16 @@ export default function useCharacterIndex(characters, input, initialIndex = 0) {
moveCursorSound.play();
};

input.addEventListener("left", decreaseColumnIndex);
input.addEventListener("right", increaseColumnIndex);
input.addEventListener("up", decreaseRowIndex);
input.addEventListener("down", increaseRowIndex);
input.addEventListener(LEFT, decreaseColumnIndex);
input.addEventListener(RIGHT, increaseColumnIndex);
input.addEventListener(UP, decreaseRowIndex);
input.addEventListener(DOWN, increaseRowIndex);

return () => {
input.removeEventListener("left", decreaseColumnIndex);
input.removeEventListener("right", increaseColumnIndex);
input.removeEventListener("up", decreaseRowIndex);
input.removeEventListener("down", increaseRowIndex);
input.removeEventListener(LEFT, decreaseColumnIndex);
input.removeEventListener(RIGHT, increaseColumnIndex);
input.removeEventListener(UP, decreaseRowIndex);
input.removeEventListener(DOWN, increaseRowIndex);
};
}, [input, characters, columnCount, initialIndex, moveCursorSound]);

Expand Down
9 changes: 5 additions & 4 deletions src/character/useCharacterStyleIndex.hook.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useState, useEffect } from "react";
import useMoveCursorSound from "../configuration/useMoveCursorSound.hook";
import { DOWN, UP } from "../input/event";

export default function useCharacterStyleIndex(input, total, initialIndex = 0) {
const moveCursorSound = useMoveCursorSound();
Expand All @@ -24,12 +25,12 @@ export default function useCharacterStyleIndex(input, total, initialIndex = 0) {
moveCursorSound.play();
};

input.addEventListener("up", decreaseIndex);
input.addEventListener("down", increaseIndex);
input.addEventListener(UP, decreaseIndex);
input.addEventListener(DOWN, increaseIndex);

return () => {
input.removeEventListener("up", decreaseIndex);
input.removeEventListener("down", increaseIndex);
input.removeEventListener(UP, decreaseIndex);
input.removeEventListener(DOWN, increaseIndex);
};
}, [input, total, currentIndex, moveCursorSound]);

Expand Down
24 changes: 24 additions & 0 deletions src/input/event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export const UP = "up";
export const LEFT = "left";
export const RIGHT = "right";
export const DOWN = "down";
export const A = "a";
export const B = "b";
export const X = "x";
export const Y = "y";
export const L1 = "l1";
export const R1 = "r1";
export const QUIT = "quit";
export default {
UP,
DOWN,
LEFT,
RIGHT,
A,
B,
X,
Y,
L1,
R1,
QUIT
};
62 changes: 28 additions & 34 deletions src/input/useInput.hook.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useEffect, useRef } from "react";
import useKeyboard from "./useKeyboard.hook";
import useGamepad from "./useGamepad.hook";
import { UP, DOWN, LEFT, RIGHT, A, B, X, Y, L1, R1, QUIT } from "./event";

export default function useInput(keyboardMapping, gamepadMapping) {
const emitterRef = useRef(new EventTarget());
Expand All @@ -10,51 +11,48 @@ export default function useInput(keyboardMapping, gamepadMapping) {

useEffect(() => {
const onPressLeft = () => {
emitter.dispatchEvent(new Event("left"));
emitter.dispatchEvent(new Event(LEFT));
emitter.dispatchEvent(new Event("*"));
};
const onPressRight = () => {
emitter.dispatchEvent(new Event("right"));
emitter.dispatchEvent(new Event(RIGHT));
emitter.dispatchEvent(new Event("*"));
};
const onPressUp = () => {
emitter.dispatchEvent(new Event("up"));
emitter.dispatchEvent(new Event(UP));
emitter.dispatchEvent(new Event("*"));
};
const onPressDown = () => {
emitter.dispatchEvent(new Event("down"));
emitter.dispatchEvent(new Event(DOWN));
emitter.dispatchEvent(new Event("*"));
};
const onPressA = () => {
emitter.dispatchEvent(new Event("a"));
emitter.dispatchEvent(new Event(A));
emitter.dispatchEvent(new Event("*"));
};
const onPressB = () => {
emitter.dispatchEvent(new Event("b"));
emitter.dispatchEvent(new Event("*"));
};
const onPressC = () => {
emitter.dispatchEvent(new Event("c"));
emitter.dispatchEvent(new Event(B));
emitter.dispatchEvent(new Event("*"));
};
const onPressX = () => {
emitter.dispatchEvent(new Event("x"));
emitter.dispatchEvent(new Event(X));
emitter.dispatchEvent(new Event("*"));
};
const onPressY = () => {
emitter.dispatchEvent(new Event("y"));
emitter.dispatchEvent(new Event(Y));
emitter.dispatchEvent(new Event("*"));
};
const onPressZ = () => {
emitter.dispatchEvent(new Event("z"));
const onPressL1 = () => {
emitter.dispatchEvent(new Event(L1));
emitter.dispatchEvent(new Event("*"));
};
const onPressEscape = () => {
emitter.dispatchEvent(new Event("escape"));
const onPressR1 = () => {
emitter.dispatchEvent(new Event(R1));
emitter.dispatchEvent(new Event("*"));
};
const onPressEnter = () => {
emitter.dispatchEvent(new Event("enter"));
const onPressQuit = () => {
console.log("onPressQuit " + QUIT);
emitter.dispatchEvent(new Event(QUIT));
emitter.dispatchEvent(new Event("*"));
};

Expand All @@ -64,24 +62,22 @@ export default function useInput(keyboardMapping, gamepadMapping) {
keyboard.addEventListener(keyboardMapping.down, onPressDown);
keyboard.addEventListener(keyboardMapping.a, onPressA);
keyboard.addEventListener(keyboardMapping.b, onPressB);
keyboard.addEventListener(keyboardMapping.c, onPressC);
keyboard.addEventListener(keyboardMapping.x, onPressX);
keyboard.addEventListener(keyboardMapping.y, onPressY);
keyboard.addEventListener(keyboardMapping.z, onPressZ);
keyboard.addEventListener(keyboardMapping.escape, onPressEscape);
keyboard.addEventListener(keyboardMapping.enter, onPressEnter);
keyboard.addEventListener(keyboardMapping.l1, onPressL1);
keyboard.addEventListener(keyboardMapping.r1, onPressR1);
keyboard.addEventListener(keyboardMapping.quit, onPressQuit);
gamepad.addEventListener(gamepadMapping.left, onPressLeft);
gamepad.addEventListener(gamepadMapping.right, onPressRight);
gamepad.addEventListener(gamepadMapping.up, onPressUp);
gamepad.addEventListener(gamepadMapping.down, onPressDown);
gamepad.addEventListener(gamepadMapping.a, onPressA);
gamepad.addEventListener(gamepadMapping.b, onPressB);
gamepad.addEventListener(gamepadMapping.c, onPressC);
gamepad.addEventListener(gamepadMapping.x, onPressX);
gamepad.addEventListener(gamepadMapping.y, onPressY);
gamepad.addEventListener(gamepadMapping.z, onPressZ);
gamepad.addEventListener(gamepadMapping.escape, onPressEscape);
gamepad.addEventListener(gamepadMapping.enter, onPressEnter);
gamepad.addEventListener(gamepadMapping.l1, onPressL1);
gamepad.addEventListener(gamepadMapping.r1, onPressR1);
gamepad.addEventListener(gamepadMapping.quit, onPressQuit);

return () => {
keyboard.removeEventListener(keyboardMapping.left, onPressLeft);
Expand All @@ -90,24 +86,22 @@ export default function useInput(keyboardMapping, gamepadMapping) {
keyboard.removeEventListener(keyboardMapping.down, onPressDown);
keyboard.removeEventListener(keyboardMapping.a, onPressA);
keyboard.removeEventListener(keyboardMapping.b, onPressB);
keyboard.removeEventListener(keyboardMapping.c, onPressC);
keyboard.removeEventListener(keyboardMapping.x, onPressX);
keyboard.removeEventListener(keyboardMapping.y, onPressY);
keyboard.removeEventListener(keyboardMapping.z, onPressZ);
keyboard.removeEventListener(keyboardMapping.escape, onPressEscape);
keyboard.removeEventListener(keyboardMapping.enter, onPressEnter);
keyboard.removeEventListener(keyboardMapping.l1, onPressL1);
keyboard.removeEventListener(keyboardMapping.r1, onPressR1);
keyboard.removeEventListener(keyboardMapping.quit, onPressQuit);
gamepad.removeEventListener(gamepadMapping.left, onPressLeft);
gamepad.removeEventListener(gamepadMapping.right, onPressRight);
gamepad.removeEventListener(gamepadMapping.up, onPressUp);
gamepad.removeEventListener(gamepadMapping.down, onPressDown);
gamepad.removeEventListener(gamepadMapping.a, onPressA);
gamepad.removeEventListener(gamepadMapping.b, onPressB);
gamepad.removeEventListener(gamepadMapping.c, onPressC);
gamepad.removeEventListener(gamepadMapping.x, onPressX);
gamepad.removeEventListener(gamepadMapping.y, onPressY);
gamepad.removeEventListener(gamepadMapping.z, onPressZ);
gamepad.removeEventListener(gamepadMapping.escape, onPressEscape);
gamepad.removeEventListener(gamepadMapping.enter, onPressEnter);
gamepad.removeEventListener(gamepadMapping.l1, onPressL1);
gamepad.removeEventListener(gamepadMapping.r1, onPressR1);
gamepad.removeEventListener(gamepadMapping.quit, onPressQuit);
};
}, [emitter, keyboardMapping, gamepadMapping, keyboard, gamepad]);

Expand Down
Loading

0 comments on commit 164a01f

Please sign in to comment.