Skip to content

Ui

const ui = require("@vectarine/ui")

This module provides the building blocks for creating user interfaces for your games. It is made for game interfaces and is thus very flexible to allow you to create playful and unique interfaces.

The core concept is a Widget, which is a rectangular area on the screen which has a size and can be drawn.

The main widget is the Generic widget obtained using the ui.widget function which is drawn using a function you provide.

You can compose widgets together using the ui.column, ui.row and ui.stack widgets.

Widgets are immutable: once they are created, you cannot change their content. Use the generic widget and its draw function to create dynamic widgets that can react to game state or user input. For example, you can wrap a column widget inside a generic widget to change its content by creating a new column widget and drawing it when changes are needed.

ui.widget<T>(size: vec.Vec2, drawFn: (event: EventState, extra: T) -> ()): Widget<T>

Create a generic widget with a fixed preferred size and a custom draw function. Can be used to create custom widgets or draw parts of the game in the UI. Can also be used for buttons, or to make part of the UI react to events

Parameters

  • size (vec.Vec2): Vec.Vec2 — the preferred size of this widget or a function that returns the preferred size
  • drawFn ((event: EventState, extra: T) -> ()): (event: EventState, extra: T) -> () — called to draw the widget content
ui.column<T>(options: ContainerOptions, children: { Widget<T> }): Widget<T>

Create a column (vertical layout) that stacks its children top to bottom. The column’s preferred size accounts for children sizes, gap between them, and padding. Children are drawn from top to bottom in the order provided.

Parameters

  • options (ContainerOptions): ContainerOptions — optional padding and gap
  • children ({ Widget<T> }): { Widget<T> } — ordered list of child widgets
ui.spacer<any>(size: vec.Vec2): Widget<any>

Create a spacer widget that takes up space but does not draw anything. This can be used to create gaps between widgets or to push widgets apart.

ui.text<T>(
text: string | ((event: EventState, extra: T) -> string),
options: TextOptions<T>,
): Widget<T>

Create a text widget that draws the given text. The size of the text is computed such that the text fits on a single line within the given size. The getText function is called each frame to get the text to draw and its color. You can use it to dynamically change the text without needing to wrap the text widget in a generic widget.

ui.image<T>(
size: vec.Vec2,
image: image.ImageResource,
options: ImageWidgetOptions<T>,
): Widget<T>

Create a widget that draws the given image. The image is scaled to fit the widget’s size

ui.row<T>(options: ContainerOptions, children: { Widget<T> }): Widget<T>

Create a row (horizontal layout) that places its children left to right. The row’s preferred size accounts for children sizes, gap between them, and padding. Children are drawn from left to right in the order provided.

Parameters

  • options (ContainerOptions): ContainerOptions — optional padding and gap
  • children ({ Widget<T> }): { Widget<T> } — ordered list of child widgets
ui.scrollableArea<T>(
viewSize: vec.Vec2,
content: Widget<T>,
drawScrollbar: ((info: ScrollbarInfo) -> number)?,
): Widget<T>

Create a scrollable area that shows a portion of its content and allows vertical scrolling to see the rest. If the content is smaller than the view size, the content is shown fully, centered and no scrollbar is shown. If the content is larger than the view size, mouse wheel scrolling is enabled. The optional drawScrollbar function is called each frame (only when scrolling is needed) to draw a custom scrollbar. It receives a ScrollbarInfo table with the current scroll state. Drawing coordinates are in the scrollable area’s local space.

Parameters

  • content (Widget<T>): Widget — the content widget to scroll
  • viewSize (vec.Vec2): Vec.Vec2 — the visible area size
  • drawScrollbar (((info: ScrollbarInfo) -> number)?): ((info: ScrollbarInfo) -> number)? — optional custom scrollbar drawing function that receives the current scroll state and returns the desired scrollbar ratio.
ui.stack<T>(options: StackOptions, content: { Widget<T> }): Widget<T>

Create a stack that places its children on top of each other. The size of the stack is the maximum size of its children. Children are drawn in the order provided, so the first child is on the bottom and the last child is on the top. This can be used to show text with a background image for example, or to draw text twice for a shadow effect.

