OptionButton: Add Tooltip to long prefix

This commit is contained in:
slprime committed 2022-08-15 22:19:38 +03:00
1 parent 4054ea04f3
commit 3f31d7d53a
3 files changed
+51 -25

No files matched your search

@@ -32,6 +32,7 @@ import java.util.Map;
import java.util.stream.Collectors;
import net.minecraft.client.Minecraft;
import net.minecraft.client.audio.PositionedSoundRecord;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
@@ -359,6 +360,24 @@ public class NEIClientUtils extends NEIServerUtils {
return concatIntegersToRanges(damages);
}
public static String cropText(FontRenderer fontRenderer, String text, int containerWidth) {
int textWidth = fontRenderer.getStringWidth(text);
if (textWidth > containerWidth) {
textWidth += fontRenderer.getStringWidth("...");
while (textWidth > containerWidth) {
textWidth -= fontRenderer.getCharWidth(text.charAt(text.length() - 1));
text = text.substring(0, text.length() - 1);
}
return text + "...";
}
return text;
}
public static boolean safeKeyDown(int keyCode) {
try {
return Keyboard.isKeyDown(keyCode);
@@ -312,7 +312,7 @@ public class PresetsWidget extends Widget {
final int directionState = direction.contains(x + mx, y + my) ? 1 : 2;
final int deleteState = delete.contains(x + mx, y + my) ? 1 : 2;
final String displayName = cropText(tag.displayName, option.w - 6);
final String displayName = NEIClientUtils.cropText(fontRenderer, tag.displayName, option.w - 6);
final String dirName =
tag.whitelist ? translate("presets.whitelist.label") : translate("presets.blacklist.label");
@@ -456,7 +456,7 @@ public class PresetsWidget extends Widget {
@Override
public void draw(int mousex, int mousey) {
final String text = cropText(this.text, w);
final String text = NEIClientUtils.cropText(fontRenderer, this.text, w);
if (text.equals(this.text)) {
GuiDraw.drawString(text, x + w - fontRenderer.getStringWidth(text), y + (h - 8) / 2, colour);
@@ -535,24 +535,6 @@ public class PresetsWidget extends Widget {
}
};
protected static String cropText(String text, int containerWidth) {
int textWidth = fontRenderer.getStringWidth(text);
if (textWidth > containerWidth) {
textWidth += fontRenderer.getStringWidth("...");
while (textWidth > containerWidth) {
textWidth -= fontRenderer.getCharWidth(text.charAt(text.length() - 1));
text = text.substring(0, text.length() - 1);
}
return text + "...";
}
return text;
}
protected static void setEditedTag(final PresetTag tag) {
edit = tag;
openListBox = false;
@@ -6,8 +6,12 @@ import static codechicken.lib.gui.GuiDraw.drawStringC;
import static codechicken.lib.gui.GuiDraw.getStringWidth;
import codechicken.nei.LayoutManager;
import codechicken.nei.NEIClientUtils;
import java.awt.Rectangle;
import java.util.List;
import net.minecraft.client.Minecraft;
import net.minecraft.util.ResourceLocation;
import org.lwjgl.opengl.GL11;
@@ -69,14 +73,35 @@ public abstract class OptionButton extends Option {
}
public String getTooltip() {
if (tooltip == null) return null;
String s = translateN(tooltip);
if (s.equals(namespaced(tooltip))) return null;
return s;
String tip = null;
if (tooltip != null) {
String s = translateN(tooltip);
if (!s.equals(namespaced(tooltip))) {
tip = s;
}
}
if (tip == null && getPrefix() != null) {
final int width = getStringWidth(getPrefix());
final Rectangle b = buttonSize();
if (width >= b.x) {
tip = translateN(name);
}
}
return tip;
}
public void drawPrefix() {
if (getPrefix() != null) drawString(getPrefix(), 10, 6, -1);
final String prefix = getPrefix();
if (prefix != null) {
final Rectangle b = buttonSize();
drawString(NEIClientUtils.cropText(Minecraft.getMinecraft().fontRenderer, prefix, b.x - 10), 10, 6, -1);
}
}
public void drawButton(int mx, int my) {