NL
apps/winzik / src / Gui / LyricsBar.php · commit 6576d6f
<?phpnamespace App\Gui;use App\Model\Karaoke;use App\Model\Lyrics;/** * The lyrics (Form5 of 2001): a black bar across the whole bottom of the screen, the line sung in red, and the "Q" * button at its right. When the pointer comes over it, it jumps to the top of the screen so as not to hide what is * under it (Label1_MouseMove), and comes back down once the pointer has left the bottom (Timer3). * Its second button (Command2_Click: the bar grew upwards, 50 steps) unrolls it to the whole screen: the line sung big in * the middle, the ones before and after it smaller; the same button or Escape rolls it back. * On the whole screen (2026): big Précédent / Lecture / Suivant buttons on the left, the translation of each line under * it in a darker blue (T shows / hides it), and lyrics without times (lyricsSynced() false) as a page that scrolls * (wheel, drag; slowly by itself after estimatedLine() unless scrolled by hand), with a "non synchronisées" badge and a * button to synchronise them in the editor. The bar: the line, its translation under it (smaller), the wheel moves in * unsynced lyrics. */final class LyricsBar extends SkinWindow{    public const HEIGHT = 25;    /** the bar is at the top, away from the pointer (pasla) */    private bool $dodged = false;    /** where the pointer was: the bar runs away only from a pointer that moves */    private float $lastX = -1;    private float $lastY = -1;    public function id(): string    {        return 'lyrics';    }    /** @var array{0: float, 1: float, 2: float, 3: float}|null another screen this bar is drawn for (all the screens), null: the player's */    public ?array $monitorOverride = null;    /** @return array{0: float, 1: float, 2: float, 3: float} */    private function monitor(): array    {        return $this->monitorOverride ?? $this->desk->playerMonitor();    }    /** as wide as the screen of the player (several screens: that one only) */    public function width(): float    {        return $this->monitor()[2];    }    public function height(): float    {        return round($this->barHeight() + ($this->monitor()[3] - $this->barHeight()) * $this->grown);    }    /** the bar's height: on a phone (narrow) its buttons on a first line, the line sung under them, across the screen */    public function barHeight(): float    {        return $this->narrow() ? self::HEIGHT + 20 : self::HEIGHT;    }    /** the phone held upright: too narrow for the line beside the buttons */    private function narrow(): bool    {        return $this->desk->phone && $this->monitorOverride === null && $this->monitor()[2] < 400;    }    /** 0: the bar, 1: the whole screen (it grows and shrinks over a few frames, like Command2_Click) */    private float $grown = 0.0;    public function full(): bool    {        return (bool)($this->wz->ui['lyrics']['full'] ?? false);    }    public function toggleFull(): void    {        $this->wz->ui['lyrics']['full'] = !$this->full();        if ($this->full()) {            $this->wz->ui['lyrics']['visible'] = true;     // from the menu or F while the bar was hidden        }        $this->dodged = false;        $this->wz->touch();    }    /** on the whole screen, the lines glide and the ball bounces: 30 times a second; the bar: its line and its time */    public function redrawEvery(): float    {        if ($this->grown > 0 && $this->wz->player->state === \App\Audio\Player::PLAYING) {            return 1 / 30;        }        return 1 / 4;    }    public function visible(): bool    {        $ui = $this->wz->ui;        return ($ui['lyrics']['visible'] || $ui['lyricsOnly']) && !$ui['tray'];    }    public function controls(): array    {        $w = $this->width();        if ($this->full() || $this->grown > 0) {      // rolling back too: what is drawn is the whole screen            // on the whole screen: Q, its size, K (the animation over the syllables), T (the translation)            $controls = ['quit' => [$w - 25, 0, 25, 25], 'grow' => [$w - 50, 0, 25, 25], 'kara' => [$w - 75, 0, 25, 25], 'trans' => [$w - 100, 0, 25, 25]];            // on the left, big: previous, play / pause, next            [$bx, $bs, $y0, $gap] = $this->sideButtons();            $controls['prev'] = [$bx, $y0, $bs, $bs];            $controls['play'] = [$bx, $y0 + $bs + $gap, $bs, $bs];            $controls['next'] = [$bx, $y0 + 2 * ($bs + $gap), $bs, $bs];            if ($this->unsynced()) {                $controls['sync'] = [138, 3, 170, 19];                $controls['scroll'] = [314, 3, 124, 19];                $left = $bx + $bs + 8;                $controls['page'] = [$left, 26, max(1, $w - $left), max(1, $this->height() - 26)];            }            return $controls;        }        $controls = ['quit' => [$w - 25, 0, 25, 25], 'grow' => [$w - 50, 0, 25, 25], 'more' => [$w - 75, 0, 25, 25]];        if (!$this->unsynced()) {            // the lyrics in time but all late / early: -0.5 s / +0.5 s (saved)            $controls += ['later' => [$w - 107, 0, 32, 25], 'earlier' => [$w - 139, 0, 32, 25]];        } elseif (!$this->narrow()) {            $controls['sync'] = [$w - 262, 5, 58, 15];      // the badge "non synchro": the editor        }        return $controls + ['screens' => [$w - 165, 0, 25, 25]];    }    // --- lyrics: the buttons on the left of the whole screen, the translations, the lyrics without times ---    /** the line of the translation, darker blue than the red of the lyrics */    public const TRANSLATION = '#3a5cd6';    /** seconds after a scroll by hand before the page (or the bar) follows the song again */    public const FOLLOW_AFTER = 6.0;    /** @return array{0: float, 1: float, 2: float, 3: float} x, size, top of the first, gap of the left buttons (whole screen) */    public function sideButtons(): array    {        $h = $this->height();        $size = max(40.0, min(76.0, round($h * 0.12)));        $gap = round($size * 0.25);        return [16.0, $size, round(($h - 3 * $size - 2 * $gap) / 2), $gap];    }    /** the lyrics playing have no times (lines to scroll) */    public function unsynced(): bool    {        return $this->wz->lyrics !== null && !$this->wz->lyrics->synced;    }    /** the translation of line $i, '' when there is none or it is hidden (T) */    public function translationOf(int $i): string    {        $lyrics = $this->wz->lyrics;        if ($lyrics === null || $i < 0 || !$this->wz->showsTranslation()) {            return '';        }        return trim($lyrics->translation($i));    }    /** the page of unsynced lyrics: how far it is scrolled (pixels), where it was when the finger went down */    public float $page = 0.0;    private float $pageFrom = 0.0;    private float $pressY = 0.0;    /** seconds since the user last scrolled the lyrics (the page, or the bar's line) */    private float $handClock = 1000.0;    /** the bar, unsynced lyrics: the line chosen with the wheel */    private int $barLine = 0;    /** The user scrolled: the page / the bar stay where they put it for FOLLOW_AFTER seconds. */    public function byHand(): void    {        $this->handClock = 0.0;    }    public function followsSong(): bool    {        return $this->handClock >= self::FOLLOW_AFTER;    }    /** the line of unsynced lyrics the bar shows: the estimated one, else the one chosen with the wheel */    public function barIndex(): int    {        $lyrics = $this->wz->lyrics;        if ($lyrics === null || $lyrics->lines === []) {            return -1;        }        if ($this->followsSong()) {            $i = max(0, $this->wz->estimatedLine());            while ($i > 0 && trim(Karaoke::plain((string)$lyrics->lines[$i][1])) === '') {                $i--;       // a blank between two verses: the verse sung keeps its line            }            $this->barLine = $i;        }        return $this->barLine = max(0, min(count($lyrics->lines) - 1, $this->barLine));    }    /**     * Where each line of unsynced lyrics is on the page (whole screen): its top, its height, and the whole height.     * @return array{0: list<float>, 1: list<float>, 2: float}     */    public function pageLayout(): array    {        $tops = $heights = [];        $y = 70.0;        foreach ($this->wz->lyrics->lines ?? [] as $i => $line) {            $blank = trim(Karaoke::plain((string)$line[1])) === '';            $hgt = $blank ? 18.0 : 36.0 + ($this->translationOf($i) !== '' ? 22.0 : 0.0);            $tops[] = $y;            $heights[] = $hgt;            $y += $hgt;        }        return [$tops, $heights, $y + 60];    }    /** the page scrolled as far as it can go */    public function pageMax(): float    {        return max(0.0, $this->pageLayout()[2] - $this->height());    }    /** The page follows the line about sung, slowly (at 40 % of the screen's height). */    private function followPage(float $dt): void    {        [$tops, $heights] = $this->pageLayout();        $i = $this->wz->estimatedLine();        $target = $i < 0 || !isset($tops[$i]) ? 0.0 : $tops[$i] + $heights[$i] / 2 - $this->height() * 0.4;        $target = max(0.0, min($this->pageMax(), $target));        $this->page += ($target - $this->page) * min(1.0, $dt * 1.2);    }    /** The big buttons of the left: the song before / after, play / pause. */    private function transport(string $id): void    {        match ($id) {            'prev' => $this->wz->previous(),            'next' => $this->wz->next(),            default => $this->wz->togglePause(),        };        if ($id !== 'play') {            $this->page = 0.0;            $this->handClock = 1000.0;        }    }    // --- end lyrics    /** seconds since the pointer was last on the bar while it is up */    private float $away = 0.0;    public function update(float $dt): void    {        // the lines on the whole screen glide to the line sung (a quarter of a second); far (a seek): at once        $sung = (float)($this->wz->lyrics?->indexAt($this->wz->player->position) ?? -1);        $gap = $sung - $this->scroll;        $this->scroll = abs($gap) > 3 ? $sung : $this->scroll + $gap * min(1.0, $dt * 9);        $this->handClock += $dt;        if ($this->unsynced() && $this->grown > 0 && $this->followsSong() && ($this->wz->options['lyricsAutoScroll'] ?? true)) {            $this->followPage($dt);        }        if ($this->unsynced()) {            $this->page = max(0.0, min($this->pageMax(), $this->page));        }        $target = $this->full() ? 1.0 : 0.0;        if ($this->grown !== $target) {            $this->grown = $target > $this->grown ? min(1.0, $this->grown + $dt * 3) : max(0.0, $this->grown - $dt * 3);        }        // Timer3: once the pointer has left it, the bar goes back down (the desktop version does not know where the        // pointer is outside WinZik's windows: after a moment without it on the bar)        if ($this->dodged) {            $this->away = $this->lastX >= 0 ? 0.0 : $this->away + $dt;            if ($this->away > 2.5) {                $this->dodged = false;                $this->away = 0.0;            }        }        [$mx, $my, , $mh] = $this->desk->playerMonitor();        $this->x = $mx;        $this->y = $this->dodged && $this->grown <= 0 ? $my : max($my, $my + $mh - $this->height() - ($this->grown > 0 ? 0 : $this->desk->bottomMargin()));    }    public function tip(string $control): string    {        if (($this->wz->lyrics === null && in_array($control, ['earlier', 'later'], true)) || ($control === 'screens' && $this->desk->screens() < 2)) {            return '';      // not drawn then        }        return match ($control) {            'quit' => 'Fermer les paroles',            'grow' => $this->full() ? 'Revenir à la barre' : 'Paroles sur tout l\'écran',            'more' => 'Trouver ou écrire les paroles',            'earlier' => 'Paroles plus tôt (molette : 0,1 s)',            'later' => 'Paroles plus tard (molette : 0,1 s)',            'screens' => ($this->wz->options['lyricsAllScreens'] ?? false) ? 'Sur l\'écran du lecteur seulement' : 'Sur tous les écrans',            'kara' => 'Animation sur les syllabes : ' . self::KARAOKE[$this->karaokeMode()],            'trans' => ($this->wz->showsTranslation() ? 'Cacher' : 'Afficher') . ' la traduction (T)',            'prev' => 'Morceau précédent',            'next' => 'Morceau suivant',            'play' => $this->wz->player->state === \App\Audio\Player::PLAYING ? 'Pause' : 'Lecture',            'sync' => 'Paroles non synchronisées (molette : défiler) : les synchroniser dans l\'éditeur',            'scroll' => 'Défilement automatique des paroles',            default => '',        };    }    /**     * The wheel over -0.5s / +0.5s: the lyrics a tenth of a second earlier or later a notch (up: later). Lyrics without     * times: the page (whole screen) or the line of the bar scrolls (up: back).     */    public function wheel(float $notches): bool    {        if ($this->unsynced() && $notches != 0.0) {            $this->byHand();            if ($this->full()) {                $this->page = max(0.0, min($this->pageMax(), $this->page - $notches * 48));            } else {                $this->barLine = $this->barIndex() - (int)($notches > 0 ? max(1, round($notches)) : min(-1, round($notches)));                $this->barIndex();            }            return true;        }        if (!in_array($this->hovered, ['earlier', 'later'], true) || $this->wz->lyrics === null || $notches == 0.0 || $this->full()) {            return false;        }        $this->wz->shiftLyrics(($notches > 0 ? 0.1 : -0.1) * max(1, (int)round(abs($notches))));        return true;    }    /** What a button does, at the left of the buttons (where the time is), on a black ground over the line. */    private function renderHint(Canvas $c, float $right): bool    {        $hint = $this->hint(0.4);        if ($hint === '') {            return false;        }        $tw = $c->textWidth($hint, 9);        $c->fill($right - $tw - 8, 0, $tw + 8, self::HEIGHT, '#000000');        $c->text($hint, $right, 7, 9, '#e0e0e0', 'right');        return true;    }    public function hover(float $x, float $y): void    {        parent::hover($x, $y);        $moved = $x >= 0 && ($x !== $this->lastX || $y !== $this->lastY) && $this->lastX >= 0;        [$this->lastX, $this->lastY] = [$x, $y];        // it runs away from the pointer on its text only: near its buttons (they can be reached from any side) it stays;        // up at the top of the screen, the pointer on its text again: it goes back down        if ($moved && $x < $this->width() - 220 && $this->grown <= 0 && $this->controlAt($x, $y) === '') {            $this->dodged = !$this->dodged;            $this->away = 0.0;            $this->update(0);        }    }    /** Timer3: the pointer is away from the bottom of the screen: the bar goes back down. */    public function pointerAt(float $dy): void    {        [, $my, , $mh] = $this->desk->playerMonitor();        if ($this->dodged && $dy < $my + $mh - self::HEIGHT - $this->desk->bottomMargin() - 4 && $dy > $my + self::HEIGHT + 4) {            $this->dodged = false;            $this->update(0);        }    }    public function restore(): void    {        $this->update(0);    }    public function render(Canvas $c): void    {        $w = $this->width();        $c->fill(0, 0, $w, $this->height(), '#000000');        if ($this->grown > 0) {            $this->renderFull($c, $w);            return;        }        $line = $this->wz->lyricLine();        $index = $this->wz->lyrics?->indexAt($this->wz->player->position) ?? -1;        if ($this->unsynced()) {            // lines without times: the one about sung (or the one the wheel chose)            $index = $this->barIndex();            $line = Karaoke::plain((string)($this->wz->lyrics?->lines[$index][1] ?? ''));        }        $color = $line === Lyrics::NONE ? '#ff0000' : '#ff2020';        $trans = $this->translationOf($index);        if ($line === '' && $this->wz->player->file !== null) {            // before the first line: the song, darker            [$line, $color, $trans] = [$this->wz->plainTitle(), '#802020', ''];        }        if ($this->narrow()) {            if ($trans !== '') {                $c->text($c->fit($line, $w - 12, 11), $w / 2, self::HEIGHT, 11, $color, 'center');     // under the buttons                $c->text($c->fit($trans, $w - 12, 8.5), $w / 2, self::HEIGHT + 12.5, 8.5, self::TRANSLATION, 'center');            } else {                $c->text($c->fit($line, $w - 12, 13), $w / 2, self::HEIGHT + 2, 13, $color, 'center');            }        } elseif ($trans !== '') {            // the line and its translation under it, smaller, darker blue            $c->text($c->fit($line, $w - 270, 11), 6, 0.5, 11, $color);            $c->text($c->fit($trans, $w - 270, 8.5), 6, 13.5, 8.5, self::TRANSLATION);        } else {            $c->text($c->fit($line, $w - 270, 13), 6, 4, 13, $color);        }        if (isset($this->controls()['sync'])) {            // the badge of lyrics without times: a click, the editor to synchronise them            [$bx, $by, $bw, $bh] = $this->controls()['sync'];            $c->fill($bx, $by, $bw, $bh, '#202a48');            $c->frame($bx, $by, $bw, $bh, '#6070a0');            $c->text('≡ non synchro', $bx + $bw / 2, $by + 3, 7, '#c8d0f0', 'center');            $this->pushed($c, 'sync');        }        // the song, small and grey, at the right (the bar of 2001 had the counter there: Label3..5)        $t = (int)$this->wz->player->position;        if (!$this->renderHint($c, $w - 170)) {            $c->text(sprintf('%02d:%02d', intdiv($t, 60), $t % 60), $w - 170, 7, 9, '#00c000', 'right', 'mono-bold.ttf');   // left of the buttons        }        $c->fill($w - 25, 0, 25, 25, '#404040');        $c->fill($w - 25, 0, 25, 1, '#808080');        $c->fill($w - 25, 0, 1, 25, '#808080');        $c->text('Q', $w - 12.5, 5, 12, '#ffffff', 'center');        $this->pushed($c, 'quit');        $this->growButton($c, $w, false);        $this->moreButton($c, $w);        if ($this->desk->screens() > 1) {            // on every screen, or only the player's            $on = (bool)($this->wz->options['lyricsAllScreens'] ?? false);            $bx = $w - 165;            $c->fill($bx, 0, 25, 25, $on ? '#305030' : '#303030');            $c->fill($bx, 0, 25, 1, '#707070');            $c->fill($bx, 0, 1, 25, '#707070');            foreach ([$bx + 4, $bx + 13] as $sx) {                $c->frame($sx, 9, 8, 6, $on ? '#80ff80' : '#b0b0b0');            }            $this->pushed($c, 'screens');        }        if ($this->wz->lyrics !== null && !$this->unsynced()) {            foreach ([['earlier', $w - 139, '-0.5s'], ['later', $w - 107, '+0.5s']] as [$id, $bx, $label]) {                $c->fill($bx, 0, 32, 25, '#303030');                $c->fill($bx, 0, 32, 1, '#707070');                $c->fill($bx, 0, 1, 25, '#707070');                $c->text($label, $bx + 16, 7, 8, '#e0e0e0', 'center');                $this->pushed($c, $id);            }        }    }    /** "+": make the lyrics (Whisper, Claude on the web, LRCLIB) or write them (the editor). */    private function moreButton(Canvas $c, float $w): void    {        $c->fill($w - 75, 0, 25, 25, '#404040');        $c->fill($w - 75, 0, 25, 1, '#808080');        $c->fill($w - 75, 0, 1, 25, '#808080');        $c->text('+', $w - 62.5, 3, 14, '#ffffff', 'center');        $this->pushed($c, 'more');    }    private function growButton(Canvas $c, float $w, bool $full): void    {        $c->fill($w - 50, 0, 25, 25, '#404040');        $c->fill($w - 50, 0, 25, 1, '#808080');        $c->fill($w - 50, 0, 1, 25, '#808080');        // an arrow up (unroll to the whole screen) or down (roll back)        $x = $w - 37.5;        for ($i = 0; $i < 6; $i++) {            $y = $full ? 15 - $i : 9 + $i;            $c->fill($x - $i, $y, $i * 2 + 1, 1, '#ffffff');        }        $this->pushed($c, 'grow');    }    /**     * The whole screen: the line sung big in the middle, the two before and the three after, dimmer the further they are,     * each with its translation under it; lyrics without times: a page to scroll. The big buttons on the left.     */    private function renderFull(Canvas $c, float $w): void    {        $h = $this->height();        $lyrics = $this->wz->lyrics;        $mid = $h / 2;        [$sx, $ss] = $this->sideButtons();        $room = $w - 2 * ($sx + $ss + 16);         // the lines stay clear of the buttons on the left (and as far on the right)        if ($lyrics === null) {            $c->text(Lyrics::NONE, $w / 2, $mid - 14, 26, '#ff2020', 'center');        } elseif (!$lyrics->synced) {            $this->renderPage($c, $w, $h, $room);        } else {            $i = $lyrics->indexAt($this->wz->player->position);            // with translations, the lines further apart (each has its translation under it)            $translated = $this->wz->showsTranslation() && $lyrics->translations !== [];            $step = $translated ? 66 : 46;            // the lines glide up (no jump from one to the next): $this->scroll follows the line sung, its whole part the            // line nearest the middle, the fraction how far it has gone up            $base = (int)floor($this->scroll);            for ($j = $base - 3; $j <= $base + 4; $j++) {                $text = $lyrics->lines[$j][1] ?? null;                if ($text === null || $j < 0) {                    continue;                }                $o = $j - $this->scroll;                  // 0: in the middle; -1: the line above; 1: the one below                $a = min(2.0, abs($o));                if ($o < -2.6 || $o > 3.6) {                    continue;                }                $size = 30 - 13 * min(1.0, $a);                $y = $mid - $size / 2 + $o * $step + ($o > 0 ? 8 * min(1.0, $o) : ($o < 0 ? -8 * min(1.0, -$o) : 0)) - ($translated ? 10 : 0);                if ($y < 26 || $y > $h - 20) {                    continue;                }                $trans = $this->translationOf($j);                if ($trans !== '') {                    $ts = max(11.0, $size * 0.62);                    $c->text($c->fit($trans, $room, $ts), $w / 2, $y + $size + 5, $ts, self::blue($a), 'center');                }                if ($j === $i && $a < 0.5 && $this->karaoke($c, $w, $y, $size, $i)) {                    continue;       // the line sung, with its ball over its syllables                }                $c->text($c->fit(Karaoke::plain($text), $room, $size), $w / 2, $y, $size, self::shade($a), 'center');            }            if ($i < 0) {                $c->text($c->fit($this->wz->plainTitle(), $room, 30), $w / 2, $mid - 15, 30, '#802020', 'center');            }        }        $this->renderSideButtons($c);        $t = (int)$this->wz->player->position;        if (!$this->renderHint($c, $w - 107)) {            $c->text(sprintf('%02d:%02d', intdiv($t, 60), $t % 60), $w - 107, 7, 9, '#00c000', 'right', 'mono-bold.ttf');   // left of T, K, its size, Q        }        $state = $this->wz->translationState();        if ($state['state'] === 'translating') {            $c->text($c->fit($state['text'] ?: 'Traduction des paroles…', 300, 9), $w - 160, 7, 9, '#8090d0', 'right');        }        $c->fill($w - 25, 0, 25, 25, '#404040');        $c->text('Q', $w - 12.5, 5, 12, '#ffffff', 'center');        $this->pushed($c, 'quit');        $this->growButton($c, $w, true);        $c->fill($w - 75, 0, 25, 25, '#404040');        $c->fill($w - 75, 0, 1, 25, '#707070');        $c->text('K', $w - 62.5, 5, 12, $this->karaokeMode() === 'off' ? '#808080' : '#ffe030', 'center');        $this->pushed($c, 'kara');        // T: the translation shown or not (dim when the lyrics have none)        $has = $lyrics !== null && $lyrics->translations !== [];        $c->fill($w - 100, 0, 25, 25, $has && $this->wz->showsTranslation() ? '#203070' : '#303030');        $c->fill($w - 100, 0, 1, 25, '#707070');        $c->text('T', $w - 87.5, 5, 12, $has ? ($this->wz->showsTranslation() ? '#a0b8ff' : '#c0c0c0') : '#606060', 'center');        $this->pushed($c, 'trans');    }    /** Lyrics without times on the whole screen: every line, the one about sung brighter, scrolled by hand or slowly by itself. */    private function renderPage(Canvas $c, float $w, float $h, float $room): void    {        $lyrics = $this->wz->lyrics;        if ($lyrics === null) {            return;        }        [$tops, $heights, $total] = $this->pageLayout();        $sung = $this->wz->estimatedLine();        foreach ($lyrics->lines as $i => $line) {            $y = $tops[$i] - $this->page;            if ($y < 28 || $y > $h - 50) {      // under the top bar or over the hint: not drawn                continue;            }            $text = Karaoke::plain((string)$line[1]);            if ($text === '') {                continue;            }            $near = abs($i - $sung);            $color = $i === $sung ? '#ff3030' : ($near <= 2 ? '#c02020' : '#901818');            $c->text($c->fit($text, $room, 22), $w / 2, $y, 22, $color, 'center');            $trans = $this->translationOf($i);            if ($trans !== '') {                $c->text($c->fit($trans, $room, 14), $w / 2, $y + 29, 14, $i === $sung ? self::TRANSLATION : '#2a4296', 'center');            }        }        // where the page is: a thin bar on the right        if ($total > $h) {            $track = $h - 60;            $c->fill($w - 6, 30, 3, $track, '#202020');            $c->fill($w - 6, 30 + $track * $this->page / $total, 3, max(12, $track * $h / $total), '#804040');        }        // the top: the badge, the button to synchronise them, the automatic scroll        $c->fill(0, 0, $w - 100, 25, '#000000');        $c->fill(8, 4, 124, 17, '#202a48');        $c->frame(8, 4, 124, 17, '#6070a0');        $c->text('≡ NON SYNCHRONISÉES', 70, 7.5, 8, '#c8d0f0', 'center');        foreach (['sync' => 'Synchroniser dans l\'éditeur', 'scroll' => 'Défilement auto'] as $id => $label) {            [$bx, $by, $bw, $bh] = $this->controls()[$id];            $on = $id === 'scroll' && ($this->wz->options['lyricsAutoScroll'] ?? true);            $c->fill($bx, $by, $bw, $bh, $on ? '#305030' : '#303030');            $c->frame($bx, $by, $bw, $bh, '#707070');            $c->text($label, $bx + $bw / 2, $by + 4, 8.5, $on ? '#a0ffa0' : '#e0e0e0', 'center');            $this->pushed($c, $id);        }        $c->text('Molette ou glisser : défiler les paroles', $w / 2, $h - 18, 9, '#707070', 'center');    }    /** Précédent, Lecture / Pause, Suivant: big, on the left of the whole screen. */    private function renderSideButtons(Canvas $c): void    {        $controls = $this->controls();        $playing = $this->wz->player->state === \App\Audio\Player::PLAYING;        foreach (['prev' => 'Précédent', 'play' => $playing ? 'Pause' : 'Lecture', 'next' => 'Suivant'] as $id => $label) {            [$x, $y, $s] = $controls[$id];            $c->fill($x, $y, $s, $s, '#181818');            $c->frame($x, $y, $s, $s, '#606060');            $c->fill($x + 1, $y + 1, $s - 2, 1, '#909090');            $cx = $x + $s / 2;            $cy = $y + $s * 0.42;            $u = $s * 0.16;            // the size of the drawing            if ($id === 'play' && $playing) {                $c->fill($cx - $u * 0.9, $cy - $u, $u * 0.6, $u * 2, '#ffffff');                $c->fill($cx + $u * 0.3, $cy - $u, $u * 0.6, $u * 2, '#ffffff');            } elseif ($id === 'play') {                $this->triangle($c, $cx - $u * 0.6, $cy, $u, 1);            } else {                $dir = $id === 'next' ? 1 : -1;                // two arrows and the bar at their tip                $this->triangle($c, $cx - $dir * $u * 1.2, $cy, $u * 0.85, $dir);                $this->triangle($c, $cx - $dir * $u * 0.25, $cy, $u * 0.85, $dir);                $bw = max(2, $u * 0.25);                $c->fill($dir > 0 ? $cx + $u * 1.13 : $cx - $u * 1.13 - $bw, $cy - $u * 0.85, $bw, $u * 1.7, '#ffffff');            }            $ls = max(7.0, min(10.0, $s * 0.15));            $c->text($c->fit($label, $s - 4, $ls), $cx, $y + $s - $ls - 5, $ls, '#c0c0c0', 'center');            $this->pushed($c, $id);        }    }    /** A filled triangle pointing right ($dir 1) or left (-1), its back at $x, $half high on each side of $y. */    private function triangle(Canvas $c, float $x, float $y, float $half, int $dir): void    {        $len = $half * 1.6;        for ($k = 0; $k < $len; $k++) {            $hh = $half * (1 - $k / $len);            $c->fill($dir > 0 ? $x + $k : $x - $k - 1, $y - $hh, 1, 2 * $hh, '#ffffff');        }    }    /** the translations: darker blue, darker still away from the line sung */    private static function blue(float $a): string    {        $mix = fn (array $p, array $q, float $t) => sprintf('#%02x%02x%02x', ...array_map(fn ($u, $v) => (int)round($u + ($v - $u) * $t), $p, $q));        return $a <= 1 ? $mix([0x3a, 0x5c, 0xd6], [0x28, 0x3e, 0x96], $a) : $mix([0x28, 0x3e, 0x96], [0x1a, 0x28, 0x60], $a - 1);    }    /** the lyrics on the whole screen: which line is in the middle, gliding to the line sung (a fraction while it moves) */    private float $scroll = -1.0;    /** red for the line sung (0), darker away from it (1, 2) */    private static function shade(float $a): string    {        $mix = fn (array $p, array $q, float $t) => sprintf('#%02x%02x%02x', ...array_map(fn ($u, $v) => (int)round($u + ($v - $u) * $t), $p, $q));        return $a <= 1 ? $mix([0xff, 0x20, 0x20], [0xa0, 0x20, 0x20], $a) : $mix([0xa0, 0x20, 0x20], [0x60, 0x18, 0x18], $a - 1);    }    /** the animations over the syllables, in the order of K */    public const KARAOKE = ['ball' => 'Balle jaune', 'star' => 'Étoile', 'note' => 'Note', 'bar' => 'Soulignement', 'off' => 'Aucune'];    public function karaokeMode(): string    {        $mode = (string)($this->wz->options['karaoke'] ?? 'ball');        return isset(self::KARAOKE[$mode]) ? $mode : 'ball';    }    /**     * The line sung now, its syllables lit as they are sung, the ball (or star, note, bar) going from one to the next and     * landing on each when it is sung. @return bool false: drawn the plain way (too long, no animation)     */    private function karaoke(Canvas $c, float $w, float $y, float $size, int $i): bool    {        $mode = $this->karaokeMode();        $lyrics = $this->wz->lyrics;        $line = $lyrics?->lines[$i] ?? null;        if ($mode === 'off' || $lyrics === null || $line === null) {            return false;        }        $start = (float)$line[0];        $next = (float)($lyrics->lines[$i + 1][0] ?? $start + 6);        [$syllables, $times, $end] = Karaoke::timing((string)$line[1], $start, $next);        $text = implode('', $syllables);        $tw = $c->textWidth($text, $size);        if ($syllables === [] || $tw > $w - 2 * ($this->sideButtons()[0] + $this->sideButtons()[1] + 16)) {            return false;        }        $x0 = $w / 2 - $tw / 2;        // where each syllable is (its letters, without the space after it)        $centres = [];        $before = '';        foreach ($syllables as $s) {            $left = $x0 + $c->textWidth($before, $size);            $centres[] = $left + $c->textWidth(rtrim($s), $size) / 2;            $before .= $s;        }        $now = $this->wz->player->position;        $cur = -1;        foreach ($times as $j => $t) {            if ($t <= $now) {                $cur = $j;            }        }        $c->text($text, $x0, $y, $size, '#ff2020');        if ($cur >= 0) {            $c->text(implode('', array_slice($syllables, 0, $cur + 1)), $x0, $y, $size, '#ffe030');      // sung        }        // the ball: from the syllable sung to the next, landing on each at its moment (before the first: waiting on it)        $last = count($syllables) - 1;        if ($cur < 0) {            [$from, $to, $frac] = [$centres[0], $centres[0], fmod(max(0.0, $now - $start + 10), 0.5) / 0.5];        } elseif ($now > $end) {            return true;    // the line is sung: the ball waits for the next one        } else {            $until = $times[$cur + 1] ?? $end;            $frac = max(0.0, min(1.0, ($now - $times[$cur]) / max(0.05, $until - $times[$cur])));            [$from, $to] = [$centres[$cur], $centres[min($last, $cur + 1)]];            if ($cur === $last) {                $to = $from;            }        }        $bx = $from + ($to - $from) * $frac;        $by = $y - 8 - 26 * sin(M_PI * $frac);        match ($mode) {            'star' => $this->star($c, $bx, $by),            'note' => $this->note($c, $bx, $by),            'bar' => $c->fill($x0, $y + $size + 4, max(0.0, $bx - $x0), 3, '#ffe030'),            default => $this->ball($c, $bx, $by),        };        return true;    }    private function ball(Canvas $c, float $x, float $y): void    {        $c->disc($x + 1.5, $y + 2, 7, [0, 0, 0, 90]);          // its shadow        $c->disc($x, $y, 7, '#ffd000');        $c->disc($x - 2, $y - 2.5, 2.4, [255, 255, 255, 170]);  // its shine    }    private function star(Canvas $c, float $x, float $y): void    {        $c->fill($x - 9, $y - 1.5, 18, 3, '#ffe030');        $c->fill($x - 1.5, $y - 9, 3, 18, '#ffe030');        $c->disc($x, $y, 4.5, '#ffe030');        $c->disc($x, $y, 2, '#ffffff');    }    private function note(Canvas $c, float $x, float $y): void    {        $c->disc($x - 3, $y + 6, 5, '#ffe030');                  // its head        $c->fill($x + 1, $y - 12, 2.5, 18, '#ffe030');          // its stem        $c->fill($x + 1, $y - 12, 7, 2.5, '#ffe030');           // its flag        $c->fill($x + 6, $y - 10, 2.5, 6, '#ffe030');    }    public function press(float $x, float $y, \JumPHP\Gui\Pointer $p): bool    {        parent::press($x, $y, $p);        [$this->pressY, $this->pageFrom] = [$y, $this->page];        return true;    // the bar does not move: it is always across the screen    }    public function dragsItself(string $control): bool    {        return $control === 'page';    }    /** Lyrics without times on the whole screen: a finger (or the mouse held) drags the page. */    public function drag(float $x, float $y): void    {        if ($this->pressed === 'page' && abs($y - $this->pressY) > 3) {            $this->byHand();            $this->page = max(0.0, min($this->pageMax(), $this->pageFrom - ($y - $this->pressY)));        }    }    protected function clicked(string $id, bool $double): void    {        if ($id === 'grow') {            $this->toggleFull();            return;        }        if (in_array($id, ['prev', 'play', 'next'], true)) {            $this->transport($id);            return;        }        if ($id === 'page') {            return;     // a drag of the page (or a click on it): nothing more        }        if ($id === 'trans') {            $this->wz->toggleTranslation();            return;        }        if ($id === 'sync') {            $this->wz->ui['lyrics']['full'] = false;            $this->desk->host()->openLyricsEditor();            return;        }        if ($id === 'scroll') {            $this->wz->options['lyricsAutoScroll'] = !($this->wz->options['lyricsAutoScroll'] ?? true);            $this->handClock = 1000.0;            $this->wz->touch();            return;        }        if ($id === 'kara') {            // K: the next animation over the syllables            $modes = array_keys(self::KARAOKE);            $next = $modes[((int)array_search($this->karaokeMode(), $modes, true) + 1) % count($modes)];            $this->wz->options['karaoke'] = $next;            $this->wz->notice('Animation des paroles : ' . self::KARAOKE[$next], 2);            $this->wz->touch();            return;        }        if ($id === 'screens') {            $this->wz->options['lyricsAllScreens'] = !($this->wz->options['lyricsAllScreens'] ?? false);            $this->wz->touch();            return;        }        if ($id === 'earlier' || $id === 'later') {            $this->wz->shiftLyrics($id === 'earlier' ? -0.5 : 0.5);            return;        }        if ($id === 'more') {            $this->desk->openLyricsMenuAt($this->x + $this->width() - 210, $this->y, $this->y + $this->height());            return;        }        $this->wz->ui['lyrics']['full'] = false;        // Command1_Click: back to the player when it was hidden by the lyrics button, else the bar hides        if ($this->wz->ui['lyricsOnly']) {            $this->wz->ui['lyricsOnly'] = false;            $this->wz->ui['lyrics']['visible'] = false;        } else {            $this->wz->ui['lyrics']['visible'] = false;        }        $this->wz->touch();    }}