# Liminalia Studio

## Variables, game settings and game screens

**Variables** is the standard screen that puts the variables used in the script (`game.`, `system.`, `temp.`) and their initial values (`variables` in liminalia.json) in one table.
They are listed by kind, in name order (numbers are compared as numbers, so `chapter_2` comes before `chapter_10`).
Editing an initial value saves it right away (emptying it removes it; strings as `"…"`, and numbers, `true` and `false` as they are), and the preview and the current position data run again from the initial values.
Click a row to open the lines that assign and read the variable, and click one of those to go to that script. Renaming a variable also rewrites the references in the script, where it is assigned, the name of its initial value, the conditions (`if`) in `ui` and `extra` of liminalia.json, and the conditions of breakpoints.
Variables that are read but have no assigning line or initial value, and variables that are assigned but read nowhere, are marked. "Add a variable" lets you set just an initial value in advance.

**Game settings** is the standard screen for editing `ui` in liminalia.json ("Game settings" in docs/player.md). On the left are the items (Text, Message window, Name box,
Click-wait icon, Choices, Chat, NVL, System screens, Title screen, Ending name, Chapter card and end, Cursor, Saves and backlog, Config defaults, Quick menu, Key bindings),
and on the right a preview drawn by the real player (switch between Text, Choices, Title, Menu, Save, Settings, Log, Ending, Chapter card and NVL; touching an item switches to the preview it affects.
Ending waits with the name of a sample ending (`@title-screen ending=good`) shown, using `?ui-preview=ending`, and applies the values of `ui.ending` as they are.
Chapter card shows `@chapter-card` in the Game screens preview (`?ui-preview=screen&screen=chapter`) and applies the values of `ui.chapter`.
NVL stacks two `@nvl on` paragraphs with `?ui-preview=nvl` and applies the values of `ui.nvl`. Touching a field in a row of named looks (`ui.nvlStyles`) switches the preview to `@nvl on style=<name>`).
Like Game screens, the preview never reads or writes the player's saves, read pages or settings (it opens with a sample store; the save and load previews get sample saves).
Edits are saved right away and take effect in the preview and any open player without a restart. An emptied item is removed (it goes back to the default).
Nested values are edited in a field for each item too, without writing JSON (`game-fields.ts`): image slicing as four numbers for top, right, bottom and left (written as one number if only the top is given or all four are the same),
text shadow as the horizontal and vertical offset, blur and color, text outline as width and color, letter spacing as a number of em (values that can't be split, such as layered shadows or letter spacing in px, are edited as CSS with "Enter as CSS"),
the title button layout as position, direction, spacing and width, the titles and help pages that change after the game is finished and so on as lists with rows added by "Add" (rows without a condition or heading aren't written),
captions per sound effect, sprite tones and tones by background (`ui.backgroundTones`) as name-value pairs, and system voices as rows of the moment (title screen, confirmation, each action) and voice, condition, assigned character, and volume
(split into `title`, `confirm` and `actions` when saved). "Startup splash" (`ui.splash`) is edited with fields for the logos shown in order (rows of image, background color and time; rows without an image aren't written),
the notice heading and items (rows of heading and text; rows without text aren't written), background color, text color, display time, whether it advances automatically, whether a click moves to the next, and the fade-out time at the end.
Items with no field (written by hand) are kept, not deleted. To add a key, click "＋" and then press the key.
"Edit layout" above the preview lays frames over the Text and Choices previews for what can be moved (the message window, name box, face window, choices, quick menu, sprites and HUD items) (`layout-overlay.ts`, `model/layout.ts`; the preview is `?ui-preview=page&layout=1`, shows up to three characters at left, center and right, shows one line per name of the script's `@hud-button` and `@hud-text` with `show-if=` removed, and reports the layout with `liminalia:layout-report` from the player's `layout-probe.ts`).
Select a frame and drag it to move it, or drag the handles on its corners and edges to resize it (the message window, face window and HUD buttons; choices keep their center and change only the width).
The handles can be pressed within 20px, or 44px on touch devices (tablets and the mobile version; CSS `pointer: coarse`).
With the keyboard, arrow keys move 1px, Shift+arrow keys move 10px, and ⌥+arrow keys change the size. Dragging and keys can't take things off the screen (the edge being dragged stops at the edge of the screen, and something already sticking out only moves back inward; sprites aren't stopped).
Selecting a frame shows X, Y, Width and Height fields in the row below the top bar, and highlights and scrolls to the same items on the left.
Changing a number in either field moves the preview and the frame, and dragging updates the numbers in the fields.
Something put off the screen with the numbers gets a red frame, with "Off screen" next to the number fields. The row below the top bar (Edit layout, the number fields, Change history) is always shown, and the size of the preview doesn't change when you start editing the layout or select something.
The result is written to `ui.message` (all four of `x` `y` `width` `height`), `x` `y` of `ui.name` and `ui.menuBar`, `x` `y` of `ui.face` (when moved) and its `width` `height`, and `y` `width` of `ui.choice` (values are in the px of the text and window coordinates, the same for portrait projects and projects of a different size).
A sprite moves that position (`frame.positions` in cast.json) by the same amount in every variant of its sprite set (so it doesn't jump when the expression changes; moving a variant without a frame makes a frame that matches how it looks now).
"Position" for a selected sprite changes which position the preview shows it at.
A HUD item rewrites every script line with that name that places it at the same spot (the `x` `y` numbers): `x` `y`, plus `width` `height` when changed or already written.
Other parameters are kept, and lines that give the position as an expression or place it somewhere else are left alone.
A drag is saved when you let go and keys when you stop pressing them, and all of it can be undone with ⌘Z in Game settings (cast.json and scripts too).
If `ui` changed elsewhere after it was loaded, it is merged item by item before saving, as with the script (see "Principles" below).
If a project plugin replaces a screen (title, startup splash, Settings, save and load, Log, quick menu, menu, confirmation, ending name,
extras, flowchart, the auto and skip indicator, codex, notifications, choices, name box, dialog boxes, controls screen, items, click-wait icon; known from the plugin's declaration),
the group heading shows "(plugin name)'s screen" (whether these settings are used is up to the plugin). When you add a way to replace a screen, also add it to `REPLACEABLE` in `packages/web/src/ui-probe.ts`
(`packages/studio/test/replaced-screens.test.ts` checks it against the list of standard screens).
In the Text and Choices previews, each item's value is tried with two different values, and items whose actual look doesn't change are marked "No effect"
(when something else decides the look, such as the project's CSS, a plugin, or a character's name color; the values are put back before drawing).
"Command defaults" at the very bottom is `commandDefaults` in liminalia.json ("Project command defaults" in [format.md](../design/format.md)). Choose a command and parameter and click "Add,"
and entering a value saves it right away (the faint text in the field is the standard default; emptied and deleted entries are removed). When saved, completion, the parameter panel and the syntax check reload with the new defaults,
and the preview runs with the new defaults from the next "New game" or load. "Reset everything to default" at the very bottom removes both `ui` and the command defaults.
"Change theme" in the theme section at the very top opens a modal where you switch between "Preset themes" and "Original themes" to choose one. Nothing in the project settings or assets changes until you click "Apply." On the Original themes tab, you can save the current look as a theme and move project themes to the trash ("Themes" in [player.md](../player.md)).
Portrait-only themes (`orientation: "portrait"`, such as chat) aren't listed for landscape games, and for portrait games they are grouped at the top under the heading "Portrait only." Cards for themes that show text in speech balloons are drawn with balloons.
The "Chat" group holds the `ui.chat` items ("The chat screen" in [player.md](../player.md)).

**Game screens** is the standard screen for opening the built-in screens (title, message window, choices, save and load, log, ending name, extras, codex, investigation and so on;
the screens in the table in [screens.md](../screens.md)) filled with sample content. On the left is the list of screens, on the right a preview opened in the real player.
Without playing the game up to that screen, you can check how filled save slots look (thumbnail, date, the start of the text, memo, lock), the log (voices, a page where two people speak at once, a choice that was picked),
the ending name and kind, and items in the extras, codex and inventory. The sample content uses neutral names (a guide, a traveler, a lighthouse and so on) and never the project's characters or scripts.
The bar at the top switches the content (Filled, Empty, Long text; long text means long lines and long names), Portrait (Landscape for portrait projects; portrait uses the layout in `system/portrait.css`),
English (the built-in screens' wording and the sample content in English; English is added inside the preview only, even if the project's `languages` don't include it),
and the theme (the current theme, or one of the bundled and project themes, listed as light themes and dark themes). Content a screen doesn't provide can't be selected.
The chosen screen and switches stay the same the next time you open it (`liminalia.studio.screens` in the browser's `localStorage`).
Screens that can't be opened (click to start, the hint shown when a phone is held upright, playtest feedback) go at the end of the list with the reason.
A screen replaced by a plugin opens as the replaced screen, and the list shows "<plugin>'s screen" (whether the plugin has the replacement hooks in the table in screens.md, such as `screens.*`, `title`, `system`
and `extensions.renderers.*`; the standard commands' `std` counts as standard).
When `ui` in liminalia.json is saved (from the Game settings screen, another app or an AI), the preview applies the look without reloading and opens the same screen again.
Saving a plugin's CSS also swaps it in place. While a project theme (`themes/<id>/theme.json`) is applied in the preview, an outside change to it reloads the preview.
The preview never reads or writes the player's saves, read pages or settings (IndexedDB, `localStorage`): before it starts, the store is swapped for a sample store that disappears when the page closes
(`useMemorySaveStore` in `packages/web/src/system/storage.ts`). The theme, language, orientation and sample extras are layered only onto the loaded project information; the project's files are never rewritten.

While "Pick parts" in the top bar is on (the default), clicking a part in the preview (a slot number, a title screen button, the message window, a choice button and so on)
outlines that part and shows, in the panel on the right, the theme values (`ui` in liminalia.json) that set its look. The parts and values come from the parts table in [screens.md](../screens.md)
(`parts` in `packages/web/src/system/screen-catalog.ts`), and the fields are the same as in Game settings (colors in color fields, text sizes and corner radii in number fields, shadows in split fields; `client/screen-parts.ts`).
The part under the mouse is outlined with a dotted line. You can also pick a part from the menu above the fields. Clicking a part under a screen opened over the preview (such as the message window) shows it as a part of that screen.
While you drag a field (picking a color, typing a number), the preview takes the unsaved value (`liminalia:screen-draft`); once you settle, it is saved to `ui` in liminalia.json
(if it changed elsewhere after it was read, items are merged as in Game settings). Saved changes can be undone one at a time with ⌘Z on this screen.
Under each field are the other parts on the same screen and the other screens the same value affects (values in `ui.system` affect the other system screens too). Hovering over or entering a field outlines the other parts the same value affects with a dotted line.
Under the theme values are the project CSS fields (text color, background color, text size, padding, corner radius, border color, border width). Choosing a value writes one line,
"part selector { property: value; }", to the project CSS (a line with the same selector and property is rewritten, and clearing the field removes it. Nothing is put in an element's style, so you can open the CSS file and read it later),
and the unsaved line is sent to the preview too. The lines go to the first file in `ui.css`; if there is none, `ui/screens.css` is created and added to `ui.css` ("Project CSS" in [player.md](../player.md)).
Like theme values, written lines can be undone one at a time with ⌘Z on this screen (a created file and `ui.css` are restored too). They also cover changes theme values can't make (a color for just that part, for example).
For a part with no theme values (the menu's dimmed backdrop, captions and so on), a single line shows the part's selector and the file the lines go to, and only the project CSS fields are shown.
Turning "Pick parts" off lets you click inside the preview (switch tabs, scroll lists and so on). In the mobile version the panel sits under the preview, and you tap a part to pick it.
Switching the panel to "CSS" with the switch above it lets you write the project CSS file directly (the same file the part fields use: the first file in `ui.css`, or `ui/screens.css`, which is created and added to `ui.css`;
`client/screen-css.ts`, a CodeMirror CSS editor). Lines written by the part fields can be read and edited here too. What you type shows in the preview right away, before it is saved
(`cssFile` in `liminalia:screen-draft`, used in place of that file's content), and is saved after a short pause (`PUT /ui/css`; if the file changed elsewhere after it was read, it isn't saved and the current content is loaded).
⌘Z inside the editor undoes typing; ⌘Z outside it undoes saved changes one at a time (a created file and `ui.css` are restored too).
Completion offers part class names (the open screen's first, with the part's name; other screens' too), `--lm-*` variables (the variables shared by system screens, the screen's own variables,
and the variables theme values go into, with the value's name), the state attributes the engine writes (`data-lm-hover` and so on; "States" in [design/screen-templates.md](../design/screen-templates.md)),
and CSS properties and values (`@codemirror/lang-css`). Clicking a part in the preview moves to the rule for its selector and highlights its lines; if there is no rule, it shows where to add one at the end (click it to add the rule).
Putting the cursor inside a rule outlines the parts its selector matches in the preview (states such as `:hover` and `data-lm-*` are dropped when looking).
CSS that can't be read (an unclosed brace or comment) and `url()` pointing outside the project get a wavy underline with the same diagnostics as `ui-css-syntax` and `ui-css-outside` in lim check.
The switch is remembered for next time (`liminalia.studio.screens.panel` in `localStorage`). In the mobile version, the editor sits under the preview like the part fields.

For screens that can be built from a template (confirmation, menu, controls help, ending name, choices, save and load, backlog, title and settings; `GET /screens/rules`, decided by `STANDARD_SCREENS` in `@liminalia/web`, so new ones show up as they are added),
the switch above the panel also offers "Template" (`client/screen-template.ts`; templates and attributes are explained in [design/screen-templates.md](../design/screen-templates.md)).
Before copying, it shows "Make this screen my own" and what to copy (the whole screen or one part). Like `lim screens copy`, it copies the built-in template to
`ui/screens/<screen>/<screen>.html`, registers it in `ui.templates` in `liminalia.json`, and keeps the copied original in `.liminalia/screens/`.
The copied file opens in this editor, and the desktop app also shows the template's folder in the file manager (the "Open folder" button does the same later).
If a plugin's `screens.*` (including one in a theme) or `system` replaces the same screen first, it says beforehand that a copy won't be used.
What you type in the template editor reopens the same screen with that template in the preview right away, before it is saved (`template` in `liminalia:screen-draft`), and is saved after a short pause (`PUT /screens/file`).
Attribute values (`data-role`, `data-field`, `data-text`, `data-part`, `data-item`, `data-setting`) are completed from the screen's rules (working roles, screen values, built-in text keys, part names and the setting names of the settings screen).
Problems from the template check in `lim check` (`template-*`) get wavy underlines for errors and warnings. When a part's built-in template has changed since the copied version (`template-outdated`),
a line saying the built-in template changed appears above the editor; clicking it shows the differences from the built-in template, the same as `lim screens diff`.
"Add JS to this screen" puts a screen JS skeleton next to the template (the same as `lim screens js`) and opens it, and you can switch between the template and the JS
(the mobile version's preview doesn't load screen JS yet). "Reset to built-in" removes the screen from `ui.templates` like `lim screens reset` and keeps the template and the screen JS
with `.reset-<date and time>` added to their names. Copying, writing, adding JS and resetting can each be undone one at a time with ⌘Z on this screen (created files and folders and `ui.templates` are restored too).
⌘Z inside the editor undoes typing.

The preview player opens at `/?ui-preview=screen&screen=<screen id>` (dev server only; it isn't included in exported games).
The parameters and the sample content are kept in one place, `packages/web/src/screen-preview/samples.ts`, and screenshot tools and the like can open screens the same way:

| Parameter | Meaning |
|---|---|
| `screen` | The screen id (`id` in `GET /screens`; the screens in the table in screens.md) |
| `state` | `empty` (Empty) or `long` (Long text). Omit it for Filled |
| `orientation` | `portrait` or `landscape`. Omit it to keep the project's orientation |
| `lang` | The language to play in (`en` for English) |
| `theme` | The id of the theme to apply (the name written in `ui.system.preset`) |
| `bg` | The sample background (a path from `bgimage`; omit it for a sample picture) |
| `sample-lang` | The language of the sample content (`ja` or `en`; omit it to use the play language, the same as the built-in screens' wording) |
| `still` | `1` shows the final state without waiting for opening animations |

When the screen is open, `data-lm-screen-preview` on html becomes `ready` (`failed` if it can't be opened), `window.__liminaliaScreenPreview` holds the state (`status`, `replaced`, `problems`),
and `{ type: "liminalia:screen-preview", screen, state, status, replaced, problems }` (`ScreenPreviewReport` in `protocol.ts`) is sent to the parent page. Each time the screen opens again, it starts over from `loading`.
When you add a screen to the list of built-in screens (`packages/web/src/system/screen-catalog.ts`), write how to open it or why it can't be opened in `SCREEN_SAMPLES` in `samples.ts`
(`packages/web/test/screen-preview.test.ts` checks them against each other; write how to check each screen's content in `CONTENT` in `packages/studio/test/screen-preview-e2e.test.ts`).
