Better HUD API - mccreery/better-hud-fabric GitHub Wiki
Adding the dependency
For most mods, Better HUD should be a soft dependency. Add Better HUD to your mod's build.gradle under the modCompile configuration:
dependencies {
modCompile 'mccreery.betterhud:betterhud:0.1.0-SNAPSHOT'
}
You may also add a 'suggests' dependency to your fabric.mod.json:
"suggests": {
"betterhud": "*"
}
Interfacing with Better HUD
Better HUD provides an entrypoint betterhud. Implement the BetterHudInitializer interface and assign your class to the entrypoint in fabric.mod.json:
"entrypoints": {
"betterhud": "my.cool.package.MyCoolBetterHudInitializer"
}
If this initializer is called then Better HUD is present and you may register new elements with the provided registry. The registry does not take an instance of your element class but the class itself:
Registry.register(registry, new Identifier("mycoolmodid", "my_cool_element"), MyCoolHudElement.class);
When Better HUD is present you should disable your normal HUD rendering and design your HudElement to mimic the original.
The translation key format for names is hudElement.mycoolmodid.my_cool_element.
Creating HUD elements
The most important class in the API is HudElement. Extend this class (and register) to create or convert existing overlays to the Better HUD system. For examples, take a look at the default elements.
Rendering
You will have to implement the render method, which returns a bounding box for the element. This does not have to be constant size. You can return null if your element is hidden. The context object offers calculateBounds to tell your element where to render.
Phase
Elements can render as a normal overlay or as billboards above any mob (like player names). Call setPhase in your constructor to change the phase from the default overlay.
Fixed Position
By default elements can be positioned anywhere on the screen and relative to any other element. If your element cannot move (for example, the crosshair) call setFixed in your constructor and use your own bounds instead of calling calculateBounds.
Options
Options can be used to further configure your elements. Simply add a field to your class and it will be loaded along with the layout. Any fields which you do not want to become options should be marked transient.
At the moment these options are only configurable directly via the layout config file. In the future each element will have an options menu which will restrict the types available to options. A schema resource file will be used to describe the options so the GUI can be richer than 'property: true/false'.