/
index.js
95 lines (80 loc) · 2.1 KB
/
index.js
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
var Keyboard = require("./keyboard");
var StandardController = require("../controllers/standardcontroller");
function Input( system ) {
this.system = system;
this.controllers = system.controllers;
this.inputHandlers = new Array( 2 );
this.initConfig();
// only enable input in browsers
if ( typeof window !== "undefined" ) {
this.enable();
}
}
Input.prototype = {
/**
* Enable all input.
*/
enable: function() {
this._setEnabled( true );
},
/**
* Disable all input.
*/
disable: function() {
this._setEnabled( false );
},
/**
* Set enabled yes/no.
* Helper method for enable and disable.
* @param {boolean} enabled - If true enable, otherwise disable.
*/
_setEnabled: function( enabled ) {
var handler,
method = enabled ? "enable" : "disable";
for ( var i = 0; i < this.inputHandlers.length; i++ ) {
handler = this.inputHandlers[ i ];
if ( handler ) {
handler[ method ]();
}
}
},
/**
* Initialize total input config.
*/
initConfig: function() {
var item,
config = this.config = this.system.config.input;
for ( var i = 0; i < config.length; i++ ) {
item = config[ i ];
this.setController( i, item.type );
this.setInputHandler( i, item.input, item.config );
}
},
/**
* Connect a controller of given type.
* @param {number} index - Either 0 or 1.
* @param {string} type - Type of controller (eg. 'standard').
*/
setController: function( index, type ) {
var Controller = controllerMap[ type ];
this.controllers.connect( index, new Controller() );
},
/**
* Bind input handler to controller.
* @param {number} index - Either 0 or 1.
* @param {string} input - Type of input handler (eg. 'keyboard').
* @param {object} config - Configuration for keyboard handler.
*/
setInputHandler: function( index, input, config ) {
var InputHandler = inputHandlerMap[ input ],
controller = this.controllers.get( index );
this.inputHandlers[ index ] = new InputHandler( controller, config );
}
};
var controllerMap = {
"standard": StandardController
};
var inputHandlerMap = {
"keyboard": Keyboard
};
module.exports = Input;