UI

May 30, 2024 ยท View on GitHub

from tdw.add_ons.ui import UI

Manager add-on for UI in TDW.

Parameter types

All parameters of type Dict[str, float] are Vector2, e.g. {"x": 0, "y": 0}. There is no "z" parameter.

"x" is the horizontal value and "y" is the vertical value.

In some cases, this document will note that Vector2 values must be integers. This is usually because they are adjusting a value that references the actual screen pixels.


Fields

  • commands These commands will be appended to the commands of the next communicate() call.

  • initialized If True, this module has been initialized.


Functions

__init__

UI()

UI(canvas_id=0)

ParameterTypeDefaultDescription
canvas_idint0The ID of the UI canvas.

get_initialization_commands

self.get_initialization_commands()

This function gets called exactly once per add-on. To re-initialize, set self.initialized = False.

Returns: A list of commands that will initialize this add-on.

on_send

self.on_send(resp)

This is called within Controller.communicate(commands) after commands are sent to the build and a response is received.

Use this function to send commands to the build on the next Controller.communicate(commands) call, given the resp response. Any commands in the self.commands list will be sent on the next Controller.communicate(commands) call.

ParameterTypeDefaultDescription
respList[bytes]The response from the build.

before_send

self.before_send(commands)

This is called within Controller.communicate(commands) before sending commands to the build. By default, this function doesn't do anything.

ParameterTypeDefaultDescription
commandsList[dict]The commands that are about to be sent to the build.

get_early_initialization_commands

self.get_early_initialization_commands()

This function gets called exactly once per add-on. To re-initialize, set self.initialized = False.

These commands are added to the list being sent on communicate() before any other commands, including those added by the user and by other add-ons.

Usually, you shouldn't override this function. It is useful for a small number of add-ons, such as loading screens, which should initialize before anything else.

Returns: A list of commands that will initialize this add-on.

add_text

self.add_text(text, font_size, position)

self.add_text(text, font_size, position, anchor=None, pivot=None, color=None, raycast_target=True)

Add UI text to the scene.

ParameterTypeDefaultDescription
textstrThe text.
font_sizeintThe size of the font.
positionDict[str, int]The screen (pixel) position as a Vector2. Values must be integers.
anchorDict[str, float]NoneThe anchor as a Vector2. Values are floats between 0 and 1. If None, defaults to {"x": 0.5, "y": 0.5}.
pivotDict[str, float]NoneThe pivot as a Vector2. Values are floats between 0 and 1. If None, defaults to {"x": 0.5, "y": 0.5}.
colorDict[str, float]NoneThe color of the text. If None, defaults to {"r": 1, "g": 1, "b": 1, "a": 1}.
raycast_targetboolTrueIf True, raycasts will hit the UI element.

Returns: The ID of the new UI element.

add_image

self.add_image(image, position, size)

self.add_image(image, position, size, rgba=True, scale_factor=None, anchor=None, pivot=None, color=None, raycast_target=True)

Add a UI image to the scene.

ParameterTypeDefaultDescription
imageUnion[str, Path, bytes, Image.Image]The image. If a string or Path, this is a filepath. If bytes, this is the image byte data. If Image.Image, this is a PIL image.
positionDict[str, int]The screen (pixel) position as a Vector2. Values must be integers.
sizeDict[str, int]The pixel size of the image as a Vector2. Values must be integers and must match the actual image size.
rgbaboolTrueIf True, this is an RGBA image. If False, this is an RGB image.
scale_factorDict[str, float]NoneScale the UI image by this factor. If None, defaults to {"x": 1, "y": 1}.
anchorDict[str, float]NoneThe anchor as a Vector2. Values are floats between 0 and 1. If None, defaults to {"x": 0.5, "y": 0.5}.
pivotDict[str, float]NoneThe pivot as a Vector2. Values are floats between 0 and 1. If None, defaults to {"x": 0.5, "y": 0.5}.
colorDict[str, float]NoneThe color of the text. If None, defaults to {"r": 1, "g": 1, "b": 1, "a": 1}.
raycast_targetboolTrueIf True, raycasts will hit the UI element.

