FR
apps/winzik / src / Gui / VideoWindow.php · commit 6576d6f
<?phpnamespace App\Gui;use App\Model\Text;/** * The video player: shown while a film of the play list plays (MP4 from YouTube, or any MP4 / WebM / MKV), its * pictures following the player's clock (the sound is the film's: play, pause, seek, volume are the player's). * Dressed by the skin: its title stripe is the top of the skin's main picture, its x the skin's close button, its * border and texts the skin's colours. x hides it (the sound goes on); a double click on the picture puts it on the * whole screen and back (Escape too); there a bar of the time shows when the pointer moves (click it to go there). */final class VideoWindow extends SkinWindow{    public const TITLE = 14;    private const BAR = 22;    /** seconds since the pointer last moved over the film (the bar of the time shows for 2.5 s) */    private float $still = 99.0;    private float $lastX = -1;    private float $lastY = -1;    public function id(): string    {        return 'video';    }    /** a film: its pictures (25 a second) */    public function redrawEvery(): float    {        return 1 / 25;    }    public function full(): bool    {        return (bool)($this->wz->ui['video']['full'] ?? false);    }    private function big(): bool    {        return (bool)($this->wz->ui['video']['big'] ?? false);    }    public function width(): float    {        if ($this->full()) {            return $this->desk->playerMonitor()[2];        }        $w = $this->big() ? 516 : 258;        // the phone: as wide as its screen at most (the big one too)        return $this->desk->phone ? min((float)$w, max(258.0, floor($this->desk->deskWidth()))) : $w;    }    public function height(): float    {        return $this->full() ? $this->desk->playerMonitor()[3] : self::TITLE + round($this->width() * 9 / 16);    }    private function top(): float    {        return $this->full() ? 0.0 : (float)self::TITLE;    }    public function visible(): bool    {        $ui = $this->wz->ui;        return ($ui['video']['visible'] ?? true) && $this->wz->playingVideo() && !$ui['compact']['active'] && !$ui['lyricsOnly'] && !$ui['tray'];    }    public function controls(): array    {        $w = $this->width();        $h = $this->height();        $c = ['picture' => [0.0, $this->top(), $w, $h - $this->top()]];        if ($this->full()) {            return ['seek' => [20.0, $h - self::BAR, $w - 40, (float)self::BAR]] + $c;        }        return ['close' => [$w - 16, 1.0, 15.0, 12.0]] + $c;    }    /** the film last seen playing ('' none): a new one opens in its window, shown */    private string $film = '';    public function update(float $dt): void    {        $this->still += $dt;        $film = $this->wz->playingVideo() ? (string)$this->wz->player->file : '';        if ($film !== $this->film) {            $this->film = $film;            if ($film !== '') {                $this->newFilm();            }        }        if ($this->full()) {            [$this->x, $this->y] = array_slice($this->desk->playerMonitor(), 0, 2);    // the whole screen of the player        }    }    public function hover(float $x, float $y): void    {        if ($x >= 0 && ($x !== $this->lastX || $y !== $this->lastY) && $this->lastX >= 0) {            $this->still = 0.0;        }        [$this->lastX, $this->lastY] = [$x, $y];    }    public function dragsItself(string $control): bool    {        return $control === 'seek' || $this->full();    }    public function render(Canvas $c): void    {        $w = $this->width();        $h = $this->height();        $colors = $this->wz->skin->colors;        $c->fill(0, 0, $w, $h, '#000000');        $top = $this->top();        $frame = $this->wz->videoFrame();        if ($frame !== null) {            $c->videoFrame($frame, 0, $top, $w, $h - $top);        } else {            $mid = $top + ($h - $top) / 2;            if ($this->wz->waitVideo !== '') {                $c->spinner($w / 2, $mid - 10, 7, $colors['barGreenLeft'], $this->desk->clock);                $c->text('Préparation de la vidéo...', $w / 2, $mid + 4, 8, $colors['barGreenLeft'], 'center');            } else {                $c->text('Vidéo', $w / 2, $mid - 6, 9, $colors['barGreenLeft'], 'center');            }        }        if ($this->full()) {            if ($this->still < 2.5 || $this->pressed === 'seek') {                $this->timeBar($c, $w, $h);            }            return;        }        // the title stripe: the top of the skin's player (its grooves, its colours), as wide as needed        $size = $c->imageSize('main');        if ($size !== null) {            $mid = 60.0;            $c->image('main', 0, 0, $mid, self::TITLE, [0, 0, $mid, self::TITLE]);            $c->image('main', $mid, 0, $w - 2 * $mid, self::TITLE, [$mid, 0, 20, self::TITLE]);            $c->image('main', $w - $mid, 0, $mid, self::TITLE, [$size[0] - $mid, 0, $mid, self::TITLE]);        } else {            $c->fill(0, 0, $w, self::TITLE, '#2a2a40');        }        $title = Text::baseName(Text::withoutExtension((string)$this->wz->player->file));        $label = $c->fit('VIDEO  -  ' . $title, $w - 60, 7.5);        $tw = $c->textWidth($label, 7.5);        $c->fill(($w - 20 - $tw) / 2 - 4, 2, $tw + 8, 10, [0, 0, 0, 170]);        $c->text($label, ($w - 20) / 2, 2, 7.5, $colors['title'], 'center');        if (!$c->image('close', $w - 16, 1, 15, 12)) {            $c->bevel($w - 16, 1, 15, 12);            $c->text('x', $w - 8.5, 1.5, 8, '#000000', 'center');        }        $this->pushed($c, 'close');        $c->frame(0, 0, $w, $h, $colors['barGreenLeft']);    }    /** The bar of the time on the whole screen: where the film is, how long it is. */    private function timeBar(Canvas $c, float $w, float $h): void    {        $player = $this->wz->player;        $len = max(1.0, $player->duration());        $c->fill(0, $h - self::BAR - 10, $w, self::BAR + 10, [0, 0, 0, 160]);        $x0 = 20;        $bw = $w - 40;        $y = $h - self::BAR / 2 - 2;        $c->fill($x0, $y, $bw, 3, '#404040');        $c->fill($x0, $y, $bw * min(1, $player->position / $len), 3, $this->wz->skin->colors['barGreenLeft']);        $c->fill($x0 + $bw * min(1, $player->position / $len) - 3, $y - 3, 6, 9, '#ffffff');        $clock = fn (float $s) => sprintf('%d:%02d', intdiv((int)$s, 60), (int)$s % 60);        $c->text($clock($player->position) . ' / ' . $clock($len), $x0, $y - 15, 9, '#ffffff');        $c->text(Text::baseName(Text::withoutExtension((string)$player->file)), $w - 20, $y - 15, 9, '#c0c0c0', 'right');    }    public function press(float $x, float $y, \JumPHP\Gui\Pointer $p): bool    {        $taken = parent::press($x, $y, $p);        if ($this->pressed === 'seek') {            $this->drag($x, $y);        }        return $taken || $this->full();     // on the whole screen it does not move    }    public function drag(float $x, float $y): void    {        if ($this->pressed === 'seek') {            $this->still = 0.0;            $this->wz->seek(max(0, min(1, ($x - 20) / max(1, $this->width() - 40))) * $this->wz->player->duration());        }    }    public function release(float $x, float $y, bool $double): void    {        if ($this->pressed === 'seek') {            $this->pressed = '';            return;        }        parent::release($x, $y, $double);    }    /**     * A film opens (another one, or the first after a start): in its window and shown, whatever the one before did (its     * whole screen, or its x, were kept for every film after it, even after a restart).     */    public function newFilm(): void    {        $ui = &$this->wz->ui['video'];        if ($this->full()) {            $ui['full'] = false;            [$this->x, $this->y] = [(float)($ui['wx'] ?? $this->x), (float)($ui['wy'] ?? $this->y)];        }        $ui['visible'] = true;    }    public function toggleFull(): void    {        $ui = &$this->wz->ui['video'];        if (!$this->full()) {            [$ui['wx'], $ui['wy']] = [$this->x, $this->y];            $ui['full'] = true;        } else {            $ui['full'] = false;            [$this->x, $this->y] = [(float)($ui['wx'] ?? $this->x), (float)($ui['wy'] ?? $this->y)];        }        $this->still = 0.0;        $this->wz->touch();    }    protected function clicked(string $id, bool $double): void    {        if ($id === 'close') {            $this->wz->ui['video']['visible'] = false;        } elseif ($id === 'picture') {            if ($double) {                $this->toggleFull();            } else {                $this->still = 0.0;            }        }        $this->wz->touch();    }}