Files
FalsePatternLib/src/main/java/com/falsepattern/lib/compat/GuiLabel.java
T
2022-05-31 14:11:33 +01:00

114 lines
3.0 KiB
Java

package com.falsepattern.lib.compat;
import java.util.ArrayList;
import java.util.List;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import lombok.*;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.client.resources.I18n;
import org.lwjgl.opengl.GL11;
/**
* A functional equivalent to GuiLabel present in Minecraft 1.12.
*/
@SideOnly(Side.CLIENT)
public class GuiLabel extends Gui {
private final List<String> lines = new ArrayList<>();
private final FontRenderer fontRenderer;
private final int textColor;
private boolean centered = false;
/**
* The label width.
*/
protected int width;
/**
* The label height.
*/
protected int height;
/**
* The id of the label.
*/
public int id;
/**
* The x position of the label.
*/
public int x;
/**
* The y position of the label.
*/
public int y;
/**
* The visibility of the label.
*/
public boolean visible = true;
/**
* Instantiates a new Gui label.
*
* @param fontRenderer the minecraft font renderer
* @param id the id
* @param x the x
* @param y the y
* @param width the width
* @param height the height
* @param textColour the text colour
*/
public GuiLabel(@NonNull FontRenderer fontRenderer, int id, int x, int y, int width, int height, int textColour) {
this.fontRenderer = fontRenderer;
this.id = id;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.textColor = textColour;
}
/**
* Add a line of text to the GuiLabel.
*
* @param text string to add
*/
public void addLine(@NonNull String text) {
lines.add(I18n.format(text));
}
/**
* Sets the label text to render centred with respect to the x and y.
*
* @return the GuiLabel itself
*/
public GuiLabel setCentered() {
centered = true;
return this;
}
/**
* Draw label.
*
* @param minecraft the minecraft
* @param mouseX the mouse x
* @param mouseY the mouse y
*/
public void drawLabel(@NonNull Minecraft minecraft, int mouseX, int mouseY) {
if (!visible)
return;
GL11.glEnable(GL11.GL_BLEND);
OpenGlHelper.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ZERO);
val topLeftY = y + height / 2 - lines.size() * 10 / 2;
for (var i = 0; i < lines.size(); ++i) {
if (centered) {
drawCenteredString(fontRenderer, lines.get(i), x + width / 2, topLeftY + i * 10, textColor);
} else {
drawString(fontRenderer, lines.get(i), x, topLeftY + i * 10, textColor);
}
}
}
}