Returns: The ID of the new UI element.

add_cutout

self.add_cutout(base_id, image, position, size)

self.add_cutout(base_id, image, position, size, scale_factor=None, anchor=None, pivot=None)

Add a UI image that cuts a transparent hole in another UI image.

ParameterTypeDefaultDescription
base_idintThe ID of the image that will have a hole in it. This can be added on the same frame as the cutout image but it must be added prior to the cutout image.
imageUnion[str, Path, bytes, Image.Image]The image. This must be an RGBA image. If a string or Path, this is a filepath. If bytes, this is the image byte data. If Image.Image, this is a PIL image.
positionDict[str, int]The screen (pixel) position as a Vector2. Values must be integers.
sizeDict[str, int]The pixel size of the image as a Vector2. Values must be integers and must match the actual image size.
scale_factorDict[str, float]NoneScale the UI image by this factor. If None, defaults to {"x": 1, "y": 1}.
anchorDict[str, float]NoneThe anchor as a Vector2. Values are floats between 0 and 1. If None, defaults to {"x": 0.5, "y": 0.5}.
pivotDict[str, float]NoneThe pivot as a Vector2. Values are floats between 0 and 1. If None, defaults to {"x": 0.5, "y": 0.5}.

Returns: The ID of the new UI element.

set_text

self.set_text(ui_id, text)

Set the text of a UI text element that is already in the scene.

ParameterTypeDefaultDescription
ui_idintThe ID of the UI text element.
textstrThe text.

attach_canvas_to_avatar

self.attach_canvas_to_avatar()

self.attach_canvas_to_avatar(avatar_id="a", focus_distance=2.5, plane_distance=0.101)

Attach the UI canvas to an avatar. This allows the UI to appear in image output data.

ParameterTypeDefaultDescription
avatar_idstr"a"The avatar ID.
focus_distancefloat2.5The focus distance. If the focus distance is less than the default value (2.5), the UI will appear blurry unless post-processing is disabled.
plane_distancefloat0.101The distance from the camera to the UI canvas. This should be slightly further than the near clipping plane.

attach_canvas_to_vr_rig

self.attach_canvas_to_vr_rig()

self.attach_canvas_to_vr_rig(plane_distance=0.25)

Attach the UI canvas to a VR rig.

ParameterTypeDefaultDescription
plane_distancefloat0.25The distance from the camera to the UI canvas.

set_position

self.set_position(ui_id, position)

Set the position of a UI element.

ParameterTypeDefaultDescription
ui_idintThe UI element's ID.
positionDict[str, float]The screen (pixel) position as a Vector2. Values must be integers.

set_size

self.set_size(ui_id, size)

Set the size of a UI element that is already in the scene.

ParameterTypeDefaultDescription
ui_idintThe ID of the UI element.
sizeDict[str, float]The size.

set_rotation

self.set_rotation(ui_id, angle)

Rotate a UI element to an angle.

ParameterTypeDefaultDescription
ui_idintThe ID of the UI element.
anglefloatThe new rotation angle in degrees.

set_depth

self.set_depth(ui_id, depth)

Set the depth (z value) of a UI element relative its canvas (not its camera).

If the canvas is attached to an avatar or VR rig, the canvas depth relative to the camera is the plane_distance.

ParameterTypeDefaultDescription
ui_idintThe UI element's ID.
depthfloatThe depth (z value) in meters.

destroy

self.destroy(ui_id)

Destroy a UI element.

ParameterTypeDefaultDescription
ui_idintThe ID of the UI element.

destroy_all

self.destroy_all()

self.destroy_all(destroy_canvas=False)

Destroy all UI elements.

ParameterTypeDefaultDescription
destroy_canvasboolFalseIf True, destroy the UI canvas and all of its UI elements. If False, destroy the canvas' UI elements but not the canvas itself.