Widget class

This page explains the abstract Widget class..

The Widget class is an abstract class that serves as a base for creating various types of widgets. It contains essential properties and methods for managing widget behavior and state.

Key Properties

  • UID: Each widget has a unique identifier (uid) for loading and saving.

  • Display: A boolean indicating whether the widget should be displayed (display).

  • Draggable: A boolean indicating whether the widget can be dragged (isDraggable).

  • Position: The widget’s position on the screen is defined by x and y coordinates.

  • Scaling: A boolean indicating whether the widget should scale with the screen (shouldScale).

*ModID* are an essential part of DynamicHUD. Each widget displayed is categorized into groups of "mod ids". This facilitates multi-Mod experience and becomes easier for DynamicHUD as well as users to segregate and identify widgets. Moreover, the secondary class WidgetRenderer uses this data to manage different widgets.

The ModID serves as a unique identifier for widgets, allowing them to be grouped under a single ID. This is particularly useful for distinguishing widgets from different mods or groups.

While it's not mandatory for the ModID to match the mod's ID, it is advisable when all widgets are associated with a single mod.

The ModID can be any chosen string.

Additionally, using ModIDs enables the creation of HUD "addons" for other mods, which can be organized under the original mod's identifier, provided that the original mod supports DynamicHUD and utilizes its ModID for identification.

Anchoring

The Anchor Enum (TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT, CENTER) sets the reference point for positioning. The widget’s x, y are calculated as offsets (offsetX, offsetY) from the anchor, ensuring consistent placement across screen sizes. Use setPosition() to update position and recalculate offsets.

Key Methods

  • renderWidget(DrawContext, int, int): Abstract method to render the widget.

  • drawWidgetBackground(DrawContext): Draws a gray (active) or red (inactive) background in the editor.

  • readFromTag(NbtCompound): Loads widget state from NBT.

  • writeToTag(NbtCompound): Saves widget state to NBT.

  • getWidgetBox(): Returns the widget’s bounding box.

  • setPosition(int, int): Sets position and updates anchor offsets.

WidgetBuilder Class

The WidgetBuilder is an abstract nested class used to construct widget instances. It provides methods to set properties such as position, visibility, and scaling.

This is how the builder is normally used:

MyWidget widget = new MyWidget.Builder()
        .setX(300)
        .setY(60)
        .setText("MyText")
        .setDisplay(true) // To display or not
        .shouldScale(true) // To scale or not
        .setModID(MyMod.MOD_ID)
        .build();

Last updated

Was this helpful?