Skip to content

Class: BaseUIElement (Base Class)

Source: src/engine/ui/BaseUIElement.js

The BaseUIElement class serves as the fundamental building block for all user interface components within the Ironclad Engine. Concrete UI elements like buttons, labels, panels, and sliders should extend this class. It provides common properties for positioning, sizing, visibility, and interactivity, as well as a basic structure for updates, rendering, and handling click events.


Constructor

new BaseUIElement(options?)

Creates a new BaseUIElement instance.

Parameters:

  • options?: object - An optional configuration object for the UI element.
    • options.x?: number - The initial x-coordinate of the element. (Default: 0)
    • options.y?: number - The initial y-coordinate of the element. (Default: 0)
    • options.width?: number - The initial width of the element. (Default: 0)
    • options.height?: number - The initial height of the element. (Default: 0)
    • options.visible?: boolean - Whether the element is initially visible. (Default: true)
    • options.enabled?: boolean - Whether the element is initially enabled for interaction. (Default: true)
    • options.id?: string | null - An optional unique identifier for the element. (Default: null)
    • options.text?: string - Optional text content associated with the element (e.g., for a button label), also used for logging. (Default: '')
    • options.onClick?: function - An optional callback function to be executed when the element is clicked.

Properties

x

Type: number The x-coordinate of the top-left corner of the UI element, relative to its parent or the screen.

y

Type: number The y-coordinate of the top-left corner of the UI element, relative to its parent or the screen.

width

Type: number The width of the UI element.

height

Type: number The height of the UI element.

visible

Type: boolean Determines if the UI element is currently visible and should be rendered.

enabled

Type: boolean Determines if the UI element is currently enabled and can be interacted with (e.g., clicked).

id

Type: string | null An optional unique identifier for this UI element instance. Useful for finding specific elements.

text

Type: string Generic text content associated with the element. For elements like buttons or labels, this might be their displayed text. Also used for more descriptive logging.

engine

Type: IroncladEngine | null A reference to the main IroncladEngine instance. This is typically set via the setEngine method, often by the BaseScene when the element is added to it. Provides access to engine services.

_clickCallback (Private)

Type: Function | null The internal storage for the function to be called when the element is clicked.


Methods

setEngine(engine)

Sets the engine reference for this UI element. This allows the element to access engine services if needed (e.g., input manager, asset loader).

Parameters:

NameTypeDescription
engineIroncladEngineThe game engine instance.

containsPoint(px, py)

Checks if the given screen coordinates (px, py) are within the rectangular bounds of this UI element.

Parameters:

NameTypeDescription
pxnumberThe x-coordinate of the point.
pynumberThe y-coordinate of the point.

Returns: boolean - true if the point is within the element's bounds, false otherwise.


update(deltaTime, engine, mousePos)

Called every frame to update the UI element's state. The base implementation is empty and is meant to be overridden by derived classes for custom update logic (e.g., handling hover states, animations).

Parameters:

NameTypeDescription
deltaTimenumberThe time elapsed since the last frame, in seconds.
engineIroncladEngineThe game engine instance.
mousePosobjectAn object containing the current mouse position (e.g., { x, y }), usually relative to the canvas.

render(context, engine)

Called every frame to render the UI element. If the element is not visible or if context is invalid, it returns early. Otherwise, it calls the _drawSelf method.

Parameters:

NameTypeDescription
contextCanvasRenderingContext2DThe 2D rendering context to draw onto.
engineIroncladEngineThe game engine instance.

_drawSelf(context, engine) (Protected/Private)

This method is intended to be overridden by derived UI element classes to implement their specific drawing logic (e.g., drawing a button's background and text, an image, etc.). The base implementation does nothing but may log a warning.

Parameters:

NameTypeDescription
contextCanvasRenderingContext2DThe 2D rendering context to draw onto.
engineIroncladEngineThe game engine instance.

onClick(callback)

Assigns or updates the callback function that will be executed when this UI element is successfully clicked.

Parameters:

NameTypeDescription
callbackFunctionThe function to call when the element is clicked.

_triggerClick() (Protected/Private)

Internally called to execute the _clickCallback if the element is enabled, visible, and a callback is assigned. Logs the click trigger or a warning if conditions for clicking aren't met.


setPosition(x, y)

Sets the top-left position of the UI element.

Parameters:

NameTypeDescription
xnumberThe new x-coordinate.
ynumberThe new y-coordinate.

setSize(width, height)

Sets the dimensions of the UI element.

Parameters:

NameTypeDescription
widthnumberThe new width.
heightnumberThe new height.

show()

Makes the UI element visible by setting its visible property to true.


hide()

Makes the UI element invisible by setting its visible property to false.


enable()

Enables the UI element for interaction by setting its enabled property to true.


disable()

Disables the UI element, preventing interaction, by setting its enabled property to false.

Released under the MIT License (Placeholder).