Files
FalsePatternLib/src/main/java/com/falsepattern/lib/compat/GuiLabel.java
T

114 lines
3.0 KiB
Java
Raw Normal View History

2022-05-07 13:38:33 +02:00
package com.falsepattern.lib.compat;
2022-05-31 11:05:07 +01:00
import java.util.ArrayList;
2022-05-07 13:38:33 +02:00
import java.util.List;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
2022-05-31 11:05:07 +01:00
import lombok.*;
2022-05-07 13:38:33 +02:00
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;
2022-05-31 12:30:22 +01:00
/**
* A functional equivalent to GuiLabel present in Minecraft 1.12
*/
2022-05-07 13:38:33 +02:00
@SideOnly(Side.CLIENT)
2022-05-31 10:09:16 +01:00
public class GuiLabel extends Gui {
2022-05-31 11:05:07 +01:00
private final List<String> lines = new ArrayList<>();
private final FontRenderer fontRenderer;
private final int textColor;
private boolean centered = false;
2022-05-31 12:30:22 +01:00
/**
* The label width.
*/
2022-05-07 13:38:33 +02:00
protected int width;
2022-05-31 12:30:22 +01:00
/**
* The label height.
*/
2022-05-07 13:38:33 +02:00
protected int height;
2022-05-31 12:30:22 +01:00
/**
* The id of the label.
*/
2022-05-31 11:05:07 +01:00
public int id;
2022-05-31 12:30:22 +01:00
/**
* The x position of the label.
*/
2022-05-07 13:38:33 +02:00
public int x;
2022-05-31 12:30:22 +01:00
/**
* The y position of the label.
*/
2022-05-07 13:38:33 +02:00
public int y;
2022-05-31 12:30:22 +01:00
/**
* The visibility of the label.
*/
2022-05-07 13:38:33 +02:00
public boolean visible = true;
2022-05-31 11:05:07 +01:00
/**
* 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;
2022-05-07 13:38:33 +02:00
}
2022-05-31 11:05:07 +01:00
/**
* Add a line of text to the GuiLabel.
*
2022-05-31 12:30:22 +01:00
* @param text string to add
2022-05-31 11:05:07 +01:00
*/
public void addLine(@NonNull String text) {
lines.add(I18n.format(text));
2022-05-07 13:38:33 +02:00
}
/**
2022-05-31 11:05:07 +01:00
* Sets the label text to render centred with respect to the x and y.
*
2022-05-31 12:30:22 +01:00
* @return the GuiLabel itself
2022-05-07 13:38:33 +02:00
*/
2022-05-31 10:09:16 +01:00
public GuiLabel setCentered() {
2022-05-31 11:05:07 +01:00
centered = true;
2022-05-07 13:38:33 +02:00
return this;
}
2022-05-31 11:05:07 +01:00
/**
* 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);
2022-05-07 13:38:33 +02:00
2022-05-31 11:05:07 +01:00
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);
}
2022-05-07 13:38:33 +02:00
}
}
}