[24, 24, 30], 'bar' => [44, 40, 66], 'gutter' => [34, 34, 42], 'gutterText' => [110, 110, 130], 'line' => [36, 36, 48], 'select' => [58, 76, 120], 'cursor' => [255, 220, 120], 'error' => [110, 30, 36], 'text' => [214, 214, 214], 'comment' => [106, 153, 85], 'string' => [206, 145, 120], 'label' => [230, 220, 140], 'jump' => [200, 160, 255], 'keyword' => [86, 156, 214], 'control' => [197, 134, 192], 'directive' => [78, 201, 176], 'variable' => [156, 220, 254], 'function' => [79, 193, 255], 'number' => [181, 206, 168], 'title' => [235, 225, 255], 'dim' => [150, 145, 175], 'ok' => [140, 220, 140], 'bad' => [255, 130, 120], ]; private SourceStore $store; private Highlighter $hl; private SourceBuffer $buf; private string $game = ''; /** @var list */ private array $scripts = []; private string $mode = 'list'; // list | edit | find | goto | help private int $listSel = 0; private int $listTop = 0; private string $script = ''; /** the script asked while its source downloads */ private string $pending = ''; private int $top = 0; private int $left = 0; private int $errorLine = -1; private string $status = ''; private string $statusKind = 'dim'; private float $statusTime = 0; private string $prompt = ''; private string $lastFind = ''; private float $time = 0; /** held keys repeat: key => seconds held */ private array $repeat = []; private ?int $overlay = null; /** layout of the last frame (window pixels): font size, char width, line height, text area */ private int $px = 16; private float $cw = 9; private int $lh = 18; private int $rows = 30; private int $cols = 80; private float $textX = 0; private float $textY = 0; private int $winW = 960; private int $winH = 720; /** @var array */ private array $fonts = []; /** @var array glyph textures (char, colour, size) */ private array $glyphs = []; private bool $dragging = false; /** the form shown: "90" (App\Alis\Lang\Human), "basic" (App\Alis\Lang\Basic) or "machine" (App\Alis\Lang\Forms) */ private string $form = Forms::NINETIES; /** a save (or a play: 'source' / 'hd') asked while the script's resources download */ private string $saveWhenReady = ''; /** the list: the first letters typed (a name is found as it is typed) */ private string $typedPrefix = ''; private float $typedAt = 0; /** the playground's externals (jumphp/externals): where the big files of the games come from (alis//) */ /** the "← Retour" button of the top bar, window pixels [x, y, w, h] (a mouse or a finger: no Escape needed) */ private array $backRect = [0, 0, 0, 0]; /** leaving with unsaved changes: the first click warns, the second one leaves */ private float $leaveWarnedAt = -10.0; public function __construct(private \JumPHP\Externals\Externals $externals) { } public function enter(array $params = []): void { $this->game = (string) ($params['game'] ?? 'robinson'); $dir = $this->engine->config->path('data/' . $this->game); $catalog = GameLibrary::catalog($this->engine->config->path('catalog'), $this->game); // the sources come like the films: from the developer's copy, else downloaded (catalog "lazy") SysVars::$storage = $this->engine->storage; SysVars::$lazy = $catalog['lazy'] ?? []; SysVars::$lazyBase = '/' . rawurlencode($this->engine->config->id()) . '/play/private/lazy/' . rawurlencode($this->game) . '/'; $dev = $this->engine->config->path('../../.alis-data/' . $this->game); SysVars::$lazyDev = is_dir($dev) ? $dev : ''; SysVars::$http = $this->engine->http(); SysVars::$externals = $this->externals->isAvailable() ? $this->externals : null; SysVars::$externalsDir = 'alis/' . $this->game; SysVars::$externalsOff = false; SysVars::$fetching = SysVars::$lazyFailed = []; $this->store = new SourceStore($this->engine->storage, $this->game, $dir . '/src'); $this->hl = new Highlighter(); $this->buf = new SourceBuffer(); $this->scripts = $this->store->scripts(); $this->mode = 'list'; $form = (string) $this->engine->options->get('alis_editor_form', $this->engine->options->get('alis_editor_human', true) === false ? Forms::MACHINE : Forms::NINETIES); $this->form = isset(Forms::NEXT[$form]) ? $form : Forms::NINETIES; $this->listSel = $this->listTop = $this->top = $this->left = 0; $this->script = $this->pending = $this->prompt = $this->saveWhenReady = ''; $this->errorLine = -1; $this->time = 0; $this->repeat = []; $this->say('Choisis un script (Entrée), F1 : aide', 'dim'); if (isset($params['script'])) { $this->open((string) $params['script']); if (isset($params['line'])) { $this->buf->moveTo((int) $params['line'] - 1, 0); } } if (function_exists('SDL_StartTextInput')) { \SDL_StartTextInput(); } $renderer = $this->sdl()->getRenderer(); $this->overlay ??= $renderer->addOverlay(fn (Renderer $r) => $this->draw($r)); } public function exit(): void { if ($this->overlay !== null) { $this->sdl()->getRenderer()->removeOverlay($this->overlay); $this->overlay = null; } foreach ($this->glyphs as $g) { \SDL_DestroyTexture($g[0]); } foreach ($this->fonts as $f) { \TTF_CloseFont($f); } $this->glyphs = $this->fonts = []; if (function_exists('SDL_StopTextInput')) { \SDL_StopTextInput(); } } // --- actions ------------------------------------------------------------------------------- private function say(string $text, string $kind = 'dim'): void { $this->status = $text; $this->statusKind = $kind; $this->statusTime = $this->time; } /** Opens a script (waits for its download when it is not here yet). */ public function open(string $script): void { $source = $this->store->load($script); if ($source === null) { if ($this->store->failed($script)) { $this->pending = ''; $this->say("Le source $script.als est introuvable", 'bad'); return; } $this->pending = $script; $this->say("Téléchargement de $script.als...", 'dim'); return; } $this->pending = ''; $this->script = $script; $this->buf->setText($this->present($source)); $this->store->resources($source); // its resources start downloading now (the compiler needs them) $this->top = $this->left = 0; $this->errorLine = -1; $this->mode = 'edit'; $this->say($script . '.als' . ($this->store->isChanged($script) ? ' (modifié)' : '') . ' — ' . count($this->buf->lines) . ' lignes', 'dim'); } /** Compiles; if it is right, saves the player's version. @return bool right */ public function save(): bool { if ($this->script === '') { return false; } if (!$this->store->resources($this->buf->text()) && SysVars::$fetching !== []) { $this->saveWhenReady = $this->saveWhenReady ?: 'save'; $this->say('Téléchargement des ressources du script...', 'dim'); return false; } $error = $this->store->check($this->buf->text()); if ($error !== null) { [$line, $message] = $error; $this->errorLine = $line - 1; if ($line > 0) { $this->buf->moveTo($line - 1, 0); $this->scrollToCursor(true); } $this->say('Erreur : ' . $message, 'bad'); return false; } $this->errorLine = -1; $this->store->save($this->script, $this->buf->text()); $this->buf->dirty = false; $this->say($this->store->isChanged($this->script) ? "$this->script.als compilé et enregistré" : "$this->script.als : identique à l'original", 'ok'); return true; } private function play(string $edition): void { if ($this->buf->dirty && !$this->save()) { if ($this->saveWhenReady !== '') { $this->saveWhenReady = $edition; // saved and played once the resources are here } return; } $this->scenes()->switchTo(PlayScene::class, ['game' => $this->game, 'edition' => $edition, 'back' => 'editor', 'script' => $this->script]); } /** A source in the form shown (ALIS 90, ALIS BASIC or the machine's words): all compile to the same bytes. */ private function present(string $source): string { return Forms::to($this->form, $source); } /** F2: the next form, the same script (a text that does not translate stays as it is). */ private function switchForm(): void { $previous = $this->form; $this->form = Forms::NEXT[$this->form]; $this->engine->options->set('alis_editor_form', $this->form); $titles = [Forms::NINETIES => 'Format 90 (langage humain)', Forms::BASIC => 'ALIS BASIC (comme sur l\'Atari ST de Silmarils)', Forms::MACHINE => 'Format machine (mots de la VM)']; if ($this->script === '' || $this->mode === 'list') { $this->say($titles[$this->form], 'dim'); return; } try { $line = $this->buf->line; $dirty = $this->buf->dirty; $text = $this->present($this->buf->text()); $this->buf->setText($text); $this->buf->dirty = $dirty; $this->buf->moveTo($line, 0); $this->errorLine = -1; $this->say($titles[$this->form], 'dim'); } catch (\Throwable $e) { $this->form = $previous; $this->say('Conversion impossible : ' . $e->getMessage(), 'bad'); } } private function revert(): void { if ($this->script === '') { return; } $this->store->revert($this->script); $original = $this->store->original($this->script); if ($original !== null) { $line = $this->buf->line; $this->buf->setText($this->present($original)); $this->buf->moveTo($line, 0); $this->errorLine = -1; $this->say("$this->script.als : retour à l'original", 'ok'); } } private function find(bool $backwards = false): void { if ($this->lastFind === '') { return; } if ($this->buf->find($this->lastFind, $backwards)) { $this->scrollToCursor(true); $this->say('« ' . $this->lastFind . ' » ligne ' . ($this->buf->line + 1), 'dim'); } else { $this->say('« ' . $this->lastFind . ' » introuvable', 'bad'); } } private function goto(string $where): void { $where = trim($where); if ($where === '') { return; } if (ctype_digit($where)) { $this->buf->moveTo((int) $where - 1, 0); } else { foreach ($this->buf->lines as $i => $l) { if (stripos(ltrim($l), $where . ':') === 0) { $this->buf->moveTo($i, 0); break; } } } $this->scrollToCursor(true); } private function scrollToCursor(bool $centre = false): void { $l = $this->buf->line; if ($centre && ($l < $this->top || $l >= $this->top + $this->rows)) { $this->top = max(0, $l - intdiv($this->rows, 3)); } if ($l < $this->top) { $this->top = $l; } elseif ($l >= $this->top + $this->rows) { $this->top = $l - $this->rows + 1; } $c = $this->buf->col; if ($c < $this->left) { $this->left = max(0, $c - 8); } elseif ($c >= $this->left + $this->cols - 1) { $this->left = $c - $this->cols + 8; } } // --- input --------------------------------------------------------------------------------- /** A key pressed this frame, or held long enough to repeat. */ private function hit(int $key, float $dt): bool { $kb = $this->engine->input->getKeyboard(); if ($kb->isKeyPressed($key)) { $this->repeat[$key] = 0.0; return true; } if (!$kb->isKeyHeld($key)) { unset($this->repeat[$key]); return false; } $before = $this->repeat[$key] ?? 0.0; $this->repeat[$key] = $after = $before + $dt; // after 0.4 s, 25 a second return $after > 0.4 && (int) (($after - 0.4) / 0.04) !== (int) (max(0, $before - 0.4) / 0.04); } public function update(float $dt): void { $this->time += $dt; if ($this->pending !== '' && SysVars::$fetching === []) { $this->open($this->pending); } if ($this->saveWhenReady !== '' && SysVars::$fetching === []) { $then = $this->saveWhenReady; $this->saveWhenReady = ''; if ($then === 'save') { $this->save(); } else { $this->play($then); } return; } $kb = $this->engine->input->getKeyboard(); $ctrl = $kb->ctrlHeld(); $shift = $kb->shiftHeld(); $text = $kb->getText(); if ($kb->isKeyPressed(\SDLK_F1)) { $this->mode = $this->mode === 'help' ? ($this->script !== '' ? 'edit' : 'list') : 'help'; return; } $m = $this->engine->input->getMouse(); if ($m->isPressed(1)) { [$wx, $wy] = $this->mouseWindow(); [$bx, $by, $bw, $bh] = $this->backRect; if ($bw > 0 && $wx >= $bx && $wx < $bx + $bw && $wy >= $by && $wy < $by + $bh) { $this->back(); return; } } match ($this->mode) { 'help' => $this->updateHelp(), 'list' => $this->updateList($dt), 'find', 'goto' => $this->updatePrompt($text, $dt), default => $this->updateEdit($dt, $ctrl, $shift, $text), }; } /** The "← Retour" button: one step back (the help, a prompt -> the text; the text -> the list; the list -> the menu). */ private function back(): void { switch ($this->mode) { case 'help': $this->mode = $this->script !== '' ? 'edit' : 'list'; return; case 'find': case 'goto': $this->mode = 'edit'; return; case 'list': if ($this->script !== '' && $this->buf->dirty && $this->time - $this->leaveWarnedAt > 5) { $this->leaveWarnedAt = $this->time; $this->say($this->script . '.als n\'est pas enregistré : Ctrl+S pour l\'enregistrer, ou recliquer Retour pour quitter quand même', 'bad'); return; } $this->scenes()->switchTo(TitleScene::class); return; default: $this->buf->anchor = null; $this->listSel = max(0, (int) array_search($this->script, $this->scripts, true)); $this->mode = 'list'; } } private function updateHelp(): void { $kb = $this->engine->input->getKeyboard(); $m = $this->engine->input->getMouse(); if ($kb->isKeyPressed(\SDLK_ESCAPE) || $kb->isKeyPressed(\SDLK_RETURN) || $m->isPressed(1)) { $this->mode = $this->script !== '' ? 'edit' : 'list'; } } private function updateList(float $dt): void { $kb = $this->engine->input->getKeyboard(); $n = count($this->scripts); if ($kb->isKeyPressed(\SDLK_ESCAPE)) { if ($this->script !== '') { $this->mode = 'edit'; } else { $this->scenes()->switchTo(TitleScene::class); } return; } $move = 0; $move -= $this->hit(\SDLK_UP, $dt) ? 1 : 0; $move += $this->hit(\SDLK_DOWN, $dt) ? 1 : 0; $move -= $this->hit(\SDLK_PAGEUP, $dt) ? $this->rows : 0; $move += $this->hit(\SDLK_PAGEDOWN, $dt) ? $this->rows : 0; $m = $this->engine->input->getMouse(); if ($m->wheelY != 0) { $this->listTop = max(0, min(max(0, $n - $this->rows), $this->listTop - (int) round($m->wheelY * 3))); } // type the first letters of a name to go there $typed = strtolower($kb->getText()); if ($typed !== '' && ctype_alnum($typed)) { $this->typedPrefix = ($this->time - $this->typedAt < 1.0 ? $this->typedPrefix : '') . $typed; $this->typedAt = $this->time; foreach ($this->scripts as $i => $s) { if (str_starts_with($s, $this->typedPrefix)) { $this->listSel = $i; break; } } } if ($move !== 0) { $this->listSel = max(0, min($n - 1, $this->listSel + $move)); } if ($this->listSel < $this->listTop) { $this->listTop = $this->listSel; } elseif ($this->listSel >= $this->listTop + $this->rows) { $this->listTop = $this->listSel - $this->rows + 1; } if ($m->isPressed(1)) { [$wx, $wy] = $this->mouseWindow(); $row = (int) floor(($wy - $this->textY) / $this->lh); if ($row >= 0 && $row < $this->rows && isset($this->scripts[$this->listTop + $row])) { $i = $this->listTop + $row; if ($i === $this->listSel) { $this->open($this->scripts[$i]); } $this->listSel = $i; } } if (($kb->isKeyPressed(\SDLK_RETURN) || $kb->isKeyPressed(\SDLK_KP_ENTER)) && isset($this->scripts[$this->listSel])) { $this->open($this->scripts[$this->listSel]); } } private function updatePrompt(string $text, float $dt): void { $kb = $this->engine->input->getKeyboard(); if ($kb->isKeyPressed(\SDLK_ESCAPE)) { $this->mode = 'edit'; return; } if ($this->hit(\SDLK_BACKSPACE, $dt)) { $this->prompt = SourceBuffer::sub($this->prompt, 0, max(0, SourceBuffer::len($this->prompt) - 1)); } $this->prompt .= str_replace(["\n", "\r"], '', $text); if ($kb->isKeyPressed(\SDLK_RETURN) || $kb->isKeyPressed(\SDLK_KP_ENTER)) { $mode = $this->mode; $this->mode = 'edit'; if ($mode === 'find') { $this->lastFind = $this->prompt; $this->find(); } else { $this->goto($this->prompt); } } } private function updateEdit(float $dt, bool $ctrl, bool $shift, string $text): void { $kb = $this->engine->input->getKeyboard(); $b = $this->buf; $moved = false; if ($kb->isKeyPressed(\SDLK_ESCAPE)) { if ($b->anchor !== null) { $b->anchor = null; } else { $this->listSel = max(0, (int) array_search($this->script, $this->scripts, true)); $this->mode = 'list'; } return; } if ($kb->isKeyPressed(\SDLK_F5)) { $this->play('source'); return; } if ($kb->isKeyPressed(\SDLK_F6)) { $this->play('hd'); return; } if ($kb->isKeyPressed(\SDLK_F2)) { $this->switchForm(); return; } if ($this->hit(\SDLK_F3, $dt)) { $this->find($shift); } if ($ctrl) { $pressed = static fn (int $k) => $kb->isKeyPressed($k); if ($pressed(\SDLK_s)) { $this->save(); } elseif ($pressed(\SDLK_f)) { $this->mode = 'find'; $this->prompt = $b->anchor !== null && !str_contains($b->selected(), "\n") ? $b->selected() : $this->lastFind; return; } elseif ($pressed(\SDLK_g)) { $this->mode = 'goto'; $this->prompt = ''; return; } elseif ($this->hit(\SDLK_z, $dt)) { $shift ? $b->redo() : $b->undo(); $moved = true; } elseif ($this->hit(\SDLK_y, $dt)) { $b->redo(); $moved = true; } elseif ($pressed(\SDLK_a)) { $b->selectAll(); } elseif ($pressed(\SDLK_c) || $pressed(\SDLK_x)) { if ($b->anchor !== null) { Clipboard::copy($b->selected()); if ($pressed(\SDLK_x)) { $b->delete(); $moved = true; } } } elseif ($pressed(\SDLK_v)) { Clipboard::paste()->then(function (mixed $t): void { if (is_string($t) && $t !== '') { $this->buf->insert($t, 'other'); $this->scrollToCursor(); } }); } elseif ($pressed(\SDLK_r)) { $this->revert(); $moved = true; } } $word = $ctrl; $page = max(1, $this->rows - 2); if ($this->hit(\SDLK_LEFT, $dt)) { $b->left($shift, $word); $moved = true; } if ($this->hit(\SDLK_RIGHT, $dt)) { $b->right($shift, $word); $moved = true; } if ($this->hit(\SDLK_UP, $dt)) { $b->vertical(-1, $shift); $moved = true; } if ($this->hit(\SDLK_DOWN, $dt)) { $b->vertical(1, $shift); $moved = true; } if ($this->hit(\SDLK_PAGEUP, $dt)) { $b->vertical(-$page, $shift); $this->top = max(0, $this->top - $page); $moved = true; } if ($this->hit(\SDLK_PAGEDOWN, $dt)) { $b->vertical($page, $shift); $this->top = min(max(0, count($b->lines) - $this->rows), $this->top + $page); $moved = true; } if ($kb->isKeyPressed(\SDLK_HOME)) { $ctrl ? $b->moveTo(0, 0, $shift) : $b->home($shift); $moved = true; } if ($kb->isKeyPressed(\SDLK_END)) { $ctrl ? $b->moveTo(PHP_INT_MAX, PHP_INT_MAX, $shift) : $b->end($shift); $moved = true; } if ($this->hit(\SDLK_BACKSPACE, $dt)) { $b->backspace($ctrl); $moved = true; } if ($this->hit(\SDLK_DELETE, $dt)) { $b->delete($ctrl); $moved = true; } if ($this->hit(\SDLK_RETURN, $dt) || $this->hit(\SDLK_KP_ENTER, $dt)) { $b->newline(); $moved = true; } if ($kb->isKeyPressed(\SDLK_TAB)) { $b->indent($shift); $moved = true; } if (!$ctrl && $text !== '') { $text = str_replace(["\r", "\n", "\t"], '', $text); if ($text !== '') { $b->insert($text); // a "}" typed alone on its line goes back one level $line = $b->lines[$b->line]; if ($text === '}' && trim($line) === '}' && str_starts_with($line, ' ')) { $b->lines[$b->line] = substr($line, 4); $b->col = SourceBuffer::len($b->lines[$b->line]); } $moved = true; } } $this->mouse($moved); if ($moved) { $this->scrollToCursor(); } } /** @return array{0: float, 1: float} the mouse in window pixels */ private function mouseWindow(): array { $m = $this->engine->input->getMouse(); [$lx, $ly, $lw, $lh] = $this->sdl()->getRenderer()->letterbox(); return [$lx + $m->x * $lw / max(1, $this->width()), $ly + $m->y * $lh / max(1, $this->height())]; } private function mouse(bool &$moved): void { $m = $this->engine->input->getMouse(); if ($m->wheelY != 0) { $this->top = max(0, min(max(0, count($this->buf->lines) - 3), $this->top - (int) round($m->wheelY * 3))); } if ($m->isPressed(1) || ($this->dragging && $m->isHeld(1))) { [$wx, $wy] = $this->mouseWindow(); if ($wy >= $this->textY && $wy < $this->textY + $this->rows * $this->lh || $this->dragging) { $line = $this->top + (int) floor(($wy - $this->textY) / $this->lh); $col = $this->left + (int) round(($wx - $this->textX) / $this->cw); $shift = $this->engine->input->getKeyboard()->shiftHeld(); $this->buf->moveTo($line, $col, $this->dragging || $shift); if ($m->isPressed(1) && !$shift) { $this->buf->anchor = [$this->buf->line, $this->buf->col]; } $this->dragging = true; $moved = true; } } if (!$m->isHeld(1)) { if ($this->dragging && $this->buf->anchor === [$this->buf->line, $this->buf->col]) { $this->buf->anchor = null; } $this->dragging = false; } } // --- drawing ------------------------------------------------------------------------------- public function render(Renderer $r): void { $r->clear(self::COLOURS['bg']); } /** The whole editor, at the window's resolution (overlay). */ private function draw(Renderer $r): void { if ($this->scenes()->current() !== $this) { return; } $sdl = $r->getRenderer(); [$this->winW, $this->winH] = $r->outputSize(); $W = $this->winW; $H = $this->winH; // about 40 lines in the window, never tiny $this->px = max(11, min(28, (int) round($H / 42))); $font = $this->monoFont($this->px); if ($font === null) { return; } $mw = 0; $mh = 0; \TTF_SizeUTF8($font, 'M', $mw, $mh); $this->cw = max(1, (float) $mw); $this->lh = (int) round($this->px * 1.35); $bar = $this->lh + 4; $this->fill($sdl, 0, 0, $W, $H, self::COLOURS['bg']); // top bar: the game, the script, its state $this->fill($sdl, 0, 0, $W, $bar, self::COLOURS['bar']); $title = 'ÉDITEUR ALIS (' . Forms::TITLES[$this->form] . ') — ' . $this->game . ($this->script !== '' && $this->mode !== 'list' ? ' / ' . $this->script . '.als' . ($this->buf->dirty ? ' •' : '') : ''); $label = $this->mode === 'list' ? '← Menu' : '← Retour'; $bw = (int) round((SourceBuffer::len($label) + 2) * $this->cw); $this->backRect = [0, 0, $bw, $bar]; $this->fill($sdl, 0, 0, $bw, $bar, self::COLOURS['select']); $this->text($sdl, $label, $this->cw, 2, self::COLOURS['title']); $this->text($sdl, $title, $bw + 8, 2, self::COLOURS['title']); $pos = $this->mode === 'edit' || $this->mode === 'find' || $this->mode === 'goto' ? sprintf('ligne %d, col %d', $this->buf->line + 1, $this->buf->col + 1) : count($this->scripts) . ' scripts'; $this->text($sdl, $pos, $W - 8 - SourceBuffer::len($pos) * $this->cw, 2, self::COLOURS['dim']); // bottom bar: the status or the prompt, the keys $this->textY = $bar + 2; $this->rows = max(1, intdiv($H - 2 * $bar - 4, $this->lh)); $this->fill($sdl, 0, $H - $bar, $W, $bar, self::COLOURS['bar']); if ($this->mode === 'find' || $this->mode === 'goto') { $label = ($this->mode === 'find' ? 'Chercher : ' : 'Aller à (ligne ou label) : ') . $this->prompt; $this->text($sdl, $label, 8, $H - $bar + 2, self::COLOURS['title']); $this->fill($sdl, (int) (8 + SourceBuffer::len($label) * $this->cw), $H - $bar + 3, 2, $this->lh - 2, self::COLOURS['cursor']); } else { $status = $this->statusKind === 'bad' || $this->time - $this->statusTime < 10 ? $this->status : ''; $this->text($sdl, $status, 8, $H - $bar + 2, self::COLOURS[$this->statusKind]); $keys = $this->mode === 'list' ? 'Entrée ouvrir · ← Menu (ou Échap) · F1 aide' : 'Ctrl+S compiler · F5 jouer · F6 HD · ← Retour · F1 aide'; $kx = $W - 8 - SourceBuffer::len($keys) * $this->cw; if ($kx > 8 + (SourceBuffer::len($status) + 2) * $this->cw) { $this->text($sdl, $keys, $kx, $H - $bar + 2, self::COLOURS['dim']); } } match ($this->mode) { 'list' => $this->drawList($sdl), 'help' => $this->drawHelp($sdl), default => $this->drawText($sdl), }; } private function drawList($sdl): void { $y = $this->textY; $this->textX = 16; if ($this->pending !== '') { $this->text($sdl, 'Téléchargement de ' . $this->pending . '.als...', 16, $y, self::COLOURS['dim']); $y += $this->lh * 2; } for ($i = 0; $i < $this->rows && isset($this->scripts[$this->listTop + $i]); $i++) { $k = $this->listTop + $i; $name = $this->scripts[$k]; if ($k === $this->listSel) { $this->fill($sdl, 0, (int) $y, $this->winW, $this->lh, self::COLOURS['select']); } $changed = $this->store->isChanged($name); $this->text($sdl, ($changed ? '● ' : ' ') . $name . '.als', 16, $y, $changed ? self::COLOURS['label'] : self::COLOURS['text']); if ($changed) { $this->text($sdl, 'modifié', 16 + 24 * $this->cw, $y, self::COLOURS['dim']); } $y += $this->lh; } } private function drawHelp($sdl): void { $x = 24; $y = $this->textY + $this->lh; $this->text($sdl, 'Raccourcis de l\'éditeur ALIS', $x, $y, self::COLOURS['title']); $y += $this->lh * 2; foreach (self::HELP as [$key, $what]) { $this->text($sdl, $key, $x, $y, self::COLOURS['keyword']); $this->text($sdl, $what, $x + 18 * $this->cw, $y, self::COLOURS['text']); $y += $this->lh; } $y += $this->lh; foreach ([ 'Le langage : docs/language.md (variables vb[...] / vw[...], expressions, if { } else { }, while { },', 'labels L0049:, goto / call, table, switch). Les scripts modifiés sont gardés dans tes fichiers et', 'servent aux éditions « ALIS sources » et « HD » du jeu ; Ctrl+R revient à l\'original.', ] as $line) { $this->text($sdl, $line, $x, $y, self::COLOURS['dim']); $y += $this->lh; } } private function drawText($sdl): void { $b = $this->buf; $n = count($b->lines); $gutter = (int) ceil((strlen((string) $n) + 2) * $this->cw); $this->textX = $gutter + 6; $this->cols = max(10, (int) floor(($this->winW - $this->textX) / $this->cw)); $this->top = max(0, min($this->top, max(0, $n - 1))); $this->fill($sdl, 0, (int) $this->textY, $gutter, $this->rows * $this->lh, self::COLOURS['gutter']); [$sa, $sb] = $b->ordered(); $headerEnd = $this->headerEnd(); $this->hl->declare($b->lines); // the names an ALIS 90 file declares for ($i = 0; $i < $this->rows; $i++) { $l = $this->top + $i; if ($l >= $n) { break; } $y = $this->textY + $i * $this->lh; if ($l === $this->errorLine) { $this->fill($sdl, $gutter, (int) $y, $this->winW - $gutter, $this->lh, self::COLOURS['error']); } elseif ($l === $b->line) { $this->fill($sdl, $gutter, (int) $y, $this->winW - $gutter, $this->lh, self::COLOURS['line']); } // the selection on this line if ($b->anchor !== null && $l >= $sa[0] && $l <= $sb[0]) { $from = $l === $sa[0] ? $sa[1] : 0; $to = $l === $sb[0] ? $sb[1] : SourceBuffer::len($b->lines[$l]) + 1; $x1 = $this->textX + max(0, $from - $this->left) * $this->cw; $x2 = $this->textX + max(0, $to - $this->left) * $this->cw; if ($x2 > $x1) { $this->fill($sdl, (int) $x1, (int) $y, (int) ceil($x2 - $x1), $this->lh, self::COLOURS['select']); } } $num = (string) ($l + 1); $this->text($sdl, $num, $gutter - 4 - strlen($num) * $this->cw, $y, $l === $b->line ? self::COLOURS['text'] : self::COLOURS['gutterText']); $col = 0; foreach ($this->hl->line($b->lines[$l], $l < $headerEnd) as [$t, $kind]) { $len = SourceBuffer::len($t); if ($col + $len > $this->left && $col < $this->left + $this->cols) { $skip = max(0, $this->left - $col); $shown = $skip > 0 || $col + $len > $this->left + $this->cols ? SourceBuffer::sub($t, $skip, $this->left + $this->cols - $col - $skip) : $t; $this->text($sdl, $shown, $this->textX + ($col + $skip - $this->left) * $this->cw, $y, self::COLOURS[$kind] ?? self::COLOURS['text']); } $col += $len; } } // the cursor (blinks) if ($this->mode === 'edit' && fmod($this->time, 1.0) < 0.6 && $b->line >= $this->top && $b->line < $this->top + $this->rows) { $x = $this->textX + ($b->col - $this->left) * $this->cw; $this->fill($sdl, (int) $x, (int) ($this->textY + ($b->line - $this->top) * $this->lh), 2, $this->lh, self::COLOURS['cursor']); } } /** The header (directives) ends at the first label. */ private function headerEnd(): int { foreach ($this->buf->lines as $i => $l) { if (preg_match('/^\s*[A-Za-z_]\w*:/', $l)) { return $i; } if ($i > 40) { break; } } return 40; } private function fill($sdl, int $x, int $y, int $w, int $h, array $c): void { \SDL_SetRenderDrawColor($sdl, $c[0], $c[1], $c[2], 255); \SDL_RenderFillRect($sdl, new \SDL_Rect($x, $y, $w, $h)); } /** Text, one glyph texture per character (monospaced: the columns stay exact), window pixels. */ private function text($sdl, string $text, float $x, float $y, array $c): void { $font = $this->monoFont($this->px); if ($font === null || $text === '') { return; } $ck = $this->px . '|' . $c[0] . ',' . $c[1] . ',' . $c[2] . '|'; $i = 0; foreach (preg_split('//u', $text, -1, PREG_SPLIT_NO_EMPTY) ?: [] as $ch) { if ($ch !== ' ') { $g = $this->glyphs[$ck . $ch] ?? null; if ($g === null) { $surface = \TTF_RenderUTF8_Blended($font, $ch, new \SDL_Color($c[0], $c[1], $c[2], 255)); if ($surface) { // @phpstan-ignore if.alwaysTrue (the binding returns false on failure) $tex = \SDL_CreateTextureFromSurface($sdl, $surface); $g = [$tex, (int) $surface->w, (int) $surface->h]; \SDL_FreeSurface($surface); if (count($this->glyphs) > 3000) { foreach ($this->glyphs as $old) { \SDL_DestroyTexture($old[0]); } $this->glyphs = []; } $this->glyphs[$ck . $ch] = $g; } } if ($g !== null && $g[0]) { // @phpstan-ignore notIdentical.alwaysTrue (null when the font failed) \SDL_RenderCopy($sdl, $g[0], null, new \SDL_Rect((int) round($x + $i * $this->cw), (int) round($y + ($this->lh - $g[2]) / 2), $g[1], $g[2])); } } $i++; } } private function monoFont(int $px): ?\TTF_Font { if (!isset($this->fonts[$px])) { if (!\TTF_WasInit()) { \TTF_Init(); } $font = \TTF_OpenFont($this->engine->config->path('assets/fonts/mono.ttf'), $px); if (!$font) { // @phpstan-ignore booleanNot.alwaysFalse (the binding returns false on failure) return null; } $this->fonts[$px] = $font; } return $this->fonts[$px]; } /** Tests: the buffer being edited. */ public function buffer(): SourceBuffer { return $this->buf; } }