Docs / Plugins / Writing plugins
Panels
omsi.ui puts the plugin's own panels on the screen, in the game's look: the Inter font, the game's icons, dark rounded cards with a shadow. They scale with the interface size setting and the window height.
A plugin describes a panel as a table and sets it again when its content changes, for example every half second. Setting the same table again changes nothing. omsi.ui.update(panel, element, {...}) changes one element in place, which is cheaper. The game lays out and redraws a panel only when it changed.
-- plugins/trip_panel.lua: the next stop, the speed and a button
local function show()
local i = omsi.info()
omsi.ui.set("trip", {
anchor = "top_left", x = 16, y = 60, width = 300, accent = "#F47F30",
children = {
{ type = "row", children = {
{ type = "icon", name = "directions_bus", color = "#F47F30" },
{ type = "text", text = "Line " .. (i.line or "-"), weight = "bold", grow = true },
{ type = "badge", text = string.format("%.0f km/h", i.speed or 0) },
} },
{ type = "text", text = "Next stop: " .. (i.next_stop or "-") },
{ type = "button", id = "horn", text = "Horn", icon = "campaign" },
},
})
end
omsi.every(0.5, show)
omsi.on("key", function(key, down)
if key == "F10" and down then omsi.ui.focus(not omsi.ui.focused()) end
end)
omsi.on("ui_click", function(panel, element)
if element == "horn" then omsi.trigger("horn") end
end)F10 gives the panels the mouse, and a click on the button sounds the horn. The full example is trip_panel.lua.
Panels belong to their plugin. One plugin cannot change or remove another's. When the plugin stops or reloads, its panels go (its notifications run out their time). Panels show over the picture and the navigator, under the game's own text, menus and windows. They are hidden while the game menu, one of its lists or the city map is open, and in VR.
Panel texts are shown as they are. They are not translated into the game's language.
The mouse#
Panels take clicks only while they have the mouse. omsi.ui.focus(true) gives it to them: the cursor shows, and a click goes to the panel under it, not to the bus. omsi.ui.focus(false), Esc or a game menu gives it back. The ui_focus event reports both.
A panel with grab = true takes a left click even while the panels do not have the mouse. Its buttons, boxes, sliders and tabs answer, and the cab behind gets nothing. Such a click does not start typing in a text field or drag the panel.
It is meant for small cards: a panel that covers more than a quarter of the screen, or whose background is less than half opaque, does not grab clicks.
The panel table#
omsi.ui.set("trip", {
anchor = "top_left", -- see below
x = 16, y = 16, -- offset from the anchor towards the middle
width = 340, -- the height follows the content
padding = 12, -- default 12
gap = 6, -- space between children, default 6
background = "#14161ACC", -- default: the game's card colour
radius = 12, -- corner radius, default 12
accent = "#F47F30", -- a stripe along the left edge
visible = true, -- false hides it and keeps it
clickable = false, -- true: a click anywhere is ui_click(id, nil)
draggable = false, -- true: the player can move it while panels have the mouse
grab = false, -- true: takes clicks without the mouse (ui.version 5)
frame = false, -- with anchor "navigator": the whole frame (ui.version 6)
children = { ... }, -- elements, top to bottom
})| Key | Values |
|---|---|
| anchor | top_left, top, top_right, left, center, right, bottom_left, bottom, bottom_right, navigator |
| x, y | from a corner or edge: towards the middle. From a middle: right and down |
| colours | "#RRGGBB" or "#RRGGBBAA" |
Sizes are pixels of a 1080p screen at the normal interface size. A panel always stays on the screen. One the player dragged keeps its new place when it is set again; omsi.ui.moved(id) says how far it was moved.
Elements#
Every element can have an id, a color, and visible = false (left out, no room kept). Elements marked (4) need omsi.ui.version 4 or later.
type |
Draws |
|---|---|
text |
a line or paragraph, wrapped to its width |
| one of the game's icons | |
row |
its children side by side |
| a progress bar, or a bar of coloured segments | |
badge |
a small rounded label |
box (4) |
its children in a tinted box with a stripe on the left |
stops (4) |
stops on a rail, one row each |
divider |
a thin line |
map (6) |
the navigator's map, in a panel that fills the navigator |
space |
empty room |
| a button | |
image |
a picture from the plugin's folder |
checkbox |
a box ticked by a click |
| a track with a knob | |
input |
a text field |
tabs |
a row of tabs |
chart |
a line or bar chart |
table |
columns of text with a bold header |
The keys of each element:
text:text;size(default 14);weight(regular,medium,bold);align(left,center,right);wrap(defaulttrue;falsekeeps one line and cuts it with "…"). A line is 1.3 ×sizehigh.icon:name, a Material Symbols name such asdirections_bus,schedule,payments,warning,star,emoji_events(every icon is inassets/icons/material);size(default 20). An unknown name draws nothing.row:children;gap(default 8);align(start,center,end,between). Children are centred on each other. A child withgrow = truetakes the width the others leave; several share it. When the children do not fit, texts, labels and buttons give up width alike.bar:value(0 to 1);height(default 6);color(the filled part, default amber);background. Or (4)segments: up to 16 parts side by side,{ { value = 3, color = "#34B26E" }, ... }, each its share of the sum. In a row a bar is 60 wide unless it grows.badge:text;color(its fill, default amber);text_color;weight(4; defaultmedium). It is 20 high.box(4):children;gap(default 4);color(default amber). For a note or a piece of advice.stops(4):stops, up to 128 of{ name, time, expected, expected_color, state, tag };size(13);color(the rail, default amber).stateis"done"(grey),"now"(marked, bold) or"ahead"(default).timeis on the right,expectedbefore it inexpected_color,tag(for example"terminus") before them.divider:color. In a row it is upright.map(6):height(default: the navigator map's, 0.8 × the width);cards(defaulttrue);overlay(8: cards of the plugin over the map). See In the navigator.space:size(default 8). In a row it is across.button:id(required);text;icon;color. 34 high and as wide as the panel; in a row as wide as its label or what it grows to. A click isui_click(panel, id).image:src(a PNG, JPEG, BMP or TGA in the plugin's folder);width;height(default 64). Shrunk to fit its width.checkbox:id(required);text;checked. A click sendsui_change(panel, id, true/false).slider:id(required);value;min(0);max(1);step(0: any). It follows the mouse while the button is held and sendsui_change(panel, id, number).input:id(required);text;placeholder;max(characters, default 100). Clicked, it takes typing, and the keys do not reach the bus. Every key sendsui_change; Enter is aui_clickon it; Esc or a click elsewhere leaves it.tabs:id(required);tabs(a list of texts);selected(from 1). Choosing one sendsui_change(panel, id, number).chart:values(up to 512 numbers);min,max(default: from the values);height(48);style(lineorbars);fill(fills below the line).table:columns(header texts);rows(a list of lists of texts; (4) a cell can be{ text = "+3:12", color = "#FF5F57" });widths(shares of the width);size(13);aligns(4: one per column,"left","right", ...).
Any other element with an id and clickable = true (a whole row, say) is clicked like a button.
Unknown keys are ignored and an element of an unknown type is left out, so a plugin written for a newer openOMSI still shows its panels. A key of the wrong kind makes ui.set return false and the reason, such as children[2].size: a number is expected.
Limits per plugin: 16 panels; 200 elements per panel (counting those in rows, boxes and map cards, and each card); 500 characters per text; 64 per id; rows and boxes 8 levels deep; 512 chart values or table cells; 8 notifications at a time.
Panel versions#
omsi.ui.version says which panel features the game has. Test it in a plugin that should also run on an older openOMSI: if omsi.ui and omsi.ui.version >= 2 then.
| Version | Added |
|---|---|
| 1 | panels |
| 2 | controls: buttons, checkboxes, sliders, text fields, tabs |
| 3 | the navigator's board (anchor = "navigator") |
| 4 | boxes, stop lists and the other elements and keys marked (4) |
| 5 | grab |
| 6 | frame (a panel in the whole navigator) and the map element |
| 7 | interface themes |
| 8 | cards over the map (overlay) |
In the navigator#
A panel with anchor = "navigator" goes into the navigator's board under its map, in place of the list of next stops. It is as wide as the navigator, at its scale, and sits on the navigator's card: no card, shadow, accent or rounded corners of its own, though background still paints it. x, y, width and draggable are ignored. The navigator grows as tall as the panel needs, as far as the window has room.
Shift+N cycles the navigator through map, map and board, and off. The first time a plugin asks for the board, the navigator opens it. Only one plugin's panel is in the board at a time; another waits until it goes.
if (omsi.ui.version or 0) >= 3 then
omsi.ui.set("board", { anchor = "navigator", padding = 10, children = { ... } })
else
omsi.ui.set("board", { anchor = "left", x = 16, width = 340, children = { ... } })
endomsi.ui.dock() returns the board's width, the most height there is room for, the height the panel came to, and whether the panel in the board is this plugin's. A panel taller than the room is cut off at the bottom, and the cut-off part takes no clicks. A plugin can leave out rows that do not fit.
The whole navigator frame#
With frame = true the panel fills the navigator's whole frame instead: from its top, as wide as the navigator, as tall as the panel. It sits on the navigator's rounded card. A background is painted as that card, with its corners and the navigator's opacity setting.
The map, the place row and the stop list make way. The map shows only where the panel has a map element, with its own rounded corners, and with the navigator's cards over it (the next turn, the clock, the speed, the next stop) unless the element says cards = false.
The navigator stays the navigator: it is moved and sized as before, and Shift+N shows and hides it. A press on the map, or on a part of the panel that takes no click, is the navigator's: a drag moves it, a click opens the city map. A grab panel here takes clicks without the mouse only on its buttons and other clickable parts.
omsi.ui.dock() then describes the whole frame and returns true as a fifth value. A game that does not know frame lays the panel out under the map, leaves out the map element and returns no fifth value, so a plugin can test for that.
omsi.ui.set("phone", { anchor = "navigator", frame = true, grab = true, padding = 10, children = {
{ type = "text", text = "Line 5 → Hauptbahnhof", weight = "bold" },
{ type = "map", height = 200, cards = false },
{ type = "button", id = "next", text = "Next stop: Markt 09:14" },
} })Cards over the map#
With overlay the map element carries the plugin's own cards over the map, instead of or beside the navigator's. overlay is a list of small panels. Each card takes these keys:
| Key | Default |
|---|---|
| place | top_left (any anchor except navigator) |
| x, y | 8: distance from the map's edges |
| width | what the content needs, never more than the map less x on both sides |
| padding | 10 |
| gap | 6 |
| background | the game's card colour |
| radius | 10 |
| children | any elements |
Unknown keys are ignored. A card is held inside the map: never wider than it, never above its top. What still crosses the map's edge (the bottom of a card taller than the map) is cut: the background stops at the edge with rounded corners, and a text, icon or picture that does not fully fit is not drawn.
Card buttons take clicks as in a panel; a press on the map beside them is still the navigator's.
A map element outside a whole-frame panel is empty room, and its cards are laid out in that room. An older openOMSI ignores overlay and shows the map without cards. navigator_map.lua is a complete example.
omsi.ui.set("phone", { anchor = "navigator", frame = true, padding = 10, children = {
{ type = "map", height = 360, cards = false, overlay = {
{ place = "top_left", width = 220, children = { { type = "text", text = "Line 5 → Hauptbahnhof", weight = "bold" } } },
{ place = "bottom_right", background = "#14161ACC", children = { { type = "text", text = "48 km/h" } } },
} },
} })Notifications and messages#
omsi.message(text, seconds)shows a line of text on the screen.omsi.ui.toast(text, { title, icon, color, seconds })shows a notification card at the top right. A plugin shows 8 at most; a ninth pushes out its oldest.omsi.game.notify(text, kind, seconds)shows one of the game's own notification cards,kindbeing"info","warning"or"alert".
A line in the pause menu#
A plugin can put up to 3 lines of its own in the pause menu (Esc), in a group before the save lines. Choosing one closes the menu and sends the plugin menu_item:
omsi.ui.set_menu_item("board", "Duty board", "departure_board")
omsi.on("menu_item", function(id)
if id == "board" then omsi.ui.toggle("board") end
end)The label (at most 40 characters) is shown as given, so write it in the player's language. The icon is a Material Symbols name; without one, or with an unknown one, the line gets the plugins' icon. The lines go when the plugin stops; omsi.ui.remove_menu_item takes one out earlier.
An interface theme#
A plugin can bring a look for the game's own screens: menu colours, frosted-glass panels over the picture, a loading screen with the plugin's logo (which can itself be the loading bar). A theme.json in the plugin's folder makes it a theme with the plugin's name; see Interface themes.
The player chooses it in Options › Interface. The plugin may switch to it with omsi.ui.set_theme, but should ask the player and do it only once:
if omsi.ui.set_theme and not omsi.data.theme_offered then
omsi.data.theme_offered = true
if omsi.ui.theme() == "" then omsi.ui.set_theme("myplugin") end
endFrom then on the choice is the player's setting, kept for the next start. omsi.ui.themes() lists the themes there are.
Something wrong or missing? Fix it on GitHub or open an issue.