Skip to content

Commit

Permalink
Replace RenderTooltipEvent.Color with RenderTooltipEvent.Texture
Browse files Browse the repository at this point in the history
  • Loading branch information
XFactHD committed Oct 18, 2024
1 parent f08da3e commit 5115cd7
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 126 deletions.
5 changes: 3 additions & 2 deletions patches/net/minecraft/client/gui/GuiGraphics.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,9 @@
int i1 = vector2ic.y();
this.pose.pushPose();
int j1 = 400;
+ net.neoforged.neoforge.client.event.RenderTooltipEvent.Color colorEvent = net.neoforged.neoforge.client.ClientHooks.onRenderTooltipColor(this.tooltipStack, this, l, i1, preEvent.getFont(), p_282615_);
TooltipRenderUtil.renderTooltipBackground(this, l, i1, i, j, 400, p_371327_);
- TooltipRenderUtil.renderTooltipBackground(this, l, i1, i, j, 400, p_371327_);
+ var textureEvent = net.neoforged.neoforge.client.ClientHooks.onRenderTooltipTexture(this.tooltipStack, this, l, i1, preEvent.getFont(), p_282615_, p_371327_);
+ TooltipRenderUtil.renderTooltipBackground(this, l, i1, i, j, 400, textureEvent.getTexture());
this.pose.translate(0.0F, 0.0F, 400.0F);
int k1 = i1;

Expand Down
7 changes: 2 additions & 5 deletions src/main/java/net/neoforged/neoforge/client/ClientHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -774,11 +774,8 @@ public static RenderTooltipEvent.Pre onRenderTooltipPre(ItemStack stack, GuiGrap
return preEvent;
}

