/*
* This file is part of OneConfig.
* OneConfig - Next Generation Config Library for Minecraft: Java Edition
* Copyright (C) 2021, 2022 Polyfrost.
*
* If you simply want to display text, extend {@link TextHud} or {@link SingleTextHud},
* whichever applies to the use case. Then, override the required methods.
*
* If you want to display something else, extend this class and override {@link Hud#getWidth(float, boolean)}, {@link Hud#getHeight(float, boolean)}, and {@link Hud#draw(UMatrixStack, float, float, float, boolean)} with the width, height, and the drawing code respectively.
*
* It should also be noted that additional options to the HUD can be added simply by declaring them.
* {@code
* public class TestHud extends SingleTextHud {
* @literal @Switch(
* name = "Additional Option"
* )
* public boolean additionalOption = true;
* }
* }
*
{@code
* * public class YourConfig extends Config {
* * @literal @HUD(
* * name = "HUD Element"
* * )
* * public YourHudElement hudElement = new YourHudElement("Title");
* * }
* * }
*/
public abstract class Hud {
protected boolean enabled;
transient private Config config;
public final Position position;
protected float scale;
/**
* @param enabled If the hud is enabled
* @param x X-coordinate of hud on a 1080p display
* @param y Y-coordinate of hud on a 1080p display
* @param scale Scale of the hud
*/
public Hud(boolean enabled, float x, float y, float scale) {
this.enabled = enabled;
this.scale = scale;
position = new Position(x, y, getWidth(scale, true), getHeight(scale, true));
}
/**
* @param enabled If the hud is enabled
* @param x X-coordinate of hud on a 1080p display
* @param y Y-coordinate of hud on a 1080p display
*/
public Hud(boolean enabled, float x, float y) {
this(enabled, x, y, 1);
}
/**
* @param enabled If the hud is enabled
*/
public Hud(boolean enabled) {
this(enabled, 0, 0, 1);
}
public Hud() {
this(false, 0, 0, 1);
}
/**
* Function called when drawing the hud
*
* @param matrices The UMatrixStack used for rendering in higher versions
* @param x Top left x-coordinate of the hud
* @param y Top left y-coordinate of the hud
* @param scale Scale of the hud
* @param example If the HUD is being rendered in example form
*/
protected abstract void draw(UMatrixStack matrices, float x, float y, float scale, boolean example);
/**
* @param scale Scale of the hud
* @param example If the HUD is being rendered in example form
* @return The width of the hud
*/
protected abstract float getWidth(float scale, boolean example);
/**
* @param scale Scale of the hud
* @param example If the HUD is being rendered in example form
* @return The height of the hud
*/
protected abstract float getHeight(float scale, boolean example);
/**
* Function to do things before rendering anything
*
* @param example If the HUD is being rendered in example form
*/
protected void preRender(boolean example) {
}
/**
* Draw the background, the hud and all childed huds, used by HudCore
*/
public void drawAll(UMatrixStack matrices, boolean example) {
if (!example && !shouldShow()) return;
preRender(example);
position.setSize(getWidth(scale, example), getHeight(scale, example));
draw(matrices, position.getX(), position.getY(), scale, example);
}
protected boolean shouldShow() {
if (!showInGuis && Platform.getGuiPlatform().getCurrentScreen() != null && !(Platform.getGuiPlatform().getCurrentScreen() instanceof OneConfigGui))
return false;
if (!showInChat && Platform.getGuiPlatform().isInChat()) return false;
return showInDebug || !Platform.getGuiPlatform().isInDebug();
}
/**
* @return If the hud is enabled
*/
public boolean isEnabled() {
return enabled && (config == null || config.enabled);
}
/**
* Set the config to disable accordingly, intended for internal use
*
* @param config The config instance
*/
public void setConfig(Config config) {
this.config = config;
}
/**
* @return The config of this HUD
*/
public Config getConfig() {
return this.config;
}
/**
* @return The scale of the Hud
*/
public float getScale() {
return scale;
}
/**
* Set a new scale value
*
* @param scale The new scale
* @param example If the HUD is being rendered in example form
*/
public void setScale(float scale, boolean example) {
this.scale = scale;
position.updateSizePosition(getWidth(scale, example), getHeight(scale, example));
}
@Switch(
name = "Show in Chat"
)
public boolean showInChat = true;
@Switch(
name = "Show in F3 (Debug)"
)
public boolean showInDebug = false;
@Switch(
name = "Show in GUIs"
)
public boolean showInGuis = true;
}