Skip to content

Commit 03cde93

Browse files
authored
Add HudWidgets to Wiki (#22)
* 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
1 parent e8d1935 commit 03cde93

File tree

2 files changed

+146
-1
lines changed

2 files changed

+146
-1
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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+
```

mkdocs.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ nav:
5858
- Use and Create Events: pages/addon/features/events.md
5959
- Create Commands: pages/addon/features/commands.md
6060
- Write Version Dependent Code: pages/addon/features/version-dependent.md
61+
- Hud Widgets: pages/addon/features/hud-widgets.md
6162
- Activity System:
6263
- What are Activities?: pages/addon/activities/activity.md
6364
- Use and Create Widgets: pages/addon/activities/widgets.md
@@ -105,4 +106,4 @@ extra_javascript: [ 'assets/theme/js/script.js' ]
105106
extra_css: [ 'assets/theme/css/main.css' ]
106107

107108
extra:
108-
generator: false
109+
generator: false

0 commit comments

Comments
 (0)