Adding Widgets
Add some widgets to your mod
To add these or custom widgets in your mod:
First create a new class and implement the Widgets interface
public class MyClass implements Wigdets
{
@Override
public void addWigdets(DynamicUtil dynamicUtil) {
}
@Override
public void addMainMenuWigdets(DynamicUtil dynamicUtil) {
}
@Override
public void loadWigdets(DynamicUtil dynamicUtil) {
}
}
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);
}
//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
addWidgets(DynamicUtil dynamicUtil)
method@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;
}
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.
To Add a Widget to the TitleScreen or MainMenu you can override the addMainMenuWigdets(DynamicUtil dynamicUtil)
addMainMenuWigdets(DynamicUtil dynamicUtil)
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
widget.setDraggable(false);
@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;
}
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.
To load the widgets with the respective text and color, edit the loadWidgets(DynamicUtil dynamicUtil)
method.
loadWidgets(DynamicUtil dynamicUtil)
method.@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;
}
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
.