From 021c099be284102898dabefa9eeea3bfb478a47b Mon Sep 17 00:00:00 2001 From: Erik Onarheim Date: Fri, 15 Sep 2023 20:16:11 -0500 Subject: [PATCH] fix: [#2762] wasPressed in input mapper (#2766) Closes #2762 ## Changes: - Changes the input mapper execution order to run input mapping before clearing keyboard events per frame --- CHANGELOG.md | 36 ++++++++++++++++++++++++++++++------ src/engine/Engine.ts | 4 ++-- src/spec/InputMapperSpec.ts | 22 ++++++++++++++++++++++ 3 files changed, 54 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a8d55f42..3cbf7da3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,11 +3,40 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). - ## [Unreleased] ### Breaking Changes +- + +### Deprecated + +- + +### Added + +- + +### Fixed + +- Fixed issue with input mapper where `keyboard.wasPressed(...)` did not fire + +### Updates + +- + +### Changed + +- + + + + + +## [v0.28.0] + +### Breaking Changes + - Removed `ex.Class` base class type, this was a common base class for many excalibur types that provided old on/off event functionality. This functionality has been preserved on the types that had it before using `ex.EventEmitter` ### Deprecated @@ -193,11 +222,6 @@ stored `ex.Graphics` causing them to be shared across clones. - Excalibur resources by default no longer add cache busting query string to resources. All built in resources now expose a `bustCache` property to allow setting this before loading, for example `ex.Sound.bustCache`. - - - - - ## [0.27.0] - 2022-07-08 ### Breaking Changes diff --git a/src/engine/Engine.ts b/src/engine/Engine.ts index 86b6fff8..36eba81c 100644 --- a/src/engine/Engine.ts +++ b/src/engine/Engine.ts @@ -1205,9 +1205,9 @@ O|===|* >________________>\n\ // suspend updates until loading is finished this._loader.update(this, delta); // Update input listeners + this.inputMapper.execute(); this.input.keyboard.update(); this.input.gamepads.update(); - this.inputMapper.execute(); return; } @@ -1225,9 +1225,9 @@ O|===|* >________________>\n\ this._postupdate(delta); // Update input listeners + this.inputMapper.execute(); this.input.keyboard.update(); this.input.gamepads.update(); - this.inputMapper.execute(); } /** diff --git a/src/spec/InputMapperSpec.ts b/src/spec/InputMapperSpec.ts index ba32d990..b9822ac1 100644 --- a/src/spec/InputMapperSpec.ts +++ b/src/spec/InputMapperSpec.ts @@ -1,4 +1,5 @@ import * as ex from '@excalibur'; +import { TestUtils } from './util/TestUtils'; describe('An InputMapper', () => { it('exists', () => { @@ -49,4 +50,25 @@ describe('An InputMapper', () => { expect(command).toHaveBeenCalledTimes(0); }); + it('can fire wasPressed events when used in a engine', () => { + + const engine = TestUtils.engine({ width: 100, height: 100 }); + + const clock = engine.clock as ex.TestClock; + clock.start(); + engine.input.keyboard.triggerEvent('down', ex.Keys.Space); + + const sut = engine.inputMapper; + const keyPressedSpy = jasmine.createSpy('keyPressed'); + const keyReleasedSpy = jasmine.createSpy('keyReleased'); + sut.on(({keyboard}) => keyboard.wasPressed(ex.Keys.Space), keyPressedSpy); + sut.on(({keyboard}) => keyboard.wasReleased(ex.Keys.Space), keyReleasedSpy); + + clock.step(); + expect(keyPressedSpy).toHaveBeenCalled(); + + engine.input.keyboard.triggerEvent('up', ex.Keys.Space); + clock.step(); + expect(keyReleasedSpy).toHaveBeenCalled(); + }); }); \ No newline at end of file -- 2.51.2