DynamicHUD Dev Guide
DownloadNeed Support?
  • Dynamic HUD
  • Import using Gradle
  • Integrating DynamicHUD
    • Integrating DynamicHUD
    • Adding Widgets
    • Adding your own WidgetRenderer
    • Passing AbstractMoveableScreen instance
    • Changing default key bind
    • Changing save and load file
    • Registering Custom Widgets
  • Widget
    • Widget class
    • How to create a custom widget
    • Widget Renderer
    • WidgetData
    • DynamicValueRegistry
    • Scaling
  • Screens
    • AbstractMoveableScreen
  • ContextMenu
    • ContextMenu class
    • Using context menu
    • Option<T> class
      • Color Option
      • Boolean Option
      • Double Option
      • Runnable Option
      • Enum Option
      • List Option
      • SubMenu Option
Powered by GitBook
On this page

Was this helpful?

  1. Widget

How to create a custom widget

Here’s an example of how you might create your own widget and use the WidgetBuilder to create a custom widget:

public class MyWidget extends Widget {
    private String text;
    
    //Check out the WidgetData page for more information.
    public static WidgetData<TextWidget> DATA = new WidgetData<>("MyWidget", "My first custom widget", MyWidget::new);

    public MyWidget(String modId, String text) {
        super(DATA, modId);
        this.text = text;
    }

    // Override the renderWidget method to display your custom widget
    @Override
    public void renderWidget(DrawContext context, int mouseX, int mouseY) {
        // Render your widget here
        // You can use DrawHelper.java class of DynamicHUD to render 2D shapes (with gradients and/or outlines)
    }

    public static class Builder extends WidgetBuilder<Builder, MyWidget> {
        private String text;

        public Builder setText(String text) {
            this.text = text;
            return this;
        }

        @Override
        protected Builder self() {
            return this;
        }

        @Override
        public MyWidget build() {
            return new MyWidget(modID, text);
        }
    }
}

In this example, MyWidget is a custom widget that displays some text. The Builder nested class is used to construct instances of MyWidget. The setText method is used to set the text that the widget will display, and the build method creates a new MyWidget instance with the specified properties.

Saving and loading widget

Now we will save and load the fields of a widget. To save override, the writeToTag() method.

Do not remove the super call otherwise the basic properties of widgets won't be saved or loaded like its position, name, modID, etc.

MyWidget.java
@Override
public void writeToTag(NbtCompound tag) {
   super.writeToTag(tag);
   tag.putString("Text", text);
}

To load fields, override the readFromTag() method.

MyWidget.java
@Override
public void readFromTag(NbtCompound tag) {
   super.readFromTag(tag);
   this.text = tag.getString("Text");
}
PreviousWidget classNextWidget Renderer

Last updated 4 months ago

Was this helpful?

You can use , , classes of DynamicHUD to render many 2D shapes (with gradients and/or outlines) and lines, manipulate textures and make easy use of colors and formatting.

DrawHelper.java
ColorHelper.java
TextureHelper.java