EN
games/rob-revisited / src / Revisited / Ui / MenuList.php · commit f9a1307
<?phpnamespace App\Revisited\Ui;/** * A vertical menu of the glass panels (title card, pause, death, options): keys / pad move the selection, Enter or a * click runs the row. Rows: [label, callable, enabled = true, value = null (shown on the right, a callable)]. */final class MenuList{    /** @var list<array{0: string, 1: callable, 2?: bool, 3?: callable|null}> */    public array $rows = [];    public int $sel = 0;    public function __construct(private string $name)    {    }    /** @param list<array{0: string, 1: callable, 2?: bool, 3?: callable|null}> $rows */    public function set(array $rows): void    {        $this->rows = $rows;        $this->sel = max(0, min($this->sel, count($rows) - 1));        if (!$this->enabled($this->sel)) {            $this->move(1);        }    }    private function enabled(int $i): bool    {        return isset($this->rows[$i]) && ($this->rows[$i][2] ?? true);    }    private function move(int $dir): void    {        $n = count($this->rows);        for ($k = 0; $k < $n; $k++) {            $this->sel = ($this->sel + $dir + $n) % $n;            if ($this->enabled($this->sel)) {                return;            }        }    }    /** Handles the input; returns true when a row ran. */    public function update(UiInput $in): bool    {        if ($this->rows === []) {            return false;        }        if ($in->up) {            $this->move(-1);        }        if ($in->down) {            $this->move(1);        }        $prefix = $this->name . ':';        if ($in->moved && $in->hover !== null && str_starts_with($in->hover, $prefix)) {            $i = (int) substr($in->hover, strlen($prefix));            if ($this->enabled($i)) {                $this->sel = $i;            }        }        $run = $in->ok;        if ($in->click && $in->hit !== null && str_starts_with($in->hit, $prefix)) {            $i = (int) substr($in->hit, strlen($prefix));            if ($this->enabled($i)) {                $this->sel = $i;                $run = true;            }        }        if (($in->left || $in->right) && isset($this->rows[$this->sel][3])) {            // a row with a value: left / right change it the same way as Enter            $run = true;        }        if ($run && $this->enabled($this->sel)) {            ($this->rows[$this->sel][1])();            return true;        }        return false;    }    /** Draws the rows from ($x, $y), $w wide, centred text. */    public function draw(Canvas $c, float $x, float $y, float $w, float $rowH = 44.0): void    {        foreach ($this->rows as $i => $row) {            $ry = $y + $i * $rowH;            $on = $i === $this->sel;            $enabled = $row[2] ?? true;            if ($on) {                $c->rect($x, $ry, $w, $rowH - 6, [120, 190, 255, 46]);                $c->border($x, $ry, $w, $rowH - 6, [150, 210, 255, 120]);                $c->rect($x, $ry, 3, $rowH - 6, [150, 210, 255, 220]);            }            $label = $row[0];            $value = isset($row[3]) ? (string) ($row[3])() : null;            $col = !$enabled ? [110, 116, 124] : ($on ? [255, 255, 255] : [206, 214, 222]);            if ($value !== null) {                // the label column and the value column share the row: the value keeps its width (up to half the row)                $shown = '‹ ' . \App\Revisited\I18n\Translator::t($value) . ' ›';                $inner = $w - 28;                $vw = min($c->measure($shown, 19) + 2 * Canvas::PAD, $inner * 0.5);                $c->pushBox($x + 14, $ry, $inner - $vw - 6, $rowH - 6);                $c->text($label, $x + 18, $ry + 9, 19, $col);                $c->popBox();                $c->pushBox($x + $w - 14 - $vw, $ry, $vw, $rowH - 6);                $c->text($shown, $x + $w - 18, $ry + 9, 19, $on ? [255, 214, 120] : [190, 176, 140], 'right');                $c->popBox();            } else {                $c->pushBox($x + 6, $ry, $w - 12, $rowH - 6);                $c->text($label, $x + $w / 2, $ry + 9, 20, $col, 'center');                $c->popBox();            }            $c->hit($x, $ry, $w, $rowH - 6, $this->name . ':' . $i);        }    }}