Create/src/main/java/com/simibubi/create/foundation/gui/TextStencilElement.java

80 lines
1.9 KiB
Java
Raw Normal View History

2021-04-07 02:17:55 +02:00
package com.simibubi.create.foundation.gui;
2021-04-25 19:54:18 +02:00
import com.mojang.blaze3d.matrix.MatrixStack;
2021-04-07 02:17:55 +02:00
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.util.text.IFormattableTextComponent;
import net.minecraft.util.text.StringTextComponent;
2021-04-10 20:00:32 +02:00
public class TextStencilElement extends DelegatedStencilElement {
2021-04-07 02:17:55 +02:00
protected FontRenderer font;
protected IFormattableTextComponent component;
2021-04-10 20:00:32 +02:00
protected boolean centerVertically = false;
protected boolean centerHorizontally = false;
2021-04-07 02:17:55 +02:00
public TextStencilElement(FontRenderer font) {
super();
this.font = font;
2021-04-10 20:00:32 +02:00
height = 10;
2021-04-07 02:17:55 +02:00
}
public TextStencilElement(FontRenderer font, String text) {
this(font);
component = new StringTextComponent(text);
}
public TextStencilElement(FontRenderer font, IFormattableTextComponent component) {
this(font);
this.component = component;
}
public TextStencilElement withText(String text) {
component = new StringTextComponent(text);
return this;
}
public TextStencilElement withText(IFormattableTextComponent component) {
this.component = component;
return this;
}
2021-04-10 20:00:32 +02:00
public TextStencilElement centered(boolean vertical, boolean horizontal) {
this.centerVertically = vertical;
this.centerHorizontally = horizontal;
return this;
}
2021-04-07 02:17:55 +02:00
@Override
protected void renderStencil(MatrixStack ms) {
2021-04-10 20:00:32 +02:00
float x = 0, y = 0;
if (centerHorizontally)
x = width / 2f - font.getWidth(component) / 2f;
if (centerVertically)
y = height / 2f - font.FONT_HEIGHT / 2f;
font.draw(ms, component, x, y, 0xff_000000);
2021-04-07 02:17:55 +02:00
}
@Override
protected void renderElement(MatrixStack ms) {
2021-04-11 03:12:43 +02:00
float x = 0, y = 0;
if (centerHorizontally)
x = width / 2f - font.getWidth(component) / 2f;
2021-04-07 02:17:55 +02:00
2021-04-11 03:12:43 +02:00
if (centerVertically)
y = height / 2f - font.FONT_HEIGHT / 2f;
ms.push();
ms.translate(x, y, 0);
2021-04-25 19:54:18 +02:00
element.render(ms, font.getWidth(component), font.FONT_HEIGHT + 2, alpha);
2021-04-11 03:12:43 +02:00
ms.pop();
}
2021-04-07 02:17:55 +02:00
2021-04-11 03:12:43 +02:00
public IFormattableTextComponent getComponent() {
return component;
}
2021-04-07 02:17:55 +02:00
}