Browse code

disable keyboard/gamepad on keyboard/gamepad option screens

devnewton authored on 13/03/2018 08:08:51
Showing 7 changed files
... ...
@@ -25,7 +25,7 @@ export class GamepadOptions extends AbstractState {
25 25
         let subtitle = this.game.add.text(0, 0, 'Move stick or press button to show gamepad number', { font: "32px monospace", fill: 'white' });
26 26
         subtitle.y = this.game.world.height - subtitle.height;
27 27
 
28
-        const menu = new Menu(this.game);
28
+        const menu = new Menu(this.game).disableGamepadCursor();
29 29
         menu.addButton(new GamepadMenuButton(this.input.gamepad.pad1, "Gamepad 1", 200, 100, () => {
30 30
             this.game.state.start('GamepadOptionsLayout', true, false, 1);
31 31
         }));
... ...
@@ -48,7 +48,7 @@ export class GamepadOptionsBindAxisOrButton extends AbstractState {
48 48
         logo.scale.x = 2;
49 49
         logo.scale.y = 2;
50 50
         logo.anchor.setTo(0.5, 0);
51
-        const menu = new Menu(this.game, false);
51
+        const menu = new Menu(this.game).disableGamepadCursor();
52 52
         menu.button("Back", 200, 600, () => this.game.state.start('GamepadOptions'));
53 53
         this.createAxisButtons();
54 54
         this.createButtonsButtons();
... ...
@@ -25,7 +25,7 @@ export class GamepadOptionsLayout extends AbstractState {
25 25
         title.scale.y = 2;
26 26
         title.anchor.setTo(0.5, 0);
27 27
 
28
-        const menu = new Menu(this.game);
28
+        const menu = new Menu(this.game).disableGamepadCursor();
29 29
         menu.button("Xbox", 200, 200, () => {
30 30
             (this.game as UnderthiefGame).controllers.getPad(this.padIndex).useXboxLayout(this.padIndex);
31 31
             this.game.state.start('Options');
... ...
@@ -21,7 +21,7 @@ export class KeyboardOptions extends AbstractState {
21 21
         logo.scale.y = 2;
22 22
         logo.anchor.setTo(0.5, 0);
23 23
 
24
-        const menu = new Menu(this.game);
24
+        const menu = new Menu(this.game).disableKeyboardCursor();
25 25
         menu.button("⬆⬇⬅➡ shift ctrl", 200, 300, () => {
26 26
             (<UnderthiefGame>this.game).controllers.getKeyboard().useOtherKeyboardLayout();
27 27
             this.game.state.start('Options');
... ...
@@ -42,7 +42,7 @@ export class KeyboardOptionsBindKey extends AbstractState {
42 42
         logo.scale.x = 2;
43 43
         logo.scale.y = 2;
44 44
         logo.anchor.setTo(0.5, 0);
45
-        const menu = new Menu(this.game, false);
45
+        const menu = new Menu(this.game).disableKeyboardCursor();
46 46
         menu.button("Back", 200, 600, () => this.game.state.start('KeyboardOptions'));
47 47
     }
48 48
 
... ...
@@ -6,17 +6,27 @@ import { MenuSelect, MenuSelectOption } from "./MenuSelect";
6 6
 
7 7
 export class Menu extends Phaser.Group {
8 8
     buttons: Phaser.Group;
9
-    constructor(game: Phaser.Game, enableCursor = true) {
9
+    menuCursor: MenuCursor;
10
+
11
+    constructor(game: Phaser.Game) {
10 12
         super(game);
11 13
         this.buttons = game.add.group();
12 14
         this.add(this.buttons);
13
-        if(enableCursor) {
14
-            const cursor = new MenuCursor(game, this.buttons);
15
-            this.add(cursor);
16
-        }
15
+        this.menuCursor = new MenuCursor(game, this.buttons);
16
+        this.add(this.menuCursor);
17 17
         game.add.existing(this);
18 18
     }
19 19
 
20
+    disableGamepadCursor(): Menu {
21
+        this.menuCursor.gamepadCursor = false;
22
+        return this;
23
+    }
24
+
25
+    disableKeyboardCursor(): Menu {
26
+        this.menuCursor.gamepadCursor = false;
27
+        return this;
28
+    }
29
+
20 30
     addButton(button: MenuButton) {
21 31
         this.buttons.add(button);
22 32
     }
... ...
@@ -25,7 +35,7 @@ export class Menu extends Phaser.Group {
25 35
         this.buttons.add(new MenuButton(this.game, label, x, y, callback));
26 36
     }
27 37
 
28
-    select<T>(x: number, y: number, options: Array<MenuSelectOption<T>>): MenuSelect<T>  {
38
+    select<T>(x: number, y: number, options: Array<MenuSelectOption<T>>): MenuSelect<T> {
29 39
         const select = new MenuSelect(this.game, x, y, options);
30 40
         this.buttons.add(select);
31 41
         return select;
... ...
@@ -8,14 +8,13 @@ export class MenuCursor extends Phaser.Text {
8 8
     private buttons: Phaser.Group;
9 9
     private currentButton = 0;
10 10
     waitUntil: number = -1;
11
+    keyboardCursor = true;
12
+    gamepadCursor = true;
11 13
 
12 14
     constructor(game: Phaser.Game, buttons: Phaser.Group) {
13 15
         super(game, 0, 0, '👉', { font: "64px monospace", fontWeight: 'bold', fill: 'white' });
14 16
         this.buttons = buttons;
15 17
         this.visible = false;
16
-        game.input.keyboard.addKey(Phaser.Keyboard.UP).onDown.add(() => this.moveToButton(-1), null);
17
-        game.input.keyboard.addKey(Phaser.Keyboard.DOWN).onDown.add(() => this.moveToButton(1), null);
18
-        game.input.keyboard.addKey(Phaser.Keyboard.ENTER).onDown.add(() => this.activateButton(), null);
19 18
     }
20 19
 
21 20
     firstPadConnected(): Phaser.SinglePad {
... ...
@@ -35,29 +34,49 @@ export class MenuCursor extends Phaser.Text {
35 34
 
36 35
     update() {
37 36
         super.update();
38
-        if (this.parent.visible && this.game.time.time > this.waitUntil && this.processPad()) {
37
+        if (this.parent.visible && this.game.time.time > this.waitUntil && (this.processPad() || this.processKeyboard())) {
39 38
             this.waitUntil = this.game.time.time + 230;
40 39
         }
41 40
     }
42 41
 
42
+    processKeyboard(): boolean {
43
+        if (this.keyboardCursor) {
44
+            if (this.game.input.keyboard.isDown(Phaser.Keyboard.UP)) {
45
+                this.moveToButton(-1);
46
+                return true;
47
+            }
48
+            if (this.game.input.keyboard.isDown(Phaser.Keyboard.DOWN)) {
49
+                this.moveToButton(1);
50
+                return true;
51
+            }
52
+            if (this.game.input.keyboard.isDown(Phaser.Keyboard.ENTER)) {
53
+                this.activateButton();
54
+                return true;
55
+            }
56
+        }
57
+        return false;
58
+    }
59
+
43 60
     processPad(): boolean {
44
-        const pad = this.firstPadConnected();
45
-        if (pad) {
46
-            for (let b = 0; b < 4; ++b) {
47
-                let button = pad.getButton(b);
48
-                if (button && button.isDown) {
49
-                    this.activateButton();
50
-                    return true;
61
+        if (this.gamepadCursor) {
62
+            const pad = this.firstPadConnected();
63
+            if (pad) {
64
+                for (let b = 0; b < 4; ++b) {
65
+                    let button = pad.getButton(b);
66
+                    if (button && button.isDown) {
67
+                        this.activateButton();
68
+                        return true;
69
+                    }
51 70
                 }
52
-            }
53
-            for (let a = 0; a < 2; ++a) {
54
-                const axis = pad.axis(a);
55
-                if (axis > pad.deadZone) {
56
-                    this.moveToButton(1);
57
-                    return true;
58
-                } else if (axis < -pad.deadZone) {
59
-                    this.moveToButton(-1);
60
-                    return true;
71
+                for (let a = 0; a < 2; ++a) {
72
+                    const axis = pad.axis(a);
73
+                    if (axis > pad.deadZone) {
74
+                        this.moveToButton(1);
75
+                        return true;
76
+                    } else if (axis < -pad.deadZone) {
77
+                        this.moveToButton(-1);
78
+                        return true;
79
+                    }
61 80
                 }
62 81
             }
63 82
         }