UNPKG

4.48 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = Object.setPrototypeOf ||
4 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
6 return function (d, b) {
7 extendStatics(d, b);
8 function __() { this.constructor = d; }
9 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
10 };
11})();
12Object.defineProperty(exports, "__esModule", { value: true });
13var index_1 = require("./index");
14var Tool_1 = require("./../Tool");
15/**
16 * Keyboard event utils
17 */
18var Keyboard = (function (_super) {
19 __extends(Keyboard, _super);
20 /* LIFECYCLE */
21 /**
22 * @constructor
23 */
24 function Keyboard() {
25 var _this = _super.call(this) || this;
26 /**
27 * list of all inputs key released or pressed relative to the game looping
28 * @readonly
29 */
30 _this.inputs = {};
31 /**
32 * List of all inputs key released or pressed
33 * @private
34 */
35 _this._inputs = {};
36 /**
37 * If true, the key event willnot be propaged
38 */
39 _this.preventInputPropagation = true;
40 _this.signals.keyChange = new Tool_1.SignalEvent();
41 _this.signals.keyPress = new Tool_1.SignalEvent();
42 _this.signals.keyRelease = new Tool_1.SignalEvent();
43 _this.signals.update.add(_this._updateInputs.bind(_this));
44 return _this;
45 }
46 /**
47 * @override
48 */
49 Keyboard.prototype.initialize = function (props) {
50 if (props === void 0) { props = {}; }
51 _super.prototype.initialize.call(this, props);
52 window.addEventListener("keydown", this._onKeydown.bind(this));
53 window.addEventListener("keyup", this._onKeyup.bind(this));
54 };
55 /**
56 * @override
57 */
58 Keyboard.prototype.kill = function () {
59 _super.prototype.kill.call(this);
60 window.removeEventListener("keydown", this._onKeydown.bind(this));
61 window.removeEventListener("keyup", this._onKeydown.bind(this));
62 };
63 /* EVENTS */
64 /**
65 * Update all device inputs
66 * @private
67 */
68 Keyboard.prototype._updateInputs = function () {
69 for (var key in this._inputs) {
70 if (!this._inputs.hasOwnProperty(key)) {
71 continue;
72 }
73 var input = this.inputs[key], _input = this._inputs[key];
74 // Pressed
75 if (_input === Tool_1.Enum.KEY_STATE.PRESSED) {
76 if (input === _input) {
77 this.inputs[key] = Tool_1.Enum.KEY_STATE.HOLD;
78 }
79 else if (input !== Tool_1.Enum.KEY_STATE.HOLD) {
80 this.inputs[key] = Tool_1.Enum.KEY_STATE.PRESSED;
81 this.signals.keyChange.dispatch(key, true);
82 this.signals.keyPress.dispatch(key);
83 }
84 // Released
85 }
86 else if (_input === Tool_1.Enum.KEY_STATE.RELEASED) {
87 if (!input) {
88 this.inputs[key] = Tool_1.Enum.KEY_STATE.PRESSED;
89 }
90 else if (input === _input) {
91 delete this.inputs[key];
92 delete this._inputs[key];
93 }
94 else {
95 this.inputs[key] = Tool_1.Enum.KEY_STATE.RELEASED;
96 this.signals.keyChange.dispatch(key, false);
97 this.signals.keyRelease.dispatch(key);
98 }
99 }
100 }
101 };
102 /**
103 * Event on keydown
104 * @param e - The event
105 * @returns Input propagation
106 */
107 Keyboard.prototype._onKeydown = function (e) {
108 if (this.preventInputPropagation) {
109 e.preventDefault();
110 e.stopPropagation();
111 }
112 this._inputs[e.keyCode] = Tool_1.Enum.KEY_STATE.PRESSED;
113 return !this.preventInputPropagation;
114 };
115 /**
116 * Event on keyup
117 * @param e - The event
118 * @returns Input propagation
119 */
120 Keyboard.prototype._onKeyup = function (e) {
121 if (this.preventInputPropagation) {
122 e.preventDefault();
123 e.stopPropagation();
124 }
125 this._inputs[e.keyCode] = Tool_1.Enum.KEY_STATE.RELEASED;
126 return !this.preventInputPropagation;
127 };
128 return Keyboard;
129}(index_1.SideralObject));
130exports.Keyboard = Keyboard;