|  | 
|  | 1 | +package mezz.jei.common.gui.elements; | 
|  | 2 | + | 
|  | 3 | +import mezz.jei.api.gui.builder.ITooltipBuilder; | 
|  | 4 | +import mezz.jei.api.gui.widgets.IRecipeWidget; | 
|  | 5 | +import mezz.jei.api.gui.widgets.ITextWidget; | 
|  | 6 | +import mezz.jei.common.config.DebugConfig; | 
|  | 7 | +import mezz.jei.common.util.HorizontalAlignment; | 
|  | 8 | +import mezz.jei.common.util.ImmutableRect2i; | 
|  | 9 | +import mezz.jei.common.util.StringUtil; | 
|  | 10 | +import mezz.jei.common.util.VerticalAlignment; | 
|  | 11 | +import mezz.jei.core.util.Pair; | 
|  | 12 | +import net.minecraft.client.Minecraft; | 
|  | 13 | +import net.minecraft.client.gui.Font; | 
|  | 14 | +import net.minecraft.client.gui.GuiGraphics; | 
|  | 15 | +import net.minecraft.client.gui.navigation.ScreenPosition; | 
|  | 16 | +import net.minecraft.locale.Language; | 
|  | 17 | +import net.minecraft.network.chat.FormattedText; | 
|  | 18 | +import net.minecraft.util.FormattedCharSequence; | 
|  | 19 | +import org.jetbrains.annotations.Nullable; | 
|  | 20 | + | 
|  | 21 | +import java.util.List; | 
|  | 22 | + | 
|  | 23 | +public class TextWidget implements ITextWidget, IRecipeWidget { | 
|  | 24 | +	private final List<FormattedText> text; | 
|  | 25 | +	private final ImmutableRect2i area; | 
|  | 26 | + | 
|  | 27 | +	private HorizontalAlignment horizontalAlignment; | 
|  | 28 | +	private VerticalAlignment verticalAlignment; | 
|  | 29 | +	private Font font; | 
|  | 30 | +	private int color; | 
|  | 31 | +	private boolean shadow; | 
|  | 32 | +	private int lineSpacing; | 
|  | 33 | +	private List<FormattedText> tooltipText = List.of(); | 
|  | 34 | +	private @Nullable List<FormattedText> wrappedText; | 
|  | 35 | + | 
|  | 36 | +	public TextWidget(List<FormattedText> text, int xPos, int yPos, int maxWidth, int maxHeight) { | 
|  | 37 | +		this.area = new ImmutableRect2i(xPos, yPos, maxWidth, maxHeight); | 
|  | 38 | +		Minecraft minecraft = Minecraft.getInstance(); | 
|  | 39 | +		this.font = minecraft.font; | 
|  | 40 | +		this.color = 0xFF000000; | 
|  | 41 | +		this.text = text; | 
|  | 42 | +		this.lineSpacing = 2; | 
|  | 43 | +		this.horizontalAlignment = HorizontalAlignment.LEFT; | 
|  | 44 | +		this.verticalAlignment = VerticalAlignment.TOP; | 
|  | 45 | +	} | 
|  | 46 | + | 
|  | 47 | +	@Override | 
|  | 48 | +	public ITextWidget alignHorizontalLeft() { | 
|  | 49 | +		this.horizontalAlignment = HorizontalAlignment.LEFT; | 
|  | 50 | +		return this; | 
|  | 51 | +	} | 
|  | 52 | + | 
|  | 53 | +	@Override | 
|  | 54 | +	public ITextWidget alignHorizontalRight() { | 
|  | 55 | +		this.horizontalAlignment = HorizontalAlignment.RIGHT; | 
|  | 56 | +		return this; | 
|  | 57 | +	} | 
|  | 58 | + | 
|  | 59 | +	@Override | 
|  | 60 | +	public ITextWidget alignHorizontalCenter() { | 
|  | 61 | +		this.horizontalAlignment = HorizontalAlignment.CENTER; | 
|  | 62 | +		return this; | 
|  | 63 | +	} | 
|  | 64 | + | 
|  | 65 | +	@Override | 
|  | 66 | +	public ITextWidget alignVerticalTop() { | 
|  | 67 | +		this.verticalAlignment = VerticalAlignment.TOP; | 
|  | 68 | +		return this; | 
|  | 69 | +	} | 
|  | 70 | + | 
|  | 71 | +	@Override | 
|  | 72 | +	public ITextWidget alignVerticalCenter() { | 
|  | 73 | +		this.verticalAlignment = VerticalAlignment.CENTER; | 
|  | 74 | +		return this; | 
|  | 75 | +	} | 
|  | 76 | + | 
|  | 77 | +	@Override | 
|  | 78 | +	public ITextWidget alignVerticalBottom() { | 
|  | 79 | +		this.verticalAlignment = VerticalAlignment.BOTTOM; | 
|  | 80 | +		return this; | 
|  | 81 | +	} | 
|  | 82 | + | 
|  | 83 | +	@Override | 
|  | 84 | +	public ITextWidget setFont(Font font) { | 
|  | 85 | +		this.font = font; | 
|  | 86 | +		return this; | 
|  | 87 | +	} | 
|  | 88 | + | 
|  | 89 | +	@Override | 
|  | 90 | +	public ITextWidget setColor(int color) { | 
|  | 91 | +		this.color = color; | 
|  | 92 | +		return this; | 
|  | 93 | +	} | 
|  | 94 | + | 
|  | 95 | +	@Override | 
|  | 96 | +	public ITextWidget setLineSpacing(int lineSpacing) { | 
|  | 97 | +		this.lineSpacing = lineSpacing; | 
|  | 98 | +		return this; | 
|  | 99 | +	} | 
|  | 100 | + | 
|  | 101 | +	@Override | 
|  | 102 | +	public ITextWidget setShadow(boolean shadow) { | 
|  | 103 | +		this.shadow = shadow; | 
|  | 104 | +		return this; | 
|  | 105 | +	} | 
|  | 106 | + | 
|  | 107 | +	@Override | 
|  | 108 | +	public ScreenPosition getPosition() { | 
|  | 109 | +		return area.getScreenPosition(); | 
|  | 110 | +	} | 
|  | 111 | + | 
|  | 112 | +	private List<FormattedText> calculateWrappedText() { | 
|  | 113 | +		if (wrappedText != null) { | 
|  | 114 | +			return wrappedText; | 
|  | 115 | +		} | 
|  | 116 | +		int lineHeight = getLineHeight(); | 
|  | 117 | +		int maxLines = area.height() / lineHeight; | 
|  | 118 | +		if (maxLines * lineHeight + font.lineHeight <= area.height()) { | 
|  | 119 | +			maxLines++; | 
|  | 120 | +		} | 
|  | 121 | +		Pair<List<FormattedText>, Boolean> result = StringUtil.splitLines(font, text, area.width(), maxLines); | 
|  | 122 | +		this.wrappedText = result.first(); | 
|  | 123 | +		boolean truncated = result.second(); | 
|  | 124 | +		if (truncated) { | 
|  | 125 | +			this.tooltipText = text; | 
|  | 126 | +		} else { | 
|  | 127 | +			this.tooltipText = List.of(); | 
|  | 128 | +		} | 
|  | 129 | +		return wrappedText; | 
|  | 130 | +	} | 
|  | 131 | + | 
|  | 132 | +	private int getLineHeight() { | 
|  | 133 | +		return font.lineHeight + lineSpacing; | 
|  | 134 | +	} | 
|  | 135 | + | 
|  | 136 | +	@Override | 
|  | 137 | +	public void drawWidget(GuiGraphics guiGraphics, double mouseX, double mouseY) { | 
|  | 138 | +		Language language = Language.getInstance(); | 
|  | 139 | + | 
|  | 140 | +		final int lineHeight = getLineHeight(); | 
|  | 141 | +		List<FormattedText> lines = calculateWrappedText(); | 
|  | 142 | +		int yPos = getYPosStart(lineHeight, lines); | 
|  | 143 | +		for (FormattedText line : lines) { | 
|  | 144 | +			FormattedCharSequence charSequence = language.getVisualOrder(line); | 
|  | 145 | +			int xPos = getXPos(charSequence); | 
|  | 146 | +			guiGraphics.drawString(font, charSequence, xPos, yPos, color, shadow); | 
|  | 147 | +			yPos += lineHeight; | 
|  | 148 | +		} | 
|  | 149 | + | 
|  | 150 | +		if (DebugConfig.isDebugGuisEnabled()) { | 
|  | 151 | +			guiGraphics.fill(0,0, area.width(), area.height(), 0xAAAAAA00); | 
|  | 152 | +		} | 
|  | 153 | +	} | 
|  | 154 | + | 
|  | 155 | +	@Override | 
|  | 156 | +	public void getTooltip(ITooltipBuilder tooltip, double mouseX, double mouseY) { | 
|  | 157 | +		if (mouseX >= 0 && mouseX < area.width() && mouseY >= 0 && mouseY < area.height()) { | 
|  | 158 | +			calculateWrappedText(); | 
|  | 159 | +			tooltip.addAll(tooltipText); | 
|  | 160 | +		} | 
|  | 161 | +	} | 
|  | 162 | + | 
|  | 163 | +	private int getXPos(FormattedCharSequence text) { | 
|  | 164 | +		return switch (horizontalAlignment) { | 
|  | 165 | +			case LEFT -> 0; | 
|  | 166 | +			case RIGHT -> this.area.width() - font.width(text); | 
|  | 167 | +			case CENTER -> Math.round((this.area.width() - font.width(text)) / 2f); | 
|  | 168 | +		}; | 
|  | 169 | +	} | 
|  | 170 | + | 
|  | 171 | +	private int getYPosStart(int lineHeight, List<FormattedText> text) { | 
|  | 172 | +		if (verticalAlignment == VerticalAlignment.TOP) { | 
|  | 173 | +			return 0; | 
|  | 174 | +		} | 
|  | 175 | + | 
|  | 176 | +		int linesHeight = (lineHeight * text.size()) - lineSpacing - 1; | 
|  | 177 | +		if (verticalAlignment == VerticalAlignment.BOTTOM) { | 
|  | 178 | +			return area.height() - linesHeight; | 
|  | 179 | +		} else if (verticalAlignment == VerticalAlignment.CENTER) { | 
|  | 180 | +			return Math.round((area.height() - linesHeight) / 2f); | 
|  | 181 | +		} else { | 
|  | 182 | +			throw new IllegalArgumentException("Unknown verticalAlignment " + verticalAlignment); | 
|  | 183 | +		} | 
|  | 184 | +	} | 
|  | 185 | +} | 
0 commit comments