Skip to content

Commit

Permalink
Add HudWidgets to Wiki (#22)
Browse files Browse the repository at this point in the history
* Fix using localhost url

* Made path relative

* Create hud-widgets.md

* Update hud-widgets.md

* Update mkdocs.yml - Add Hud Widgets

* Added "Registering HudWidgets"

* Update hud-widgets.md

* Added HudWidget Configuration

* Removed final modifier from Category

* Added SimpleHudWidget to Widget Types

* Removed constructor from Widget Config

* Fixed Custom Widget Configurations
  • Loading branch information
MisterCore authored Sep 2, 2024
1 parent e8d1935 commit 03cde93
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 1 deletion.
144 changes: 144 additions & 0 deletions docs/pages/addon/features/hud-widgets.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
# Create Hud Widgets

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.
Our Hud Widget System allows you to create own Widgets for your addon.

### Create own Hud Widget Category

You're able to create own Widget categories so you can create an own category for the Widgets your addon provides.
Just create a HudWidgetCategory and register it in your Addon enable method.

```java
@AddonMain
public class ExampleAddon extends LabyAddon<ExampleConfiguration> {

private HudWidgetCategory widgetCategory;

@Override
protected void enable() {
labyAPI().hudWidgetRegistry().categoryRegistry().register(this.widgetCategory = new HudWidgetCategory("example_category"));
}

{
```

# Registering HudWidgets

You can register a created Hud Widget in your Addon enable method.

```java
@AddonMain
public class ExampleAddon extends LabyAddon<ExampleConfiguration> {

@Override
protected void enable() {
labyAPI().hudWidgetRegistry().register(new ExampleHudWidget(this));
}

{
```

# Different HudWidget Types

LabyMod provides different Widget types that help you to easily create HudWidgets.

+ TextHudWidget - a simple text widget that supports multiple lines
+ ItemHudWidget - an item widget with that you can display an image with a text next to it
+ 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>


# Example TextHudWidget

```java
public class ExampleHudWidget extends TextHudWidget<TextHudWidgetConfig> {

private TextLine textLine;

public ExampleHudWidget(ExampleAddon addon) {
super("example_id");
// Bind the Widget to our created category in our main class
this.bindCategory(addon.widgetCategory());

// 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
this.setIcon(ResourceLocation.create("minecraft", "textures/item/name_tag.png"));
}

@Override
public void load(TextHudWidgetConfig config) {
super.load(config);
this.textLine = createLine("Own Username", "");
}

@Override
public void onTick(boolean isEditorContext) {

String value = null;
if(Laby.labyAPI().serverController().isConnected()) {
value = Laby.labyAPI().getName();
}

// Update the text line an flush it
this.textLine.updateAndFlush(value);

// Set the state of the text line
// DISABLED - text line is fully disabled also not available in the Widget Editor
// HIDDEN - text line is hidden ingame, but is displayed in the Widget Editor
// VISIBLE - text line is visible ingame
this.textLine.setState(value != null ? State.VISIBLE : State.HIDDEN);
}

}
```

# Hud Widget Configurations

You can also create Configurations for your Hud Widgets, in our example we are using a configuration for the TextHudWidget.
You can easily create configurations by creating a class that inherit the `TextHudWidgetConfig` class.
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>.

```java

public class ExampleHudWidget extends TextHudWidget<ExampleHudWidgetConfig> {

private TextLine textLine;

public ExampleHudWidget(ExampleAddon addon) {
super("example_id", ExampleHudWidgetConfig.class);
this.bindCategory(addon.widgetCategory());
}

@Override
public void load(ExampleHudWidgetConfig config) {
super.load(config);
this.textLine = createLine("Own UUID", "");
}

@Override
public void onTick(boolean isEditorContext) {

String value = null;
if(Laby.labyAPI().serverController().isConnected()) {
value = Laby.labyAPI().getUniqueId().toString();
// Access our created Setting
if(!this.getConfig().showDashes().get()) {
value = value.replace("-", "");
}
}

this.textLine.updateAndFlush(value);
this.textLine.setState(value != null ? State.VISIBLE : State.HIDDEN);
}

public static class ExampleHudWidgetConfig extends TextHudWidgetConfig {

@SwitchSetting
private final ConfigProperty<Boolean> showDashes = new ConfigProperty<>(true);

public ConfigProperty<Boolean> showDashes() {
return showDashes;
}

}

}
```
3 changes: 2 additions & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ nav:
- Use and Create Events: pages/addon/features/events.md
- Create Commands: pages/addon/features/commands.md
- Write Version Dependent Code: pages/addon/features/version-dependent.md
- Hud Widgets: pages/addon/features/hud-widgets.md
- Activity System:
- What are Activities?: pages/addon/activities/activity.md
- Use and Create Widgets: pages/addon/activities/widgets.md
Expand Down Expand Up @@ -105,4 +106,4 @@ extra_javascript: [ 'assets/theme/js/script.js' ]
extra_css: [ 'assets/theme/css/main.css' ]

extra:
generator: false
generator: false

0 comments on commit 03cde93

Please sign in to comment.