Browse code

use json for keyboard layouts

devnewton authored on 13/03/2018 14:02:31
Showing 2 changed files
... ...
@@ -23,19 +23,18 @@ export class KeyboardOptions extends AbstractState {
23 23
 
24 24
         const menu = new Menu(this.game).disableKeyboardCursor();
25 25
         menu.button("⬆⬇⬅➡ shift ctrl", 200, 300, () => {
26
-            (<UnderthiefGame>this.game).controllers.getKeyboard().useOtherKeyboardLayout();
26
+            localStorage.setItem('keyboard.layout', 'other');
27 27
             this.game.state.start('Options');
28 28
         });
29 29
         menu.button("Azerty zsqd jk", 200, 100, () => {
30
-            (<UnderthiefGame>this.game).controllers.getKeyboard().useAzertyLayout();
30
+            localStorage.setItem('keyboard.layout', 'azerty');
31 31
             this.game.state.start('Options');
32 32
         });
33 33
         menu.button("Qwerty wsad jk", 200, 200, () => {
34
-            (<UnderthiefGame>this.game).controllers.getKeyboard().useQwertyLayout();
34
+            localStorage.setItem('keyboard.layout', 'qwerty');
35 35
             this.game.state.start('Options');
36 36
         });
37 37
         menu.button("Custom", 200, 400, () => {
38
-            (<UnderthiefGame>this.game).controllers.getKeyboard().useOtherKeyboardLayout();
39 38
             this.game.state.start('KeyboardOptionsBindKey', true, false);
40 39
         });
41 40
         menu.button("Back", 200, 600, () => this.game.state.start('Options'));
... ...
@@ -103,6 +103,16 @@ export class CPUControls extends AbstractControls {
103 103
     }
104 104
 }
105 105
 
106
+export interface KeyboardControlsMapping {
107
+    moveUp?: number;
108
+    moveDown?: number;
109
+    moveLeft?: number;
110
+    moveRight?: number;
111
+    hammer?: number;
112
+    dash?: number;
113
+    menu?: number;
114
+}
115
+
106 116
 export class KeyboardControls extends AbstractControls {
107 117
     kb: Phaser.Keyboard;
108 118
     game: Phaser.Game;
... ...
@@ -113,31 +123,43 @@ export class KeyboardControls extends AbstractControls {
113 123
     keyCodeHammerTime: number;
114 124
     keyCodeDash: number;
115 125
     keyCodeMenu: number;
116
-    moveXAxis: number;
117
-    moveYAxis: number;
118 126
 
119 127
     constructor(game: Phaser.Game) {
120 128
         super();
121 129
         this.game = game;
122 130
         game.input.gamepad.start();
123
-        this.setupKeyboardLayout();
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
+        });
124 137
     }
125 138
 
