How to create a custom widget

Creating a Custom Widget

Extend the Widget class to create custom widgets in DynamicHUD, defining unique rendering and behavior. Use DynamicValueWidget for widgets with dynamic data (e.g., TextWidget, GraphWidget), which links to DynamicValueRegistry for real-time values. Widgets use WidgetBuilder for fluent construction and support saving/loading via NBT.

Example: Custom Widget

public class MyWidget extends DynamicValueWidget {
    public static WidgetData<MyWidget> DATA = new WidgetData<>("MyWidget", "Custom widget", MyWidget::new);

    public MyWidget(String modId, String registryID, String registryKey) {
        super(DATA, modId, registryID, registryKey);
    }

    @Override
    public void renderWidget(DrawContext context, int mouseX, int mouseY) {
        String text = (String) getValue();
        DrawHelper.drawText(context, text, x, y, 0xFFFFFF);
    }

    @Override
    public Object getValue() {
        return valueSupplier != null ? valueSupplier.get() : "";
    }

    public static class Builder extends DynamicValueWidgetBuilder<Builder, MyWidget> {
        @Override
        public MyWidget build() {
            return new MyWidget(modID, registryID, registryKey)
                .setPosition(x, y)
                .setDisplay(display)
                .setDraggable(isDraggable)
                .setShouldScale(shouldScale);
        }
    }
}
  • Purpose: Displays dynamic text from DynamicValueRegistry.

  • Builder: Configures position, visibility, and registry keys.

  • Rendering: Uses DrawHelper for text rendering (supports shapes, gradients, textures via DrawHelper, ColorHelper, TextureHelper).

Saving and Loading

Override writeToTag and readFromTag to save/load custom fields. Always call super to preserve base properties (e.g., position, modId).

@Override
public void writeToTag(NbtCompound tag) {
    super.writeToTag(tag);
    // Add custom fields
}

@Override
public void readFromTag(NbtCompound tag) {
    super.readFromTag(tag);
    // Read custom fields
}

Last updated

Was this helpful?