> For the complete documentation index, see [llms.txt](https://tanishisherewith.gitbook.io/dynamic-hud/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tanishisherewith.gitbook.io/dynamic-hud/widget/widget-class.md).

# 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`).
* **Locked**: A boolean indicating whether the widget is locked in place (`isLocked`).
* **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`).

Each widget displayed is categorized into groups of "mod ids". The [WidgetRenderer](/dynamic-hud/widget/widget-renderer.md) uses this data to manage different widgets.

{% hint style="info" %}
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.
{% endhint %}

### 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:

```java
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();
```

{% hint style="warning" %}
Ensure renderWidget handles scaling correctly if shouldScale is true. You can use WidgetBox's setDimension() method to automatically adjust your widget's dimensions with scaling.
{% endhint %}
