69 lines
2.2 KiB
Java
69 lines
2.2 KiB
Java
package net.minecraft.client.gui.components;
|
|
|
|
import net.fabricmc.api.EnvType;
|
|
import net.fabricmc.api.Environment;
|
|
import net.minecraft.client.gui.Font;
|
|
import net.minecraft.client.gui.GuiGraphics;
|
|
import net.minecraft.locale.Language;
|
|
import net.minecraft.network.chat.CommonComponents;
|
|
import net.minecraft.network.chat.Component;
|
|
import net.minecraft.network.chat.FormattedText;
|
|
import net.minecraft.util.FormattedCharSequence;
|
|
|
|
@Environment(EnvType.CLIENT)
|
|
public class StringWidget extends AbstractStringWidget {
|
|
private float alignX = 0.5F;
|
|
|
|
public StringWidget(Component message, Font font) {
|
|
this(0, 0, font.width(message.getVisualOrderText()), 9, message, font);
|
|
}
|
|
|
|
public StringWidget(int width, int height, Component message, Font font) {
|
|
this(0, 0, width, height, message, font);
|
|
}
|
|
|
|
public StringWidget(int i, int j, int k, int l, Component component, Font font) {
|
|
super(i, j, k, l, component, font);
|
|
this.active = false;
|
|
}
|
|
|
|
public StringWidget setColor(int i) {
|
|
super.setColor(i);
|
|
return this;
|
|
}
|
|
|
|
private StringWidget horizontalAlignment(float horizontalAlignment) {
|
|
this.alignX = horizontalAlignment;
|
|
return this;
|
|
}
|
|
|
|
public StringWidget alignLeft() {
|
|
return this.horizontalAlignment(0.0F);
|
|
}
|
|
|
|
public StringWidget alignCenter() {
|
|
return this.horizontalAlignment(0.5F);
|
|
}
|
|
|
|
public StringWidget alignRight() {
|
|
return this.horizontalAlignment(1.0F);
|
|
}
|
|
|
|
@Override
|
|
public void renderWidget(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
|
|
Component component = this.getMessage();
|
|
Font font = this.getFont();
|
|
int i = this.getWidth();
|
|
int j = font.width(component);
|
|
int k = this.getX() + Math.round(this.alignX * (i - j));
|
|
int l = this.getY() + (this.getHeight() - 9) / 2;
|
|
FormattedCharSequence formattedCharSequence = j > i ? this.clipText(component, i) : component.getVisualOrderText();
|
|
guiGraphics.drawString(font, formattedCharSequence, k, l, this.getColor());
|
|
}
|
|
|
|
private FormattedCharSequence clipText(Component message, int width) {
|
|
Font font = this.getFont();
|
|
FormattedText formattedText = font.substrByWidth(message, width - font.width(CommonComponents.ELLIPSIS));
|
|
return Language.getInstance().getVisualOrder(FormattedText.composite(formattedText, CommonComponents.ELLIPSIS));
|
|
}
|
|
}
|