|
| 1 | +# Create Hud Widgets |
| 2 | + |
| 3 | +A Hud Widget is a block of content, which can be enabled within our "Widget Editor". Users can customize and move Widgets by drag&drop. |
| 4 | +Our Hud Widget System allows you to create own Widgets for your addon. |
| 5 | + |
| 6 | +### Create own Hud Widget Category |
| 7 | + |
| 8 | +You're able to create own Widget categories so you can create an own category for the Widgets your addon provides. |
| 9 | +Just create a HudWidgetCategory and register it in your Addon enable method. |
| 10 | + |
| 11 | +```java |
| 12 | +@AddonMain |
| 13 | +public class ExampleAddon extends LabyAddon<ExampleConfiguration> { |
| 14 | + |
| 15 | + private HudWidgetCategory widgetCategory; |
| 16 | + |
| 17 | + @Override |
| 18 | + protected void enable() { |
| 19 | + labyAPI().hudWidgetRegistry().categoryRegistry().register(this.widgetCategory = new HudWidgetCategory("example_category")); |
| 20 | + } |
| 21 | + |
| 22 | +{ |
| 23 | +``` |
| 24 | + |
| 25 | +# Registering HudWidgets |
| 26 | + |
| 27 | +You can register a created Hud Widget in your Addon enable method. |
| 28 | + |
| 29 | +```java |
| 30 | +@AddonMain |
| 31 | +public class ExampleAddon extends LabyAddon<ExampleConfiguration> { |
| 32 | + |
| 33 | + @Override |
| 34 | + protected void enable() { |
| 35 | + labyAPI().hudWidgetRegistry().register(new ExampleHudWidget(this)); |
| 36 | + } |
| 37 | + |
| 38 | +{ |
| 39 | +``` |
| 40 | + |
| 41 | +# Different HudWidget Types |
| 42 | + |
| 43 | +LabyMod provides different Widget types that help you to easily create HudWidgets. |
| 44 | + |
| 45 | ++ TextHudWidget - a simple text widget that supports multiple lines |
| 46 | ++ ItemHudWidget - an item widget with that you can display an image with a text next to it |
| 47 | ++ SimpleHudWidget - a widget that render custom icons/text etc. - an example can be found in the <a href="https://github.com/labymod-addons/teamspeak/blob/master/core/src/main/java/net/labymod/addons/teamspeak/core/hud/TeamSpeakHudWidget.java">Teamspeak LabyMod Addon</a> |
| 48 | + |
| 49 | + |
| 50 | +# Example TextHudWidget |
| 51 | + |
| 52 | +```java |
| 53 | +public class ExampleHudWidget extends TextHudWidget<TextHudWidgetConfig> { |
| 54 | + |
| 55 | + private TextLine textLine; |
| 56 | + |
| 57 | + public ExampleHudWidget(ExampleAddon addon) { |
| 58 | + super("example_id"); |
| 59 | + // Bind the Widget to our created category in our main class |
| 60 | + this.bindCategory(addon.widgetCategory()); |
| 61 | + |
| 62 | + // Optional - set an icon for the Widget Editor in the Constructor; you can also annotate the icon via a SpriteSlot at the top of the class |
| 63 | + this.setIcon(ResourceLocation.create("minecraft", "textures/item/name_tag.png")); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void load(TextHudWidgetConfig config) { |
| 68 | + super.load(config); |
| 69 | + this.textLine = createLine("Own Username", ""); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void onTick(boolean isEditorContext) { |
| 74 | + |
| 75 | + String value = null; |
| 76 | + if(Laby.labyAPI().serverController().isConnected()) { |
| 77 | + value = Laby.labyAPI().getName(); |
| 78 | + } |
| 79 | + |
| 80 | + // Update the text line an flush it |
| 81 | + this.textLine.updateAndFlush(value); |
| 82 | + |
| 83 | + // Set the state of the text line |
| 84 | + // DISABLED - text line is fully disabled also not available in the Widget Editor |
| 85 | + // HIDDEN - text line is hidden ingame, but is displayed in the Widget Editor |
| 86 | + // VISIBLE - text line is visible ingame |
| 87 | + this.textLine.setState(value != null ? State.VISIBLE : State.HIDDEN); |
| 88 | + } |
| 89 | + |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +# Hud Widget Configurations |
| 94 | + |
| 95 | +You can also create Configurations for your Hud Widgets, in our example we are using a configuration for the TextHudWidget. |
| 96 | +You can easily create configurations by creating a class that inherit the `TextHudWidgetConfig` class. |
| 97 | +You can use the same Setting Elements that you are using to create the configuration of your addon, take a look <a href="/pages/addon/features/config/#using-predefined-setting-widgets">here</a>. |
| 98 | + |
| 99 | +```java |
| 100 | + |
| 101 | +public class ExampleHudWidget extends TextHudWidget<ExampleHudWidgetConfig> { |
| 102 | + |
| 103 | + private TextLine textLine; |
| 104 | + |
| 105 | + public ExampleHudWidget(ExampleAddon addon) { |
| 106 | + super("example_id", ExampleHudWidgetConfig.class); |
| 107 | + this.bindCategory(addon.widgetCategory()); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public void load(ExampleHudWidgetConfig config) { |
| 112 | + super.load(config); |
| 113 | + this.textLine = createLine("Own UUID", ""); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public void onTick(boolean isEditorContext) { |
| 118 | + |
| 119 | + String value = null; |
| 120 | + if(Laby.labyAPI().serverController().isConnected()) { |
| 121 | + value = Laby.labyAPI().getUniqueId().toString(); |
| 122 | + // Access our created Setting |
| 123 | + if(!this.getConfig().showDashes().get()) { |
| 124 | + value = value.replace("-", ""); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + this.textLine.updateAndFlush(value); |
| 129 | + this.textLine.setState(value != null ? State.VISIBLE : State.HIDDEN); |
| 130 | + } |
| 131 | + |
| 132 | + public static class ExampleHudWidgetConfig extends TextHudWidgetConfig { |
| 133 | + |
| 134 | + @SwitchSetting |
| 135 | + private final ConfigProperty<Boolean> showDashes = new ConfigProperty<>(true); |
| 136 | + |
| 137 | + public ConfigProperty<Boolean> showDashes() { |
| 138 | + return showDashes; |
| 139 | + } |
| 140 | + |
| 141 | + } |
| 142 | + |
| 143 | +} |
| 144 | +``` |
0 commit comments