🧩 Canvas Engine UI Components

The UI system allows you to create buttons, text, images, progress bars and input fields directly on top of the game. UI elements are always drawn on a layer above the scene and can react to clicks, keys, or custom events. Even though UI elements are always visible above the scene, they are still managed inside the editor’s layer system.

πŸ”Ή All UI elements are treated the same as any game object. You can access them with getObjectByName() and modify their properties from your scripts.

πŸ“¦ Main UI Types

πŸ–±οΈ UIButton (Interactive Buttons)

UI buttons react to _onClickDown and _onClickUp events. You can use them to open menus, load scenes or execute any action.

Basic Example

function Start(obj) {
  const boton = getObjectByName("BtnIniciar");

  boton._onClickUp = () => {
    PlaySound("click.mp3");
    LoadSceneByName("MainScene");
  };
}

Modify appearance and state

boton.text = "Start Game";
boton.color = "#00bcd4";
boton.visible = true;
boton.active = true;

πŸ“ UILabel (On-screen Text)

UILabel elements show static or dynamic text. You can update them in real time from Update().

Example: score display

function Update(obj, dt) {
  const label = getObjectByName("ScoreText");
  label.text = "Score: " + currentScore;
}

Useful Properties

πŸ”€ UIInputField (Editable Text Field)

Allows the player to type text directly from the keyboard. When the user clicks the field, it gains focus (focused = true) and starts receiving input.

Usage Example

function Start(obj) {
  const campo = getObjectByName("InputNombre");
  campo.text = "Enter your name...";
  campo.color = "#ffffff";
  campo.focused = true;
}

Reading text in real time

function Update(obj, dt) {
  const campo = getObjectByName("InputNombre");
  if (campo.focused) {
    console.log("Typing:", campo.text);
  }
}

Input system events

Combined example: field + button

function Start(obj) {
  const campo = getObjectByName("InputNombre");
  const boton = getObjectByName("BtnAceptar");

  boton._onClickUp = () => {
    PlaySound("confirm.mp3");
    console.log("πŸ‘€ Entered name:", campo.text);
  };
}

πŸ–ΌοΈ UIImage (Interface Images)

Displays an image inside the HUD, ideal for icons, logos or decorative UI elements.

function Start(obj) {
  const icono = getObjectByName("IconVida");
  icono.visible = true;
  icono.w = 64;
  icono.h = 64;
  icono.x = 20;
  icono.y = 20;
}

πŸ“Š UIProgressBar (Progress Bar)

The UIProgressBar visually shows a value between 0 and 1, useful for health, energy, loading or download progress. You can customize its color, background, border and percentage text.

Example: health bar

function Update(obj, dt) {
  const vida = getObjectByName("HealthBar");
  vida.value = player.hp / player.maxHp;
}

Properties

Example with dynamic color and text

function Update(obj, dt) {
  const bar = getObjectByName("LoadingBar");
  bar.value += dt * 0.1;
  if (bar.value > 1) bar.value = 1;

  if (bar.value < 0.5) bar.color = "#ff4444";
  else bar.color = "#00cc44";
}

🎨 Common UI Properties

⚑ Visibility and Activation Control

You can enable or disable UI elements the same way as any other object:

const menu = getObjectByName("MainMenu");
menu.visible = false;   // Hide
menu.active = false;    // Disable interaction

πŸ’‘ Practical Tips

πŸ”Έ Summary:
- UI objects are controlled the same as any other engine object.
- Access them via getObjectByName().
- They can show text, images or receive keyboard input.
- _onClickDown and _onClickUp handle user interaction.
- UIInputField supports typing, deletion and automatic focus loss.
- UIProgressBar represents real-time values (health, loading, etc.).