# Adding Widgets

**To add these or custom widgets in your mod:**

{% content-ref url="/spaces/DVOerbBYig9gngF4saif/pages/Pg8T3wWof9eNNnE01W25" %}
[Broken mention](broken://spaces/DVOerbBYig9gngF4saif/pages/Pg8T3wWof9eNNnE01W25)
{% endcontent-ref %}

{% content-ref url="/spaces/DVOerbBYig9gngF4saif/pages/FaImDY1oTTtOALOfFupP" %}
[Broken mention](broken://spaces/DVOerbBYig9gngF4saif/pages/FaImDY1oTTtOALOfFupP)
{% endcontent-ref %}

{% content-ref url="/spaces/DVOerbBYig9gngF4saif/pages/bCbClqt2csavXvkgjiBU" %}
[Broken mention](broken://spaces/DVOerbBYig9gngF4saif/pages/bCbClqt2csavXvkgjiBU)
{% endcontent-ref %}

### &#x20;First create a new class and implement the Widgets interface

{% code overflow="wrap" lineNumbers="true" %}

```java
public class MyClass implements Wigdets
{

  @Override
  public void addWigdets(DynamicUtil dynamicUtil) {
  }
  @Override
  public void addMainMenuWigdets(DynamicUtil dynamicUtil) {
  }
  @Override
  public void loadWigdets(DynamicUtil dynamicUtil) {
  }
  
}
```

{% endcode %}

{% hint style="info" %}
You may create a list of widgets and add widgets to them. You can then add them to WidgetManager by iterating through each widget element.
{% endhint %}

{% code overflow="wrap" lineNumbers="true" %}

```java
List<Widget> widgets = new ArrayList<>();
widgets.add(MyWidget);

//Add the widgets to the WidgetManager
for (Widget wigdet : widgets)  {
    dynamicutil.getWidgetManager().addWidget(wigdet);
    //To add widgets to the TitleScreen
    dynamicutil.getWidgetManager().addMainMenuWidget(wigdet);
}
```

{% endcode %}

{% hint style="info" %}
Or you can directly add the widgets to&#x20;
{% endhint %}

```java
//To add widgets to the InGameHud
dynamicutil.getWidgetManager().addWidget()
//To add widgets to the TitleScreen
dynamicutil.getWidgetManager().addMainMenuWidget(wigdet);
```

### Create all the widgets in the `addWidgets(DynamicUtil dynamicUtil)` method

{% code overflow="wrap" lineNumbers="true" fullWidth="true" %}

```java
@Override
public void addWigdets(DynamicUtil dynamicUtil) {
  
   List<Widget> widgets = new ArrayList<>();
   
   widgets.add(new TextWidget(mc, text, () -> dataText, xPercent, yPercent, true, true, false, -1, -1,       true));

   widgets.add(new ArmorWidget(mc, EquipmentSlot.CHEST,xPercent, yPercent, true,     TextureHelper.Position.ABOVE, () -> text, ()->color));

   widgets.add(new ItemWidget(mc,() -> itemStack, xPercent, yPercent, true, TextureHelper.Position.ABOVE, () -> text,()-> color));

  //Add the widgets to the WidgetManager widget List
  for (Widget wigdet : widgets)  {
      dynamicUtil.getWidgetManager().addWidget(wigdet);
   }
   dynamicUtil.WidgetAdded = true;
 }
```

{% endcode %}

This code is a method named `addWigdets` that takes in a `DynamicUtil` object as a parameter. The method creates an `ArrayList` of `Widget` objects and adds three widgets to it: a `TextWidget`, an `ArmorWidget`, and an `ItemWidget`.

After creating and adding the widgets to the list, the method loops through the list and adds each widget to be displayed on screen using the `addWidget` method in the `WidgetManager` class of the `DynamicUtil` object.

{% hint style="info" %}

### To Add a Widget to the TitleScreen or MainMenu you can override the `addMainMenuWigdets(DynamicUtil dynamicUtil)`

{% endhint %}

> The MainMenuWidgets don't contain any contextMenu, slider or a colorPicker. It is just to display message or any widget that is simple. The user can move the widgets around in the TitleScreen without opening any other screens. If you don't want the user to be able to move the widgets, then you can make it undraggable by&#x20;
>
> ```
> widget.setDraggable(false);
> ```

{% code lineNumbers="true" fullWidth="true" %}

```java
@Override
public void addMainMenuWigdets(DynamicUtil dynamicUtil) {
  
   List<Widget> MainMenuwidgets = new ArrayList<>();
   
   MainMenuwidgets.add(new TextWidget(mc, text, () -> dataText, xPercent, yPercent, true, true, false, -1, -1, true));

   MainMenuwidgets.add(new ArmorWidget(mc, EquipmentSlot.CHEST,xPercent, yPercent, true, TextureHelper.Position.ABOVE, () -> text, ()->color));

   MainMenuwidgets.add(new ItemWidget(mc,() -> itemStack, xPercent, yPercent, true, TextureHelper.Position.ABOVE, () -> text,()-> color));

  //Add the widgets to the WidgetManager MainMenuWidget List
  for (Widget wigdet : MainMenuwidgets )  {
      dynamicUtil.getWidgetManager().addMainMenuWidget(wigdet);
   }
dynamicUtil.MainMenuWidgetAdded=true;
 }
```

{% endcode %}

The `TextWidget` is created using its constructor, which takes in several parameters such as the Minecraft instance (`mc`), the text to display, a lambda expression for the data text, the x and y position of the widget as percentages, and several boolean values for various settings.

The `ArmorWidget` is created similarly, with additional parameters for the equipment slot and the position of the texture.

The `ItemWidget` is also created similarly, with additional parameters for the item stack and the position of the texture.

{% hint style="info" %}

### To load the widgets with the respective text and color, edit the `loadWidgets(DynamicUtil dynamicUtil)` method.

{% endhint %}

{% code lineNumbers="true" fullWidth="false" %}

```java
@Override
public void loadWigdets(DynamicUtil dynamicUtil) {

// Load the widgets from the loadWidgets method in the widgetManager Class
List<Widget> widgets = dynamicUtil.getWidgetManager().loadWigdets(WIDGETS_FILE);
    
// Load the MainMenuWidgets from the loadMainMenuWidgets method in the widgetManager Class
List<Widget> MainMenuWidgets = dynamicUtil.getWidgetManager().loadMainMenuWigdets(WIDGETS_FILE);
    
Widget.addTextGenerator("Dynamic", () -> "HUD");
Widget.addTextGenerator("FPS", () -> String.valueOf(mc.getCurrentFps()));
Widget.addTextGenerator("Label", () -> "Label");
Widget.addTextGenerator(text, () -> dataText);


 for (Widget widget : widgets) {
    dynamicUtil.getWidgetManager().addWidget(widget);
 }

 for (Widget widgetItem : MainMenuWidgets ) {
    dynamicUtil.getWidgetManager().addMainMenuWidget(widgetItem);
 }

        dynamicUtil.WidgetLoaded = true;
}
```

{% endcode %}

This code is a method named `loadWigdets` that takes in a `DynamicUtil` object as a parameter. The method is used to load widgets from a file named `WIDGETS_FILE` using the `loadWidgets` method in the `WidgetManager` class of the `DynamicUtil` object. This method is universal for all widgets meaning any widget with the same label will have the text displayed.

The method calls the static `addTextGenerator` method on the `Widget` class several times. This method takes two arguments: a label and a text generator function. The label is used to identify which widget should display the text generated by the text generator function. The text generator function is responsible for generating the text that will be displayed by the widget. In this code, text generators are added for various pieces of information such as FPS, Label, DynamicHud, etc.

After that, the method iterates over the lists of widgets and MainMenu widgets, calling the `addWidget` and `addMainMenuWidget` methods on the `WidgetManager` object obtained from the `DynamicUtil` object for each widget. These methods add the widgets to the `WidgetManager`.

{% hint style="info" %}
If you don't want to load the widgets with their color and text, you can simply add the looped widgets to the `widgetManager` to be displayed.
{% endhint %}

#### Instead of creating a new class to add and load the widgets, you can simple implement the Widgets interface in your main entry point class.

### If you have created your widgets and want it to be saved and loaded, then head on to this page:

{% content-ref url="/spaces/DVOerbBYig9gngF4saif/pages/CQvdEugP2zkf2PrqARcn" %}
[Broken mention](broken://spaces/DVOerbBYig9gngF4saif/pages/CQvdEugP2zkf2PrqARcn)
{% endcontent-ref %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://tanishisherewith.gitbook.io/archived-legacy-dynamichud/widgets/adding-widgets.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
