diff --git a/src/main/java/com/github/lunatrius/ingameinfo/client/gui/Info.java b/src/main/java/com/github/lunatrius/ingameinfo/client/gui/Info.java index 9cc80a7..253b8a2 100644 --- a/src/main/java/com/github/lunatrius/ingameinfo/client/gui/Info.java +++ b/src/main/java/com/github/lunatrius/ingameinfo/client/gui/Info.java @@ -58,6 +58,8 @@ public void setIdentifier(@NotNull String identifier) { this.identifier = identifier; } + public void setValue(@NotNull Object value) {} + public String getIdentifier() { return this.identifier; } diff --git a/src/main/java/com/github/lunatrius/ingameinfo/client/gui/InfoIcon.java b/src/main/java/com/github/lunatrius/ingameinfo/client/gui/InfoIcon.java index 9d34cf1..006b435 100644 --- a/src/main/java/com/github/lunatrius/ingameinfo/client/gui/InfoIcon.java +++ b/src/main/java/com/github/lunatrius/ingameinfo/client/gui/InfoIcon.java @@ -4,6 +4,7 @@ import net.minecraft.client.renderer.Tessellator; import net.minecraft.util.ResourceLocation; +import org.jetbrains.annotations.NotNull; import org.lwjgl.opengl.GL11; import com.github.lunatrius.core.util.vector.Vector2f; @@ -11,7 +12,7 @@ public class InfoIcon extends Info { - private final ResourceLocation resourceLocation; + private ResourceLocation resourceLocation; private final Vector2f xy0 = new Vector2f(); private final Vector2f xy1 = new Vector2f(); private final Vector2f uv0 = new Vector2f(); @@ -67,6 +68,11 @@ public void drawInfo() { } } + @Override + public void setValue(@NotNull Object value) { + this.resourceLocation = new ResourceLocation(value.toString()); + } + @Override public int getWidth() { return this.displayWidth; diff --git a/src/main/java/com/github/lunatrius/ingameinfo/client/gui/InfoItem.java b/src/main/java/com/github/lunatrius/ingameinfo/client/gui/InfoItem.java index ca94bcd..fe173d3 100644 --- a/src/main/java/com/github/lunatrius/ingameinfo/client/gui/InfoItem.java +++ b/src/main/java/com/github/lunatrius/ingameinfo/client/gui/InfoItem.java @@ -6,13 +6,14 @@ import net.minecraft.client.renderer.texture.TextureManager; import net.minecraft.item.ItemStack; +import org.jetbrains.annotations.NotNull; import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL12; public class InfoItem extends Info { private static final RenderItem renderItem = new RenderItem(); - private final ItemStack itemStack; + private ItemStack itemStack; private final boolean large; private final int size; @@ -64,6 +65,12 @@ public void drawInfo() { } } + @Override + public void setValue(@NotNull Object value) { + if (!(value instanceof ItemStack stack)) return; + this.itemStack = stack; + } + @Override public int getWidth() { return itemStack != null && itemStack.getItem() != null ? size : 0; diff --git a/src/main/java/com/github/lunatrius/ingameinfo/client/gui/InfoText.java b/src/main/java/com/github/lunatrius/ingameinfo/client/gui/InfoText.java index 769702d..45ad8dd 100644 --- a/src/main/java/com/github/lunatrius/ingameinfo/client/gui/InfoText.java +++ b/src/main/java/com/github/lunatrius/ingameinfo/client/gui/InfoText.java @@ -1,16 +1,13 @@ package com.github.lunatrius.ingameinfo.client.gui; -import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import javax.annotation.Nullable; import org.jetbrains.annotations.NotNull; -import com.github.lunatrius.core.client.gui.FontRendererHelper; import com.github.lunatrius.ingameinfo.Alignment; import com.github.lunatrius.ingameinfo.InGameInfoCore; import com.github.lunatrius.ingameinfo.reference.Reference; @@ -18,14 +15,14 @@ public class InfoText extends Info { - private static final Pattern ICON_PATTERN = Pattern.compile("\\{ICON\\|( *)\\}", Pattern.CASE_INSENSITIVE); - private static final Matcher ICON_MATCHER = ICON_PATTERN.matcher(""); - private final Map attachedValues = new HashMap<>(); + private static final String ICON_START = "{ICON|"; + private final Map attachedValues = new LinkedHashMap<>(); private String text; private final List values; private final Alignment alignment; private final int index; - private boolean needsUpdate = true; + private int scaledWidth, scaledHeight; + private boolean updatePos = true; public InfoText(int index, Alignment alignment, List values) { super(0, 0); @@ -38,26 +35,28 @@ public InfoText(int index, Alignment alignment, List values) { } public void update() { + int newHeight = InGameInfoCore.INSTANCE.scaledHeight; + int newWidth = InGameInfoCore.INSTANCE.scaledWidth; + if (newHeight != scaledHeight || newWidth != scaledWidth) { + scaledHeight = newHeight; + scaledWidth = newWidth; + updatePos = true; + attachedValues.clear(); + } + StringBuilder builder = new StringBuilder(); for (Value value : this.values) { - String valueStr = getValue(value); - if (!needsUpdate && valueStr.startsWith("{ICON")) { - continue; - } - builder.append(valueStr); + builder.append(getValue(value)); } + + updateChildren(builder); text = builder.toString(); updatePosition(); } @Override public void drawInfo() { - if (needsUpdate) { - updateChildren(); - needsUpdate = false; - } - - FontRendererHelper.drawLeftAlignedString(fontRenderer, text, getX(), getY(), 0x00FFFFFF); + fontRenderer.drawStringWithShadow(text, getX(), getY(), 0x00FFFFFF); for (Info child : attachedValues.values()) { child.offsetX = x; @@ -66,31 +65,43 @@ public void drawInfo() { } } - private void updateChildren() { - if (attachedValues.isEmpty()) { + private void updateChildren(StringBuilder builder) { + if (builder.length() == 0 && !attachedValues.isEmpty()) { + attachedValues.clear(); return; } - ICON_MATCHER.reset(text); - for (Info child : attachedValues.values()) { - if (!ICON_MATCHER.find()) break; - int newX = fontRenderer.getStringWidth(text.substring(0, ICON_MATCHER.start())); - if (newX == 0) { - offsetX = child.getWidth(); - } + if (builder.indexOf(ICON_START) == -1) { + return; + } - child.x = newX; - text = text.replaceFirst(Pattern.quote(ICON_MATCHER.group(0)), ICON_MATCHER.group(1)); - ICON_MATCHER.reset(text); + for (Info child : attachedValues.values()) { + int index = builder.indexOf(ICON_START); + child.x = fontRenderer.getStringWidth(builder.substring(0, index)); + int end = builder.indexOf("}", index) + 1; + builder.replace(index, end + 1, ""); + updatePos = true; } - updatePosition(); } private void updatePosition() { - int scaledWidth = InGameInfoCore.INSTANCE.scaledWidth; - int scaledHeight = InGameInfoCore.INSTANCE.scaledHeight; - x = alignment.getX(scaledWidth, getWidth()); + if (!updatePos) return; + updatePos = false; + x = alignment.getX(scaledWidth, fontRenderer.getStringWidth(text)); y = alignment.getY(scaledHeight, getHeight()); + + for (Info child : attachedValues.values()) { + if (child.x == 0) { + offsetX = child.getWidth(); + } + + int actualX = child.x + x + child.getWidth(); + if (actualX > scaledWidth) { + int diff = actualX + 1 - scaledWidth; + child.x = child.x - diff; + offsetX = -diff; + } + } } public @Nullable Info getAttachedValue(String tag) { @@ -102,14 +113,6 @@ public void removeAttachedValue(String tag) { } public void attachValue(@NotNull String tag, @NotNull Info value) { - Info old = attachedValues.get(tag); - if (old != null) { - value.y = old.y; - value.x = old.x; - } else { - needsUpdate = true; - } - attachedValues.put(tag, value); } @@ -120,6 +123,9 @@ public int getWidth() { @Override public int getHeight() { + if (alignment.ordinal() >= Alignment.BOTTOMLEFT.ordinal()) { + return (index + 1) * (fontRenderer.FONT_HEIGHT + 1); + } return index * (fontRenderer.FONT_HEIGHT + 1); } diff --git a/src/main/java/com/github/lunatrius/ingameinfo/tag/TagPlayerEquipment.java b/src/main/java/com/github/lunatrius/ingameinfo/tag/TagPlayerEquipment.java index c65df5a..6181aad 100644 --- a/src/main/java/com/github/lunatrius/ingameinfo/tag/TagPlayerEquipment.java +++ b/src/main/java/com/github/lunatrius/ingameinfo/tag/TagPlayerEquipment.java @@ -144,13 +144,16 @@ public Icon(int slot, boolean large) { } Info value = caller.getAttachedValue(getName()); - if (value == null || !value.getIdentifier().equals(itemStack.getDisplayName())) { - InfoItem item = new InfoItem(itemStack, this.large); - item.setIdentifier(itemStack.getDisplayName()); - caller.attachValue(getName(), item); - return getIconTag(item); + + if (value != null) { + value.setValue(itemStack); + return ""; } - return ""; + + InfoItem item = new InfoItem(itemStack, this.large); + item.setIdentifier(itemStack.getDisplayName()); + caller.attachValue(getName(), item); + return getIconTag(item); } @Override diff --git a/src/main/java/com/github/lunatrius/ingameinfo/tag/TagPlayerPotion.java b/src/main/java/com/github/lunatrius/ingameinfo/tag/TagPlayerPotion.java index d06ffaf..7c668fa 100644 --- a/src/main/java/com/github/lunatrius/ingameinfo/tag/TagPlayerPotion.java +++ b/src/main/java/com/github/lunatrius/ingameinfo/tag/TagPlayerPotion.java @@ -161,7 +161,13 @@ public Icon(int index, boolean large) { if (potionEffects.length > this.index) { Potion potion = Potion.potionTypes[potionEffects[this.index].getPotionID()]; if (potion.hasStatusIcon() && shouldUpdate(value, potion.id)) { - InfoIcon icon = new InfoIcon("textures/gui/container/inventory.png"); + InfoIcon icon; + if (value == null) { + icon = new InfoIcon("textures/gui/container/inventory.png"); + } else { + icon = (InfoIcon) value; + } + int i = potion.getStatusIconIndex(); if (this.large) { icon.setDisplayDimensions(1, -5, 18, 18); @@ -171,11 +177,14 @@ public Icon(int index, boolean large) { icon.setIdentifier(String.valueOf(potion.id)); icon.setTextureData((i % 8) * 18, 198 + (i / 8) * 18, 18, 18, 256, 256); + + if (value != null) { + return ""; + } + parent.attachValue(getName(), icon); return getIconTag(icon); } - } else if (value != null) { - parent.removeAttachedValue(getName()); } return ""; diff --git a/src/main/java/com/github/lunatrius/ingameinfo/value/ValueComplex.java b/src/main/java/com/github/lunatrius/ingameinfo/value/ValueComplex.java index f38287d..f087ae6 100644 --- a/src/main/java/com/github/lunatrius/ingameinfo/value/ValueComplex.java +++ b/src/main/java/com/github/lunatrius/ingameinfo/value/ValueComplex.java @@ -286,13 +286,25 @@ public String getValue() { if (itemStack.getItem() == null) return ""; - InfoItem item = new InfoItem(itemStack); - item.setIdentifier(what); - parent.attachValue(getName(), item); - return Tag.getIconTag(item); + if (value == null) { + InfoItem item = new InfoItem(itemStack); + item.setIdentifier(what); + parent.attachValue(getName(), item); + return Tag.getIconTag(item); + } else { + value.setIdentifier(what); + value.setValue(itemStack); + return ""; + } + } + + InfoIcon icon; + if (value == null) { + icon = new InfoIcon(what); + } else { + icon = (InfoIcon) value; } - InfoIcon icon = new InfoIcon(what); icon.setIdentifier(what); int index = 0; @@ -314,8 +326,13 @@ public String getValue() { icon.setTextureData(iconX, iconY, iconWidth, iconHeight, textureWidth, textureHeight); } - parent.attachValue(getName(), icon); - return Tag.getIconTag(icon); + if (value == null) { + parent.attachValue(getName(), icon); + return Tag.getIconTag(icon); + } else { + value.setValue(what); + return ""; + } } catch (Exception e) { return "?"; }