Browse code

fix keyboard

devnewton authored on 13/03/2018 16:16:17
Showing 4 changed files
... ...
@@ -8,11 +8,11 @@ import { GamepadUtils } from "../utils/GamepadUtils";
8 8
 export class GamepadOptionsBindAxisOrButton extends AbstractState {
9 9
 
10 10
     bindingsDescription = [
11
-        { label: 'Pull move X axis', localStorageKeySuffix: 'moveXAxis' },
12
-        { label: 'Pull move Y axis', localStorageKeySuffix: 'moveYAxis' },
13
-        { label: 'Press hammer button', localStorageKeySuffix: 'hammerButton' },
14
-        { label: 'Press dash button', localStorageKeySuffix: 'dashButton' },
15
-        { label: 'Press menu button', localStorageKeySuffix: 'menuButton' }
11
+        { label: 'Pull move X axis', localStorageKey: 'moveXAxis' },
12
+        { label: 'Pull move Y axis', localStorageKey: 'moveYAxis' },
13
+        { label: 'Press hammer button', localStorageKey: 'hammerButton' },
14
+        { label: 'Press dash button', localStorageKey: 'dashButton' },
15
+        { label: 'Press menu button', localStorageKey: 'menuButton' }
16 16
     ];
17 17
 
18 18
     currentBinding: number = 0;
... ...
@@ -37,6 +37,7 @@ export class GamepadOptionsBindAxisOrButton extends AbstractState {
37 37
         if (binding >= this.bindingsDescription.length) {
38 38
             this.currentBinding = 0;
39 39
             localStorage.setItem('gamepad.' + GamepadUtils.gamepadId(this.pad) + '.layout', JSON.stringify(this.bindings));
40
+            (this.game as UnderthiefGame).controllers.updatePadLayout();
40 41
             this.game.state.start('GamepadOptions');
41 42
         } else {
42 43
             this.currentBinding = binding;
... ...
@@ -84,7 +85,7 @@ export class GamepadOptionsBindAxisOrButton extends AbstractState {
84 85
 
85 86
     update() {
86 87
         super.update();
87
-        if (this.bindingsDescription[this.currentBinding].localStorageKeySuffix.match(/axis/gi)) {
88
+        if (this.bindingsDescription[this.currentBinding].localStorageKey.match(/axis/gi)) {
88 89
             this.axisButtons.visible = true;
89 90
             this.buttonsButtons.visible = false;
90 91
         } else {
... ...
@@ -94,13 +95,13 @@ export class GamepadOptionsBindAxisOrButton extends AbstractState {
94 95
     }
95 96
 
96 97
     bindAxis(axisCode: number) {
97
-        this.bindings[this.bindingsDescription[this.currentBinding].localStorageKeySuffix] = axisCode;
98
-        this.game.state.start('GamepadOptionsBindAxisOrButton', true, false, this.padIndex, this.currentBinding + 1);
98
+        this.bindings[this.bindingsDescription[this.currentBinding].localStorageKey] = axisCode;
99
+        this.game.state.start('GamepadOptionsBindAxisOrButton', true, false, this.padIndex, this.currentBinding + 1, this.bindings);
99 100
     }
100 101
 
101 102
     bindButton(buttonCode: number) {
102
-        this.bindings[this.bindingsDescription[this.currentBinding].localStorageKeySuffix] = buttonCode;
103
-        this.game.state.start('GamepadOptionsBindAxisOrButton', true, false, this.padIndex, this.currentBinding + 1);
103
+        this.bindings[this.bindingsDescription[this.currentBinding].localStorageKey] = buttonCode;
104
+        this.game.state.start('GamepadOptionsBindAxisOrButton', true, false, this.padIndex, this.currentBinding + 1, this.bindings);
104 105
     }
105 106
 }
106 107
 
... ...
@@ -22,16 +22,19 @@ export class KeyboardOptions extends AbstractState {
22 22
         logo.anchor.setTo(0.5, 0);
23 23
 
24 24
         const menu = new Menu(this.game).disableKeyboardCursor();
25
-        menu.button("⬆⬇⬅➡ shift ctrl", 200, 300, () => {
26
-            localStorage.setItem('keyboard.layout', 'other');
27
-            this.game.state.start('Options');
28
-        });
29 25
         menu.button("Azerty zsqd jk", 200, 100, () => {
30 26
             localStorage.setItem('keyboard.layout', 'azerty');
27
+            (this.game as UnderthiefGame).controllers.getKeyboard().setupKeyboardLayout();
31 28
             this.game.state.start('Options');
32 29
         });
33 30
         menu.button("Qwerty wsad jk", 200, 200, () => {
34 31
             localStorage.setItem('keyboard.layout', 'qwerty');
32
+            (this.game as UnderthiefGame).controllers.getKeyboard().setupKeyboardLayout();
33
+            this.game.state.start('Options');
34
+        });
35
+        menu.button("⬆⬇⬅➡ shift ctrl", 200, 300, () => {
36
+            localStorage.setItem('keyboard.layout', 'other');
37
+            (this.game as UnderthiefGame).controllers.getKeyboard().setupKeyboardLayout();
35 38
             this.game.state.start('Options');
36 39
         });
37 40
         menu.button("Custom", 200, 400, () => {
... ...
@@ -6,17 +6,18 @@ import { UnderthiefGame } from "../UnderthiefGame";
6 6
 
7 7
 export class KeyboardOptionsBindKey extends AbstractState {
8 8
 
9
-    bindings = [
10
-        { label: 'Press move up key', localStorageKey: 'keyboard.layout.custom.moveUp' },
11
-        { label: 'Press move down key', localStorageKey: 'keyboard.layout.custom.moveDown' },
12
-        { label: 'Press move left key', localStorageKey: 'keyboard.layout.custom.moveLeft' },
13
-        { label: 'Press move right key', localStorageKey: 'keyboard.layout.custom.moveRight' },
14
-        { label: 'Press hammer key', localStorageKey: 'keyboard.layout.custom.hammer' },
15
-        { label: 'Press dash key', localStorageKey: 'keyboard.layout.custom.dash' },
16
-        { label: 'Press menu key', localStorageKey: 'keyboard.layout.custom.menu' }
9
+    bindingsDescription = [
10
+        { label: 'Press move up key', localStorageKey: 'moveUp' },
11
+        { label: 'Press move down key', localStorageKey: 'moveDown' },
12
+        { label: 'Press move left key', localStorageKey: 'moveLeft' },
13
+        { label: 'Press move right key', localStorageKey: 'moveRight' },
14
+        { label: 'Press hammer key', localStorageKey: 'hammer' },
15
+        { label: 'Press dash key', localStorageKey: 'dash' },
16
+        { label: 'Press menu key', localStorageKey: 'menu' }
17 17
     ];
18 18
 
19 19
     currentBinding: number = 0;
20
+    bindings = {};
20 21
 
21 22
     constructor() {
22 23
         super();
... ...
@@ -26,10 +27,12 @@ export class KeyboardOptionsBindKey extends AbstractState {
26 27
         Menu.preload(this.game);
27 28
     }
28 29
 
29
-    init(binding: number = 0) {
30
-        if (binding >= this.bindings.length) {
30
+    init(binding: number = 0, bindings?: any) {
31
+        this.bindings = bindings || {};
32
+        if (binding >= this.bindingsDescription.length) {
31 33
             this.currentBinding = 0;
32
-            (this.game as UnderthiefGame).controllers.getKeyboard().useCustomKeyboardLayout();
34
+            localStorage.setItem('keyboard.layout', JSON.stringify(this.bindings));
35
+            (this.game as UnderthiefGame).controllers.getKeyboard().setupKeyboardLayout();
33 36
             this.game.state.start('KeyboardOptions');
34 37
         } else {
35 38
             this.currentBinding = binding;
... ...
@@ -38,7 +41,7 @@ export class KeyboardOptionsBindKey extends AbstractState {
38 41
 
39 42
     create() {
40 43
         super.create();
41
-        let logo = this.game.add.text(this.game.world.centerX, 0, this.bindings[this.currentBinding].label, { font: "42px monospace", fill: 'white' });
44
+        let logo = this.game.add.text(this.game.world.centerX, 0, this.bindingsDescription[this.currentBinding].label, { font: "42px monospace", fill: 'white' });
42 45
         logo.scale.x = 2;
43 46
         logo.scale.y = 2;
44 47
         logo.anchor.setTo(0.5, 0);
... ...
@@ -50,8 +53,8 @@ export class KeyboardOptionsBindKey extends AbstractState {
50 53
         for (var k in Phaser.KeyCode) {
51 54
             let keycode = Phaser.KeyCode[k];
52 55
             if (this.input.keyboard.isDown(keycode)) {
53
-                localStorage.setItem(this.bindings[this.currentBinding].localStorageKey, keycode);
54
-                this.game.state.start('KeyboardOptionsBindKey', true, false, this.currentBinding + 1);
56
+                this.bindings[this.bindingsDescription[this.currentBinding].localStorageKey] = keycode;
57
+                this.game.state.start('KeyboardOptionsBindKey', true, false, this.currentBinding + 1, this.bindings);
55 58
                 break;
56 59
             }
57 60
         }
... ...
@@ -15,6 +15,7 @@ export class Controllers {
15 15
     controllers: Array<AbstractControls>;
16 16
 
17 17
     constructor(game: Phaser.Game) {
18
+        game.input.gamepad.start();
18 19
         this.controllers = [
19 20
             new KeyboardControls(game),
20 21
             new PadControls(game, 1),
... ...
@@ -40,6 +41,12 @@ export class Controllers {
40 41
     getPad(padIndex: number): PadControls {
41 42
         return this.controllers[padIndex] as PadControls;
42 43
     }
44
+
45
+    updatePadLayout() {
46
+        for(let i = 1; i<4; ++i) {
47
+            (<PadControls>this.controllers[i]).updatePadLayout();
48
+        }
49
+    }
43 50
 }
44 51
 
45 52
 export abstract class AbstractControls {
... ...
@@ -127,22 +134,17 @@ export class KeyboardControls extends AbstractControls {
127 134
     constructor(game: Phaser.Game) {
128 135
         super();
129 136
         this.game = game;
130
-        game.input.gamepad.start();
131
-        this.setupKeyboardLayout(localStorage.getItem('keyboard.layout'));
132
-        window.addEventListener('storage', (e) => {
133
-            if (e.key == 'keyboard.layout') {
134
-                this.setupKeyboardLayout(e.newValue);
135
-            }
136
-        });
137
+        this.setupKeyboardLayout();
137 138
     }
138 139
 
139
-    setupKeyboardLayout(layout: string) {
140
+    setupKeyboardLayout() {
141
+        const layout = localStorage.getItem('keyboard.layout');
140 142
         this.kb = this.game.input.keyboard;
141 143
         try {
142 144
             let mapping: KeyboardControlsMapping = JSON.parse(layout) || {};
143 145
             this.keyCodeMoveUp = mapping.moveUp || Phaser.KeyCode.UP;
144 146
             this.keyCodeMoveDown = mapping.moveDown || Phaser.KeyCode.DOWN;
145
-            this.keyCodeMoveLeft = mapping.moveDown || Phaser.KeyCode.LEFT;
147
+            this.keyCodeMoveLeft = mapping.moveLeft || Phaser.KeyCode.LEFT;
146 148
             this.keyCodeMoveRight = mapping.moveRight || Phaser.KeyCode.RIGHT;
147 149
             this.keyCodeHammerTime = mapping.hammer || Phaser.KeyCode.SHIFT;
148 150
             this.keyCodeDash = mapping.dash || Phaser.KeyCode.CONTROL;
... ...
@@ -260,13 +262,11 @@ export class PadControls extends AbstractControls {
260 262
     constructor(game: Phaser.Game, padIndex: number) {
261 263
         super();
262 264
         this.game = game;
263
-        game.input.gamepad.start();
264 265
         this.padIndex = padIndex;
265
-        window.addEventListener('storage', (e) => {
266
-            if (e.key.startsWith('gamepad.') && e.key.endsWith('.layout')) {
267
-                this.pad = null;
268
-            }
269
-        });
266
+    }
267
+
268
+    updatePadLayout() {
269
+        this.pad = null;
270 270
     }
271 271
 
272 272
     private checkPad(): boolean {