UNPKG

3.1 kBJavaScriptView Raw
1var Gamepad = require("html5-gamepad");
2var Keyboard = require("game-keyboard");
3var keyMap = require("game-keyboard/key_map").US;
4var keyboard = new Keyboard(keyMap);
5var Mouse = require("./mouse");
6
7function Input(config, canvas) {
8 this.config = config;
9 this.gamepad = new Gamepad();
10 this.mouse = new Mouse(canvas);
11 this.lastButtonState = {};
12 this.delayedButtonUpdates = {};
13 // FIXME: add support for virtual axes
14 this.virtualButtons = {};
15}
16Input.prototype.axis = function(name) {
17 var input = this.config.axes[name];
18 if (input === undefined) {
19 console.error("No such axis: " + name);
20 return false;
21 }
22 for (var i = 0; i < input.length; i++) {
23 var physicalInput = input[i];
24 var device = physicalInput.device;
25 if (device === "mouse") {
26 if (physicalInput.axis === "x") {
27 return this.mouse.x;
28 }
29 if (physicalInput.axis === "y") {
30 return this.mouse.y;
31 }
32 }
33 if (device === "gamepad") {
34 return this.gamepad.axis(0, physicalInput.axis);
35 }
36 }
37};
38Input.prototype.button = function(name) {
39 var input = this.config.buttons[name];
40 if (input === undefined) {
41 console.error("No such button: " + name);
42 return false;
43 }
44 for (var i = 0; i < input.length; i++) {
45 var physicalInput = input[i];
46 var device = physicalInput.device;
47 if (device === "keyboard") {
48 if (keyboard.isPressed(physicalInput.button)) {
49 return true;
50 }
51 }
52 if (device === "mouse") {
53 if (this.mouse.isPressed(physicalInput.button)) {
54 return true;
55 }
56 }
57 if (device === "gamepad") {
58 if (this.gamepad.button(0, physicalInput.button)) {
59 return true;
60 }
61 }
62 if (device === "virtual") {
63 if (physicalInput.state) {
64 return true;
65 }
66 }
67 }
68 return false;
69};
70Input.prototype.buttonPressed = function(name) {
71 var current = this.button(name);
72 var last = this.lastButtonState[name];
73 if (last === undefined) {
74 last = true;
75 }
76 this.delayedButtonUpdates[name] = current;
77 return current && !last;
78};
79Input.prototype.buttonReleased = function(name) {
80 var current = this.button(name);
81 var last = this.lastButtonState[name];
82 if (last === undefined) {
83 last = false;
84 }
85 this.delayedButtonUpdates[name] = current;
86 return !current && last;
87};
88Input.prototype.setButton = function(name, instance, state) {
89 var virtualName = name + "|" + instance;
90 var virtual = this.virtualButtons[virtualName];
91 if (virtual) {
92 virtual.state = state;
93 } else {
94 virtual = {
95 device: "virtual",
96 state: state
97 };
98 this.virtualButtons[virtualName] = virtual;
99 var inputs = this.config.buttons[name];
100 if (inputs) {
101 inputs.push(virtual);
102 } else {
103 this.config.buttons[name] = [virtual];
104 }
105 }
106};
107Input.prototype.processUpdates = function() {
108 this.gamepad.update();
109 Object.keys(this.delayedButtonUpdates).forEach(function(name) {
110 this.lastButtonState[name] = this.delayedButtonUpdates[name];
111 delete this.delayedButtonUpdates[name];
112 }.bind(this));
113};
114
115module.exports = Input;