EN
apps/winzik / src / Gui / SyllableStrip.php · commit 6576d6f
<?phpnamespace App\Gui;use JumPHP\Gui\Pointer;use JumPHP\Gui\Theme;use JumPHP\Gui\Widget;use JumPHP\Renderer\Renderer;/** * The syllables of a line of the lyrics editor, side by side, each with its moment under it: a click chooses one (its * moment is then put right with the -100 / -10 / +10 / +100 ms buttons), the one sung now is lit while the song plays. * Event: select (int $index). * * @property list<string> $syllables * @property list<float|null> $times * @property int $selected * @property float $now */final class SyllableStrip extends Widget{    public const DEFAULTS = ['syllables' => [], 'times' => [], 'selected' => -1, 'now' => 0.0];    /** @var list<array{0: float, 1: float, 2: float, 3: float}> where each syllable was drawn */    private array $boxes = [];    public static function events(): array    {        return ['select'];    }    protected function content(Theme $t, float $maxWidth, string $breakpoint): array    {        return [$t->px(300), $t->px(56)];    }    public function render(Renderer $r, Theme $t): void    {        $t->frame($r, $this->x, $this->y, $this->w, $this->h, $t->color('surface'), $t->color('border'));        $font = $t->font();        $small = $t->font($t->fontSize * 0.75);        $syllables = array_values((array)$this->props['syllables']);        $times = array_values((array)$this->props['times']);        $pad = $t->px(6);        if ($syllables === []) {            // on as many lines as its width needs (a phone held upright: two)            $lines = [''];            foreach (explode(' ', 'Choisir une ligne de la synchronisation : ses syllabes viennent ici.') as $word) {                $try = ltrim(end($lines) . ' ' . $word);                if (end($lines) !== '' && $font->width($try) > $this->w - 2 * $pad) {                    $lines[] = $word;                } else {                    $lines[count($lines) - 1] = $try;                }            }            $lh = $font->height() * 1.15;            $top = $this->y + ($this->h - $lh * count($lines)) / 2;            foreach ($lines as $i => $line) {                $font->draw($r, $line, $this->x + $pad, $top + $i * $lh, 1, $t->color('muted'));            }            return;        }        // the one sung now        $now = (float)$this->props['now'];        $sung = -1;        foreach ($times as $k => $time) {            if ($time !== null && $time <= $now) {                $sung = $k;            }        }        $this->boxes = [];        $x = $this->x + $pad;        $y = $this->y + $pad;        $chipH = $font->lineHeight() + $small->lineHeight() + $t->px(6);        foreach ($syllables as $k => $syl) {            $label = trim($syl) === '' ? '·' : trim($syl);            $time = $times[$k] ?? null;            $stamp = $time === null ? '?' : sprintf('%.2f', $time);            $cw = max($font->width($label), $small->width($stamp)) + $t->px(12);            if ($x + $cw > $this->x + $this->w - $pad && $x > $this->x + $pad) {                $x = $this->x + $pad;                $y += $chipH + $t->px(4);            }            if ($y + $chipH > $this->y + $this->h) {                break;      // no room: the rest of the line is not shown            }            $chosen = $k === (int)$this->props['selected'];            $back = $chosen ? $t->color('accent') : ($k === $sung ? $t->color('selection') : $t->color('track'));            $t->box($r, $x, $y, $cw, $chipH, $back, $t->px(4));            $font->draw($r, $label, $x + $cw / 2, $y + $t->px(2), 1, $k === $sung && !$chosen ? [255, 224, 48] : $t->color('text'), 'center');            $small->draw($r, $stamp, $x + $cw / 2, $y + $t->px(3) + $font->lineHeight(), 1, $time === null ? $t->color('muted') : $t->color('text'), 'center');            $this->boxes[$k] = [$x, $y, $cw, $chipH];            $x += $cw + $t->px(4);        }    }    public function pointerDown(Pointer $p): void    {        foreach ($this->boxes as $k => [$bx, $by, $bw, $bh]) {            if ($p->x >= $bx && $p->x < $bx + $bw && $p->y >= $by && $p->y < $by + $bh) {                $this->props['selected'] = $k;                $this->emit('select', $k);                return;            }        }    }}