锚点系统(Alignment)
基本信息
| 属性 | 值 |
|---|---|
| 数据类 | lumien.custommainmenu.configuration.Alignment(factorX / factorY,两个 float) |
| 装载器 | GuiConfig.loadAlignments(GuiConfig.java:86-110) |
| 应用点 | GuiCustom.modX / modY(GuiCustom.java:496-502) |
计算公式
实际 X = (int)(posX + width × factorX)
实际 Y = (int)(posY + height × factorY)
width / height 是当前 GUI 的像素尺寸(即 MC 缩放后的逻辑分辨率)。所以同一个 posX 配不同锚点,在不同分辨率下会落在不同相对位置——这就是锚点系统的全部意义:让布局自适应。
10 个内置锚点
GuiConfig.loadAlignments 硬编码 10 个(GuiConfig.java:88-97),与 mainmenu.schema.json 的枚举完全一致:
| 锚点名 | factorX |
factorY |
含义 |
|---|---|---|---|
bottom_left |
0.0 | 1.0 | 左下角 |
top_left |
0.0 | 0.0 | 左上角 |
top_right |
1.0 | 0.0 | 右上角 |
bottom_right |
1.0 | 1.0 | 右下角 |
center |
0.5 | 0.5 | 正中 |
button |
0.5 | 0.25 | ⚠️ 特殊,见下 |
top_center |
0.5 | 0.0 | 顶部居中 |
left_center |
0.0 | 0.5 | 左侧居中 |
bottom_center |
0.5 | 1.0 | 底部居中 |
right_center |
1.0 | 0.5 | 右侧居中 |
button(0.5, 0.25)不是几何中心,是按钮栏的惯例位置(屏幕高度 1/4 处)。
各元素默认锚点
| 元素 | 默认锚点 | 来源 |
|---|---|---|
| 按钮(Button) | button |
Button.java:41-43 |
| 文本(Text) | top_left |
Text.java:37-39 |
| 图片(Image) | top_left |
GuiConfig.java:299 |
| 飘字(SplashText) | 构造时未给则 button |
SplashText.java:20-22 |
| 背景(Background) / 全景图(Panorama) | 不适用 | 固定全屏 |
⚠️ 按钮用 posX: -100 + button 锚点实现水平居中(-100 = 200/2),而不是 posX: 0 + center——因为 center 会把按钮中心放在屏幕正中,button 锚点才是主菜单按钮列的位置。
自定义锚点
顶层 alignments 对象可加任意名字,会覆盖同名内置值(GuiConfig.java:98-109):
"alignments": {
"gui_anchor": { "factorWidth": 0.5, "factorHeight": 0.5 }
}
⚠️ factorWidth / factorHeight 是无判空硬读(GuiConfig.java:105-106),缺任一即崩。
⚠️ 未定义的锚点名静默回退到 top_left(GuiConfig.getAlignment,GuiConfig.java:422-427),不报错。
⚠️ mainmenu.schema.json 把 alignments 画在 other 段里,而代码从顶层读(GuiConfig.java:98)——以代码为准。
相关条目
- 配置文件格式 -
alignments的层级位置 - GUI 缩放变体 - 各档位共用同一套锚点
- 按钮(Button) -
button锚点的由来