aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthony Hilyard <anthony.hilyard@gmail.com>2021-08-26 16:25:52 -0700
committerAnthony Hilyard <anthony.hilyard@gmail.com>2021-08-26 16:25:52 -0700
commit58ae2b0de8e093b011d3df06be7c61266a7ed56e (patch)
tree01bd7bd939762843c73148c212a3953981417bab
parentfca13e61adcfbdbf38497c20a2e41ef120ef1d13 (diff)
downloadIceberg-58ae2b0de8e093b011d3df06be7c61266a7ed56e.tar.gz
Iceberg-58ae2b0de8e093b011d3df06be7c61266a7ed56e.tar.bz2
Iceberg-58ae2b0de8e093b011d3df06be7c61266a7ed56e.zip
Added tooltip helper.
-rw-r--r--gradle.properties2
-rw-r--r--src/main/java/com/anthonyhilyard/iceberg/util/Tooltips.java139
-rw-r--r--src/main/resources/META-INF/mods.toml2
3 files changed, 141 insertions, 2 deletions
diff --git a/gradle.properties b/gradle.properties
index 7f277e7..8cdea8b 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -6,6 +6,6 @@ org.gradle.daemon=false
name=Iceberg
group=com.anthonyhilyard.iceberg
author=anthonyhilyard
-version=1.0.0
+version=1.0.1
mcVersion=1.16.5
forgeVersion=36.2.2 \ No newline at end of file
diff --git a/src/main/java/com/anthonyhilyard/iceberg/util/Tooltips.java b/src/main/java/com/anthonyhilyard/iceberg/util/Tooltips.java
new file mode 100644
index 0000000..3761fc5
--- /dev/null
+++ b/src/main/java/com/anthonyhilyard/iceberg/util/Tooltips.java
@@ -0,0 +1,139 @@
+package com.anthonyhilyard.iceberg.util;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import com.mojang.blaze3d.matrix.MatrixStack;
+
+import net.minecraft.client.gui.FontRenderer;
+import net.minecraft.client.renderer.Rectangle2d;
+import net.minecraft.item.ItemStack;
+import net.minecraft.util.text.ITextProperties;
+import net.minecraft.util.text.Style;
+import net.minecraftforge.client.event.RenderTooltipEvent;
+import net.minecraftforge.common.MinecraftForge;
+
+public class Tooltips
+{
+ public static Rectangle2d calculateRect(final ItemStack stack, MatrixStack mStack, List<? extends ITextProperties> textLines, int mouseX, int mouseY,
+ int screenWidth, int screenHeight, int maxTextWidth, FontRenderer font)
+ {
+ Rectangle2d rect = new Rectangle2d(0, 0, 0, 0);
+ if (textLines.isEmpty())
+ {
+ 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, mStack, mouseX, mouseY, screenWidth, screenHeight, maxTextWidth, font);
+ if (MinecraftForge.EVENT_BUS.post(event))
+ {
+ return rect;
+ }
+
+ mouseX = event.getX();
+ mouseY = event.getY();
+ screenWidth = event.getScreenWidth();
+ screenHeight = event.getScreenHeight();
+ maxTextWidth = event.getMaxWidth();
+ font = event.getFontRenderer();
+
+ int tooltipTextWidth = 0;
+
+ for (ITextProperties 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)
+ {
+ int wrappedTooltipWidth = 0;
+ List<ITextProperties> wrappedTextLines = new ArrayList<>();
+ for (int i = 0; i < textLines.size(); i++)
+ {
+ ITextProperties textLine = textLines.get(i);
+ List<ITextProperties> wrappedLine = font.getSplitter().splitLines(textLine, tooltipTextWidth, Style.EMPTY);
+ if (i == 0)
+ {
+ titleLinesCount = wrappedLine.size();
+ }
+
+ for (ITextProperties line : wrappedLine)
+ {
+ int lineWidth = font.width(line);
+ if (lineWidth > wrappedTooltipWidth)
+ {
+ wrappedTooltipWidth = lineWidth;
+ }
+ wrappedTextLines.add(line);
+ }
+ }
+ tooltipTextWidth = wrappedTooltipWidth;
+ textLines = wrappedTextLines;
+
+ if (mouseX > screenWidth / 2)
+ {
+ tooltipX = mouseX - 16 - tooltipTextWidth;
+ }
+ else
+ {
+ tooltipX = mouseX + 14;
+ }
+ }
+
+ int tooltipY = mouseY - 14;
+ int tooltipHeight = 8;
+
+ if (textLines.size() > 1)
+ {
+ tooltipHeight += (textLines.size() - 1) * 10;
+ if (textLines.size() > titleLinesCount)
+ {
+ tooltipHeight += 2; // gap between title lines and next lines
+ }
+ }
+
+ if (tooltipY < 4)
+ {
+ tooltipY = 4;
+ }
+ else if (tooltipY + tooltipHeight + 4 > screenHeight)
+ {
+ tooltipY = screenHeight - tooltipHeight - 4;
+ }
+
+ rect = new Rectangle2d(tooltipX - 4, tooltipY - 4, tooltipTextWidth + 8, tooltipHeight + 8);
+ return rect;
+ }
+}
diff --git a/src/main/resources/META-INF/mods.toml b/src/main/resources/META-INF/mods.toml
index bfb58bd..77fa6e8 100644
--- a/src/main/resources/META-INF/mods.toml
+++ b/src/main/resources/META-INF/mods.toml
@@ -1,6 +1,6 @@
modLoader="javafml"
loaderVersion="[36,)"
-license="All rights reserved."
+license="CC BY-NC-ND 4.0"
[[mods]]
modId="iceberg"