diff --git a/src/main.ts b/src/main.ts index 0710453..7b28fa8 100644 --- a/src/main.ts +++ b/src/main.ts @@ -9,10 +9,12 @@ import createAnimation from "./encoders/createAnimation"; import WebFont from "webfontloader"; import Player from "./player/Player"; import * as Hammer from "hammerjs"; -import Moves from "./ui/moves/Moves"; +import Moves from "./ui/Moves"; +import Controls from "./ui/Controls"; const $board = document.querySelector("#board"); const $moves = document.querySelector("#moves"); +const $controls = document.querySelector("#controls"); const boardConfig: BoardConfig = { size: 1024, @@ -63,21 +65,6 @@ const main = async () => { const player = new Player(board, gameConfig); const game = new Game().loadPGN(pgn); - new Moves($moves as HTMLElement, player).load(game.getMoves()); - - // @ts-ignore - window.game = game; - - await player.load(game); - - // @ts-ignore - window.player = player; - - // @ts-ignore - window.load = async (pgn: string) => { - await player.load(new Game().loadPGN(pgn)); - }; - const handlers = { prev() { player.pause(); @@ -116,6 +103,19 @@ const main = async () => { }, }; + const moves = new Moves($moves as HTMLElement, player).load(game.getMoves()); + const controls = new Controls($controls as HTMLElement, handlers).load(); + + await player.load(game); + + // @ts-ignore + window.load = async (pgn: string) => { + const game = new Game().loadPGN(pgn); + await player.load(game); + moves.load(game.getMoves()); + controls.load(); + }; + document.addEventListener( "contextmenu", (e) => { @@ -155,7 +155,7 @@ const main = async () => { hammer.on("swipedown", handlers.last); hammer.on("pinchin", handlers.showBorder); hammer.on("pinchout", handlers.hideBorder); - hammer.on("tap", handlers.togglePlay); + hammer.on("tap", handlers.next); hammer.on("press", handlers.flip); // createDownloadLink(pgn, boardConfig).then((link) => { diff --git a/src/style.css b/src/style.css index b0b0e2e..6a23346 100644 --- a/src/style.css +++ b/src/style.css @@ -48,6 +48,7 @@ body { .moves-box { background: rgba(255, 192, 203, 0.5); + height: 85vh; grid-area: moves; } @@ -65,7 +66,7 @@ body { .controls-box { background: rgba(0, 255, 0, 0.5); - height: 100px; + height: 15vh; grid-area: controls; } diff --git a/src/types.ts b/src/types.ts index be2f7ea..77fa3a7 100644 --- a/src/types.ts +++ b/src/types.ts @@ -161,3 +161,16 @@ export type Position = { placement: Placement; last: boolean; }; + +export type Handlers = { + prev(): void; + next(): void; + first(): void; + last(): void; + toggleBorder(): void; + showBorder(): void; + hideBorder(): void; + toggleExtraInfo(): void; + flip(): void; + togglePlay(): void; +}; diff --git a/src/ui/Controls.ts b/src/ui/Controls.ts new file mode 100644 index 0000000..b19d18f --- /dev/null +++ b/src/ui/Controls.ts @@ -0,0 +1,38 @@ +import { Handlers } from "./../types"; +import { html } from "common-tags"; + +class Controls { + constructor(private element: HTMLElement, handlers: Handlers) { + this.element.addEventListener("click", (e) => { + const target = e.target as HTMLElement; + + if (target?.dataset?.type === "control") { + const action = target?.dataset?.action; + console.log(action); + if (action && handlers.hasOwnProperty(action)) { + // @ts-ignore + handlers[action](); + } + } + }); + } + + load() { + const content = html`
+ + + + + + + + + `; + + this.element.innerHTML = content; + + return this; + } +} + +export default Controls; diff --git a/src/ui/moves/Moves.ts b/src/ui/Moves.ts similarity index 95% rename from src/ui/moves/Moves.ts rename to src/ui/Moves.ts index 3e14f99..761204b 100644 --- a/src/ui/moves/Moves.ts +++ b/src/ui/Moves.ts @@ -1,5 +1,5 @@ import { html } from "common-tags"; -import Player from "../../player/Player"; +import Player from "../player/Player"; import chunk_ from "@arrows/array/chunk_"; class Moves {