1 | "use strict";
|
2 | var __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 | })();
|
12 | Object.defineProperty(exports, "__esModule", { value: true });
|
13 | var SideralObject_1 = require("./../SideralObject");
|
14 | var Tool_1 = require("./../Tool");
|
15 |
|
16 |
|
17 |
|
18 | var Keyboard = (function (_super) {
|
19 | __extends(Keyboard, _super);
|
20 |
|
21 | |
22 |
|
23 |
|
24 | function Keyboard() {
|
25 | var _this = _super.call(this) || this;
|
26 | |
27 |
|
28 |
|
29 |
|
30 | _this.inputs = {};
|
31 | |
32 |
|
33 |
|
34 |
|
35 | _this._inputs = {};
|
36 | |
37 |
|
38 |
|
39 | _this.preventInputPropagation = true;
|
40 | _this.signals.keyChange = new SideralObject_1.Signal(_this);
|
41 | _this.signals.keyPress = new SideralObject_1.Signal(_this);
|
42 | _this.signals.keyRelease = new SideralObject_1.Signal(_this);
|
43 | _this.connect(_this.signals.update, _this._updateInputs.bind(_this));
|
44 | return _this;
|
45 | }
|
46 | |
47 |
|
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 |
|
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 |
|
64 | |
65 |
|
66 |
|
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 |
|
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 |
|
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 |
|
104 |
|
105 |
|
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 |
|
117 |
|
118 |
|
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 | }(SideralObject_1.SideralObject));
|
130 | exports.Keyboard = Keyboard;
|