Log inGet started
Package

viewport.toolbox

Newest version

This package holds more than this page lists. See all of its files in its world.

README.md

viewport

The size the engine draws the game at — read it, and change it while the engine runs.

Everything a screen is laid out against comes from this one number pair: the pixels getViewportSize() reports, the logical points ui.screenSize() reports over them, each camera's aspect, and the raster a capture encodes. Until an engine can be told to draw at another size, a layout can be proven at exactly one shape per boot, and the second shape has to be read off a picture taken at the first.

  • size — what the engine is drawing the game at right now, in pixels and in the logical points the game's UI is laid out in, with the factor between them, the window around it and the largest surface this device will draw.
  • setSize: draw the game at another size for the rest of the call. Every reader of it follows within a frame, so the UI tree is rebuilt at the new shape rather than scaled from the old one; the call returns the size reading at that shape, and the size goes back to what it was when the call ends.
  • labels: where every world-space label (Text3D, a 3d UiPanel) lands on screen for a camera, in pixels: each label's rect and line height, the pairs that overlap, the ones a screen edge cuts or that read mirrored, and the ones that reach a HUD widget. Call it after setSize in the same call to check a layout of signs at a phone's shape.

The window, or the picture in a Scene panel

Where the scene fills the window (a runtime window, a browser canvas, a headless engine) setSize resizes the window's surface. In an editor layout a Scene panel presents the scene inside the window, and setSize leaves the window alone: the game's picture is drawn at the size asked, standing for the screen of a device that many pixels across. The panel shows it at that shape, scaled to fit, and the game's UI lays out over it the way a window of that size lays its UI out, so setSize(390, 844) shows what an upright 390x844 phone shows. ui.simulateSafeArea adds its notch and bars on that picture, and ui.simulateDisplayScale its density.

Why a resized capture is not this

capture { width, height } re-runs layout at the size it encodes, so a shot comes back looking like a genuine render of the other shape. It is not one. The widget tree was built one frame earlier from the live screen size, so every absolute number in it — font sizes, padding, a fixed maxWidth — was computed against the old short edge, and any branch the tree took on the live size (a one-column portrait layout against a two-column landscape one) is the branch the old size chose. setSize changes the size the tree is built from, which is the half a resized capture cannot reach.

Who honours the size

A headless engine and a browser canvas own the surface they draw into, so the size is honoured exactly; a browser page that lays its canvas out at a new size meanwhile ends the hold with its own size. Where an OS window owns it, the window manager is asked and has the last word: a tiled or maximized window keeps the size it has. Either way size afterwards reports what was realized. A size larger than the device draws is refused at the call, naming the bound.

Held for the call

A size is held for the call (the task) that asked for it. When that call ends, however it ends, the surface goes back to the size it had before, so the window or browser canvas a person is looking at is handed back as it was and no check leaves it at a shape nobody asked it to keep.

Two spaces, one picture

size reports the game's picture in pixels and in the logical points a HUD is positioned in, plus the ratio between them. The logical canvas is normalised to about 1280 points wide, one point per pixel below that, so a resize to a size of the SAME aspect above that width moves the ratio and leaves the logical shape where it was, while a resize that changes the aspect changes the logical shape too. Both are real resizes; the second is the one an orientation branch reacts to.

surfaceWidth/surfaceHeight is the whole window, where the editor's chrome lays out. sceneWidth/sceneHeight is the rect the 3D scene is drawn into: the whole surface in a runtime window, a browser canvas and a headless engine, and the Scene panel's picture in an editor layout.

Checking a layout at two shapes

In one execute, so the readings are taken while the size is held:

local viewport = require("@builtin::tools.viewport.setSize")
local portrait = viewport.setSize(1080, 1440)   -- waits until drawn there
local hud = ui.getLayoutInfo("hud-root")          -- measured at portrait
local landscape = viewport.setSize(1920, 1080)  -- same call, same tree
local hudWide = ui.getLayoutInfo("hud-root")
return { portrait = portrait, hud = hud, landscape = landscape, hudWide = hudWide }

setSize returns once the picture reports the size, with the reading size gives there. A capture taken in the same call encodes that shape. When the call returns the surface goes back to the size it had before the first setSize.