JA
games/dmmotor / src / Dm / MenuScreen.php · commit 9d248a4
<?php/* * dmmotor — the menus drawn with the WAD's own pictures (after m_menu.c of linuxdoom-1.10): a title picture, * items of 16 pixels, the skull cursor. * Copyright (C) 1993-1996 by id Software, Inc.; PHP port for dmmotor. GPL v2 (see LICENSE). */namespace App\Dm;use App\Dm\Render\Framebuffer;final class MenuScreen{    public const LINEHEIGHT = 16;    /**     * @param list<array{0:string,1:string}> $items [picture, id] ("" picture = an empty line)     * @param list<array{0:string,1:int,2:int}> $titles [picture, x, y]     */    public function __construct(        public string $id,        public array $items,        public int $x,        public int $y,        public array $titles = [],        public int $selected = 0,    ) {    }    public function move(int $dir): void    {        $n = count($this->items);        do {            $this->selected = ($this->selected + $dir + $n) % $n;        } while ($this->items[$this->selected][0] === '');    }    public function selectedId(): string    {        return $this->items[$this->selected][1];    }    public function draw(Gfx $g, Framebuffer $fb, int $tic): void    {        foreach ($this->titles as [$pic, $x, $y]) {            if ($g->wad->has($pic)) {                $fb->drawPatch($x, $y, $g->patch($pic));            }        }        foreach ($this->items as $i => [$pic, $_]) {            $y = $this->y + $i * self::LINEHEIGHT;            if (str_starts_with($pic, '@')) {                // a save slot: the text in its box (M_LSLEFT, M_LSCNTR x 24, M_LSRGHT)                if ($g->wad->has('M_LSLEFT')) {                    $fb->drawPatch($this->x - 8, $y + 7, $g->patch('M_LSLEFT'));                    for ($c = 0; $c < 24; $c++) {                        $fb->drawPatch($this->x + $c * 8, $y + 7, $g->patch('M_LSCNTR'));                    }                    $fb->drawPatch($this->x + 24 * 8, $y + 7, $g->patch('M_LSRGHT'));                }                StatusBar::drawText($g, $fb, $this->x, $y, substr($pic, 1));            } elseif ($pic !== '' && $g->wad->has($pic)) {                $fb->drawPatch($this->x, $y, $g->patch($pic));            }        }        $skull = (intdiv($tic, 8) & 1) ? 'M_SKULL2' : 'M_SKULL1';        $fb->drawPatch($this->x - 32, $this->y - 5 + $this->selected * self::LINEHEIGHT, $g->patch($skull));    }    /** A screen-wide picture (TITLEPIC, HELP1, INTERPIC...): patches of 320 x 200. */    public static function fullPicture(Gfx $g, Framebuffer $fb, string $name): void    {        if ($g->wad->has($name)) {            $fb->drawPatch(0, 0, $g->patch($name));        }    }    /** A flat tiled over the screen (finale texts). */    public static function tileFlat(Gfx $g, Framebuffer $fb, string $flat): void    {        $num = $g->flatNum[strtoupper($flat)] ?? 0;        $data = $g->flat($num);        $rows = [];        for ($y = 0; $y < 64; $y++) {            $rows[$y] = str_repeat(substr($data, $y * 64, 64), 5);        }        $pixels = '';        for ($y = 0; $y < $fb->height; $y++) {            $pixels .= $rows[$y & 63];        }        $fb->pixels = $pixels;    }}