Skip to content

Commit

Permalink
Fixed joint inputs being global and modifier keys changing button bin…
Browse files Browse the repository at this point in the history
…dings (ex: shift+1 -> shift+!)
  • Loading branch information
LucaHaverty committed Jun 20, 2024
1 parent c7ea0b9 commit 1f67780
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 36 deletions.
33 changes: 13 additions & 20 deletions fission/src/systems/input/InputSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ declare global {

type Input = {
name: string
keybind: string
keyCode: string
isGlobal: boolean
modifiers: ModifierState
}
Expand All @@ -20,14 +20,14 @@ export const emptyModifierState: ModifierState = { ctrl: false, alt: false, shif

// When a robot is loaded, default inputs replace any unassigned inputs
const defaultInputs: { [key: string]: Input } = {
"intake": { name: "intake", keybind: "e", isGlobal: true, modifiers: emptyModifierState },
"shootGamepiece": { name: "shootGamepiece", keybind: "q", isGlobal: true, modifiers: emptyModifierState },
"enableGodMode": { name: "enableGodMode", keybind: "g", isGlobal: true, modifiers: emptyModifierState },

"arcadeForward": { name: "arcadeForward", keybind: "w", isGlobal: false, modifiers: emptyModifierState },
"arcadeBackward": { name: "arcadeBackward", keybind: "s", isGlobal: false, modifiers: emptyModifierState },
"arcadeLeft": { name: "arcadeLeft", keybind: "a", isGlobal: false, modifiers: emptyModifierState },
"arcadeRight": { name: "arcadeRight", keybind: "d", isGlobal: false, modifiers: emptyModifierState },
"intake": { name: "intake", keyCode: "KeyE", isGlobal: true, modifiers: emptyModifierState },
"shootGamepiece": { name: "shootGamepiece", keyCode: "KeyQ", isGlobal: true, modifiers: emptyModifierState },
"enableGodMode": { name: "enableGodMode", keyCode: "KeyG", isGlobal: true, modifiers: emptyModifierState },

"arcadeForward": { name: "arcadeForward", keyCode: "KeyW", isGlobal: false, modifiers: emptyModifierState },
"arcadeBackward": { name: "arcadeBackward", keyCode: "KeyS", isGlobal: false, modifiers: emptyModifierState },
"arcadeLeft": { name: "arcadeLeft", keyCode: "KeyA", isGlobal: false, modifiers: emptyModifierState },
"arcadeRight": { name: "arcadeRight", keyCode: "KeyD", isGlobal: false, modifiers: emptyModifierState },
}

class InputSystem extends WorldSystem {
Expand Down Expand Up @@ -70,7 +70,7 @@ class InputSystem extends WorldSystem {
}

public Update(_: number): void {InputSystem
InputSystem._currentModifierState = { ctrl: InputSystem.isKeyPressed("Control"), alt: InputSystem.isKeyPressed("Alt"), shift: InputSystem.isKeyPressed("Shift"), meta: InputSystem.isKeyPressed("Meta") }
InputSystem._currentModifierState = { ctrl: InputSystem.isKeyPressed("ControlLeft") || InputSystem.isKeyPressed("ControlRight"), alt: InputSystem.isKeyPressed("AltLeft") || InputSystem.isKeyPressed("AltRight"), shift: InputSystem.isKeyPressed("ShiftLeft") || InputSystem.isKeyPressed("ShiftRight"), meta: InputSystem.isKeyPressed("MetaLeft") || InputSystem.isKeyPressed("MetaRight") }
}

public Destroy(): void {
Expand All @@ -80,12 +80,12 @@ class InputSystem extends WorldSystem {

// Called when any key is pressed
private HandleKeyDown(event: KeyboardEvent) {
InputSystem._keysPressed[event.key] = true;
InputSystem._keysPressed[event.code] = true;
}

// Called when any key is released
private HandleKeyUp(event: KeyboardEvent) {
InputSystem._keysPressed[event.key] = false;
InputSystem._keysPressed[event.code] = false;
}

// Returns true if the given key is currently down
Expand All @@ -103,7 +103,7 @@ class InputSystem extends WorldSystem {
if (!this.CompareModifiers(InputSystem._currentModifierState, targetInput.modifiers))
return false;

return this.isKeyPressed(targetInput.keybind);
return this.isKeyPressed(targetInput.keyCode);
}

// If the input does not exist, returns false
Expand All @@ -115,13 +115,6 @@ class InputSystem extends WorldSystem {
return (this.getInput(positive) ? 1 : 0) - (this.getInput(negative) ? 1 : 0);
}

// Converts camelCase to Title Case for the inputs modal
public static ToTitleCase(camelCase: string) : string {
const result = camelCase.replace(/([A-Z])/g, " $1");
const finalResult = result.charAt(0).toUpperCase() + result.slice(1);
return finalResult;
}

// Returns true if two modifier states are identical
private static CompareModifiers(state1: ModifierState, state2: ModifierState) : boolean {
return state1.alt == state2.alt && state1.ctrl == state2.ctrl && state1.meta == state2.meta && state1.shift == state2.shift;
Expand Down
10 changes: 5 additions & 5 deletions fission/src/systems/simulation/behavior/GenericArmBehavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ class GenericArmBehavior extends Behavior {
super([hingeDriver], [hingeStimulus]);
this._hingeDriver = hingeDriver;

this._positiveInput = "jointPositive" + jointIndex;
this._negativeInput = "jointNegative" + jointIndex;
this._positiveInput = "joint " + jointIndex + " Positive";
this._negativeInput = "joint " + jointIndex + " Negative";

// TODO: load inputs from mira
InputSystem.allInputs[this._positiveInput] = { name: this._positiveInput, keybind: jointIndex.toString(), isGlobal: true, modifiers: emptyModifierState };
InputSystem.allInputs[this._negativeInput] = { name: this._negativeInput, keybind: jointIndex.toString(), isGlobal: true,
modifiers: { ctrl: false, alt: true, shift: false, meta: false } };
InputSystem.allInputs[this._positiveInput] = { name: this._positiveInput, keyCode: "Digit" + jointIndex.toString(), isGlobal: false, modifiers: emptyModifierState };
InputSystem.allInputs[this._negativeInput] = { name: this._negativeInput, keyCode: "Digit" + jointIndex.toString(), isGlobal: false,
modifiers: { ctrl: false, alt: false, shift: true, meta: false } };
}

// Sets the arms target rotational velocity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ class GenericElevatorBehavior extends Behavior {
super([sliderDriver], [sliderStimulus]);
this._sliderDriver = sliderDriver;

this._positiveInput = "jointPositive" + jointIndex;
this._negativeInput = "jointNegative" + jointIndex;
this._positiveInput = "joint " + jointIndex + " Positive";
this._negativeInput = "joint " + jointIndex + " Negative";

// TODO: load inputs from mira
InputSystem.allInputs[this._positiveInput] = { name: this._positiveInput, keybind: jointIndex.toString(), isGlobal: true, modifiers: emptyModifierState };
InputSystem.allInputs[this._negativeInput] = { name: this._negativeInput, keybind: jointIndex.toString(), isGlobal: true,
modifiers: { ctrl: false, alt: true, shift: false, meta: false } };
InputSystem.allInputs[this._positiveInput] = { name: this._positiveInput, keyCode: "Digit" + jointIndex.toString(), isGlobal: false, modifiers: emptyModifierState };
InputSystem.allInputs[this._negativeInput] = { name: this._negativeInput, keyCode: "Digit" + jointIndex.toString(), isGlobal: false,
modifiers: { ctrl: false, alt: false, shift: true, meta: false } };
}

// Changes the elevators target position
moveElevator(positionDelta: number) {
this._sliderDriver.targetPosition += positionDelta;
this._sliderDriver.targetPosition += positionDelta;
}

public Update(deltaT: number): void {
Expand Down
46 changes: 41 additions & 5 deletions fission/src/ui/modals/configuring/ChangeInputsModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,43 @@ const transformKeyName = (control: Input) => {
if (control.modifiers.ctrl) prefix += "Ctrl + "
if (control.modifiers.alt) prefix += "Alt + "
}
return prefix + control.keybind[0].toUpperCase() + control.keybind.substring(1)

return prefix + keyCodeToCharacter(control.keyCode);
}

// Converts camelCase to Title Case for the inputs modal
const toTitleCase = (camelCase: string) => {
const result = camelCase.replace(/([A-Z])/g, " $1");
const finalResult = result.charAt(0).toUpperCase() + result.slice(1);
return finalResult;
}

const codeToCharacterMap: { [code: string]: string } = {
"Slash": "/",
"Comma": ",",
"Period": ".",
"BracketLeft": "{",
"BracketRight": "}",
"BackQuote": "`",
"Minus": "-",
"Equal": "=",
"Backslash": "\\", //TODO
"Semicolon": ";",
"Quote": "\""
};

// Converts a key code to displayable character (ex: KeyA -> "A")
const keyCodeToCharacter = (code: string) => {
if (code.startsWith("Key"))
return code.charAt(3);

if (code.startsWith("Digit"))
return code.charAt(5);

if (code in codeToCharacterMap)
return codeToCharacterMap[code];

return code;
}

const ChangeInputsModal: React.FC<ModalPropsImpl> = ({ modalId }) => {
Expand All @@ -30,7 +66,7 @@ const ChangeInputsModal: React.FC<ModalPropsImpl> = ({ modalId }) => {

if (selectedInput && chosenKey) {
const selected = InputSystem.allInputs[selectedInput]
selected.keybind = chosenKey
selected.keyCode = chosenKey
selected.modifiers = modifierState
setChosenKey("")
setSelectedInput("")
Expand All @@ -43,7 +79,7 @@ const ChangeInputsModal: React.FC<ModalPropsImpl> = ({ modalId }) => {
<div
className="w-max"
onKeyUp={e => {
setChosenKey(selectedInput ? e.key : "")
setChosenKey(selectedInput ? e.code : "")
setModifierState({
ctrl: e.ctrlKey,
alt: e.altKey,
Expand All @@ -58,7 +94,7 @@ const ChangeInputsModal: React.FC<ModalPropsImpl> = ({ modalId }) => {
{Object.values(InputSystem.robotInputs).map(c => (
<LabeledButton
key={c.name}
label={InputSystem.ToTitleCase(c.name)}
label={toTitleCase(c.name)}
placement={LabelPlacement.Left}
value={
c.name == selectedInput
Expand Down Expand Up @@ -91,7 +127,7 @@ const ChangeInputsModal: React.FC<ModalPropsImpl> = ({ modalId }) => {
{Object.values(InputSystem.globalInputs).map(c => (
<LabeledButton
key={c.name}
label={InputSystem.ToTitleCase(c.name)}
label={toTitleCase(c.name)}
placement={LabelPlacement.Left}
value={
c.name == selectedInput
Expand Down

0 comments on commit 1f67780

Please sign in to comment.