@remvst/game-navigation-core 1.9.1 → 1.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,6 +7,7 @@ export declare abstract class GamePlugin {
7
7
  bind(game: Game): void;
8
8
  setup(): void;
9
9
  setupScreen(screen: Screen): void;
10
+ onCurrentScreenChanged(screen: Screen | null): void;
10
11
  cycle(elapsed: number): void;
11
12
  onCycleStart(): void;
12
13
  onCycleEnd(): void;
@@ -10,6 +10,7 @@ class GamePlugin {
10
10
  }
11
11
  setup() { }
12
12
  setupScreen(screen) { }
13
+ onCurrentScreenChanged(screen) { }
13
14
  cycle(elapsed) { }
14
15
  onCycleStart() { }
15
16
  onCycleEnd() { }
package/lib/game/game.js CHANGED
@@ -121,7 +121,11 @@ class Game {
121
121
  }
122
122
  }
123
123
  onCurrentScreenChanged() {
124
- this.container.style.cursor = this.screenStack.current()?.cursorType;
124
+ const screen = this.screenStack.current();
125
+ this.container.style.cursor = screen?.cursorType;
126
+ for (const plugin of this.plugins) {
127
+ plugin.onCurrentScreenChanged(screen);
128
+ }
125
129
  }
126
130
  }
127
131
  exports.Game = Game;
package/lib/index.d.ts CHANGED
@@ -3,11 +3,13 @@ export * from "./game/game-params";
3
3
  export * from "./game/game-plugin";
4
4
  export * from "./interaction/inputs";
5
5
  export * from "./interaction/screen-mouse-inputs";
6
+ export * from "./navigation/back-navigation-listener";
6
7
  export * from "./navigation/resolver";
7
8
  export * from "./navigation/screen-stack";
8
9
  export * from "./ui/screen";
9
10
  export * from "./ui/screen-delegate";
10
11
  export * from "./plugins/automation-game-plugin";
12
+ export * from "./plugins/back-navigation-plugin";
11
13
  export * from "./plugins/blur-pausing-game-plugin";
12
14
  export * from "./plugins/fullscreen-button-game-plugin";
13
15
  export * from "./plugins/metrics-game-plugin";
package/lib/index.js CHANGED
@@ -19,11 +19,13 @@ __exportStar(require("./game/game-params"), exports);
19
19
  __exportStar(require("./game/game-plugin"), exports);
20
20
  __exportStar(require("./interaction/inputs"), exports);
21
21
  __exportStar(require("./interaction/screen-mouse-inputs"), exports);
22
+ __exportStar(require("./navigation/back-navigation-listener"), exports);
22
23
  __exportStar(require("./navigation/resolver"), exports);
23
24
  __exportStar(require("./navigation/screen-stack"), exports);
24
25
  __exportStar(require("./ui/screen"), exports);
25
26
  __exportStar(require("./ui/screen-delegate"), exports);
26
27
  __exportStar(require("./plugins/automation-game-plugin"), exports);
28
+ __exportStar(require("./plugins/back-navigation-plugin"), exports);
27
29
  __exportStar(require("./plugins/blur-pausing-game-plugin"), exports);
28
30
  __exportStar(require("./plugins/fullscreen-button-game-plugin"), exports);
29
31
  __exportStar(require("./plugins/metrics-game-plugin"), exports);
@@ -0,0 +1,3 @@
1
+ export interface BackNavigationListener {
2
+ onBackNavigation(): void;
3
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -32,7 +32,7 @@ class Resolver {
32
32
  };
33
33
  }
34
34
  dismiss() {
35
- this.screen.game.screenStack.pop();
35
+ this.screen.game.screenStack.popTo(this.screen, true);
36
36
  }
37
37
  }
38
38
  exports.Resolver = Resolver;
