From 3b801c97ff21bfcec1cf43ebf94cfe7c34f7b98f Mon Sep 17 00:00:00 2001 From: Pihkaal Date: Sat, 13 Dec 2025 19:56:57 +0100 Subject: [PATCH] refactor(home): use new context system --- src/screens/home/bottom/index.ts | 78 ++++++++++++++++++-------------- src/screens/home/index.ts | 17 ++++--- 2 files changed, 55 insertions(+), 40 deletions(-) diff --git a/src/screens/home/bottom/index.ts b/src/screens/home/bottom/index.ts index a22a222..337afb2 100644 --- a/src/screens/home/bottom/index.ts +++ b/src/screens/home/bottom/index.ts @@ -1,6 +1,8 @@ import { ImageLoader } from "../../../utils/loadImages"; import { ButtonNavigation } from "../../../utils/buttonNavigation"; import { ButtonSelector } from "../../../utils/buttonSelector"; +import type { ScreenContext } from "../../../screen"; +import { ContactScreen } from "../../contact"; type HomeButton = "projects" | "contact" | "downloadPlay" | "settings"; @@ -13,52 +15,60 @@ export class HomeBottomScreen { settings: "/images/home/bottom-screen/buttons/settings.webp", }); - private navigation = new ButtonNavigation({ - buttons: { - projects: [31, 23, 193, 49], - contact: [31, 71, 97, 49], - downloadPlay: [127, 71, 97, 49], - settings: [112, 167, 31, 26], - }, - initialButton: "projects", - navigation: { - projects: { - down: "last", - left: "contact", - right: "downloadPlay", - horizontalMode: "preview", - }, - contact: { - up: "projects", - right: "downloadPlay", - down: "settings", - }, - downloadPlay: { - up: "projects", - left: "contact", - down: "settings", - }, - settings: { - up: "last", - }, - }, - onButtonClick: () => { - // TODO: navigate to correct screen - }, - }); + private navigation: ButtonNavigation; + private selector = new ButtonSelector([31, 23, 193, 49]); - private selector = new ButtonSelector(this.navigation.getSelectorPosition()); + constructor(context: ScreenContext) { + this.navigation = new ButtonNavigation({ + buttons: { + projects: [31, 23, 193, 49], + contact: [31, 71, 97, 49], + downloadPlay: [127, 71, 97, 49], + settings: [112, 167, 31, 26], + }, + initialButton: "projects", + navigation: { + projects: { + down: "last", + left: "contact", + right: "downloadPlay", + horizontalMode: "preview", + }, + contact: { + up: "projects", + right: "downloadPlay", + down: "settings", + }, + downloadPlay: { + up: "projects", + left: "contact", + down: "settings", + }, + settings: { + up: "last", + }, + }, + onButtonClick: (button) => { + if (button === "contact") { + context.navigate(new ContactScreen(context)); + } + }, + }); + } render(ctx: CanvasRenderingContext2D): void { if (!this.images.isReady) return; + // background ctx.drawImage(this.images.require("background"), 0, 0); + // buttons ctx.drawImage(this.images.require("game"), 33, 25); ctx.drawImage(this.images.require("contact"), 32, 72); ctx.drawImage(this.images.require("downloadPlay"), 128, 72); ctx.drawImage(this.images.require("settings"), 117, 170); + // selector this.selector.render(ctx, this.navigation.getSelectorPosition()); } diff --git a/src/screens/home/index.ts b/src/screens/home/index.ts index 73c3ec8..1b4092a 100644 --- a/src/screens/home/index.ts +++ b/src/screens/home/index.ts @@ -1,11 +1,14 @@ import type { Screen, ScreenContext } from "../../screen"; -import { ContactScreen } from "../contact-screen"; import { HomeTopScreen } from "./top"; import { HomeBottomScreen } from "./bottom"; export class HomeScreen implements Screen { private topScreen = new HomeTopScreen(); - private bottomScreen = new HomeBottomScreen(); + private bottomScreen: HomeBottomScreen; + + constructor(context: ScreenContext) { + this.bottomScreen = new HomeBottomScreen(context); + } renderTop(ctx: CanvasRenderingContext2D) { this.topScreen.render(ctx); @@ -15,9 +18,11 @@ export class HomeScreen implements Screen { this.bottomScreen.render(ctx); } - handleTouch(x: number, y: number, context: ScreenContext): void { - if (x >= 205 && x <= 256 && y >= 178 && y <= 192) { - context.navigate(new ContactScreen()); - } + handleTouch(x: number, y: number): void { + this.bottomScreen.handleTouch(x, y); + } + + destroy(): void { + this.bottomScreen.destroy(); } }