PT
apps/winzik / src / Scene / SplashScene.php · commit 6576d6f
<?phpnamespace App\Scene;use App\Form\MainForm;use App\Form\MobileForm;use App\Gui\Canvas;use JumPHP\Kernel\Platform;use JumPHP\Renderer\Renderer;/** * Form15 / Form16 of 2001, "Chargement": the WinZik picture over the desktop and what is being loaded * ("Chargement de la configuration", "Chargement du fichier : main"...), then the player. Option of Form17. */final class SplashScene extends FullScreen{    /** the steps of Form1.Form_Load, shown one after the other */    public const STEPS = [        'Initialisation...', "Recherche de l'utilisateur", 'Chargement de la configuration', 'Création des différentes options',        'Réglage des couleurs', 'Chargement du fichier : main', 'Chargement du fichier : mainpl', 'Chargement du fichier : bplay',        'Chargement du fichier : lyr', 'Chargement des images principales', 'Chargement des options du dernier démarrage', 'Init OK',    ];    private const SECONDS = 2.2;    /**     * on the phone: as long as the jingle WinZik::greet() plays at each start (assets/sfx/winzik.ogg, 4.9 s: "WinZik! It     * really kicks the Winamp ass!"), heard with the picture; a tap skips it only after a second (the picture is seen)     */    public const PHONE_SECONDS = 5.0;    private const PHONE_SKIP = 1.0;    /** the picture of Form15 (assets/images/winzik.png) */    public const PICTURE_W = 489;    public const PICTURE_H = 153;    /** the phone app held upright (or the test param `phone`): the splash on the whole screen, turned a quarter */    private bool $phone = false;    /** @var array{0: int, 1: int} the size of the texture the turned splash is drawn in */    private array $turnedSize = [0, 0];    /** the update looked for during the splash (the installed WinZik): it waits for it, then starts again on it */    private bool $updating = false;    /**     * What comes after the splash: the skinned windows of 2001, or on a phone (the Android / iOS app) its own screens,     * unless the option phoneUi says 'original' (WinZik original: the windows of 2001 on the phone too).     */    public static function main(?\App\Core\WinZik $wz = null): string    {        return Platform::isPhoneApp() && ($wz === null || !MainForm::originalOnPhone($wz)) ? MobileForm::class : MainForm::class;    }    public function enter(array $params = []): void    {        parent::enter($params);        $this->phone = !empty($params['phone']) || Platform::isPhoneApp();        $updater = $this->wz->updater;        $this->updating = $updater !== null && $updater->enabled() && (bool)($this->wz->options['autoUpdate'] ?? true);        if ($this->updating) {            $updater->check();        }        $this->wz->greet();        if (!($this->wz->options['splash'] ?? true)) {            $this->scenes()->switchTo(self::main($this->wz), [], false);        }    }    public function update(float $dt): void    {        parent::update($dt);        $updater = $this->wz->updater;        if ($this->updating && $updater !== null) {            if ($updater->state === 'installed' && $updater->relaunch()) {                // the new version is in place: it starts at once, this one ends                $this->wz->save();                $this->engine->quit();                return;            }            // the check waits 5 seconds at most (no network: the player opens), a download as long as it takes (3 minutes at most)            $waiting = ($updater->state === 'checking' && $this->time < 5) || ($updater->state === 'installing' && $this->time < 180);            if ($waiting) {                return;            }        }        if ($this->time >= $this->seconds() || ($this->time > ($this->phone ? self::PHONE_SKIP : 0.3) && $this->anyInput())) {            $this->scenes()->switchTo(self::main($this->wz));        }    }    /** How long the splash stays (seconds). */    public function seconds(): float    {        return $this->phone ? self::PHONE_SECONDS : self::SECONDS;    }    /** The splash is drawn on the whole screen of a phone, turned a quarter. */    public function phone(): bool    {        return $this->phone;    }    /** The line of the splash while the update is looked for / downloaded, '' otherwise. */    public function updateLine(): string    {        $u = $this->wz->updater;        if (!$this->updating || $u === null) {            return '';        }        return match ($u->state) {            'checking' => "Recherche d'une mise à jour...",            'installing' => sprintf('Mise à jour vers WinZik %s : %d %%', $u->version, (int)round($u->progress * 100)),            'installed' => 'Redémarrage sur WinZik ' . $u->version . '...',            default => '',        };    }    public function render(Renderer $r): void    {        if ($this->phone) {            $this->renderPhone($r);            return;        }        // Form15 (507 x 170 pixels): the picture alone; its light blue border was ANDed with the screen behind it        // (BitBlt of the desktop, SRCAND): see-through, the wallpaper slightly tinted (on the teal desktop of Windows: the        // desktop itself). So nothing is drawn around the picture, and the yellow line is written on it (Label1).        $x = (FullScreen::W - self::PICTURE_W) / 2;        $y = 160;        $c = $this->canvas($r, '#008080', $this->wz->native !== null ? [$x, $y, (float)self::PICTURE_W, (float)self::PICTURE_H] : null);        $this->drawPicture($c, $x, $y);    }    /**     * The phone held upright: the picture is much wider than tall, so it is drawn turned a quarter (like the karaoke of     * the original interface, Desktop::renderTurned()), as big as the screen's height allows: drawn in a texture as wide     * as the screen is tall, then copied at 90° (its top on the right edge). The teal of the desktop of 2001 around it.     */    private function renderPhone(Renderer $r): void    {        $r->clear('#008080');        $sdl = $this->sdl();        [$w, $h] = [$r->getWidth(), $r->getHeight()];        [$tw, $th] = $w <= $h ? [$h, $w] : [$w, $h];          // a screen already on its side: drawn as it is        if ($this->turnedSize !== [$tw, $th] || !$sdl->hasTexture('winzik-splash-turned')) {            if ($sdl->hasTexture('winzik-splash-turned')) {                $sdl->releaseTexture('winzik-splash-turned');            }            $sdl->createTexture('winzik-splash-turned', $tw, $th);            $this->turnedSize = [$tw, $th];        }        $texture = $sdl->getTexture('winzik-splash-turned')['texture'];        [$ox, $oy] = $r->translation();        $before = $r->setTarget($texture);        $r->translate(-$ox, -$oy);        $r->clear('#008080');        $c = new Canvas($r, $sdl);        $c->skin = $this->wz->skin;        $c->scale = min($tw / self::PICTURE_W, $th / self::PICTURE_H);        $c->at(($tw / $c->scale - self::PICTURE_W) / 2, ($th / $c->scale - self::PICTURE_H) / 2);        $this->drawPicture($c, 0, 0);        $r->translate($ox, $oy);        $r->setTarget($before);        $r->copy($texture, null, ($w - $tw) / 2, ($h - $th) / 2, $tw, $th, false, false, $w <= $h ? 90.0 : 0.0);    }    /** Form15's picture at (x, y) with its yellow lines: what is loaded (or the update), the version. */    private function drawPicture(Canvas $c, float $x, float $y): void    {        $c->file($this->engine->config->path('assets/images/winzik.png'), $x, $y, self::PICTURE_W, self::PICTURE_H);        $seconds = $this->seconds();        $step = self::STEPS[min(count(self::STEPS) - 1, (int)($this->time / $seconds * count(self::STEPS)))];        if ($this->wz->firstStart && $step === 'Chargement de la configuration') {            $step = "Premier démarage ; création d'un fichier CFG";        }        $update = $this->updateLine();        if ($update !== '') {            $step = $update;        }        if (($this->wz->options['splashSteps'] ?? true) || $update !== '') {            $c->text($step, $x + 7, $y + 135, 10, '#ffff00');     // afc = 1: Form15.Label1 (Left 240, Top 2160 twips)        }        $c->text('v' . $this->wz->version(), $x + 482, $y + 135, 10, '#ffff00', 'right');    }}