127 lines
		
	
	
	
		
			3.9 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
			
		
		
	
	
			127 lines
		
	
	
	
		
			3.9 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
| package net.minecraft.client.gui.components;
 | |
| 
 | |
| import java.util.OptionalInt;
 | |
| import java.util.function.Consumer;
 | |
| import net.fabricmc.api.EnvType;
 | |
| import net.fabricmc.api.Environment;
 | |
| import net.minecraft.Util;
 | |
| import net.minecraft.client.gui.Font;
 | |
| import net.minecraft.client.gui.GuiGraphics;
 | |
| import net.minecraft.network.chat.Component;
 | |
| import net.minecraft.network.chat.Style;
 | |
| import net.minecraft.util.SingleKeyCache;
 | |
| import org.jetbrains.annotations.Nullable;
 | |
| 
 | |
| @Environment(EnvType.CLIENT)
 | |
| public class MultiLineTextWidget extends AbstractStringWidget {
 | |
| 	private OptionalInt maxWidth = OptionalInt.empty();
 | |
| 	private OptionalInt maxRows = OptionalInt.empty();
 | |
| 	private final SingleKeyCache<MultiLineTextWidget.CacheKey, MultiLineLabel> cache;
 | |
| 	private boolean centered = false;
 | |
| 	private boolean allowHoverComponents = false;
 | |
| 	@Nullable
 | |
| 	private Consumer<Style> componentClickHandler = null;
 | |
| 
 | |
| 	public MultiLineTextWidget(Component message, Font font) {
 | |
| 		this(0, 0, message, font);
 | |
| 	}
 | |
| 
 | |
| 	public MultiLineTextWidget(int x, int y, Component message, Font font) {
 | |
| 		super(x, y, 0, 0, message, font);
 | |
| 		this.cache = Util.singleKeyCache(
 | |
| 			cacheKey -> cacheKey.maxRows.isPresent()
 | |
| 				? MultiLineLabel.create(font, cacheKey.maxWidth, cacheKey.maxRows.getAsInt(), cacheKey.message)
 | |
| 				: MultiLineLabel.create(font, cacheKey.message, cacheKey.maxWidth)
 | |
| 		);
 | |
| 		this.active = false;
 | |
| 	}
 | |
| 
 | |
| 	public MultiLineTextWidget setColor(int color) {
 | |
| 		super.setColor(color);
 | |
| 		return this;
 | |
| 	}
 | |
| 
 | |
| 	public MultiLineTextWidget setMaxWidth(int maxWidth) {
 | |
| 		this.maxWidth = OptionalInt.of(maxWidth);
 | |
| 		return this;
 | |
| 	}
 | |
| 
 | |
| 	public MultiLineTextWidget setMaxRows(int maxRows) {
 | |
| 		this.maxRows = OptionalInt.of(maxRows);
 | |
| 		return this;
 | |
| 	}
 | |
| 
 | |
| 	public MultiLineTextWidget setCentered(boolean centered) {
 | |
| 		this.centered = centered;
 | |
| 		return this;
 | |
| 	}
 | |
| 
 | |
| 	public MultiLineTextWidget configureStyleHandling(boolean allowHoverComponents, @Nullable Consumer<Style> componentClickHandler) {
 | |
| 		this.allowHoverComponents = allowHoverComponents;
 | |
| 		this.componentClickHandler = componentClickHandler;
 | |
| 		return this;
 | |
| 	}
 | |
| 
 | |
| 	@Override
 | |
| 	public int getWidth() {
 | |
| 		return this.cache.getValue(this.getFreshCacheKey()).getWidth();
 | |
| 	}
 | |
| 
 | |
| 	@Override
 | |
| 	public int getHeight() {
 | |
| 		return this.cache.getValue(this.getFreshCacheKey()).getLineCount() * 9;
 | |
| 	}
 | |
| 
 | |
| 	@Override
 | |
| 	public void renderWidget(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
 | |
| 		MultiLineLabel multiLineLabel = this.cache.getValue(this.getFreshCacheKey());
 | |
| 		int i = this.getX();
 | |
| 		int j = this.getY();
 | |
| 		int k = 9;
 | |
| 		int l = this.getColor();
 | |
| 		if (this.centered) {
 | |
| 			multiLineLabel.renderCentered(guiGraphics, i + this.getWidth() / 2, j, k, l);
 | |
| 		} else {
 | |
| 			multiLineLabel.renderLeftAligned(guiGraphics, i, j, k, l);
 | |
| 		}
 | |
| 
 | |
| 		if (this.allowHoverComponents) {
 | |
| 			Style style = this.getComponentStyleAt(mouseX, mouseY);
 | |
| 			if (this.isHovered()) {
 | |
| 				guiGraphics.renderComponentHoverEffect(this.getFont(), style, mouseX, mouseY);
 | |
| 			}
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	@Nullable
 | |
| 	private Style getComponentStyleAt(double mouseX, double mouseY) {
 | |
| 		MultiLineLabel multiLineLabel = this.cache.getValue(this.getFreshCacheKey());
 | |
| 		int i = this.getX();
 | |
| 		int j = this.getY();
 | |
| 		int k = 9;
 | |
| 		return this.centered
 | |
| 			? multiLineLabel.getStyleAtCentered(i + this.getWidth() / 2, j, k, mouseX, mouseY)
 | |
| 			: multiLineLabel.getStyleAtLeftAligned(i, j, k, mouseX, mouseY);
 | |
| 	}
 | |
| 
 | |
| 	@Override
 | |
| 	public void onClick(double mouseX, double mouseY) {
 | |
| 		if (this.componentClickHandler != null) {
 | |
| 			Style style = this.getComponentStyleAt(mouseX, mouseY);
 | |
| 			if (style != null) {
 | |
| 				this.componentClickHandler.accept(style);
 | |
| 				return;
 | |
| 			}
 | |
| 		}
 | |
| 
 | |
| 		super.onClick(mouseX, mouseY);
 | |
| 	}
 | |
| 
 | |
| 	private MultiLineTextWidget.CacheKey getFreshCacheKey() {
 | |
| 		return new MultiLineTextWidget.CacheKey(this.getMessage(), this.maxWidth.orElse(Integer.MAX_VALUE), this.maxRows);
 | |
| 	}
 | |
| 
 | |
| 	@Environment(EnvType.CLIENT)
 | |
| 	record CacheKey(Component message, int maxWidth, OptionalInt maxRows) {
 | |
| 	}
 | |
| }
 |