ES
games/runtime-demo / src / Scene / DemoScene.php · commit 459a354
<?phpnamespace App\Scene;use JumPHP\Gui\Form;use JumPHP\Gui\Label;use JumPHP\Net\TcpConnection;use JumPHP\Runtime\RuntimeBadge;/** * The extension at work: a form with the runtime badge and a connection that goes where net_mode says (the user's network when the badge * is green, the server's bridge otherwise; on the desktop a plain socket). Click the badge: orange asks, red opens the guide. * * @property RuntimeBadge $badge * @property Label $mode * @property Label $result */final class DemoScene extends Form{    private ?TcpConnection $connection = null;    public function enter(array $params = []): void    {        $this->connection = null;        $this->build(['layout' => 'column', 'padding' => 24, 'gap' => 12, 'children' => [            ['type' => 'label', 'text' => 'JumPHP runtime', 'style' => 'title'],            ['type' => 'label', 'id' => 'mode', 'style' => 'muted', 'text' => ''],            ['class' => RuntimeBadge::class, 'id' => 'badge'],            ['class' => \JumPHP\Runtime\RuntimeBanner::class, 'id' => 'notice'],            ['type' => 'button', 'id' => 'connect', 'text' => 'Connect to 127.0.0.1:6379', 'style' => 'primary', 'events' => ['click' => 'onConnectClick']],            ['type' => 'label', 'id' => 'result', 'text' => ''],            ['type' => 'button', 'id' => 'menu', 'text' => '< MENU', 'events' => ['click' => 'onMenuClick']],        ]]);        $this->mode->text = 'Network mode: ' . $this->engine->config->netMode() . ' (project.yaml net_mode)';    }    public function onMenuClick(mixed $sender = null): void    {        $this->scenes()->switchTo(TitleScene::class);    }    public function onConnectClick(mixed $sender = null): void    {        $this->connection?->close();        $this->result->text = 'Connecting...';        $this->connection = $this->engine->tcp()->connect('127.0.0.1', 6379, 3.0);        $this->connection->on('open', fn () => $this->result->text = 'Connected');        $this->connection->on('error', fn (string $why) => $this->result->text = 'Not connected: ' . $why);    }}