Screens and Interface
The game’s interface is a set of screens inside the <visual-novel> element: the main menu, the game itself, the save, load and settings screens and the others. One shows at a time. This page lists the methods that switch between them, show a dialog over them, reach the game’s elements, and run your code when the player presses a button or a key. The Interface describes the interface as the player sees it.
Screens
Each screen has a name, which is its tag without -screen:
| Name | Element | What it is |
|---|---|---|
main |
<main-screen> |
The main menu. |
game |
<game-screen> |
The game: scene, characters, text box, quick menu. |
load |
<load-screen> |
The saves the player can load. |
save |
<save-screen> |
Where the player saves. |
settings |
<settings-screen> |
The player’s preferences. |
help |
<help-screen> |
The keys and what they do. |
gallery |
<gallery-screen> |
The image gallery. |
credits |
<credits-screen> |
The credits. |
language-selection |
<language-selection-screen> |
The first screen of a game in several languages. |
loading |
<loading-screen> |
The progress bar while the assets load. |
The element carries the name in its data-screen attribute, and the open one has the active class.
showScreen()
Opens a screen and closes the others.
showScreen(screen: string): voidIt also stops auto-play and skip mode. When no element in the page has data-screen with that name, nothing changes: the current screen stays open, and with the debug module loaded the engine:screen:not_found dialog names the screen. Opening the save screen while the game plays, with the Screenshots setting on, first takes the picture of the game screen that the save will use (see Save screenshots), so the screen appears a moment later.
monogatari.showScreen('settings');Opening game shows the game screen, but it doesn’t start a game: use the Start button, or loadFromSlot().
hideScreens()
Closes every screen, leaving none open.
hideScreens(): voidgoBack()
What the back button of a menu screen does: closes the screen and returns to the game when a game is being played (or the splash screen plays), or to the main menu otherwise.
goBack(event: Event, selector: string): voidThe engine calls it for every element with data-action="back" inside a screen other than the game screen. selector is the one init() received. On the game screen, data-action="back" means going back one statement instead.
showMainScreen() and showSplashScreen() are on Setting Up and Starting.
Dialogs
alert()
Shows a dialog over the game, with a message and buttons.
alert(id: string, options: object): void| Option | Type | What it is |
|---|---|---|
message |
string |
The message. Text shows as written; the name of an interface string shows that string in the player’s language (see Translations). |
context |
string |
A second line under the message, as it is. Optional. |
editable |
boolean |
true shows context in a text field the player can change. false by default. |
actions |
array |
The buttons. Each one is { label, listener }: label is its text, or the name of an interface string, and listener the name of the listener it runs. |
id names the dialog, for dismissAlert(). The dialog is an <alert-modal> element, added at the start of <visual-novel>. While it’s open, the game doesn’t advance. Called before the game has started (from js/options.js or js/script.js), alert() shows the dialog as soon as the game sets up its interface; before 2.9.0 it threw setProps is not a function.
The message and the labels can be plain text. In a game with several languages, add them to the interface strings and give their names instead, so each player reads them in their own language. This asks the player before going to the next chapter:
monogatari.translation('English', {
'SkipChapter': 'Skip to the next chapter?',
});
monogatari.registerListener('skip-chapter', {
callback: () => {
monogatari.dismissAlert('skip-chapter');
monogatari.run('jump Chapter2');
},
});
monogatari.alert('skip-chapter', {
message: 'SkipChapter',
actions: [
{ label: 'Skip', listener: 'skip-chapter' },
{ label: 'Cancel', listener: 'dismiss-alert' },
],
});
Skip and Cancel already exist in the engine’s English strings, and dismiss-alert is a built-in listener that closes the dialog its button is in. A message or label that isn’t the name of an interface string shows as written (before 2.9.0 it showed undefined).
dismissAlert()
Closes a dialog opened with alert().
dismissAlert(id?: string | null): voidWith an id, it closes that dialog; without one, every dialog alert() opened.
The game’s elements
element()
Returns the <visual-novel> element.
element(): DOM
element(pure: true, handled?: boolean): HTMLElement | nullWithout arguments it returns the element wrapped in an Artemis DOM object, which has find(), on(), addClass() and the like. With true, it returns the plain element.
If the element isn’t in the page yet (your code runs before the page has loaded), it opens the error dialog engine:element:not_ready; pass true as the second argument to skip the dialog, and check for null yourself.
monogatari.element().find('[data-component="text-box"]').addClass('wide');parentElement()
Returns the element init() was given (#monogatari), wrapped in an Artemis DOM object.
parentElement(): DOMwidth() and height()
Return the width and the height of the <visual-novel> element, in pixels.
width(): number
height(): numberresize()
What the ForceAspectRatio setting uses: sizes every element with the forceAspectRatio class to the largest box of the given proportions that fits in the parent element, centred vertically.
resize(element: unknown, proportionWidth: number, proportionHeight: number): voidThe first argument is ignored. The engine calls it when the game starts and whenever the window changes size.
Buttons and keys
The engine connects buttons to code by name. A button names a listener in its data-action attribute; the engine runs the listener registered under that name when the player clicks it:
<button data-action="open-screen" data-open="settings">Settings</button>This button opens the settings screen, through the built-in open-screen listener.
registerListener()
Registers a listener: code that runs when the player clicks an element whose data-action names it, or presses its keys.
registerListener(name: string, listener: { keys?: string | string[]; callback: function }, replace?: boolean): void| Parameter | Type | What it does |
|---|---|---|
name |
string |
The name buttons use in data-action. |
listener.callback |
function |
What runs. It gets the click or key event and the element (an Artemis DOM object), and runs with the engine as this (unless it’s an arrow function). |
listener.keys |
string or string[] |
Optional. The keys that run it too, as keyboard shortcuts. |
replace |
boolean |
true replaces the listener registered under that name. false by default: a second listener with the same name runs too. |
monogatari.registerListener('show-points', {
keys: 'p',
callback: () => {
console.log('Points:', monogatari.storage('points'));
},
});A button with data-action="show-points", or the P key, prints the points on the console.
The button has to be inside <visual-novel>: the engine listens for clicks there, and a button outside it does nothing.
A click on a button with a data-action doesn’t advance the game, even in the game screen, where a click anywhere else moves the story on. That includes clicks on whatever is inside the button, like its icon. To advance from your button, call proceed() in the callback.
What the callback returns decides whether the engine stops the click itself. Nothing (or another value that isn’t truthy) stops it from reaching anything else on the page; a truthy value such as true lets it go on, which is what the engine’s own set-volume listener does so the volume sliders keep working.
NOTE
Before 2.9.0, a click on a data-action button in the game screen also advanced the game when the callback returned a truthy value, and sometimes when it was clicked from code (element.click()).
Register listeners before init() (at the top of js/main.js, say), or after: one registered after init() gets its keys bound at once. Replacing a listener with replace doesn’t bind its keys again; use keyboardShortcut() for that.
The listeners the engine registers:
| Name | What it does | Keys |
|---|---|---|
start |
Starts a new game: the main menu’s Start. | |
open-screen |
Opens the screen named by the element’s data-open. |
|
back |
Goes back one statement. Registered only when AllowRollback is on as the game starts. |
← |
distraction-free |
Turns distraction-free mode on and off. | H |
auto-play |
Turns auto-play on and off. | |
skip |
Turns skip mode on and off. | S |
end |
Asks the player whether to quit the game. | Shift+Q |
quit |
Ends the game (the Quit button of that dialog). | |
dismiss-alert |
Closes the dialog its button is in. Run from code, with no button, it closes every dialog alert() opened. (Before 2.9.0 it always closed every dialog.) |
|
jump |
Jumps to the label named by the element’s data-jump: <button data-action="jump" data-jump="Extra">. |
|
dialog-log |
Opens and closes the dialog log. | |
save |
Saves to a new slot, named with what the save screen’s text field holds. | |
delete-slot, overwrite-slot |
The buttons of the dialogs that delete and overwrite a slot. | |
set-volume |
Sets the volume of the music, sound, voice or video, from the element’s data-target and value. |
Besides these, the engine binds → and Space to proceed(), Esc to open and close the settings while playing, Shift+S to the save screen and Shift+L to the load screen.
unregisterListener()
Removes every listener registered under a name, and unbinds its keys.
unregisterListener(name: string): voidThe name is matched without regard to case.
runListener()
Runs every listener registered under a name, as a click would.
runListener(name: string, event?: Event | null, element?: DOM | null): Promise<void>The callbacks get the event and element you pass, or null. A callback that reads them has to handle null when you call it from code.
monogatari.runListener('dialog-log');The dialog log opens.
keyboardShortcut()
Runs a function when the player presses a key or a combination.
keyboardShortcut(shortcut: string | string[], callback: (event: KeyboardEvent, element: DOM) => void): voidThe engine uses the Mousetrap library: name keys as 'k', 'shift+k', 'left', 'space', 'esc', 'enter', and give an array for several. The callback runs with the engine as this and gets the key event and the focused element. It doesn’t run while the player types in a text field, and the key’s default action (scrolling, for example) is prevented.
monogatari.keyboardShortcut('m', () => {
monogatari.showScreen('settings');
});Binding a key again replaces what it did, including a built-in shortcut.