feat: improve screen context system
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
import * as THREE from "three";
|
||||
import { GLTFLoader } from "three/addons/loaders/GLTFLoader.js";
|
||||
import { ScreenManager } from "./screen-manager";
|
||||
import { HomeScreen } from "./screens/home";
|
||||
|
||||
const SCREEN_SOURCE_TEX_SIZE = 1024;
|
||||
const TOP_SCREEN_SOURCE_TEX_HEIGHT = SCREEN_SOURCE_TEX_SIZE / 404;
|
||||
@@ -37,11 +36,10 @@ export class NDS extends THREE.Object3D {
|
||||
public constructor(camera: THREE.Camera, domElement: HTMLCanvasElement) {
|
||||
super();
|
||||
|
||||
// Initialize screen manager
|
||||
this.screenManager = new ScreenManager(new HomeScreen());
|
||||
this.screenManager = new ScreenManager();
|
||||
|
||||
const loader = new GLTFLoader();
|
||||
// load model
|
||||
const loader = new GLTFLoader();
|
||||
loader.load("/nintendo-ds/scene.gltf", ({ scene: model }) => {
|
||||
model.scale.set(50, 50, 50);
|
||||
|
||||
|
||||
@@ -1,16 +1,20 @@
|
||||
import type { Screen, ScreenContext } from "./screen";
|
||||
import type { Screen } from "./screen";
|
||||
import { HomeScreen } from "./screens/home";
|
||||
|
||||
export class ScreenManager {
|
||||
private currentScreen: Screen;
|
||||
private context: ScreenContext;
|
||||
|
||||
constructor(initialScreen: Screen) {
|
||||
this.currentScreen = initialScreen;
|
||||
this.context = {
|
||||
constructor() {
|
||||
const context = {
|
||||
navigate: (screen: Screen) => {
|
||||
if (this.currentScreen.destroy) {
|
||||
this.currentScreen.destroy();
|
||||
}
|
||||
this.currentScreen = screen;
|
||||
},
|
||||
};
|
||||
|
||||
this.currentScreen = new HomeScreen(context);
|
||||
}
|
||||
|
||||
renderTop(ctx: CanvasRenderingContext2D) {
|
||||
@@ -29,7 +33,7 @@ export class ScreenManager {
|
||||
|
||||
handleTouch(x: number, y: number) {
|
||||
if (this.currentScreen.handleTouch) {
|
||||
this.currentScreen.handleTouch(x, y, this.context);
|
||||
this.currentScreen.handleTouch(x, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,5 +5,6 @@ export interface ScreenContext {
|
||||
export interface Screen {
|
||||
renderTop(ctx: CanvasRenderingContext2D): void;
|
||||
renderBottom(ctx: CanvasRenderingContext2D): void;
|
||||
handleTouch?(x: number, y: number, context: ScreenContext): void;
|
||||
handleTouch?(x: number, y: number): void;
|
||||
destroy?(): void;
|
||||
}
|
||||
|
||||
@@ -27,7 +27,6 @@ export class ButtonNavigation<T extends string> {
|
||||
this.navigation = config.navigation;
|
||||
this.onButtonClick = config.onButtonClick;
|
||||
|
||||
// TODO: this should be handled by the nds itself i think, and dispatched
|
||||
this.keydownHandler = this.handleKeyPress.bind(this);
|
||||
window.addEventListener("keydown", this.keydownHandler);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user