Skip to content

Commit

Permalink
Controller inputs no longer “stick” on window blur
Browse files Browse the repository at this point in the history
`Keyboard` - Reset key states on window blur
`Mouse` - Reset mouse state on window blur and window leave
`Sutra` - Remaps default keyboard inputs, pressed is now default
  • Loading branch information
Marak committed Mar 5, 2024
1 parent 9097100 commit 0c3511a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 13 deletions.
17 changes: 17 additions & 0 deletions mantra-game/plugins/keyboard/Keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export default class Keyboard {
// Bind methods and store them as class properties
this.boundHandleKeyDown = this.handleKeyDown.bind(this);
this.boundHandleKeyUp = this.handleKeyUp.bind(this);
this.boundHandleWindowBlur = this.handleWindowBlur.bind(this); // Bind the window blur event handler

}

Expand All @@ -70,6 +71,8 @@ export default class Keyboard {
bindInputControls() {
document.addEventListener('keydown', this.boundHandleKeyDown);
document.addEventListener('keyup', this.boundHandleKeyUp);
window.addEventListener('blur', this.boundHandleWindowBlur); // Listen for window blur event

}

update() {
Expand Down Expand Up @@ -116,6 +119,20 @@ export default class Keyboard {

}

handleWindowBlur() {

// On window blur we must clear all key inputs, as we may have keydown events
// that will never recieve a keyup event ( since the window is not focused )

// Iterate over the controls object and set each key's value to false
Object.keys(this.controls).forEach(key => {
this.controls[key] = false;
});
// Reset the input pool and key states
this.inputPool = {};
this.keyStates = {};
}

sendInputs() {

const trueInputs = this.inputPool;
Expand Down
36 changes: 31 additions & 5 deletions mantra-game/plugins/mouse/Mouse.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Mouse.js - Marak Squires 2023
// TODO: Split this file into multiple functions
let inputsBound = false;
export default class Mouse {

Expand Down Expand Up @@ -54,6 +55,7 @@ export default class Mouse {
this.boundHandlePointerUp = this.handlePointerUp.bind(this);

this.boundHandleWindowBlur = this.handleWindowBlur.bind(this);
this.boundHandleMouseLeave = this.handleMouseLeave.bind(this);

}

Expand Down Expand Up @@ -157,6 +159,7 @@ export default class Mouse {

// Method to handle window blur event
handleWindowBlur() {

// Reset pointer states
this.isDragging = false;
this.activeTouches = {};
Expand All @@ -172,6 +175,30 @@ export default class Mouse {
MIDDLE: null
};

// update the game with the new mouse state ( all off )
this.sendMouseData();

}

handleMouseLeave() {

// Reset pointer states
this.isDragging = false;
this.activeTouches = {};
this.firstTouchId = null;
this.secondTouchId = null;
this.endedFirstTouch = false;
this.endedSecondTouch = false;

// Reset mouse buttons states
this.mouseButtons = {
LEFT: null,
RIGHT: null,
MIDDLE: null
};

// update the game with the new mouse state ( all off )
this.sendMouseData();
}

handleMouseMove(event) {
Expand All @@ -188,7 +215,6 @@ export default class Mouse {
this.mousePosition = { x: event.clientX, y: event.clientY };
}


if (event.target instanceof HTMLCanvasElement) {
const canvas = event.target;
const rect = canvas.getBoundingClientRect();
Expand Down Expand Up @@ -254,10 +280,6 @@ export default class Mouse {
let game = this.game;
let preventDefault = false





this.updateMouseButtons(event, true);

// middle mouse button
Expand Down Expand Up @@ -512,7 +534,10 @@ export default class Mouse {
document.addEventListener('mouseup', this.boundHandleMouseUp);
}

// window / tab has lost focused
window.addEventListener('blur', this.boundHandleWindowBlur);
// mouse leaves window, window may still have focus
document.body.addEventListener('mouseleave', this.boundHandleMouseLeave);

// TODO: could be a config option
// TODO: this should be able to bind / unbind based on user actions, defaultMouseMovement
Expand Down Expand Up @@ -610,6 +635,7 @@ export default class Mouse {
}

window.removeEventListener('blur', this.boundHandleWindowBlur);
window.removeEventListener('mouseleave', this.boundHandleMouseLeave);

}

Expand Down
19 changes: 11 additions & 8 deletions mantra-game/plugins/sutra/Sutra.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class Sutra {
let rules = createSutra(game);
this.setSutra(rules);
}

// Once the game is ready, register the keyboard controls as conditions
// This allows for game.rules.if('keycode').then('action') style rules
if (!this.inputsBound) {
Expand Down Expand Up @@ -98,22 +97,26 @@ class Sutra {
// _DOWN implied as default
// Remark: 3/3/2024 - Basic key press test seems to indicate this is continually true
// It is expected to be true only once per press, TODO: add tests
this.game.rules.addCondition(mantraCode, (entity, gameState) =>

// Key down, only once per press
this.game.rules.addCondition(mantraCode + '_DOWN', (entity, gameState) =>
entity.id === this.game.currentPlayerId && gameState.input.keyStates[mantraCode]?.down
// gameState.input.keyStates[mantraCode]?.down
);

// Key Up Condition

// Key held / pressed condition, default
this.game.rules.addCondition(mantraCode, (entity, gameState) =>
entity.id === this.game.currentPlayerId && gameState.input.keyStates[mantraCode]?.pressed
// gameState.input.keyStates[mantraCode]?.pressed
);

// Key Up Condition, only once per release
this.game.rules.addCondition(mantraCode + '_UP', (entity, gameState) =>
entity.id === this.game.currentPlayerId && gameState.input.keyStates[mantraCode]?.up
// gameState.input.keyStates[mantraCode]?.up
);

// Key held Condition
this.game.rules.addCondition(mantraCode + '_HOLD', (entity, gameState) =>
entity.id === this.game.currentPlayerId && gameState.input.keyStates[mantraCode]?.pressed
// gameState.input.keyStates[mantraCode]?.pressed
);
}
}
}
Expand Down

0 comments on commit 0c3511a

Please sign in to comment.