Settings
Live preview
Settings that change how something looks are miserable to tune blind. Opt in, and the top of your page's right-hand column carries a PREVIEW card: your module, drawn live at the size it will really be.
Opting in
Nothing appears until you opt in. getPreview() returns null by default, and a null preview means no card at all rather than an empty one: the INFO, PRESETS and SHARE cards move up to fill the space.
A preview is three methods. Two report size in screen pixels, the same units as getWidth() with scale already applied, and one draws:
package com.eymistaken.simplecps.api;
public interface HudPreview {
// Size in screen pixels, the same units as HudModule.getWidth().
int width();
int height();
// Draw with the top-left corner at (0, 0).
void render(GuiGraphicsExtractor ctx, float tickDelta);
// A preview that is simply the module drawing itself.
static HudPreview ofModule(HudModule module) { ... }
}The module drawing itself
You will rarely implement it by hand. Most previews are the module rendering exactly as it always does, with stand-in values for data the game is not producing: no clicks have been counted, no server is connected, and since the settings screen opens from the title screen too, there may be no player or world at all. HudPreview.ofModule wires up the module's own render path, and isPreviewing() is where you substitute the data:
import com.eymistaken.simplecps.api.*;
import java.util.List;
// Inside your MyCustomModule class:
@Override
public HudPreview getPreview() {
return HudPreview.ofModule(this);
}
// Stand-in rows, so the card has something to show with no world loaded.
private static final List<String> PREVIEW_ROWS =
List.of("Water Breathing 1:27", "Strength II 3:34");
private List<String> rows() {
if (isPreviewing()) return PREVIEW_ROWS;
return client.player == null ? List.of() : liveRows();
}
@Override
public void extractRenderState(GuiGraphicsExtractor ctx, float tickDelta) {
int y = 0;
for (String row : rows()) {
drawText(ctx, row, 0, y, MyAddonConfig.textColor);
y += client.font.lineHeight + 1;
}
}
@Override
public int getWidth() {
// The same helper, so the card is sized for what it actually draws.
int w = 0;
for (String row : rows()) w = Math.max(w, textWidth(row));
return w;
}The detail that matters is rows() feeding both the render path and the measurement. isPreviewing() is true for the whole pass, sizing included, precisely so the two agree: report one size and draw another, and the card is built for something it never shows.
The card owns the geometry. It measures you, sizes itself, centers you, applies the transform that puts one of your pixels on one real screen pixel, and clips you to the box, so render draws from the origin and never translates or scales itself. The card grows downward to fit and stops before it would push the panels below it off screen. It never widens, and a module wider than the column is clipped rather than shrunk, because a preview scaled to fit would answer the wrong question about how much room your module takes. Focus mode shows the rest.
Never step animation state the live HUD reads. With the settings screen open in game your module is drawn twice a frame, once on the real HUD and once in the preview, so any per-frame counter advanced inside your render path runs at double speed while the menu is up. Keep a preview-local copy, or leave the counter alone while isPreviewing(). The built-in keystrokes module holds separate squish and ripple maps for exactly this reason.
A preview of your own
When the preview should not be the module at all, such as a legend, a labeled comparison or a fixed illustration, implement the interface directly and draw whatever you like:
@Override
public HudPreview getPreview() {
return new HudPreview() {
@Override public int width() { return 96; }
@Override public int height() { return 28; }
@Override
public void render(GuiGraphicsExtractor ctx, float tickDelta) {
ctx.fill(0, 0, 96, 28, 0x40FFFFFF);
drawText(ctx, "Water Breathing 1:27", 2, 2, MyAddonConfig.textColor);
drawText(ctx, "Strength II 3:34", 2, 14, MyAddonConfig.textColor);
}
};
}getPreview() is called once a frame while your page is open, so keep it cheap, and prefer handing back a cached instance to building one per call. Anything that throws, whether the getter, either size or the drawing, costs you the preview for that frame and nothing else. Like every other hook here it is additive and defaults to nothing, so an addon built against an older release keeps running, simply without a card.