Repository navigation
Expand file tree
/
Copy pathKeyboardController.js
More file actions
156 lines (141 loc) · 5.01 KB
/
Copy pathKeyboardController.js
File metadata and controls
156 lines (141 loc) · 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
const isTouchable = 'createTouch' in document;
/**
* Simple actions avoid a bunch of repetition
*/
function ControllerAction(object, onStart, onStop) {
this.object = object;
this.onStart = onStart;
this.onStop = onStop;
this.uiElement = null;
}
ControllerAction.prototype.start = function(event) {
if (this.uiElement) { this.uiElement.classList.add("pressed"); }
this.onStart.call(this.object, event);
if (event) { event.preventDefault() };
}
ControllerAction.prototype.stop = function(event) {
if (this.uiElement) { this.uiElement.classList.remove("pressed"); }
this.onStop.call(this.object, event);
if (event) { event.preventDefault() };
}
ControllerAction.prototype.setUIElement = function(uiElement) {
this.uiElement = uiElement;
}
ControllerAction.prototype.initEventHandlers = function() {
if (this.uiElement == null) { return this; }
let self = this;
if (isTouchable) {
this.eventListeners = {
touchstart: function(event) { self.start(event); },
touchend: function(event) { self.stop(event); }
};
} else { // mouse events
this.eventListeners = {
mouseover: function(event) { self.start(event); },
mouseout: function(event) { self.stop(event); }
};
}
for (var [name, listener] of Object.entries(this.eventListeners)) {
this.uiElement.addEventListener(name, listener);
// $(this.uiElement).on(name, listener);
}
return self;
}
ControllerAction.prototype.removeEventHandlers = function() {
if (this.eventListeners) {
for (var [name, listener] of Object.entries(this.eventListeners)) {
this.uiElement.removeEventListener(name, listener);
}
}
}
/**
* KeyboardController
* Attaches to the Document's event handler to listen for given key-up/down events, and
* fires off events to the ship. Attaches to a "controlUI" element to provide visual
* feedback.
*/
function KeyboardController(actions, controlUI, listenNode = document) {
this.actions = actions;
this.actions.ignore = new ControllerAction(this, this.ignore, this.ignore);
this.controlUI = controlUI;
this.listenNode = listenNode;
this.initControlUI().initKeymap().initEventHandlers();
}
KeyboardController.prototype.onDestroy = function() {
this.removeEventHandlers();
}
KeyboardController.prototype.initControlUI = function() {
// TODO: in the future, create these elements?
this.keypadUI = controlUI.getElementsByClassName("keypad")[0];
this.messageUI = controlUI.getElementsByClassName("message")[0];
for (var [id, action] of Object.entries(this.actions)) {
action.setUIElement(this.controlUI.getElementsByClassName(id)[0]);
}
return this;
}
KeyboardController.prototype.initKeymap = function() {
this.keyMap = {
73: this.actions.forward, // i
38: this.actions.forward, // up
75: this.actions.backward, // k
40: this.actions.backward, // down
74: this.actions.ccw, // j
37: this.actions.ccw, // left
76: this.actions.cw, // l
39: this.actions.cw, // right
32: this.actions.fire, // space
87: this.actions.weapon, // w
13: this.actions.ignore // <enter>
};
return this;
}
KeyboardController.prototype.initEventHandlers = function() {
let self = this;
this.eventListeners = { // save them so we can remove them later
keydown: function(event) { self.handleKeyDown(event); },
keyup: function(event) { self.handleKeyUp(event); },
blur: function(event) { self.handleBlur(event); },
focus: function(event) { self.handleFocus(event); }
};
for (var [name, listener] of Object.entries(this.eventListeners)) {
this.listenNode.addEventListener(name, listener);
}
// add mouse / touch events to each UI element:
for (var [id, action] of Object.entries(this.actions)) {
action.initEventHandlers();
}
return self;
}
KeyboardController.prototype.removeEventHandlers = function() {
if (this.eventListeners) {
for (var [name, listener] of Object.entries(this.eventListeners)) {
this.listenNode.removeEventListener(name, listener);
}
}
for (var [id, action] of Object.entries(this.actions)) {
action.removeEventHandlers();
}
return self;
}
/**
* Event Handlers
*/
KeyboardController.prototype.handleKeyDown = function(event) {
let key = event.which;
let action = this.keyMap[key];
if (action) { action.start(event); }
}
KeyboardController.prototype.handleKeyUp = function(event) {
let key = event.which;
let action = this.keyMap[key];
if (action) { action.stop(event); }
}
KeyboardController.prototype.handleBlur = function(event) {
this.messageUI.innerHTML("Lost focus...");
for (var action in this.eventMap) {
action.stop(event);
}
}
KeyboardController.prototype.handleFocus = function(event) {
this.messageUI.innerHTML("Regained focus...");
}