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
andy
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.
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();
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.
Last updated
Was this helpful?