InfoIcon

纹理图集子矩形渲染节点

基本信息

属性 值
路径 com.github.lunatrius.ingameinfo.client.gui.InfoIcon
行数 102
父类 Info

与 InfoItem 并列的两种叶子节点: InfoItem 画物品堆,InfoIcon 画纹理图集的子矩形。

字段(:15-22)

private ResourceLocation resourceLocation;   // :15
private boolean invalidResource;             // :16
private final Vector2f xy0 = new Vector2f(); // :17
private final Vector2f xy1 = new Vector2f(); // :18
private final Vector2f uv0 = new Vector2f(); // :19
private final Vector2f uv1 = new Vector2f(); // :20
private int displayWidth;                    // :21
private int displayHeight;                   // :22
字段 含义
xy0 / xy1 图标在屏幕上的左上/右下角
uv0 / uv1 对应的纹理 UV 坐标(0–1 浮点)
displayWidth / displayHeight 显示尺寸(像素)
invalidResource 纹理是否加载失败

⚠️ 四个 Vector2f 是 final 的可变对象(LunatriusCore 的向量类), final 只保证引用不变,内部坐标可反复改写。

⚠️ displayWidth / displayHeight 不是 final, 由 setDisplayDimensions 改写。

构造器(:24-34)

构造器 行 参数
InfoIcon(String location) :24-26 资源位置字符串
InfoIcon(String location, int displayX, int displayY, int displayWidth, int displayHeight, int iconX, ...) :28-34 完整参数(≥7 个)

完整版构造器一次性给出显示位置、显示尺寸、图标在图集内的坐标。

两个 setter

setDisplayDimensions(:36-42)

public void setDisplayDimensions(int displayX, int displayY, int displayWidth, int displayHeight)

单独设置屏幕上的矩形。

setTextureData(:44-49)

public void setTextureData(int iconX, int iconY, int iconWidth, int iconHeight, int textureWidth, int textureHeight)

6 个参数:图集内图标的左上角、宽高,以及整张图集的宽高。 UV 由这 6 个值算出:

u0 = iconX / textureWidth
u1 = (iconX + iconWidth) / textureWidth
v0 = iconY / textureHeight
v1 = (iconY + iconHeight) / textureHeight

⚠️ textureWidth / textureHeight 必须手动传入正确值 —— 若图集是 16×16 的物品贴图(256×256),传错会导致 UV 越界或图标错位。 本 mod 无法从 ResourceLocation 推断图集尺寸(需查 atlas 定义), 所以完全依赖调用方正确传参。

drawInfo(:51-75)

⚠️ 占 25 行,是本类最长的方法。流程:

步骤 说明
1 invalidResource 为真 → 直接返回
2 Minecraft.getMinecraft().getTextureManager().bindTexture(resourceLocation)
3 开 GL 混合 / 关闭光照
4 用 Tessellator 画两个四边形(xy0–xy1 + uv0–uv1)
5 恢复 GL 状态

⚠️ 手写 Tessellator 而不用 Gui.drawTexturedModalRect —— 因为要画子矩形(非 0,0 起、宽高非整张贴图), drawTexturedModalRect 只能画左上角对齐的整图。

setValue(:77-80)

覆写 Info 的空实现,参数 Object。 ⚠️ 与 InfoItem 同样的问题: @NotNull Object 无法在编译期保证类型。

getWidth / getHeight(:82-90)

直接返回 displayWidth / displayHeight —— 是唯一返回非 0 基类默认值的两个方法(对比 Info 基类返回 0)。

toString(:92-101)

输出 InfoIcon 的资源位置与尺寸。

挂载与定位

由 InfoText 的 attachedValues 挂载, 位置由 updateChildren(InfoText 的 :64-82) 按 {ICON|...} 标记算出 —— 见该条目的三个缺陷说明。

用途

两处产生 InfoIcon:

  1. ValueComplex 的 ValueIcon(注册名 icon,别名 img / image)
  2. TagPlayerEquipment 与 TagPlayerPotion 的 Icon tag (<type>icon / <type>largeicon / potionicon / potionlargeicon)

相关