PT
apps/winzik / src / Form / MobileForm.php · commit 6576d6f
<?phpnamespace App\Form;use App\Audio\Player;use App\Core\WinZik;use App\Mobile\MobileView;use App\Scene\AboutScene;use JumPHP\Gui\Form;/** * WinZik on a phone (Platform::isPhoneApp(): the Android / iOS app), held upright: App\Mobile\MobileView draws a player * of today, used with a finger, on WinZik's own state (the same player, play list, stars and lyrics as the desktop). * Params: fresh (start from the defaults, tests), phone (tests: the phone's screen in the middle of the view, true = * 390 x 844 dp, or [width, height]), play (a line played), screen (player, playlist, add), demo (the smoke test). * * @property MobileView $view */final class MobileForm extends Form{    public function __construct(private WinZik $wz)    {        parent::__construct();    }    public function winzik(): WinZik    {        return $this->wz;    }    public function enter(array $params = []): void    {        $this->wz->attach($this->engine);        if (!empty($params['fresh'])) {            $this->engine->storage->delete(WinZik::SETTINGS);            $this->wz->reset();        }        parent::enter($params);    }    protected function loaded(array $params = []): void    {        $view = $this->view;        $view->wz = $this->wz;        $view->open = function (string $what): void {            match ($what) {                'about' => $this->scenes()->push(AboutScene::class),                // the interface of 2001 (its skinned windows): MainForm stays in it while phoneUi is 'original'                'original' => $this->scenes()->switchTo(MainForm::class, [], false),                default => null,            };        };        if (!empty($params['phone'])) {            $view->phone = is_array($params['phone']) ? array_map('floatval', array_values($params['phone'])) : [390.0, 844.0];        }        $this->wz->ui['lyrics']['visible'] = false;        $this->wz->greet();        $this->setFocus($view);        if (isset($params['screen'])) {            $view->go((string)$params['screen']);        }        if (isset($params['sheet'])) {            $view->openSheet((string)$params['sheet']);        }        if (!empty($params['demo'])) {            $this->wz->playIndex(0);        }        if (isset($params['play'])) {            $this->wz->playIndex((int)$params['play']);        }        if (isset($params['seek'])) {            $this->wz->seek((float)$params['seek']);        }    }    public function resume(mixed $result = null): void    {        $this->setFocus($this->view);    }    protected function tick(float $dt): void    {        $player = $this->wz->player;        $before = $player->state === Player::PLAYING && $player->file !== null            ? ['file' => $player->file, 'line' => $this->wz->playlist->active, 'at' => $player->position, 'length' => $player->duration()] : null;        $acted = $this->view->acted;        $this->view->acted = false;        $this->wz->tick($dt);        // Répéter: the song that just ended by itself plays again (a song changed by the user is not)        if ($before !== null && !$acted && $this->view->repeat() && $before['length'] > 0 && $before['at'] >= $before['length'] - 1.5            && ($player->file !== $before['file'] || $player->position < $before['at'] - 1)) {            $line = $this->wz->playlist->items[$before['line']] ?? null;            if ($line !== null && $this->wz->path($line) === $before['file']) {                $this->wz->playIndex($before['line']);            }        }    }    public function exit(): void    {        $this->wz->save();    }}