Parameters

  • options (StackOptions): StackOptions — optional alignment of children within the stack
ui.slider<T>(
size: vec.Vec2,
options: SliderOptions,
track: Widget<T>,
handle: Widget<T>,
): Widget<T>

Create a slider widget that allows the user to select a value by horizontally dragging a handle along a track. You need to pass a track and handle widget to provide the appearance of the slider.

ui.tabs<T>(tabs: { [string]: Widget<T> }): TabWidget<T>

Create a tab widget that allows switching between different child widgets using setActiveTab. The tabs widgets provides built-in animations when switching tabs. The tabs widget has the size of the widget it is currently displaying. The tabs parameter is a table mapping tab names to widgets. By default, widget corresponding to the “” tabName is shown. If there is no “” tab, another tab is picked in an unspecified manner.

ui.withDebugOutline<T>(drawFn: () -> (), toggle: boolean?): ()

Debugging function that makes all widgets drawn inside to show a debugging outline. This can be used to visualize the layout of widgets and understand what is taking up space on the screen. You can set toggle to true / false to quickly enable or disable the outline.

export type Widget<T> = ...
size: (self: Widget<T>, extra: T) -> vec.Vec2

Returns the preferred size of this widget.

draw: (self: Widget<T>, extra: T) -> ()

Draws this widget at the current position in the batch.

eventState: (self: Widget<T>) -> EventState

Return the event state from the last time this widget was drawn. You can use this to react to events on the widget (e.g. mouse hover, clicks).

export type EventState = ...
isMouseInside: boolean

Whether the mouse is currently inside this widget’s area.

isMouseDown: boolean

Whether the left mouse button is currently down on this widget.

isMouseJustPressed: boolean

Whether the left mouse button was just pressed on this widget

isMouseJustReleased: boolean

Whether the left mouse button was just released on this widget

isMouseJustEntered: boolean

Whether the mouse just entered this widget’s area

isMouseJustExited: boolean

Whether the mouse just exited this widget’s area

export type Padding = ...
export type ContainerOptions = ...

Options for container widgets (column, row).

padding: (Padding | number)?

Padding around the container’s content. Can be a single number (uniform on all sides) or a table with optional top/bottom/left/right fields. Defaults to 0.

gap: number?

Distance between children. Defaults to 0.

align: ("start" | "center" | "end")?

Cross-axis alignment of children, similar to CSS align-items. For a column: aligns children horizontally (“start” = left, “center”, “end” = right). For a row: aligns children vertically (“start” = bottom, “center”, “end” = top). Defaults to “start”.

type TextOptions<T> = ...

Options for text widgets, allows you to customize the color, font, size and alignment of the text.

align: ("left" | "center" | "right")?

Horizontal alignment of the text within the widget. Defaults to “center”.

font: text.FontResource?

Optional font to use for this text. If not provided, the default font will be used.

color: (vec4.Vec4 | ((event: EventState, extra: T) -> vec4.Vec4))?

Optional color of the text. If not provided, the text will be black.

fontSize: number?

Optional font size of the text. If not provided, the 0.1 will be used.

type ImageWidgetOptions<T> = ...
export type ScrollbarInfo = ...

Information passed to the scrollbar draw function.

scrollRatio: number

Scroll progress from 0 (top) to 1 (bottom).

visibleRatio: number

Fraction of the content that is visible (0..1). Can be used to size the scrollbar thumb.

viewWidth: number

Width of the scrollable area’s view.

viewHeight: number

Height of the scrollable area’s view.

contentHeight: number

Total height of the content being scrolled.

type StackOptions = ...
type SliderOptions = ...
export type TabAnimation<T> = ...
export type TabWidget<T> = ...
setActiveTab: (self: TabWidget<T>, tabName: string, animation: TabAnimation<T>?) -> ()

Switch to the tab with the given name. The content of the tab will be drawn and its events will be processed.

getActiveTab: (self: TabWidget<T>) -> string

Get the currently active tab. During a transition, this will return the new tab name.