diff options
author | Anthony Hilyard <anthony.hilyard@gmail.com> | 2021-12-04 21:27:28 -0800 |
---|---|---|
committer | Anthony Hilyard <anthony.hilyard@gmail.com> | 2021-12-04 21:27:28 -0800 |
commit | f5d35697f524cda1736740e739f8afd902651c53 (patch) | |
tree | 1b582f6420418f62fbef9755434fbaebe7644ff6 /src/main/java/com/anthonyhilyard/iceberg/util/Tooltips.java | |
parent | 36a1f98bba70892f4e06c3df8b2b0bb4d0d5fe03 (diff) | |
download | Iceberg-f5d35697f524cda1736740e739f8afd902651c53.tar.gz Iceberg-f5d35697f524cda1736740e739f8afd902651c53.tar.bz2 Iceberg-f5d35697f524cda1736740e739f8afd902651c53.zip |
Initial 1.18 port. Overhauled tooltip handling, added color code item
color support.
Diffstat (limited to 'src/main/java/com/anthonyhilyard/iceberg/util/Tooltips.java')
-rw-r--r-- | src/main/java/com/anthonyhilyard/iceberg/util/Tooltips.java | 325 |
1 files changed, 89 insertions, 236 deletions
diff --git a/src/main/java/com/anthonyhilyard/iceberg/util/Tooltips.java b/src/main/java/com/anthonyhilyard/iceberg/util/Tooltips.java index bea4b5a..b3ab741 100644 --- a/src/main/java/com/anthonyhilyard/iceberg/util/Tooltips.java +++ b/src/main/java/com/anthonyhilyard/iceberg/util/Tooltips.java @@ -7,84 +7,58 @@ import javax.annotation.Nonnull; import com.anthonyhilyard.iceberg.events.RenderTooltipExtEvent; import com.mojang.blaze3d.vertex.PoseStack; -import com.mojang.blaze3d.systems.RenderSystem; +import net.minecraft.client.Minecraft; import net.minecraft.client.gui.Font; +import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent; import net.minecraft.client.renderer.MultiBufferSource; import net.minecraft.client.renderer.Rect2i; import net.minecraft.client.renderer.MultiBufferSource.BufferSource; +import net.minecraft.client.renderer.entity.ItemRenderer; import com.mojang.blaze3d.vertex.Tesselator; import net.minecraft.world.item.ItemStack; import com.mojang.math.Matrix4f; -import net.minecraft.network.chat.FormattedText; -import net.minecraft.locale.Language; -import net.minecraft.network.chat.Style; import net.minecraftforge.client.event.RenderTooltipEvent; import net.minecraftforge.common.MinecraftForge; -import net.minecraftforge.fmlclient.gui.GuiUtils; +import net.minecraftforge.client.gui.GuiUtils; public class Tooltips { + private static ItemRenderer itemRenderer = null; + public static class TooltipInfo { private int tooltipWidth = 0; private int titleLines = 1; private Font font; - private List<? extends FormattedText> lines = new ArrayList<>(); + private List<ClientTooltipComponent> components = new ArrayList<>(); - public TooltipInfo(List<? extends FormattedText> lines, Font font) + public TooltipInfo(List<ClientTooltipComponent> components, Font font) { - this.lines = lines; + this.components = components; this.font = font; } public int getTooltipWidth() { return tooltipWidth; } public int getTitleLines() { return titleLines; } public Font getFont() { return font; } - public List<? extends FormattedText> getLines() { return lines; } + public List<ClientTooltipComponent> getComponents() { return components; } public void setFont(Font font) { this.font = font; } public int getMaxLineWidth() { - int textWidth = 0; - for (FormattedText textLine : lines) + int width = 0; + for (ClientTooltipComponent component : components) { - int textLineWidth = font.width(textLine); - if (textLineWidth > textWidth) + int componentWidth = component.getWidth(font); + if (componentWidth > width) { - textWidth = textLineWidth; + width = componentWidth; } } - return textWidth; - } - - public void wrap(int maxWidth) - { - tooltipWidth = 0; - List<FormattedText> wrappedLines = new ArrayList<>(); - for (int i = 0; i < lines.size(); i++) - { - FormattedText textLine = lines.get(i); - List<FormattedText> wrappedLine = font.getSplitter().splitLines(textLine, maxWidth, Style.EMPTY); - if (i == 0) - { - titleLines = wrappedLine.size(); - } - - for (FormattedText line : wrappedLine) - { - int lineWidth = font.width(line); - if (lineWidth > tooltipWidth) - { - tooltipWidth = lineWidth; - } - wrappedLines.add(line); - } - } - - lines = wrappedLines; + return width; } } @@ -102,159 +76,108 @@ public class Tooltips renderItemTooltip(stack, poseStack, info, rect, screenWidth, screenHeight, backgroundColor, borderColorStart, borderColorEnd, comparison, false); } - @SuppressWarnings("removal") public static void renderItemTooltip(@Nonnull final ItemStack stack, PoseStack poseStack, TooltipInfo info, Rect2i rect, int screenWidth, int screenHeight, int backgroundColor, int borderColorStart, int borderColorEnd, boolean comparison, boolean constrain) { - if (info.getLines().isEmpty()) - { - return; - } - - int rectX = rect.getX() - 8; - int rectY = rect.getY() + 18; - int maxTextWidth = rect.getWidth() - 8; + renderItemTooltip(stack, poseStack, info, rect, screenWidth, screenHeight, backgroundColor, backgroundColor, borderColorStart, borderColorEnd, comparison, constrain); + } - RenderTooltipExtEvent.Pre event = new RenderTooltipExtEvent.Pre(stack, info.getLines(), poseStack, rectX, rectY, screenWidth, screenHeight, maxTextWidth, info.getFont(), comparison); - if (MinecraftForge.EVENT_BUS.post(event)) + public static void renderItemTooltip(@Nonnull final ItemStack stack, PoseStack poseStack, TooltipInfo info, + Rect2i rect, int screenWidth, int screenHeight, + int backgroundColorStart, int backgroundColorEnd, int borderColorStart, int borderColorEnd, boolean comparison, boolean constrain) + { + if (info.getComponents().isEmpty()) { return; } - rectX = event.getX(); - rectY = event.getY(); - screenWidth = event.getScreenWidth(); - screenHeight = event.getScreenHeight(); - maxTextWidth = event.getMaxWidth(); - info.setFont(event.getFontRenderer()); - - RenderSystem.disableDepthTest(); - int tooltipTextWidth = info.getMaxLineWidth(); - - // Constrain the minimum width to the rect. - if (constrain) - { - tooltipTextWidth = Math.max(info.getMaxLineWidth(), rect.getWidth() - 8); - } - - boolean needsWrap = false; - - int tooltipX = rectX + 12; - if (tooltipX + tooltipTextWidth + 4 > screenWidth) + // Grab the itemRenderer now if needed. + if (itemRenderer == null) { - tooltipX = rectX - 16 - tooltipTextWidth; - if (tooltipX < 4) // if the tooltip doesn't fit on the screen - { - if (rectX > screenWidth / 2) - { - tooltipTextWidth = rectX - 12 - 8; - } - else - { - tooltipTextWidth = screenWidth - 16 - rectX; - } - needsWrap = true; - } + itemRenderer = Minecraft.getInstance().getItemRenderer(); } - if (maxTextWidth > 0 && tooltipTextWidth > maxTextWidth) - { - tooltipTextWidth = maxTextWidth; - needsWrap = true; - } - - if (needsWrap) + int rectX = rect.getX() - 8; + int rectY = rect.getY() + 18; + + RenderTooltipExtEvent.Pre preEvent = new RenderTooltipExtEvent.Pre(stack, poseStack, rectX, rectY, screenWidth, screenHeight, info.getFont(), info.getComponents(), comparison); + if (MinecraftForge.EVENT_BUS.post(preEvent)) { - info.wrap(tooltipTextWidth); - tooltipTextWidth = info.getTooltipWidth(); - tooltipX = rectX + 12; + return; } - int tooltipY = rectY - 12; - int tooltipHeight = 8; - - if (info.getLines().size() > 1) - { - tooltipHeight += (info.getLines().size() - 1) * 10; - if (info.getLines().size() > info.getTitleLines()) - { - tooltipHeight += 2; // gap between title lines and next lines - } - } + rectX = preEvent.getX(); + rectY = preEvent.getY(); + screenWidth = preEvent.getScreenWidth(); + screenHeight = preEvent.getScreenHeight(); + info.setFont(preEvent.getFont()); - if (tooltipY < 4) - { - tooltipY = 4; - } - else if (tooltipY + tooltipHeight + 4 > screenHeight) - { - tooltipY = screenHeight - tooltipHeight - 4; - } + poseStack.pushPose(); final int zLevel = 400; - RenderTooltipExtEvent.Color colorEvent = new RenderTooltipExtEvent.Color(stack, info.getLines(), poseStack, tooltipX, tooltipY, info.getFont(), backgroundColor, borderColorStart, borderColorEnd, comparison); + float f = itemRenderer.blitOffset; + itemRenderer.blitOffset = zLevel; + Matrix4f mat = poseStack.last().pose(); + + RenderTooltipExtEvent.Color colorEvent = new RenderTooltipExtEvent.Color(stack, poseStack, rectX, rectY, info.getFont(), backgroundColorStart, backgroundColorEnd, borderColorStart, borderColorEnd, info.getComponents(), comparison); MinecraftForge.EVENT_BUS.post(colorEvent); - backgroundColor = colorEvent.getBackground(); + + backgroundColorStart = colorEvent.getBackgroundStart(); + backgroundColorEnd = colorEvent.getBackgroundEnd(); borderColorStart = colorEvent.getBorderStart(); borderColorEnd = colorEvent.getBorderEnd(); - poseStack.pushPose(); - Matrix4f mat = poseStack.last().pose(); - - GuiUtils.drawGradientRect(mat, zLevel, tooltipX - 3, tooltipY - 4, tooltipX + tooltipTextWidth + 3, tooltipY - 3, backgroundColor, backgroundColor); - GuiUtils.drawGradientRect(mat, zLevel, tooltipX - 3, tooltipY + tooltipHeight + 3, tooltipX + tooltipTextWidth + 3, tooltipY + tooltipHeight + 4, backgroundColor, backgroundColor); - GuiUtils.drawGradientRect(mat, zLevel, tooltipX - 3, tooltipY - 3, tooltipX + tooltipTextWidth + 3, tooltipY + tooltipHeight + 3, backgroundColor, backgroundColor); - GuiUtils.drawGradientRect(mat, zLevel, tooltipX - 4, tooltipY - 3, tooltipX - 3, tooltipY + tooltipHeight + 3, backgroundColor, backgroundColor); - GuiUtils.drawGradientRect(mat, zLevel, tooltipX + tooltipTextWidth + 3, tooltipY - 3, tooltipX + tooltipTextWidth + 4, tooltipY + tooltipHeight + 3, backgroundColor, backgroundColor); - GuiUtils.drawGradientRect(mat, zLevel, tooltipX - 3, tooltipY - 3 + 1, tooltipX - 3 + 1, tooltipY + tooltipHeight + 3 - 1, borderColorStart, borderColorEnd); - GuiUtils.drawGradientRect(mat, zLevel, tooltipX + tooltipTextWidth + 2, tooltipY - 3 + 1, tooltipX + tooltipTextWidth + 3, tooltipY + tooltipHeight + 3 - 1, borderColorStart, borderColorEnd); - GuiUtils.drawGradientRect(mat, zLevel, tooltipX - 3, tooltipY - 3, tooltipX + tooltipTextWidth + 3, tooltipY - 3 + 1, borderColorStart, borderColorStart); - GuiUtils.drawGradientRect(mat, zLevel, tooltipX - 3, tooltipY + tooltipHeight + 2, tooltipX + tooltipTextWidth + 3, tooltipY + tooltipHeight + 3, borderColorEnd, borderColorEnd); - - MinecraftForge.EVENT_BUS.post(new RenderTooltipEvent.PostBackground(stack, info.getLines(), poseStack, tooltipX, tooltipY, info.getFont(), tooltipTextWidth, tooltipHeight)); + GuiUtils.drawGradientRect(mat, zLevel, rectX - 3, rectY - 4, rectX + rect.getWidth() + 3, rectY - 3, backgroundColorStart, backgroundColorStart); + GuiUtils.drawGradientRect(mat, zLevel, rectX - 3, rectY + rect.getHeight() + 3, rectX + rect.getWidth() + 3, rectY + rect.getHeight() + 4, backgroundColorEnd, backgroundColorEnd); + GuiUtils.drawGradientRect(mat, zLevel, rectX - 3, rectY - 3, rectX + rect.getWidth() + 3, rectY + rect.getHeight() + 3, backgroundColorStart, backgroundColorEnd); + GuiUtils.drawGradientRect(mat, zLevel, rectX - 4, rectY - 3, rectX - 3, rectY + rect.getHeight() + 3, backgroundColorStart, backgroundColorEnd); + GuiUtils.drawGradientRect(mat, zLevel, rectX + rect.getWidth() + 3, rectY - 3, rectX + rect.getWidth() + 4, rectY + rect.getHeight() + 3, backgroundColorStart, backgroundColorEnd); + GuiUtils.drawGradientRect(mat, zLevel, rectX - 3, rectY - 3 + 1, rectX - 3 + 1, rectY + rect.getHeight() + 3 - 1, borderColorStart, borderColorEnd); + GuiUtils.drawGradientRect(mat, zLevel, rectX + rect.getWidth() + 2, rectY - 3 + 1, rectX + rect.getWidth() + 3, rectY + rect.getHeight() + 3 - 1, borderColorStart, borderColorEnd); + GuiUtils.drawGradientRect(mat, zLevel, rectX - 3, rectY - 3, rectX + rect.getWidth() + 3, rectY - 3 + 1, borderColorStart, borderColorStart); + GuiUtils.drawGradientRect(mat, zLevel, rectX - 3, rectY + rect.getHeight() + 2, rectX + rect.getWidth() + 3, rectY + rect.getHeight() + 3, borderColorEnd, borderColorEnd); BufferSource renderType = MultiBufferSource.immediate(Tesselator.getInstance().getBuilder()); poseStack.translate(0.0D, 0.0D, zLevel); - int tooltipTop = tooltipY; + int tooltipTop = rectY; - for (int lineNumber = 0; lineNumber < info.getLines().size(); ++lineNumber) + for (int componentNumber = 0; componentNumber < info.getComponents().size(); ++componentNumber) { - FormattedText line = info.getLines().get(lineNumber); - if (line != null) - { - info.getFont().drawInBatch(Language.getInstance().getVisualOrder(line), (float)tooltipX, (float)tooltipY, -1, true, mat, renderType, false, 0, 15728880); - } - - if (lineNumber + 1 == info.getTitleLines()) - { - tooltipY += 2; - } - - tooltipY += 10; + ClientTooltipComponent textComponent = info.getComponents().get(componentNumber); + textComponent.renderText(preEvent.getFont(), rectX, tooltipTop, mat, renderType); + tooltipTop += textComponent.getHeight() + (componentNumber == 0 ? 2 : 0); } renderType.endBatch(); poseStack.popPose(); + tooltipTop = rectY; + + for (int componentNumber = 0; componentNumber < info.getComponents().size(); ++componentNumber) + { + ClientTooltipComponent imageComponent = info.getComponents().get(componentNumber); + imageComponent.renderImage(preEvent.getFont(), rectX, tooltipTop, poseStack, itemRenderer, 400); + tooltipTop += imageComponent.getHeight() + (componentNumber == 0 ? 2 : 0); + } - MinecraftForge.EVENT_BUS.post(new RenderTooltipExtEvent.PostText(stack, info.getLines(), poseStack, tooltipX, tooltipTop, info.getFont(), tooltipTextWidth, tooltipHeight, comparison)); + itemRenderer.blitOffset = f; - RenderSystem.enableDepthTest(); + RenderTooltipExtEvent.Post postEvent = new RenderTooltipExtEvent.Post(stack, poseStack, rectX, rectY, info.getFont(), rect.getWidth(), rect.getHeight(), info.getComponents(), comparison); + MinecraftForge.EVENT_BUS.post(postEvent); } - @SuppressWarnings("removal") - public static Rect2i calculateRect(final ItemStack stack, PoseStack poseStack, List<? extends FormattedText> textLines, int mouseX, int mouseY, - int screenWidth, int screenHeight, int maxTextWidth, Font font) + public static Rect2i calculateRect(final ItemStack stack, PoseStack poseStack, List<ClientTooltipComponent> components, + int mouseX, int mouseY,int screenWidth, int screenHeight, int maxTextWidth, Font font) { Rect2i rect = new Rect2i(0, 0, 0, 0); - if (textLines == null || textLines.isEmpty() || stack == null) + if (components == null || components.isEmpty() || stack == null) { return rect; } - // Generate a tooltip event even though we aren't rendering anything in case the event handlers are modifying the input values. - RenderTooltipEvent.Pre event = new RenderTooltipEvent.Pre(stack, textLines, poseStack, mouseX, mouseY, screenWidth, screenHeight, maxTextWidth, font); + // Generate a tooltip event even though we aren't rendering anything in case event handlers are modifying the input values. + RenderTooltipEvent.Pre event = new RenderTooltipEvent.Pre(stack, poseStack, mouseX, mouseY, screenWidth, screenHeight, font, components); if (MinecraftForge.EVENT_BUS.post(event)) { return rect; @@ -264,102 +187,32 @@ public class Tooltips mouseY = event.getY(); screenWidth = event.getScreenWidth(); screenHeight = event.getScreenHeight(); - maxTextWidth = event.getMaxWidth(); - font = event.getFontRenderer(); + font = event.getFont(); int tooltipTextWidth = 0; + int tooltipHeight = components.size() == 1 ? -2 : 0; - for (FormattedText textLine : textLines) - { - int textLineWidth = font.width(textLine); - if (textLineWidth > tooltipTextWidth) - { - tooltipTextWidth = textLineWidth; - } - } - - boolean needsWrap = false; - - int titleLinesCount = 1; - int tooltipX = mouseX + 14; - 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 - 14 - 8; - } - else - { - tooltipTextWidth = screenWidth - 16 - mouseX; - } - needsWrap = true; - } - } - - if (maxTextWidth > 0 && tooltipTextWidth > maxTextWidth) - { - tooltipTextWidth = maxTextWidth; - needsWrap = true; - } - - if (needsWrap) + for (ClientTooltipComponent component : components) { - int wrappedTooltipWidth = 0; - List<FormattedText> wrappedTextLines = new ArrayList<>(); - for (int i = 0; i < textLines.size(); i++) + int componentWidth = component.getWidth(event.getFont()); + if (componentWidth > tooltipTextWidth) { - FormattedText textLine = textLines.get(i); - List<FormattedText> wrappedLine = font.getSplitter().splitLines(textLine, tooltipTextWidth, Style.EMPTY); - if (i == 0) - { - titleLinesCount = wrappedLine.size(); - } - - for (FormattedText line : wrappedLine) - { - int lineWidth = font.width(line); - if (lineWidth > wrappedTooltipWidth) - { - wrappedTooltipWidth = lineWidth; - } - wrappedTextLines.add(line); - } + tooltipTextWidth = componentWidth; } - tooltipTextWidth = wrappedTooltipWidth; - textLines = wrappedTextLines; - if (mouseX > screenWidth / 2) - { - tooltipX = mouseX - 16 - tooltipTextWidth; - } - else - { - tooltipX = mouseX + 14; - } + tooltipHeight += component.getHeight(); } - int tooltipY = mouseY - 14; - int tooltipHeight = 8; - - if (textLines.size() > 1) + int tooltipX = mouseX + 12; + int tooltipY = mouseY - 12; + if (tooltipX + tooltipTextWidth > screenWidth) { - tooltipHeight += (textLines.size() - 1) * 10; - if (textLines.size() > titleLinesCount) - { - tooltipHeight += 2; // gap between title lines and next lines - } + tooltipX -= 28 + tooltipTextWidth; } - if (tooltipY < 4) - { - tooltipY = 4; - } - else if (tooltipY + tooltipHeight + 4 > screenHeight) + if (tooltipY + tooltipHeight + 6 > screenHeight) { - tooltipY = screenHeight - tooltipHeight - 4; + tooltipY = screenHeight - tooltipHeight - 6; } rect = new Rect2i(tooltipX - 4, tooltipY - 4, tooltipTextWidth + 8, tooltipHeight + 8); |