Skip to content

Commit

Permalink
fix positioning issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyfts committed Oct 27, 2024
1 parent 36e6496 commit 0f38225
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
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,28 @@
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;
private int scaledWidth, scaledHeight;
private boolean updatePos = true;

public InfoText(int index, Alignment alignment, List<Value> values) {
super(0, 0);
Expand All @@ -38,26 +35,28 @@ public InfoText(int index, Alignment alignment, List<Value> 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;
Expand All @@ -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) {
Expand All @@ -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);
}

Expand All @@ -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);
}

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

Expand All @@ -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 "?";
}
Expand Down

0 comments on commit 0f38225

Please sign in to comment.