Use viewer to change how your project appears on screen, inspector to adjust values while it runs, and ui to add menus and controls to a scene.
Read WIDTH and HEIGHT when positioning your drawings. These values can change when the device rotates or the window is resized. Leave space around screen edges so controls stay clear of rounded corners and system gestures.
Use the inspector to try different colors, sizes, or speeds without restarting the project. Add objects when you create them; changes in the inspector update the same objects your drawing code uses.
See Viewer for display settings, User Interface for scene controls, and Inspector for inspecting objects. Use Device and Hinge to read device information and the fold angle on supported devices.
Use WIDTH and HEIGHT for the current drawing area. Read these values when drawing or handling a resize; the window can change size without changing device orientation. Safe-area insets identify the space occupied by system UI, rounded corners, or other obstructions. Draw backgrounds edge to edge, but keep interactive controls inside the safe area.
function draw()
background(30)
local inset = layout.safeArea
shapeMode(CORNER)
fill(255)
text("Score: 120", inset.left + 16, inset.bottom + 16)
end
layout.safeArea
Read-only insets in points: left, right, top, and bottom. Read them again after the viewer changes size.
layout.horizontal
Horizontal size class: layout.COMPACT, layout.REGULAR, or layout.UNSPECIFIED.
layout.vertical
Vertical size class, using the same constants as horizontal.
layout.orientation
Current interface orientation, or nil when unknown. Compare with the orientation constants; use viewer dimensions for layout decisions.
layout.COMPACT
Compact size class.
layout.REGULAR
Regular size class.
layout.UNSPECIFIED
Unspecified size class.
layout.portrait
Upright portrait orientation.
layout.portraitUpsideDown
Upside-down portrait orientation.
layout.landscapeLeft
Landscape-left interface orientation.
layout.landscapeRight
Landscape-right interface orientation.
Control the window where your project runs, including its display mode and layout.
viewer.mode = STANDARD
viewer.mode = FULLSCREEN
viewer.mode = FULLSCREEN_NO_BUTTONS
Changes the display mode of the viewer. Use this to render your games and simulations in fullscreen mode, fullscreen mode without buttons, or standard mode. Standard mode includes the sidebar with output and parameters, plus controls for project execution.
viewer.framerate = 30
Sets the preferred framerate of the viewer. You can set this to 0, 15, 30, 60 or 120. The value 0 uses the maximum framerate of your device.
Note that this sets the preferred framerate. If the framerate cannot be maintained, it may drop below your preferred setting to the next lower value.
viewer.pointerLocked = true
Setting this property to true indicates the renderer’s preference to lock the pointer, although the system may not honor the request. For the system to consider locking the pointer, the viewer must be running fullscreen on your device.
if viewer.runtime == viewer.MODERN then
strokeWidth(5)
end
The active runtime type, either LEGACY or MODERN.
print(viewer.safeArea.bottom)
A table specifying the current safe area insets of the viewer. Use these values to avoid rendering important visible or interactive content under system interface areas.
viewer.uniformResizing
Controls whether the viewer preserves a uniform resizing behavior when the view changes size. This is only supported on platforms where the viewer is resizable.
viewer.resize(800, 600)
Resizes the viewer to the specified width and height. This is only supported on platforms where the viewer is resizable.
| width | number. The new viewer width. |
| height | number. The new viewer height. |
viewer.paused
A boolean value that indicates whether the viewer is paused.
viewer.displayStats
Controls whether runtime statistics are displayed in the viewer.
viewer.drawOnRequest
Controls whether the viewer draws only when a redraw is requested.
viewer.showWarnings
Determines whether warnings should be displayed in the viewer. For example, warnings will be printed when using deprecated Codea APIs.
viewer.close()
Closes the viewer and returns to the editor. Calling viewer.close() is functionally the same as pressing the on-screen Back button.
viewer.restart()
Restarts the viewer, starting your project again. Calling viewer.restart() is functionally the same as pressing the on-screen Restart button.
local img = viewer.snapshot()
Captures the rendered contents of the viewer and returns them as an image. This captures the rendered scene and does not include the sidebar UI.
image |
The rendered viewer contents. |
viewer.alert("Hello World")
viewer.alert("Hello World", "Title")
Shows a system alert. The message parameter specifies the message to display. The optional title parameter provides the title of the alert. If no title is specified, "Alert" is used.
| message | string. Message to display. |
| title | string, optional. Alert title. |
if not viewer.isPresenting then
viewer.alert("Ready")
end
Returns whether the viewer is presenting an alert, share sheet, or another view that obscures the viewer.
STANDARD
Standard display mode. The output and parameters panes are visible, and the Back, Pause, Play and Reset buttons are shown.
FULLSCREEN
Fullscreen display mode. An exit fullscreen button remains visible.
FULLSCREEN_NO_BUTTONS
Fullscreen display mode with all buttons hidden. Use viewer.close() if your project needs an explicit way to leave the viewer.
Read hinge.angle to get the fold angle on supported devices, or define hingeChanged to respond as the device folds.
The angle is nil when no reading is available. Choose a fallback before using it in calculations, such as local angle = hinge.angle or 0.
local angle = hinge.angle
if angle ~= nil then
local radians = math.rad(angle)
end
The hinge angle, in degrees. The value may be nil if no hinge angle is available yet or the device is not supported.
function hingeChanged(current, previous)
if current == nil then
print("Hinge input unavailable")
elseif previous == nil then
print("Hinge angle:", current)
else
print("Hinge angle change:", current - previous)
end
end
Use this global function to receive changes to the hinge angle. Codea calls it before drawing with the new and previous angles in degrees.
previous is nil for the first reading and when readings resume after being unavailable. current is nil when hinge input becomes unavailable. The function is not called while both values are nil.
In draw-on-request mode, a hinge change causes Codea to draw a frame and call this function.
| current | number | nil. The new angle in degrees, or nil when unavailable. |
| previous | number | nil. The previous angle in degrees, or nil if none was available. |
Use device to read the model, operating system, and device name. For screen layout, use WIDTH and HEIGHT: a window’s size can change.
device.id
The identifierForVendor UUID string of the device.
device.name
The user provided name of the device.
device.systemName
The name of the operating system running on the device.
device.systemVersion
The version of the operating system running on the device.
device.model
The model of the device.
device.localizedModel
The localized model of the device.
device.batteryLevel
The battery level of the device, from 0.0 to 1.0. -1.0 if the battery state is unknown.
device.batteryState
The battery state of the device. One of “unknown”, “unplugged”, “charging”, “full”.
Use the inspector to change an object’s values while your project runs. Call inspector.set(object) to show it, or inspector.add(object) to add it alongside other objects. Changes take effect in your project immediately.
Use push and pop to move between inspector pages, and clear to remove the contents. Add objects once when creating or selecting them.
inspector.set(object)
Displays any viewable/editable properties and actions for object
| object | table or userdata. Object whose inspectable properties and actions are displayed. |
inspector.add(object)
Appends object’s data to the inspector
| object | table or userdata. Object whose inspectable properties and actions are displayed. |
inspector.push(object)
Pushes a new page onto the inspector stack and adds object to it
| object | table or userdata. Object whose inspectable properties and actions are displayed. |
inspector.pop()
inspector.clear()
Add menus, labels, and controls to a scene with ui.canvas. Use anchors and pivots to position them relative to their container, and set their size in canvas units.
ui.canvas
Provides a 2D layer for rendering UI elements within a scene
Canvas Entities
Child entities created with ui.canvas, are 2D elements that use an automatic layout system
Canvas children have additional properties and methods:
entity.size, vec2 - the 2D size of the entity in canvas unitsentity.pivot, vec2 - the centre privot point of the entityentity.anchorX, enum - the horizontal alignment of the entityentity.anchorY, enum - the vertical alignment of the entityentity:anchor(x, y) - shortcut for setting anchorX, anchorYAnchors
Canvas entities are laid out using an anchor and pivot system
For horizontal alignment use:
LEFT - Align relative to the left side of the parent entityRIGHT - Align relative to the right side of the parent entityCENTER - Align relative to the center of the parent entityFor vertical alignment use:
BOTTOM - Align relative to the bottom of the parent entityMODDLE - Align relative to the middle of the parent entityTOP - Align relative to the top of the parent entityBoth horizonal and vertical alignment can be stretched
STRETCH - Stretch the component relative to it’s parentscn = scene()
-- Create an entity to contain the button
ent = scn.canvas:child("button")
-- Adjust the button size
ent.size = vec2(300, 150)
-- Add a button component
btn = ent:add(ui.button)
btn.label.text = "Press Me!"
-- Add a callback function to react to button taps
function ent:tapped()
print("Tapped!")
end
ui.canvas.entity
The entity for this canvas
ui.canvas.scale
Scale factor for canvas drawing
ui.label
Draws text on a canvas entity
ui.label.text
The text to render on the label
ui.label.color
The label’s text color
ui.label.font
Optional system font name. Use a face such as "Helvetica-Bold" for a bold label. When omitted, the label uses the current style font.
ui.label.fontSize
The label’s font size
ui.label.align
The label’s text alignment
ui.label.textRenderer
The label’s renderer: style.native (the default) or style.atlas. Atlas labels interpret tags; native labels support color emoji.
ui.label.shadowOffset
The offset of the labels text shadow
ui.label.shadowRadius
The blur radius of the label’s text shadow, in points
ui.image
Draws an image on a canvas entity
ui.image.image
The image to draw
ui.button
Makes a canvas entity into an interactive button
ui.button.style
The button’s style. Background image and color tint can be set for each button state
btn.style =
{
normal = { sprite = ..., color = ... },
pressed = { sprite = ..., color = ... },
disabled = { sprite = ..., color = ... },
selected = { sprite = ..., color = ... }
}
ui.vstack
Lays out child entities in a vertical stack
ui.vstack.padding
The outer padding for the stack, used to create a border around the edge
ui.vstack.spacing
The spacing between each item in the stack
ui.hstack
Lays out child entities in a horizontal stack
ui.hstack.padding
The outer padding for the stack, used to create a border around the edge
ui.hstack.spacing
The spacing between each item in the stack