Skip to content

Commit

Permalink
Fix positioning issues (#27)
Browse files Browse the repository at this point in the history
* fix positioning issues

* fix crash & the rest of the positioning issues
  • Loading branch information
Lyfts authored Oct 30, 2024
1 parent 36e6496 commit cc3facc
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 90 deletions.
21 changes: 21 additions & 0 deletions src/main/java/com/github/lunatrius/ingameinfo/client/gui/Info.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public abstract class Info {
public int offsetX;
public int offsetY;
private String identifier = "";
private String iconSpacing = "";
public boolean hasPosition = false;
private int oldWidth;

protected Info(int x, int y) {
this.x = x;
Expand Down Expand Up @@ -46,6 +49,14 @@ public int getY() {
return this.y + this.offsetY;
}

public String getIconSpacing() {
if (oldWidth != getWidth()) {
setIconSpacing();
oldWidth = getWidth();
}
return iconSpacing;
}

public int getWidth() {
return 0;
}
Expand All @@ -58,10 +69,20 @@ public void setIdentifier(@NotNull String identifier) {
this.identifier = identifier;
}

public void setValue(@NotNull Object value) {}

public String getIdentifier() {
return this.identifier;
}

private void setIconSpacing() {
String str = "";
for (int i = 0; i < getWidth() && fontRenderer.getStringWidth(str) < getWidth(); i++) {
str += " ";
}
iconSpacing = str;
}

@Override
public String toString() {
return "Info";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
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;
import com.github.lunatrius.ingameinfo.reference.Reference;

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();
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
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;
import com.github.lunatrius.ingameinfo.value.Value;

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<String, Info> attachedValues = new HashMap<>();
private static final String ICON_START = "{ICON|";
private final Map<String, Info> attachedValues = new LinkedHashMap<>();
private String text;
private final List<Value> values;
private final Alignment alignment;
private final int index;
private boolean needsUpdate = true;

public InfoText(int index, Alignment alignment, List<Value> values) {
super(0, 0);
Expand All @@ -40,24 +35,17 @@ public InfoText(int index, Alignment alignment, List<Value> values) {
public void update() {
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;
Expand All @@ -66,30 +54,31 @@ 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()) {
if (child.hasPosition) continue;
int iconStart = builder.indexOf(ICON_START);
int widthStart = builder.indexOf("|", iconStart) + 1;
child.hasPosition = true;
child.x = fontRenderer.getStringWidth(builder.substring(0, iconStart));
builder.replace(iconStart, widthStart, "");
builder.deleteCharAt(builder.indexOf("}"));
}
updatePosition();
}

private void updatePosition() {
int scaledWidth = InGameInfoCore.INSTANCE.scaledWidth;
int scaledHeight = InGameInfoCore.INSTANCE.scaledHeight;
x = alignment.getX(scaledWidth, getWidth());
x = alignment.getX(scaledWidth, fontRenderer.getStringWidth(text));
y = alignment.getY(scaledHeight, getHeight());
}

Expand All @@ -102,14 +91,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);
}

Expand All @@ -120,6 +101,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);
}

Expand Down
6 changes: 1 addition & 5 deletions src/main/java/com/github/lunatrius/ingameinfo/tag/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,6 @@ public static void releaseResources() {
}

public static String getIconTag(Info info) {
String str = "";
for (int i = 0; i < info.getWidth() && minecraft.fontRenderer.getStringWidth(str) < info.getWidth(); i++) {
str += " ";
}
return String.format("{ICON|%s}", str);
return String.format("{ICON|%s}", info.getIconSpacing());
}
}
38 changes: 15 additions & 23 deletions src/main/java/com/github/lunatrius/ingameinfo/tag/TagMisc.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import net.minecraft.client.gui.GuiPlayerInfo;
import net.minecraft.client.resources.ResourcePackRepository;
import net.minecraft.util.EnumChatFormatting;

import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -174,31 +173,24 @@ public String getValue() {

public static class PingIcon extends TagMisc {

private boolean needsUpdate(InfoText caller, int ping) {
Info value = caller.getAttachedValue(getName());
if (value == null) return true;
return Integer.parseInt(value.getIdentifier()) != ping;
}

@Override
public @NotNull String getValue(@NotNull InfoText caller) {
List<GuiPlayerInfo> list = player.sendQueue.playerInfoList;
for (GuiPlayerInfo playerInfo : list) {
if (player.getGameProfile().getName()
.equals(EnumChatFormatting.getTextWithoutFormattingCodes(playerInfo.name))) {
int pingIndex = getPingIndex(playerInfo);
if (needsUpdate(caller, playerInfo.responseTime)) {
InfoIcon icon = new InfoIcon("textures/gui/icons.png");
icon.setIdentifier(String.valueOf(playerInfo.responseTime));
icon.setDisplayDimensions(0, 0, 10, 8);
icon.setTextureData(0, 176 + pingIndex * 8, 10, 8, 256, 256);
caller.attachValue(getName(), icon);
return getIconTag(icon);
}
return "";
}
GuiPlayerInfo playerInfo = (GuiPlayerInfo) player.sendQueue.playerInfoMap
.get(player.getCommandSenderName());
if (playerInfo == null) return "-1";
int pingIndex = getPingIndex(playerInfo);
Info value = caller.getAttachedValue(getName());
if (value == null) {
InfoIcon icon = new InfoIcon("textures/gui/icons.png");
icon.setIdentifier(String.valueOf(playerInfo.responseTime));
icon.setDisplayDimensions(0, 0, 10, 8);
icon.setTextureData(0, 176 + pingIndex * 8, 10, 8, 256, 256);
caller.attachValue(getName(), icon);
return getIconTag(icon);
} else {
((InfoIcon) value).setTextureData(0, 176 + pingIndex * 8, 10, 8, 256, 256);
return value.getIconSpacing();
}
return "-1";
}

private static int getPingIndex(GuiPlayerInfo playerInfo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 value.getIconSpacing();
}
return "";

InfoItem item = new InfoItem(itemStack, this.large);
item.setIdentifier(itemStack.getDisplayName());
caller.attachValue(getName(), item);
return getIconTag(item);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,18 @@ public Icon(int index, boolean large) {
@Override
public @NotNull String getValue(@NotNull InfoText parent) {
updatePotionEffects();
Info value = parent.getAttachedValue(getName());
if (potionEffects.length > this.index) {
Info value = parent.getAttachedValue(getName());
Potion potion = Potion.potionTypes[potionEffects[this.index].getPotionID()];
if (potion.hasStatusIcon() && shouldUpdate(value, potion.id)) {
InfoIcon icon = new InfoIcon("textures/gui/container/inventory.png");
if (!potion.hasStatusIcon()) return "";
if (shouldUpdate(value, potion.id)) {
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);
Expand All @@ -171,11 +178,16 @@ 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 value.getIconSpacing();
}

parent.attachValue(getName(), icon);
return getIconTag(icon);
} else {
return value.getIconSpacing();
}
} else if (value != null) {
parent.removeAttachedValue(getName());
}

return "";
Expand Down
Loading

0 comments on commit cc3facc

Please sign in to comment.