Tooltip event rewrite (#8038)

Redesigns the tooltip events, and adds a feature where mods can register factories for ClientTooltipComponents, which are used for custom rendering in tooltips. The old events are maintained for binary compatibility.
This commit is contained in:
Take Weiland authored and GitHub committed 2021-10-11 20:37:14 +02:00
1 parent 95be0d87d9
commit 6a25c99097
10 files changed
+574 -50

No files matched your search

@@ -23,7 +23,9 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.*;
import com.mojang.datafixers.util.Either;
import net.minecraft.client.gui.chat.NarratorChatListener;
import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent;
import net.minecraft.client.gui.screens.multiplayer.JoinMultiplayerScreen;
import net.minecraft.client.model.geom.ModelLayerLocation;
import net.minecraft.client.model.geom.builders.LayerDefinition;
@@ -31,10 +33,13 @@ import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.multiplayer.MultiPlayerGameMode;
import net.minecraft.client.multiplayer.ServerData;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.locale.Language;
import net.minecraft.network.Connection;
import net.minecraft.network.chat.FormattedText;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.network.protocol.status.ServerStatus;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.inventory.tooltip.TooltipComponent;
import net.minecraft.world.level.block.state.BlockState;
import com.mojang.blaze3d.platform.Window;
import net.minecraft.client.Minecraft;
@@ -139,6 +144,7 @@ import java.util.Random;
import java.util.Set;
import java.util.Stack;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -1073,4 +1079,92 @@ public class ForgeHooksClient
});
}
}
public static Font getTooltipFont(@Nullable Font forcedFont, @Nonnull ItemStack stack, Font fallbackFont)
{
if (forcedFont != null)
{
return forcedFont;
}
Font stackFont = RenderProperties.get(stack).getFont(stack);
return stackFont == null ? fallbackFont : stackFont;
}
public static RenderTooltipEvent.Pre preTooltipEvent(@Nonnull ItemStack stack, PoseStack matrixStack, int x, int y, int screenWidth, int screenHeight, @Nonnull List<ClientTooltipComponent> components, @Nullable Font forcedFont, @Nonnull Font fallbackFont)
{
var preEvent = new RenderTooltipEvent.Pre(stack, matrixStack, x, y, screenWidth, screenHeight, getTooltipFont(forcedFont, stack, fallbackFont), components);
MinecraftForge.EVENT_BUS.post(preEvent);
return preEvent;
}
public static RenderTooltipEvent.Color colorTooltipEvent(@Nonnull ItemStack stack, PoseStack matrixStack, int x, int y, @Nonnull Font font, @Nonnull List<ClientTooltipComponent> components)
{
var colorEvent = new RenderTooltipEvent.Color(stack, matrixStack, x, y, font, 0xf0100010, 0x505000FF, 0x5028007f, components);
MinecraftForge.EVENT_BUS.post(colorEvent);
return colorEvent;
}
public static List<ClientTooltipComponent> gatherTooltipComponents(ItemStack stack, List<? extends FormattedText> textElements, int mouseX, int screenWidth, int screenHeight, @Nullable Font forcedFont, Font fallbackFont)
{
return gatherTooltipComponents(stack, textElements, Optional.empty(), mouseX, screenWidth, screenHeight, forcedFont, fallbackFont);
}
public static List<ClientTooltipComponent> gatherTooltipComponents(ItemStack stack, List<? extends FormattedText> textElements, Optional<TooltipComponent> itemComponent, int mouseX, int screenWidth, int screenHeight, @Nullable Font forcedFont, Font fallbackFont)
{
Font font = getTooltipFont(forcedFont, stack, fallbackFont);
List<Either<FormattedText, TooltipComponent>> elements = textElements.stream()
.map((Function<FormattedText, Either<FormattedText, TooltipComponent>>) Either::left)
.collect(Collectors.toCollection(ArrayList::new));
itemComponent.ifPresent(c -> elements.add(Either.right(c)));
var event = new RenderTooltipEvent.GatherComponents(stack, screenWidth, screenHeight, elements, -1);
MinecraftForge.EVENT_BUS.post(event);
if (event.isCanceled()) return List.of();
// text wrapping
int tooltipTextWidth = event.getTooltipElements().stream()
.mapToInt(either -> either.map(font::width, component -> 0))
.max()
.orElse(0);
boolean needsWrap = false;
int tooltipX = mouseX + 12;
if (tooltipX + tooltipTextWidth + 4 > screenWidth)
{
tooltipX = mouseX - 16 - tooltipTextWidth;
if (tooltipX < 4) // if the tooltip doesn't fit on the screen
{
if (mouseX > screenWidth / 2)
tooltipTextWidth = mouseX - 12 - 8;
else
tooltipTextWidth = screenWidth - 16 - mouseX;
needsWrap = true;
}
}
if (event.getMaxWidth() > 0 && tooltipTextWidth > event.getMaxWidth())
{
tooltipTextWidth = event.getMaxWidth();
needsWrap = true;
}
int tooltipTextWidthF = tooltipTextWidth;
if (needsWrap)
{
return event.getTooltipElements().stream()
.flatMap(either -> either.map(
text -> font.split(text, tooltipTextWidthF).stream().map(ClientTooltipComponent::create),
component -> Stream.of(ClientTooltipComponent.create(component))
))
.toList();
}
return event.getTooltipElements().stream()
.map(either -> either.map(
text -> ClientTooltipComponent.create(text instanceof Component ? ((Component) text).getVisualOrderText() : Language.getInstance().getVisualOrder(text)),
ClientTooltipComponent::create
))
.toList();
}
}
@@ -27,12 +27,15 @@ import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.function.Supplier;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.world.inventory.tooltip.TooltipComponent;
import org.apache.commons.lang3.tuple.Pair;
import com.google.common.cache.CacheBuilder;
@@ -176,4 +179,24 @@ public class MinecraftForgeClient
return textureAtlasSpriteLoaders.get(name);
}
private static final Map<Class<? extends TooltipComponent>, Function<TooltipComponent, ClientTooltipComponent>> tooltipComponentFactories = new ConcurrentHashMap<>();
/**
* Register a factory for ClientTooltipComponents.
* @param cls the class for the component
* @param factory the factory for the ClientTooltipComponent
*/
@SuppressWarnings("unchecked")
public static <T extends TooltipComponent> void registerTooltipComponentFactory(Class<T> cls, Function<? super T, ? extends ClientTooltipComponent> factory)
{
tooltipComponentFactories.put(cls, (Function<TooltipComponent, ClientTooltipComponent>) factory);
}
@Nullable
public static ClientTooltipComponent getClientTooltipComponent(TooltipComponent component)
{
var factory = tooltipComponentFactories.get(component.getClass());
return factory == null ? null : factory.apply(component);
}
}
@@ -25,9 +25,14 @@ import java.util.List;
import javax.annotation.Nonnull;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.datafixers.util.Either;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent;
import net.minecraft.world.inventory.tooltip.TooltipComponent;
import net.minecraft.world.item.ItemStack;
import net.minecraft.network.chat.FormattedText;
import net.minecraftforge.eventbus.api.Cancelable;
import net.minecraftforge.eventbus.api.Event;
/**
* A set of events which are fired at various points during tooltip rendering.
@@ -45,20 +50,35 @@ public abstract class RenderTooltipEvent extends net.minecraftforge.eventbus.api
{
@Nonnull
protected final ItemStack stack;
@Deprecated(since = "1.18", forRemoval = true) // TODO: remove in 1.18
protected final List<? extends FormattedText> lines;
protected final PoseStack matrixStack;
protected final PoseStack matrixStack; // TODO: rename in 1.18
protected int x;
protected int y;
protected Font fr;
protected final List<ClientTooltipComponent> components;
@Deprecated(since = "1.18", forRemoval = true) // TODO: remove in 1.18
public RenderTooltipEvent(@Nonnull ItemStack stack, @Nonnull List<? extends FormattedText> lines, PoseStack matrixStack, int x, int y, @Nonnull Font fr)
{
this.stack = stack;
this.lines = Collections.unmodifiableList(lines); // Leave editing to ItemTooltipEvent
this.lines = Collections.unmodifiableList(lines);
this.matrixStack = matrixStack;
this.x = x;
this.y = y;
this.fr = fr;
this.components = List.of();
}
public RenderTooltipEvent(@Nonnull ItemStack stack, PoseStack poseStack, int x, int y, @Nonnull Font font, @Nonnull List<ClientTooltipComponent> components)
{
this.stack = stack;
this.lines = List.of();
this.components = Collections.unmodifiableList(components);
this.matrixStack = poseStack;
this.x = x;
this.y = y;
this.fr = font;
}
/**
@@ -73,14 +93,27 @@ public abstract class RenderTooltipEvent extends net.minecraftforge.eventbus.api
/**
* The lines to be drawn. May change between {@link RenderTooltipEvent.Pre} and {@link RenderTooltipEvent.Post}.
*
* @return An <i>unmodifiable</i> list of strings. Use {@link ItemTooltipEvent} to modify tooltip text.
* @return An <i>unmodifiable</i> list of strings. Use {@link net.minecraftforge.event.entity.player.ItemTooltipEvent} to modify tooltip text.
* @deprecated use {@link #getComponents()}
*/
@Nonnull
@Deprecated(since = "1.18", forRemoval = true) // TODO: remove in 1.18
public List<? extends FormattedText> getLines()
{
return lines;
}
/**
* The components to be drawn.
* To modify this list use {@link GatherComponents}.
* @return an unmodifiable list of tooltip components to be drawn.
*/
@Nonnull
public List<ClientTooltipComponent> getComponents()
{
return components;
}
/**
* @return The MatrixStack of the current rendering context
*/
@@ -106,7 +139,7 @@ public abstract class RenderTooltipEvent extends net.minecraftforge.eventbus.api
}
/**
* @return The {@link FontRenderer} instance the current render is using.
* @return The {@link Font} instance the current render is using.
*/
@Nonnull
public Font getFontRenderer()
@@ -114,18 +147,93 @@ public abstract class RenderTooltipEvent extends net.minecraftforge.eventbus.api
return fr;
}
/**
* Fires when a tooltip gathers the {@link TooltipComponent}s to render. This event fires before any text wrapping
* or text processing.
* This event allows modifying the components to be rendered as well as specifying a maximum width for the tooltip.
* The maximum width will cause any text components to be wrapped.
*/
@Cancelable
public static class GatherComponents extends Event
{
private final ItemStack stack;
private final int screenWidth;
private final int screenHeight;
private final List<Either<FormattedText, TooltipComponent>> tooltipElements;
private int maxWidth;
public GatherComponents(ItemStack stack, int screenWidth, int screenHeight, List<Either<FormattedText, TooltipComponent>> tooltipElements, int maxWidth)
{
this.stack = stack;
this.screenWidth = screenWidth;
this.screenHeight = screenHeight;
this.tooltipElements = tooltipElements;
this.maxWidth = maxWidth;
}
/**
* @return the ItemStack whose tooltip is being rendered or an empty stack if this tooltip is not for a stack
*/
public ItemStack getStack()
{
return stack;
}
/**
* @return the width of the screen
*/
public int getScreenWidth()
{
return screenWidth;
}
/**
* @return the height of the screen
*/
public int getScreenHeight()
{
return screenHeight;
}
/**
* The elements to be rendered. These can be either formatted text or custom tooltip components.
* This list is modifiable.
*/
public List<Either<FormattedText, TooltipComponent>> getTooltipElements()
{
return tooltipElements;
}
/**
* @return the current maximum width for the text components of the tooltip (or -1 for no maximum width)
*/
public int getMaxWidth()
{
return maxWidth;
}
/**
* Set the maximum width for the text components of the tooltip (or -1 for no maximum width)
*/
public void setMaxWidth(int maxWidth)
{
this.maxWidth = maxWidth;
}
}
/**
* This event is fired before any tooltip calculations are done. It provides setters for all aspects of the tooltip, so the final render can be modified.
* <p>
* This event is {@link Cancelable}.
*/
@net.minecraftforge.eventbus.api.Cancelable
@Cancelable
public static class Pre extends RenderTooltipEvent
{
private int screenWidth;
private int screenHeight;
private int maxWidth;
@Deprecated(since = "1.18", forRemoval = true) // TODO: remove in 1.18
public Pre(@Nonnull ItemStack stack, @Nonnull List<? extends FormattedText> lines, PoseStack matrixStack, int x, int y, int screenWidth, int screenHeight, int maxWidth, @Nonnull Font fr)
{
super(stack, lines, matrixStack, x, y, fr);
@@ -134,11 +242,20 @@ public abstract class RenderTooltipEvent extends net.minecraftforge.eventbus.api
this.maxWidth = maxWidth;
}
public Pre(@Nonnull ItemStack stack, PoseStack matrixStack, int x, int y, int screenWidth, int screenHeight, @Nonnull Font font, @Nonnull List<ClientTooltipComponent> components)
{
super(stack, matrixStack, x, y, font, components);
this.screenWidth = screenWidth;
this.screenHeight = screenHeight;
this.maxWidth = -1;
}
public int getScreenWidth()
{
return screenWidth;
}
@Deprecated(since = "1.18", forRemoval = true) // TODO: remove in 1.18
public void setScreenWidth(int screenWidth)
{
this.screenWidth = screenWidth;
@@ -149,6 +266,7 @@ public abstract class RenderTooltipEvent extends net.minecraftforge.eventbus.api
return screenHeight;
}
@Deprecated(since = "1.18", forRemoval = true) // TODO: remove in 1.18
public void setScreenHeight(int screenHeight)
{
this.screenHeight = screenHeight;
@@ -156,7 +274,9 @@ public abstract class RenderTooltipEvent extends net.minecraftforge.eventbus.api
/**
* @return The max width the tooltip can be. Defaults to -1 (unlimited).
* @deprecated use {@link GatherComponents}
*/
@Deprecated(since = "1.18", forRemoval = true) // TODO: remove in 1.18
public int getMaxWidth()
{
return maxWidth;
@@ -164,14 +284,16 @@ public abstract class RenderTooltipEvent extends net.minecraftforge.eventbus.api
/**
* Sets the max width of the tooltip. Use -1 for unlimited.
* @deprecated use {@link GatherComponents}
*/
@Deprecated(since = "1.18", forRemoval = true) // TODO: remove in 1.18
public void setMaxWidth(int maxWidth)
{
this.maxWidth = maxWidth;
}
/**
* Sets the {@link FontRenderer} to be used to render text.
* Sets the {@link Font} to be used to render text.
*/
public void setFontRenderer(@Nonnull Font fr)
{
@@ -204,6 +326,7 @@ public abstract class RenderTooltipEvent extends net.minecraftforge.eventbus.api
* <li>{@link RenderTooltipEvent.PostText}</li>
* </ul>
*/
@Deprecated(since = "1.18", forRemoval = true) // TODO: remove in 1.18
protected static abstract class Post extends RenderTooltipEvent
{
private final int width;
@@ -236,6 +359,7 @@ public abstract class RenderTooltipEvent extends net.minecraftforge.eventbus.api
/**
* This event is fired directly after the tooltip background is drawn, but before any text is drawn.
*/
@Deprecated(since = "1.18", forRemoval = true) // TODO: remove in 1.18
public static class PostBackground extends Post
{
public PostBackground(@Nonnull ItemStack stack, @Nonnull List<? extends FormattedText> textLines, PoseStack matrixStack, int x, int y, @Nonnull Font fr, int width, int height)
@@ -245,6 +369,7 @@ public abstract class RenderTooltipEvent extends net.minecraftforge.eventbus.api
/**
* This event is fired directly after the tooltip text is drawn, but before the GL state is reset.
*/
@Deprecated(since = "1.18", forRemoval = true) // TODO: remove in 1.18
public static class PostText extends Post
{
public PostText(@Nonnull ItemStack stack, @Nonnull List<? extends FormattedText> textLines, PoseStack matrixStack, int x, int y, @Nonnull Font fr, int width, int height)
@@ -259,30 +384,69 @@ public abstract class RenderTooltipEvent extends net.minecraftforge.eventbus.api
private final int originalBackground;
private final int originalBorderStart;
private final int originalBorderEnd;
private int background;
private int backgroundStart;
private int backgroundEnd;
private int borderStart;
private int borderEnd;
public Color(@Nonnull ItemStack stack, @Nonnull List<? extends FormattedText> textLines, PoseStack matrixStack, int x, int y, @Nonnull Font fr, int background, int borderStart,
int borderEnd)
@Deprecated(since = "1.18", forRemoval = true) // TODO: remove in 1.18
public Color(@Nonnull ItemStack stack, @Nonnull List<? extends FormattedText> textLines, PoseStack matrixStack, int x, int y, @Nonnull Font fr, int background, int borderStart, int borderEnd)
{
super(stack, textLines, matrixStack, x, y, fr);
this.originalBackground = background;
this.originalBorderStart = borderStart;
this.originalBorderEnd = borderEnd;
this.background = background;
this.backgroundStart = background;
this.backgroundEnd = background;
this.borderStart = borderStart;
this.borderEnd = borderEnd;
}
public Color(@Nonnull ItemStack stack, PoseStack matrixStack, int x, int y, @Nonnull Font fr, int background, int borderStart, int borderEnd, @Nonnull List<ClientTooltipComponent> components)
{
super(stack, matrixStack, x, y, fr, components);
this.originalBackground = background;
this.originalBorderStart = borderStart;
this.originalBorderEnd = borderEnd;
this.backgroundStart = background;
this.backgroundEnd = background;
this.borderStart = borderStart;
this.borderEnd = borderEnd;
}
/**
* @deprecated use {@link #getBackgroundStart()} and {@link #getBackgroundEnd}
*/
@Deprecated(since = "1.18", forRemoval = true) // TODO: remove in 1.18
public int getBackground()
{
return background;
return backgroundStart;
}
public int getBackgroundStart()
{
return backgroundStart;
}
public int getBackgroundEnd()
{
return backgroundEnd;
}
public void setBackground(int background)
{
this.background = background;
this.backgroundStart = background;
this.backgroundEnd = background;
}
public void setBackgroundStart(int backgroundStart)
{
this.backgroundStart = backgroundStart;
}
public void setBackgroundEnd(int backgroundEnd)
{
this.backgroundEnd = backgroundEnd;
}
public int getBorderStart()
@@ -305,11 +469,22 @@ public abstract class RenderTooltipEvent extends net.minecraftforge.eventbus.api
this.borderEnd = borderEnd;
}
@Deprecated(since = "1.18", forRemoval = true) // TODO: remove in 1.18
public int getOriginalBackground()
{
return originalBackground;
}
public int getOriginalBackgroundStart()
{
return originalBackground;
}
public int getOriginalBackgroundEnd()
{
return originalBackground;
}
public int getOriginalBorderStart()
{
return originalBorderStart;
@@ -237,6 +237,7 @@ public class GuiUtils
*
* @param stack The stack for which a tooltip is about to be drawn.
*/
@Deprecated(since = "1.18", forRemoval = true) // TODO: remove in 1.18
public static void preItemToolTip(@Nonnull ItemStack stack)
{
cachedTooltipStack = stack;
@@ -245,11 +246,13 @@ public class GuiUtils
/**
* Must be called from {@code GuiScreen.renderToolTip} after {@code GuiScreen.drawHoveringText} is called.
*/
@Deprecated(since = "1.18", forRemoval = true) // TODO: remove in 1.18
public static void postItemToolTip()
{
cachedTooltipStack = ItemStack.EMPTY;
}
@Deprecated(since = "1.18", forRemoval = true) // TODO: remove in 1.18
public static void drawHoveringText(PoseStack mStack, List<? extends FormattedText> textLines, int mouseX, int mouseY, int screenWidth, int screenHeight, int maxTextWidth, Font font)
{
drawHoveringText(mStack, textLines, mouseX, mouseY, screenWidth, screenHeight, maxTextWidth, DEFAULT_BACKGROUND_COLOR, DEFAULT_BORDER_COLOR_START, DEFAULT_BORDER_COLOR_END, font);
@@ -274,12 +277,14 @@ public class GuiUtils
* between the start and end values.
* @param font the font for drawing the text in the tooltip box
*/
@Deprecated(since = "1.18", forRemoval = true) // TODO: remove in 1.18
public static void drawHoveringText(PoseStack mStack, List<? extends FormattedText> textLines, int mouseX, int mouseY, int screenWidth, int screenHeight,
int maxTextWidth, int backgroundColor, int borderColorStart, int borderColorEnd, Font font)
{
drawHoveringText(cachedTooltipStack, mStack, textLines, mouseX, mouseY, screenWidth, screenHeight, maxTextWidth, backgroundColor, borderColorStart, borderColorEnd, font);
}
@Deprecated(since = "1.18", forRemoval = true) // TODO: remove in 1.18
public static void drawHoveringText(@Nonnull final ItemStack stack, PoseStack mStack, List<? extends FormattedText> textLines, int mouseX, int mouseY, int screenWidth, int screenHeight, int maxTextWidth, Font font)
{
drawHoveringText(stack, mStack, textLines, mouseX, mouseY, screenWidth, screenHeight, maxTextWidth, DEFAULT_BACKGROUND_COLOR, DEFAULT_BORDER_COLOR_START, DEFAULT_BORDER_COLOR_END, font);
@@ -290,7 +295,7 @@ public class GuiUtils
*
* @see #drawHoveringText(PoseStack, List, int, int, int, int, int, int, int, int, Font)
*/
//TODO, Validate rendering is the same as the original
@Deprecated(since = "1.18", forRemoval = true) // TODO: remove in 1.18
public static void drawHoveringText(@Nonnull final ItemStack stack, PoseStack pStack, List<? extends FormattedText> textLines, int mouseX, int mouseY,
int screenWidth, int screenHeight, int maxTextWidth,
int backgroundColor, int borderColorStart, int borderColorEnd, Font font)