// TODO porting: potentially replace with a background texture event
public static RenderTooltipEvent.Color onRenderTooltipColor(ItemStack stack, GuiGraphics graphics, int x, int y, Font font, List<ClientTooltipComponent> components) {
var colorEvent = new RenderTooltipEvent.Color(stack, graphics, x, y, font, 0xf0100010, 0x505000FF, 0x5028007f, components);
NeoForge.EVENT_BUS.post(colorEvent);
return colorEvent;
public static RenderTooltipEvent.Texture onRenderTooltipTexture(ItemStack stack, GuiGraphics graphics, int x, int y, Font font, List<ClientTooltipComponent> components, @Nullable ResourceLocation texture) {
return NeoForge.EVENT_BUS.post(new RenderTooltipEvent.Texture(stack, graphics, x, y, font, components, texture));
}

public static List<ClientTooltipComponent> gatherTooltipComponents(ItemStack stack, List<? extends FormattedText> textElements, int mouseX, int screenWidth, int screenHeight, Font fallbackFont) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent;
import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipPositioner;
import net.minecraft.core.component.DataComponents;
import net.minecraft.network.chat.FormattedText;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.inventory.tooltip.TooltipComponent;
import net.minecraft.world.item.ItemStack;
import net.neoforged.bus.api.Event;
Expand All @@ -21,14 +23,15 @@
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.entity.player.ItemTooltipEvent;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

/**
* Fired during tooltip rendering.
* See the various subclasses for listening to specific events.
*
* @see RenderTooltipEvent.GatherComponents
* @see RenderTooltipEvent.Pre
* @see RenderTooltipEvent.Color
* @see RenderTooltipEvent.Texture
*/
public abstract class RenderTooltipEvent extends Event {
protected final ItemStack itemStack;
Expand Down Expand Up @@ -99,7 +102,7 @@ public Font getFont() {
*
* <p>This event is {@linkplain ICancellableEvent cancellable}, and does not {@linkplain HasResult have a result}.
* If this event is cancelled, then the list of components will be empty, causing the tooltip to not be rendered and
* the corresponding {@link RenderTooltipEvent.Pre} and {@link RenderTooltipEvent.Color} to not be fired.</p>
* the corresponding {@link RenderTooltipEvent.Pre} and {@link RenderTooltipEvent.Texture} to not be fired.</p>
*
* <p>This event is fired on the {@linkplain NeoForge#EVENT_BUS main Forge event bus},
* only on the {@linkplain LogicalSide#CLIENT logical client}.</p>
Expand Down Expand Up @@ -179,7 +182,7 @@ public void setMaxWidth(int maxWidth) {
*
* <p>This event is {@linkplain ICancellableEvent cancellable}, and does not {@linkplain HasResult have a result}.
* If this event is cancelled, then the tooltip will not be rendered and the corresponding
* {@link RenderTooltipEvent.Color} will not be fired.</p>
* {@link RenderTooltipEvent.Texture} will not be fired.</p>
*
* <p>This event is fired on the {@linkplain NeoForge#EVENT_BUS main Forge event bus},
* only on the {@linkplain LogicalSide#CLIENT logical client}.</p>
Expand Down Expand Up @@ -247,136 +250,51 @@ public void setY(int y) {
}

/**
* Fired when the colours for the tooltip background are determined.
* This can be used to modify the background color and the border's gradient colors.
* Fired when the textures for the tooltip background are determined.
* This can be used to modify the background and frame texture.
*
* <p>This event is not {@linkplain ICancellableEvent cancellable}, and does not {@linkplain HasResult have a result}.</p>
* <p>This event is not {@linkplain ICancellableEvent cancellable}.</p>
*
* <p>This event is fired on the {@linkplain NeoForge#EVENT_BUS main Forge event bus},
* only on the {@linkplain LogicalSide#CLIENT logical client}.</p>
*/
public static class Color extends RenderTooltipEvent {
private final int originalBackground;
private final int originalBorderStart;
private final int originalBorderEnd;
private int backgroundStart;
private int backgroundEnd;
private int borderStart;
private int borderEnd;
public static class Texture extends RenderTooltipEvent {
@Nullable
private final ResourceLocation originalTexture;
@Nullable
private ResourceLocation texture;

@ApiStatus.Internal
public Color(ItemStack stack, GuiGraphics graphics, int x, int y, Font fr, int background, int borderStart, int borderEnd, List<ClientTooltipComponent> components) {
super(stack, graphics, 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;
}

/**
* {@return the gradient start color for the tooltip background (top edge)}
*/
public int getBackgroundStart() {
return backgroundStart;
}

/**
* {@return the gradient end color for the tooltip background (bottom edge)}
*/
public int getBackgroundEnd() {
return backgroundEnd;
}

/**
* Sets the new color for the tooltip background. This sets both the gradient start and end color for the
* background to this color.
*
* @param background the new color for the tooltip background
*/
public void setBackground(int background) {
this.backgroundStart = background;
this.backgroundEnd = background;
}

/**
* Sets the new start color for the gradient of the tooltip background (top edge).
*
* @param backgroundStart the new start color for the tooltip background
*/
public void setBackgroundStart(int backgroundStart) {
this.backgroundStart = backgroundStart;
}

/**
* Sets the new end color for the gradient of the tooltip background (bottom edge).
*
* @param backgroundEnd the new end color for the tooltip background
*/
public void setBackgroundEnd(int backgroundEnd) {
this.backgroundEnd = backgroundEnd;
}

/**
* {@return the gradient start color for the tooltip border (top edge)}
*/
public int getBorderStart() {
return borderStart;
}

/**
* Sets the new start color for the gradient of the tooltip border (top edge).
*
* @param borderStart the new start color for the tooltip border
*/
public void setBorderStart(int borderStart) {
this.borderStart = borderStart;
}

/**
* {@return the gradient end color for the tooltip border (bottom edge)}
*/
public int getBorderEnd() {
return borderEnd;
}

/**
* Sets the new end color for the gradient of the tooltip border (bottom edge).
*
* @param borderEnd the new end color for the tooltip border
*/
public void setBorderEnd(int borderEnd) {
this.borderEnd = borderEnd;
}

/**
* {@return the original tooltip background's gradient start color (top edge)}
*/
public int getOriginalBackgroundStart() {
return originalBackground;
public Texture(ItemStack stack, GuiGraphics graphics, int x, int y, Font font, List<ClientTooltipComponent> components, @Nullable ResourceLocation texture) {
super(stack, graphics, x, y, font, components);
this.originalTexture = texture;
this.texture = texture;
}

/**
* {@return the original tooltip background's gradient end color (bottom edge)}
* {@return the original texture location given to the tooltip render method (may originate from {@link DataComponents#TOOLTIP_STYLE})}
*/
public int getOriginalBackgroundEnd() {
return originalBackground;
@Nullable
public ResourceLocation getOriginalTexture() {
return originalTexture;
}

/**
* {@return the original tooltip border's gradient start color (top edge)}
* {@return the texture location that will be used to render the tooltip}
*/
public int getOriginalBorderStart() {
return originalBorderStart;
@Nullable
public ResourceLocation getTexture() {
return texture;
}

/**
* {@return the original tooltip border's gradient end color (bottom edge)}
* Set the texture to use for the tooltip background and frame or {@code null} to use the default textures.
* <p>
* The given {@link ResourceLocation} will be prefixed with {@code tooltip/} and suffixed with {@code _background}
* and {@code _frame} to determine the background and frame texture respectively
*/
public int getOriginalBorderEnd() {
return originalBorderEnd;
public void setTexture(@Nullable ResourceLocation texture) {
this.texture = texture;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
Expand Down Expand Up @@ -115,6 +116,8 @@ public static void onRegisterClientTooltipComponentFactories(RegisterClientToolt
}

private static class ClientEventHandler {
private static final ResourceLocation TEXTURE = ResourceLocation.fromNamespaceAndPath(ID, "test");

@SubscribeEvent
public static void gatherTooltips(RenderTooltipEvent.GatherComponents event) {
if (event.getItemStack().getItem() == Items.STICK) {
Expand All @@ -126,12 +129,9 @@ public static void gatherTooltips(RenderTooltipEvent.GatherComponents event) {
}

@SubscribeEvent
public static void preTooltip(RenderTooltipEvent.Color event) {
public static void preTooltip(RenderTooltipEvent.Texture event) {
if (event.getItemStack().getItem() == Items.APPLE) {
event.setBackgroundStart(0xFF0000FF);
event.setBackgroundEnd(0xFFFFFF00);
event.setBorderStart(0xFFFF0000);
event.setBorderEnd(0xFF000011);
event.setTexture(TEXTURE);
}
}
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"gui": {
"scaling": {
"type": "nine_slice",
"width": 100,
"height": 100,
"border": 9,
"stretch_inner": true
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"gui": {
"scaling": {
"type": "nine_slice",
"width": 100,
"height": 100,
"border": 10,
"stretch_inner": true
}
}
}

0 comments on commit 5115cd7

Please sign in to comment.