Ui
const ui = require("@vectarine/ui")UI module
Section titled “UI module”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.
Functions
Section titled “Functions”ui.widget
Section titled “ui.widget”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 sizedrawFn((event: EventState, extra: T) -> ()): (event: EventState, extra: T) -> () — called to draw the widget content
ui.column
Section titled “ui.column”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 gapchildren({ Widget<T> }): { Widget<T> } — ordered list of child widgets
ui.spacer
Section titled “ui.spacer”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
Section titled “ui.text”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
Section titled “ui.image”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
Section titled “ui.row”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 gapchildren({ Widget<T> }): { Widget<T> } — ordered list of child widgets
ui.scrollableArea
Section titled “ui.scrollableArea”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 scrollviewSize(vec.Vec2): Vec.Vec2 — the visible area sizedrawScrollbar(((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
Section titled “ui.stack”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
Section titled “ui.slider”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
Section titled “ui.tabs”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
Section titled “ui.withDebugOutline”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.
Widget
Section titled “Widget”export type Widget<T> = ...Widget.size
Section titled “Widget.size”size: (self: Widget<T>, extra: T) -> vec.Vec2Returns the preferred size of this widget.
Widget.draw
Section titled “Widget.draw”draw: (self: Widget<T>, extra: T) -> ()Draws this widget at the current position in the batch.
Widget.eventState
Section titled “Widget.eventState”eventState: (self: Widget<T>) -> EventStateReturn 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).
EventState
Section titled “EventState”export type EventState = ...EventState.isMouseInside
Section titled “EventState.isMouseInside”isMouseInside: booleanWhether the mouse is currently inside this widget’s area.
EventState.isMouseDown
Section titled “EventState.isMouseDown”isMouseDown: booleanWhether the left mouse button is currently down on this widget.
EventState.isMouseJustPressed
Section titled “EventState.isMouseJustPressed”isMouseJustPressed: booleanWhether the left mouse button was just pressed on this widget
EventState.isMouseJustReleased
Section titled “EventState.isMouseJustReleased”isMouseJustReleased: booleanWhether the left mouse button was just released on this widget
EventState.isMouseJustEntered
Section titled “EventState.isMouseJustEntered”isMouseJustEntered: booleanWhether the mouse just entered this widget’s area
EventState.isMouseJustExited
Section titled “EventState.isMouseJustExited”isMouseJustExited: booleanWhether the mouse just exited this widget’s area
Padding
Section titled “Padding”export type Padding = ...ContainerOptions
Section titled “ContainerOptions”export type ContainerOptions = ...Options for container widgets (column, row).
ContainerOptions.padding
Section titled “ContainerOptions.padding”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.
ContainerOptions.gap
Section titled “ContainerOptions.gap”gap: number?Distance between children. Defaults to 0.
ContainerOptions.align
Section titled “ContainerOptions.align”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”.
TextOptions
Section titled “TextOptions”type TextOptions<T> = ...Options for text widgets, allows you to customize the color, font, size and alignment of the text.
TextOptions.align
Section titled “TextOptions.align”align: ("left" | "center" | "right")?Horizontal alignment of the text within the widget. Defaults to “center”.
TextOptions.font
Section titled “TextOptions.font”font: text.FontResource?Optional font to use for this text. If not provided, the default font will be used.
TextOptions.color
Section titled “TextOptions.color”color: (vec4.Vec4 | ((event: EventState, extra: T) -> vec4.Vec4))?Optional color of the text. If not provided, the text will be black.
TextOptions.fontSize
Section titled “TextOptions.fontSize”fontSize: number?Optional font size of the text. If not provided, the 0.1 will be used.
ImageWidgetOptions
Section titled “ImageWidgetOptions”type ImageWidgetOptions<T> = ...ScrollbarInfo
Section titled “ScrollbarInfo”export type ScrollbarInfo = ...Information passed to the scrollbar draw function.
ScrollbarInfo.scrollRatio
Section titled “ScrollbarInfo.scrollRatio”scrollRatio: numberScroll progress from 0 (top) to 1 (bottom).
ScrollbarInfo.visibleRatio
Section titled “ScrollbarInfo.visibleRatio”visibleRatio: numberFraction of the content that is visible (0..1). Can be used to size the scrollbar thumb.
ScrollbarInfo.viewWidth
Section titled “ScrollbarInfo.viewWidth”viewWidth: numberWidth of the scrollable area’s view.
ScrollbarInfo.viewHeight
Section titled “ScrollbarInfo.viewHeight”viewHeight: numberHeight of the scrollable area’s view.
ScrollbarInfo.contentHeight
Section titled “ScrollbarInfo.contentHeight”contentHeight: numberTotal height of the content being scrolled.
StackOptions
Section titled “StackOptions”type StackOptions = ...SliderOptions
Section titled “SliderOptions”type SliderOptions = ...TabAnimation
Section titled “TabAnimation”export type TabAnimation<T> = ...TabWidget
Section titled “TabWidget”export type TabWidget<T> = ...TabWidget.setActiveTab
Section titled “TabWidget.setActiveTab”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.
TabWidget.getActiveTab
Section titled “TabWidget.getActiveTab”getActiveTab: (self: TabWidget<T>) -> stringGet the currently active tab. During a transition, this will return the new tab name.