126
-    setupKeyboardLayout() {
139
+    setupKeyboardLayout(layout: string) {
127 140
         this.kb = this.game.input.keyboard;
128
-        let layout = localStorage.getItem('keyboard.layout');
141
+        try {
142
+            let mapping: KeyboardControlsMapping = JSON.parse(layout) || {};
143
+            this.keyCodeMoveUp = mapping.moveUp || Phaser.KeyCode.UP;
144
+            this.keyCodeMoveDown = mapping.moveDown || Phaser.KeyCode.DOWN;
145
+            this.keyCodeMoveLeft = mapping.moveDown || Phaser.KeyCode.LEFT;
146
+            this.keyCodeMoveRight = mapping.moveRight || Phaser.KeyCode.RIGHT;
147
+            this.keyCodeHammerTime = mapping.hammer || Phaser.KeyCode.SHIFT;
148
+            this.keyCodeDash = mapping.dash || Phaser.KeyCode.CONTROL;
149
+            this.keyCodeMenu = mapping.menu || Phaser.KeyCode.ESC;
150
+            return;
151
+        } catch (e) {
152
+        }
129 153
         if (layout == 'azerty') {
130 154
             this.useAzertyLayout();
131 155
         } else if (layout == 'qwerty') {
132 156
             this.useQwertyLayout();
133
-        } else if (layout == 'custom') {
134
-            this.useCustomKeyboardLayout();
135 157
         } else {
136 158
             this.useOtherKeyboardLayout();
137 159
         }
138 160
     }
139 161
 
140
-    useAzertyLayout() {
162
+    private useAzertyLayout() {
141 163
         this.keyCodeMoveUp = Phaser.KeyCode.Z;
142 164
         this.keyCodeMoveDown = Phaser.KeyCode.S;
143 165
         this.keyCodeMoveLeft = Phaser.KeyCode.Q;
... ...
@@ -145,10 +167,9 @@ export class KeyboardControls extends AbstractControls {
145 167
         this.keyCodeHammerTime = Phaser.KeyCode.K;
146 168
         this.keyCodeDash = Phaser.KeyCode.J;
147 169
         this.keyCodeMenu = Phaser.KeyCode.ESC;
148
-        localStorage.setItem('keyboard.layout', 'azerty');
149 170
     }
150 171
 
151
-    useQwertyLayout() {
172
+    private useQwertyLayout() {
152 173
         this.keyCodeMoveUp = Phaser.KeyCode.W;
153 174
         this.keyCodeMoveDown = Phaser.KeyCode.S;
154 175
         this.keyCodeMoveLeft = Phaser.KeyCode.A;
... ...
@@ -156,10 +177,9 @@ export class KeyboardControls extends AbstractControls {
156 177
         this.keyCodeHammerTime = Phaser.KeyCode.K;
157 178
         this.keyCodeDash = Phaser.KeyCode.J;
158 179
         this.keyCodeMenu = Phaser.KeyCode.ESC;
159
-        localStorage.setItem('keyboard.layout', 'qwerty');
160 180
     }
161 181
 
162
-    useOtherKeyboardLayout() {
182
+    private useOtherKeyboardLayout() {
163 183
         this.keyCodeMoveUp = Phaser.KeyCode.UP;
164 184
         this.keyCodeMoveDown = Phaser.KeyCode.DOWN;
165 185
         this.keyCodeMoveLeft = Phaser.KeyCode.LEFT;
... ...
@@ -167,18 +187,6 @@ export class KeyboardControls extends AbstractControls {
167 187
         this.keyCodeHammerTime = Phaser.KeyCode.SHIFT;
168 188
         this.keyCodeDash = Phaser.KeyCode.CONTROL;
169 189
         this.keyCodeMenu = Phaser.KeyCode.ESC;
170
-        localStorage.setItem('keyboard.layout', 'other');
171
-    }
172
-
173
-    useCustomKeyboardLayout() {
174
-        this.keyCodeMoveUp = this.readNumberFromLocalStorage('keyboard.layout.custom.moveUp', Phaser.KeyCode.UP);
175
-        this.keyCodeMoveDown = this.readNumberFromLocalStorage('keyboard.layout.custom.moveDown', Phaser.KeyCode.DOWN);
176
-        this.keyCodeMoveLeft = this.readNumberFromLocalStorage('keyboard.layout.custom.moveLeft', Phaser.KeyCode.LEFT);
177
-        this.keyCodeMoveRight = this.readNumberFromLocalStorage('keyboard.layout.custom.moveRight', Phaser.KeyCode.RIGHT);
178
-        this.keyCodeHammerTime = this.readNumberFromLocalStorage('keyboard.layout.custom.hammer', Phaser.KeyCode.SHIFT);
179
-        this.keyCodeDash = this.readNumberFromLocalStorage('keyboard.layout.custom.dash', Phaser.KeyCode.CONTROL);
180
-        this.keyCodeMenu = this.readNumberFromLocalStorage('keyboard.layout.custom.menu', Phaser.KeyCode.ESC);
181
-        localStorage.setItem('keyboard.layout', 'custom');
182 190
     }
183 191
 
184 192
     dashingAngle(playerPos: Phaser.Point): number {
... ...
@@ -254,6 +262,11 @@ export class PadControls extends AbstractControls {
254 262
         this.game = game;
255 263
         game.input.gamepad.start();
256 264
         this.padIndex = padIndex;
265
+        window.addEventListener('storage', (e) => {
266
+            if (e.key.startsWith('gamepad.') && e.key.endsWith('.layout')) {
267
+                this.pad = null;
268
+            }
269
+        });
257 270
     }
258 271
 
259 272
     private checkPad(): boolean {