UNPKG

phaser4-rex-plugins

Version:
2 lines (1 loc) 2.63 kB
var e,t;e=void 0,t=function(){const e=Phaser.Input.Keyboard.Key,t=Phaser.Input.Keyboard.KeyCodes,s=["up","down","left","right"];class n{constructor(e){this.scene=e,this.keys={},this.cursorKeys={},this.noKeyDown=!0;for(var t=0,n=s.length;t<n;t++){var i=s[t];this.addKey(i),this.cursorKeys[i]=this.keys[i]}}shutdown(e){for(var t in this.scene=void 0,this.keys)this.keys[t].destroy();this.keys=void 0,this.cursorKeys=void 0}destroy(e){shutdown(e)}createCursorKeys(){return this.cursorKeys}setKeyState(e,t){var s=this.keys[e];return s.enabled?(t&&(this.noKeyDown=!1),s.isDown!==t&&(i.timeStamp=Date.now(),i.keyCode=s.keyCode,t?s.onDown(i):s.onUp(i)),this):this}clearAllKeysState(){for(var e in this.noKeyDown=!0,this.keys)this.setKeyState(e,!1);return this}getKeyState(e){return this.keys[e]}get upKeyDown(){return this.keys.up.isDown}get downKeyDown(){return this.keys.down.isDown}get leftKeyDown(){return this.keys.left.isDown}get rightKeyDown(){return this.keys.right.isDown}get anyKeyDown(){return!this.noKeyDown}addKey(s,n){return void 0===n&&(n=s),"string"==typeof n&&(n=n.toUpperCase(),t.hasOwnProperty(n)&&(n=t[n])),this.keys[s]=new e(this.scene,n),this}addKeys(e){for(var t=0,s=e.length;t<s;t++)this.addKey(e[t]);return this}}var i={timeStamp:0,keyCode:0,altKey:!1,ctrlKey:!1,shiftKey:!1,metaKey:!1,location:0};class o extends n{constructor(e,t){super(e),this.scene=e,this.boot()}boot(){this.scene.input.on("wheel",this.onWheeling,this),this.scene.sys.events.on("postupdate",this.clearAllKeysState,this),this.scene.sys.events.once("shutdown",this.destroy,this)}shutdown(){this.scene&&(this.scene.input.off("wheel",this.onWheeling,this),this.scene.sys.events.off("postupdate",this.clearAllKeysState,this),this.scene.sys.events.off("shutdown",this.destroy,this),this.scene=void 0,super.shutdown())}destroy(){this.shutdown()}get enable(){return this._enable}set enable(e){if(this._enable!==e)return e||this.clearAllKeysState(),this._enable=e,this}setEnable(e){return void 0===e&&(e=!0),this.enable=e,this}toggleEnable(){return this.setEnable(!this.enable),this}onWheeling(e,t,s,n,i,o){this.enable&&(this.setKeyState("up",n<0),this.setKeyState("down",n>0))}get up(){return this.upKeyDown}get down(){return this.downKeyDown}get noKey(){return this.noKeyDown}}class r extends Phaser.Plugins.BasePlugin{constructor(e){super(e)}start(){this.game.events.on("destroy",this.destroy,this)}add(e,t){return new o(e,t)}}return r},"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).rexmousewheeltoupdownplugin=t();