Extending existing Skins

Extending Existing Skins for Custom Option Renderers

Steps

  1. Extend the Skin:

    • Create a subclass of an existing skin (e.g., ClassicSkin).

    • Call the superclass constructor to inherit existing renderers.

  2. Add Custom Renderer:

    • Use addRenderer to register the renderer for the custom option.

  3. Override Methods (Optional):

    • Customize renderContextMenu or input methods if needed.

Example Extended Skin

public class ExtendedClassicSkin extends ClassicSkin {
    public ExtendedClassicSkin() {
        super();
        addRenderer(StringOption.class, CustomStringRenderer::new);
    }

    public static class CustomStringRenderer implements SkinRenderer<StringOption> {
        @Override
        public void render(DrawContext drawContext, StringOption option, int x, int y, int mouseX, int mouseY) {
            option.setPosition(x, y);
            option.setHeight(mc.textRenderer.fontHeight);
            option.setWidth(mc.textRenderer.getWidth(option.get()) + 10);
            drawContext.drawText(mc.textRenderer, option.get(), x, y, 0x00FFFF, false);
        }
    }
}

// Usage
ContextMenu<ContextMenuProperties> menu = new ContextMenu<>(100, 100, ContextMenuProperties.createGenericSimplified().skin(new ExtendedClassicSkin()));
menu.addOption(new StringOption(Text.of("Text"), () -> "Hello", v -> System.out.println(v)));

Last updated

Was this helpful?