@@ -11,6 +11,7 @@ export declare class ScreenStack {
11
11
  readonly screens: Screen[];
12
12
  private readonly queue;
13
13
  prepareScreen: (screen: Screen) => void;
14
+ onStackChanged: () => void;
14
15
  constructor(game: Game);
15
16
  findByType<T extends Screen>(screenType: new (...params: any) => T): T | null;
16
17
  current(): Screen;
@@ -15,6 +15,7 @@ class ScreenStack {
15
15
  this.screens = [];
16
16
  this.queue = new optimization_1.OperationQueue();
17
17
  this.prepareScreen = () => { };
18
+ this.onStackChanged = () => { };
18
19
  }
19
20
  findByType(screenType) {
20
21
  return (this.screens.find((scr) => scr instanceof screenType) || null);
@@ -23,12 +24,16 @@ class ScreenStack {
23
24
  return this.screens[this.screens.length - 1] || null;
24
25
  }
25
26
  popTo(screen, inclusive = false) {
27
+ if (!this.screens.includes(screen)) {
28
+ return;
29
+ }
26
30
  while (this.current() && this.current() !== screen) {
27
31
  this.pop();
28
32
  }
29
33
  if (inclusive && this.current() === screen) {
30
34
  this.pop();
31
35
  }
36
+ this.onStackChanged();
32
37
  }
33
38
  push(screen) {
34
39
  this.queue.execute(() => {
@@ -0,0 +1,12 @@
1
+ import { GamePlugin } from "../game/game-plugin";
2
+ import { Screen } from "../ui/screen";
3
+ export declare class BackNavigationPlugin extends GamePlugin {
4
+ readonly key = "back-navigation";
5
+ private pushedState;
6
+ setup(): void;
7
+ pushState(): void;
8
+ popState(): void;
9
+ private onPopState;
10
+ setupScreen(screen: Screen): void;
11
+ onCurrentScreenChanged(screen: Screen | null): void;
12
+ }
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BackNavigationPlugin = void 0;
4
+ const game_plugin_1 = require("../game/game-plugin");
5
+ class BackNavigationPlugin extends game_plugin_1.GamePlugin {
6
+ constructor() {
7
+ super(...arguments);
8
+ this.key = "back-navigation";
9
+ this.pushedState = false;
10
+ }
11
+ setup() {
12
+ super.setup();
13
+ const { length } = history;
14
+ if (length > 0)
15
+ history.go(-length);
16
+ window.addEventListener("popstate", () => this.onPopState());
17
+ }
18
+ pushState() {
19
+ if (this.pushedState)
20
+ return;
21
+ this.pushedState = true;
22
+ window.history.pushState({}, "", "");
23
+ }
24
+ popState() {
25
+ if (!this.pushedState)
26
+ return;
27
+ this.pushedState = false;
28
+ window.history.back();
29
+ }
30
+ onPopState() {
31
+ this.pushedState = false;
32
+ const currentScreen = this.game.screenStack.current();
33
+ if (!currentScreen?.onBackNavigation)
34
+ return;
35
+ this.pushState();
36
+ currentScreen.onBackNavigation();
37
+ }
38
+ setupScreen(screen) {
39
+ super.setupScreen(screen);
40
+ }
41
+ onCurrentScreenChanged(screen) {
42
+ const handlesBackNavigation = screen?.onBackNavigation;
43
+ if (handlesBackNavigation) {
44
+ this.pushState();
45
+ }
46
+ else {
47
+ this.popState();
48
+ }
49
+ }
50
+ }
51
+ exports.BackNavigationPlugin = BackNavigationPlugin;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@remvst/game-navigation-core",
3
- "version": "1.9.1",
3
+ "version": "1.10.1",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
@@ -17,6 +17,7 @@ export abstract class GamePlugin {
17
17
  setup(): void {}
18
18
 
19
19
  setupScreen(screen: Screen): void {}
20
+ onCurrentScreenChanged(screen: Screen | null): void {}
20
21
 
21
22
  cycle(elapsed: number) {}
22
23
 
package/src/game/game.ts CHANGED
@@ -165,6 +165,11 @@ export abstract class Game<ParamsType extends GameParams = GameParams> {
165
165
  }
166
166
 
167
167
  onCurrentScreenChanged() {
168
- this.container.style.cursor = this.screenStack.current()?.cursorType;
168
+ const screen = this.screenStack.current();
169
+ this.container.style.cursor = screen?.cursorType;
170
+
171
+ for (const plugin of this.plugins) {
172
+ plugin.onCurrentScreenChanged(screen);
173
+ }
169
174
  }
170
175
  }
package/src/index.ts CHANGED
@@ -5,6 +5,7 @@ export * from "./game/game-plugin";
5
5
  export * from "./interaction/inputs";
6
6
  export * from "./interaction/screen-mouse-inputs";
7
7
 
8
+ export * from "./navigation/back-navigation-listener";
8
9
  export * from "./navigation/resolver";
9
10
  export * from "./navigation/screen-stack";
10
11
 
@@ -12,6 +13,7 @@ export * from "./ui/screen";
12
13
  export * from "./ui/screen-delegate";
13
14
 
14
15
  export * from "./plugins/automation-game-plugin";
16
+ export * from "./plugins/back-navigation-plugin";
15
17
  export * from "./plugins/blur-pausing-game-plugin";
16
18
  export * from "./plugins/fullscreen-button-game-plugin";
17
19
  export * from "./plugins/metrics-game-plugin";
@@ -0,0 +1,3 @@
1
+ export interface BackNavigationListener {
2
+ onBackNavigation(): void;
3
+ }
@@ -43,7 +43,7 @@ export class Resolver<T> {
43
43
  }
44
44
 
45
45
  private dismiss() {
46
- this.screen.game.screenStack.pop();
46
+ this.screen.game.screenStack.popTo(this.screen, true);
47
47
  }
48
48
  }
49
49
 
@@ -14,6 +14,8 @@ export class ScreenStack {
14
14
  private readonly queue = new OperationQueue();
15
15
  prepareScreen: (screen: Screen) => void = () => {};
16
16
 
17
+ onStackChanged: () => void = () => {};
18
+
17
19
  constructor(private readonly game: Game) {}
18
20
 
19
21
  findByType<T extends Screen>(
@@ -29,6 +31,10 @@ export class ScreenStack {
29
31
  }
30
32
 
31
33
  popTo(screen: Screen, inclusive: boolean = false) {
34
+ if (!this.screens.includes(screen)) {
35
+ return;
36
+ }
37
+
32
38
  while (this.current() && this.current() !== screen) {
33
39
  this.pop();
34
40
  }
@@ -36,6 +42,8 @@ export class ScreenStack {
36
42
  if (inclusive && this.current() === screen) {
37
43
  this.pop();
38
44
  }
45
+
46
+ this.onStackChanged();
39
47
  }
40
48
 
41
49
  push(screen: Screen) {
@@ -0,0 +1,56 @@
1
+ import { GamePlugin } from "../game/game-plugin";
2
+ import { BackNavigationListener } from "../navigation/back-navigation-listener";
3
+ import { Screen } from "../ui/screen";
4
+
5
+ export class BackNavigationPlugin extends GamePlugin {
6
+ readonly key = "back-navigation";
7
+
8
+ private pushedState = false;
9
+
10
+ setup(): void {
11
+ super.setup();
12
+
13
+ const { length } = history;
14
+ if (length > 0) history.go(-length);
15
+
16
+ window.addEventListener("popstate", () => this.onPopState());
17
+ }
18
+
19
+ pushState() {
20
+ if (this.pushedState) return;
21
+ this.pushedState = true;
22
+ window.history.pushState({}, "", "");
23
+ }
24
+
25
+ popState() {
26
+ if (!this.pushedState) return;
27
+ this.pushedState = false;
28
+ window.history.back();
29
+ }
30
+
31
+ private onPopState() {
32
+ this.pushedState = false;
33
+
34
+ const currentScreen =
35
+ this.game.screenStack.current() as unknown as BackNavigationListener | null;
36
+ if (!currentScreen?.onBackNavigation) return;
37
+
38
+ this.pushState();
39
+ currentScreen.onBackNavigation();
40
+ }
41
+
42
+ setupScreen(screen: Screen) {
43
+ super.setupScreen(screen);
44
+ }
45
+
46
+ onCurrentScreenChanged(screen: Screen | null): void {
47
+ const handlesBackNavigation = (
48
+ screen as unknown as BackNavigationListener
49
+ )?.onBackNavigation;
50
+ if (handlesBackNavigation) {
51
+ this.pushState();
52
+ } else {
53
+ this.popState();
54
+ }
55
+ }
56
+ }