Added TooltipRenderer to mimic Item Tooltip Rendering for hover texts and replaced some error/info messages to use the TooltipRenderer
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
package eu.crushedpixel.replaymod.utils;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class StringUtils {
|
||||
|
||||
private static final Minecraft mc = Minecraft.getMinecraft();
|
||||
|
||||
public static String[] splitStringInMultipleRows(String string, int maxWidth) {
|
||||
List<String> rows = new ArrayList<String>();
|
||||
String remaining = string;
|
||||
while(remaining.length() > 0) {
|
||||
String[] split = remaining.split(" ");
|
||||
String b = "";
|
||||
for(String sp : split) {
|
||||
b += sp + " ";
|
||||
if(mc.fontRendererObj.getStringWidth(b.trim()) > maxWidth) {
|
||||
b = b.substring(0, b.trim().length() - (sp.length()));
|
||||
break;
|
||||
}
|
||||
}
|
||||
String trimmed = b.trim();
|
||||
rows.add(trimmed);
|
||||
try {
|
||||
remaining = remaining.substring(trimmed.length() + 1);
|
||||
} catch(Exception e) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return rows.toArray(new String[rows.size()]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package eu.crushedpixel.replaymod.utils;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.Gui;
|
||||
import net.minecraft.client.gui.GuiScreen;
|
||||
|
||||
public class TooltipRenderer extends Gui {
|
||||
|
||||
private final Minecraft mc = Minecraft.getMinecraft();
|
||||
|
||||
public void drawTooltip(int x, int y, String text, GuiScreen parent, int textColor) {
|
||||
drawTooltip(x, y, StringUtils.splitStringInMultipleRows(text, 250), parent, textColor);
|
||||
}
|
||||
|
||||
public void drawTooltip(int x, int y, String[] textLines, GuiScreen parent, int textColor) {
|
||||
int maxLineWidth = 0;
|
||||
|
||||
int screenWidth, screenHeight;
|
||||
if(parent == null) {
|
||||
screenWidth = mc.displayWidth;
|
||||
screenHeight = mc.displayHeight;
|
||||
} else {
|
||||
screenWidth = parent.width;
|
||||
screenHeight = parent.height;
|
||||
}
|
||||
|
||||
for(String line : textLines) {
|
||||
int stringWidth = mc.fontRendererObj.getStringWidth(line);
|
||||
|
||||
if(stringWidth > maxLineWidth) {
|
||||
maxLineWidth = stringWidth;
|
||||
}
|
||||
}
|
||||
|
||||
int j2 = x + 12;
|
||||
int k2 = y - 12;
|
||||
int i1 = 8;
|
||||
|
||||
if (textLines.length > 1) {
|
||||
i1 += 2 + (textLines.length - 1) * 10;
|
||||
}
|
||||
|
||||
if (j2 + maxLineWidth > screenWidth) {
|
||||
j2 -= 28 + maxLineWidth;
|
||||
}
|
||||
|
||||
if (k2 + i1 + 6 > screenHeight) {
|
||||
k2 = screenHeight - i1 - 6;
|
||||
}
|
||||
|
||||
int j1 = -267386864;
|
||||
|
||||
this.drawGradientRect(j2 - 3, k2 - 4, j2 + maxLineWidth + 3, k2 - 3, j1, j1);
|
||||
this.drawGradientRect(j2 - 3, k2 + i1 + 3, j2 + maxLineWidth + 3, k2 + i1 + 4, j1, j1);
|
||||
this.drawGradientRect(j2 - 3, k2 - 3, j2 + maxLineWidth + 3, k2 + i1 + 3, j1, j1);
|
||||
this.drawGradientRect(j2 - 4, k2 - 3, j2 - 3, k2 + i1 + 3, j1, j1);
|
||||
this.drawGradientRect(j2 + maxLineWidth + 3, k2 - 3, j2 + maxLineWidth + 4, k2 + i1 + 3, j1, j1);
|
||||
|
||||
int k1 = 1347420415;
|
||||
int l1 = (k1 & 16711422) >> 1 | k1 & -16777216;
|
||||
|
||||
this.drawGradientRect(j2 - 3, k2 - 3 + 1, j2 - 3 + 1, k2 + i1 + 3 - 1, k1, l1);
|
||||
this.drawGradientRect(j2 + maxLineWidth + 2, k2 - 3 + 1, j2 + maxLineWidth + 3, k2 + i1 + 3 - 1, k1, l1);
|
||||
this.drawGradientRect(j2 - 3, k2 - 3, j2 + maxLineWidth + 3, k2 - 3 + 1, k1, k1);
|
||||
this.drawGradientRect(j2 - 3, k2 + i1 + 2, j2 + maxLineWidth + 3, k2 + i1 + 3, l1, l1);
|
||||
|
||||
int i = 0;
|
||||
for(String line : textLines) {
|
||||
mc.fontRendererObj.drawStringWithShadow(line, j2, k2, textColor);
|
||||
|
||||
if(i == 0) {
|
||||
k2 += 2;
|
||||
}
|
||||
|
||||
k2 += 12;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user