MenuKit
MenuKit 是用于 Fabric 模组的 UI 库。它包含了 Minecraft 界面的各个部分(按钮、滑块、选项卡、面板、HUD 读数以及设置控件),并将它们放置在 HUD、原版屏幕或模组自行打开的屏幕上。模组可以使用它来构建菜单和覆盖层,而无需从零开始编写原版屏幕代码。
我最初是为自己的模组构建了这个库,随后将其开源,以便其他模组开发者也能使用。如果你是玩家,并且另一个模组要求你安装 MenuKit,只需将其与该模组一起放入你的 mods 文件夹即可。单独运行时,它的变化非常小:按住按钮时会有按下效果的视觉反馈;单个箱子、 barrel、末影箱和潜影盒的界面会被裁剪为与背包界面相同的高度,使你的背包在所有这些界面中都保持在同一位置;“控制”界面会新增一个“移动面板”按键,在未绑定之前该按键无效。
UI 绘制在客户端进行。MenuKit 也会在服务器上加载,因为基于它构建的库可以运行服务器端代码,但仅负责绘制 UI 的模组可以保持纯客户端。对于插槽功能,有一个 SlotKit,这是另一个基于 MenuKit 构建的独立库,它能够查找并对任何菜单中的插槽进行操作,并构建真实的插槽和自定义菜单。
功能特性
- 一套组件适用于五种上下文:HUD、容器屏幕、其他原版屏幕、锚点(Anchors)和独立屏幕。锚点是模组在任何屏幕上命名并提供的一个矩形区域,例如暂停菜单中的一列按钮。
- 提供按钮、开关、复选框、单选按钮、滑块、下拉菜单(单选和多选)、文本字段、标签、信息框、工具提示、图标、物品显示(可选彩色轮廓)、进度条、分隔线、滚动容器、运行时行(
Flow,普通模式或卡片绘制模式)、可折叠区域、选项卡、颜色字段、物品选择器以及确认和警告对话框。
- 所有构建器使用相同的命名方法(
at、size、visibleWhen、disabledWhen、tooltip、state、onClick)。每个显示值的控件都会读取和写入模组拥有的字段。
- 组件被分组到面板中。面板作为一个单元进行渲染、接收输入以及显示或隐藏,且一个
disabledWhen 条件会使整个面板变灰。
- 面板的位置只需在面板上声明一次,并且可以根据游戏模式有所不同,因此面板可以在创造模式下位于其他位置。面板会根据 GUI 缩放比例自动调整大小,并在需要时进行换行和滚动。
- 多个模组可以向同一个屏幕添加 UI。共享区域的面板按优先级顺序堆叠,而不是重叠,并且在每次启动时顺序一致。协同工作的模组还可以共享一个面板,当数量超过模组设定的阈值时,它会折行进入第二列。
- 面板覆盖的内容是无交互的。不透明面板占据其矩形的所有像素,而半透明面板仅占据其控件所在的区域。位于面板下方的插槽或小部件不会高亮、响应点击或显示工具提示。
- 控件具备原版的点击音效、语音朗读和键盘焦点功能。
- 每个元素根据其所在的屏幕类型进行绘制。背包和箱子采用“Panel”外观,而选项、控制及其他类似屏幕采用“Default”外观(即原版暂停菜单的外观)。文本根据角色(标题、正文、淡化或禁用)着色,确保在两种外观下均清晰可读。
- 在容器屏幕上,箱子及其下方打开的子容器可以堆叠在一个滚动列表中,上方是一个固定不动的背包面板。其他模组的按钮和配方查看器在这两者周围仍可正常工作。
- 类似创意背包那样的选项卡可以出现在除创意背包本身之外的所有容器屏幕上。如果模组希望,每个玩家打开的容器都可以拥有独立的选项卡,以组的形式在标题下滑动展开。
- 当窗口高度不足以容纳插槽网格时,网格会在其所在面板内滚动;模组也可以为此功能开启原版箱子自身的行。
- 模组可以更改插槽中物品的绘制方式,例如开启盖子的潜影盒,或者替换为其他物品,并可配合缓动动画实现。
- 模组可以在任意容器插槽的物品上方或下方绘制标记,例如锁形图标或显示应放置物品的淡色幽灵图像,并附带独立的工具提示。同一插槽上多个模组的标记均可同时显示。
- 当一个容器屏幕替代另一个打开时,帧会从旧的大小和位置平滑过渡到新的大小和位置,而不是直接跳转。
- 面板可以置于屏幕的最底层,从而永远不会遮挡其他模组的按钮。
- 玩家可以移动面板。默认未绑定的“移动面板”按键允许玩家拖拽任何未被模组固定在原位的面板,MenuKit 会记住其放置位置。
- 颜色字段允许设置页面选择颜色,支持 Minecraft 的 16 种染料颜色,也支持带有十六进制输入框的完整拾色器。
- 物品选择器将单个物品或物品列表作为设置值存储。其对话框以创意背包的方式列出所有物品及其变体,并允许编辑名称、数量、附魔和其他组件。使用一个共享的比较器进行堆栈比较,且绝不会从玩家的背包中移除任何物品。
- 由其他库制作的小部件(例如配置库自带的控件)可以嵌入到面板中。它会随面板一起滚动和裁剪,并能响应点击、按键输入、获取焦点以及语音朗读。
- 插槽会裁剪至其所在的滚动区域,因此超出边缘部分的插槽仅在可见区域内绘制、悬停和高亮响应。插槽库会向 MenuKit 报告其插槽,SlotKit 也会这样做。
- 列表会跟随模组拥有的数据源动态变化,并可选地提供添加、删除、重排序、搜索和排序控件,以及一个用于打开行操作菜单的按钮。
- 文本字段支持使用 Escape 键取消编辑,而无需关闭屏幕。
- 快捷栏旁提供了一个循环条,任何将手持槽位中的堆栈转换为环形切换的模组均可使用。每次切换时,该条带会滑动显示。
- HUD 上在心棒和饥饿条之间设有一个状态列,用于堆叠显示来自多个模组的小型图标。
- 工具提示可以通过一个共享的检查项进行隐藏、停靠、换行、保持屏幕内或滚动浏览。
兼容大多数资源包
使用 MenuKit 构建的菜单会跟随你游玩时使用的资源包,就像 Minecraft 自带的屏幕一样。面板的边框、背景和槽位来自资源包的容器艺术素材,按钮、开关、复选框和下拉菜单来自资源包自身的按钮,而文本仅在资源包的颜色会导致难以阅读时才改变颜色。基于 MenuKit 构建的模组无需额外工作即可获得此功能,且未安装任何资源包时外观不会有任何变化。
- 深色模式资源包、高分辨率资源包以及重新着色或重新设计容器样式的资源包均能正常工作。过于精细而无法干净切割的艺术素材(例如完全绘制的背景)将保留 MenuKit 自身的外观。
- 在容器列表中,每个容器都保留其自身的艺术素材,因此位于箱子下方的潜影盒看起来与其单独屏幕上的样子一致。当资源包隐藏容器标题时,如果有多个容器,盒子会重新显示它们的名称。
- 安装了 OptiGUI 后,为每个潜影盒提供独立屏幕颜色的资源包会按盒子应用,因此位于箱子下方的 lime(青柠色)盒子会获得 lime 艺术素材。
- 资源包作者可以直接使用 MenuKit 自身的精灵图对其进行主题化:
menukit:panel_raised 用于面板,menukit:control_raised 集用于按钮。资源包列出了它们。
示例
HUD 读数,从客户端入口点注册一次。
HudPanel.builder("mymod:readout", ModeScope.BOTH)
.region(InsideRegion.CENTER)
.padding(4)
.style(PanelStyle.NONE)
.visibleWhen(() -> Minecraft.getInstance().gui.screen() == null)
.text(0, 0, () -> "Hello from MenuKit")
.build();
在游戏过程中,文本直接渲染在准星下方,并在打开任何屏幕时消失。
入门指南
Minecraft 26.3 是非混淆版本,因此 MenuKit 直接放在 implementation 配置中。
repositories {
maven { url 'https://api.modrinth.com/maven' }
}
dependencies {
implementation 'maven.modrinth:menukit:7.5.0+26.3'
}
在 fabric.mod.json 中声明它,包含上下界,用空格分隔(原因)。
"depends": { "menukit": ">=7.5.0 <8.0.0" }
玩家需单独安装 MenuKit。Trevlar 模组列表在其 Modrinth 页面上将其列为必需依赖项,而不是打包在一起。
仓库中的源代码作为父级 Gradle 工作区项目的一部分进行构建,而该工作区不在仓库中,因此它们目前无法单独构建。
要求
兼容性
- Minecraft 26.3,搭配 Fabric Loader 0.15.0 或更高版本。
- 客户端必需。服务器端可选,除非基于 MenuKit 构建的模组需要在服务器端使用它。
- Inventory Plus、Keybindery 和少数其他 Trevlar 模组的旧版本(针对早期 MenuKit 版本构建)标记为不兼容。请随 MenuKit 一起更新它们。
被以下模组使用
使用方法
本指南按照模组通常接触 MenuKit 的顺序进行介绍,从第一个面板到设置屏幕。每一部分都链接到完整的开发者文档。
公共 API
API 是名为 api 的软件包中的所有类型(com.trevlar.menukit.api.*),版本合约涵盖这些内容。其他所有软件包均为内部实现,标记为 @ApiStatus.Internal,可能在任何发行版中更改。如果一个任务似乎需要内部类型,那是 API 的缺失,请为此提交 issue。
| 软件包 |
包含 |
api |
MK,HUD 外观类,用于注册 HUD 面板和通知、发送通知以及读取或设置配方书。Declarations,MenuKit 及其上构建的所有库通过它声明的唯一冻结项。 |
api.element |
所有元素,以及它们渲染和接收输入的上下文 |
api.layout, api.dialog |
放置一次的行和列,以及确认、警告和编辑对话框 |
api.panel |
Panel、放置(Placement、PanelPosition 和区域)和 ModeScope、将面板放置在原版屏幕周围和锚点附近的适配器、容器列表(ContainerRegion)、容器标签(ContainerTabs)、屏幕边框(ScreenFrames)、槽位物品外观和装饰(SlotItemLooks、SlotDecorations)、共享面板(SharedPanels)和 MKScreen |
api.hud |
HUD 面板、通知和循环条 |
api.host |
适用于自带屏幕的库。它持有屏幕自身面板的主机、将输入路由到面板中绘制的槽位的钩子,以及 SlotClip,后者将这些槽位裁剪到滚动区域 |
HUD 上的第一个面板
HudPanel.builder(id, scope) 在游戏窗口注册一个面板,如上面的示例所示。scope 指定它在哪个游戏模式中显示:ModeScope.SURVIVAL、CREATIVE 或 BOTH。它位于九个 InsideRegion 位置之一,距离所接触的屏幕边缘向内 4 像素。.offset(dx, dy) 可微调其位置,.priority(n) 用于对处于同一位置的多个面板进行排序。使用 .region(OutsideRegion.X) 时,HUD 面板将位于原版快捷栏的该侧。HUD 面板仅负责绘制,不接受输入。请在客户端初始化器中注册它们,因为一旦客户端完成启动,注册即会关闭。Getting started 提供了逐步指南,而 Recipes 包含一个 HUD 栏以及快捷栏旁边的图标。
容器屏幕上的面板和元素
Panel 持有一个有序的元素列表,且面板之间不支持嵌套。每个元素都通过 X.builder()、所需设置参数以及 build() 构建,构建完成后便不可更改。用于显示值的控件接受针对模组自身字段的 state(get, set),并且自身不存储任何数据。ScreenPanelAdapter 将面板放置在容器屏幕上。如果没有指定目标,则会在所有容器屏幕上显示;而 .on(InventoryScreen.class) 或 .onPlayerInventory() 可将其限制在特定屏幕上。面板 ID 是全局唯一的,因此请为每个面板 ID 加上模组 ID 前缀。若希望面板仅在特定条件下显示,请为其提供 visibleWhen 条件,而不是移除其适配器。Concepts 列出了所有元素及共享的构建器方法。
放置与区域
面板通过 Panel.builder(id).in(scope, placement) 声明其位置,需为每个它要显示的游戏模式分别调用一次:可以是 ModeScope.BOTH,也可以将 SURVIVAL 和 CREATIVE 分开设置,从而使面板在创造模式下位于其他位置。Placement.screen(position) 使其紧贴屏幕边缘,Placement.around(anchor, position) 围绕锚点的矩形框放置,Placement.at(anchor, dx, dy) 则位于距锚点矩形框固定偏移量的位置。在定位选项中,PanelPosition.region(OutsideRegion) 将其放置在屏幕框架的一条边之外,screenAnchor(InsideRegion) 放置在九个屏幕位置之一,center() 作为顶层覆盖层居中显示,pixel(...) 放置在每帧读取的原点处,main() 则作为独立屏幕的居中框架。.priority(n) 用于对共享同一区域的面板进行排序(默认值为 100),.gap(n) 设置面板前的间距,.offset(dx, dy) 可在面板放置后对其进一步微调。面板会自动包裹其放置区域所允许的最大宽度,并在高度超出可用空间时启用滚动。有关详细信息,请参阅 Concepts 中的 Mode scope、Placement 和 Region 章节。
其他屏幕与锚点
VanillaScreenPanelAdapter 可将面板放置在非容器类的原版屏幕上,例如暂停菜单或选项界面,并需要配合 .on(...) 或 .onAny() 使用。Anchor 是一个矩形区域,由模组命名并通过 Anchor.of(id, screen -> box) 按屏幕提供,使用 Placement.around(anchor, position) 放置的面板会根据与菜单框架相同的区域规则围绕该锚点排列,并通过 AnchorPanelAdapter 添加到屏幕上。若传入 null 矩形框,则表示该锚点在当前屏幕上不存在。适配器和锚点需在初始化阶段声明,且在游戏运行期间无法添加或移除。Recipes 展示了如何在暂停菜单按钮下方锚定一行内容。
面板的遮挡范围
显示中的面板会占用其所覆盖的区域,因此其后的内容不会高亮、响应点击或显示工具提示。面板默认是不透明的,并占据其整个矩形区域。.opaque(false) 可使面板变为透明,此时只有其按钮和其他控件会阻挡背后的内容。.popover() 会在屏幕上每个普通面板之上绘制一个面板,而模态对话框仍会在此之上打开。有关层级顺序的详细说明,请参阅 Concepts 中的 Claims 章节。
库存上方的容器
ContainerRegion.section(id, scope) 向容器屏幕的容器列表中添加一个区块,这将使箱子自身的网格及其下方打开的任何箱子在一个滚动列中堆叠于玩家库存上方。插槽属于菜单自身,屏幕边界则是涵盖两个面板的矩形区域,因此其他模组的按钮和配方查看器会环绕在这些面板周围。区块的 source(...) 指定了它所显示的容器或物品,从而在资源包下呈现该容器的艺术风格外观及其名称。ContainerRegion.CONTAINERS、ContainerRegion.INVENTORY 和 ContainerRegion.anchor(sectionId) 分别是列表旁、库存旁或某个箱子旁的按钮锚点。有关详细信息,请参阅 Concepts 中的 Container region 章节,而 Recipes 提供了构建此类布局的示例。
对于玩家在屏幕开启期间打开的箱子,ContainerRegion.sections(kind, scope, keys) 可声明一种类型的区块,每个键对应一个区块,且每帧都会读取。ContainerRegion.fitted(scope, screens) 使列表和库存面板合并为一个整体框架,高度根据当前显示的区块自适应,并为每个区块配备标签页和 visibleWhen 条件,以便模组实现类似创造模式库存那样一次只显示一个页面的效果。隐藏区块的插槽在客户端会被禁用,而当Shift+点击将物品放入其中一个隐藏区块时,会触发 onHiddenLanding(...)。参见 Show one container at a time under tabs。
容器屏幕上的标签页
ContainerTabs.tab(id, scope) 在容器屏幕的边缘放置一个类似创造模式背包的标签页:位于容器列表上方或下方,以及物品栏面板上方或下方。同一边缘上所有模组的标签页共享一行,按 order 排序。MenuKit 绘制该行、为其留出空间,并在溢出时使用箭头进行分页。其余部分由模组自行处理,每帧读取:onClick、图标(物品、精灵或使用 icon(Icon) 绘制的模组自定义内容)、tooltip、visibleWhen、selectedWhen 和 disabledWhen。MenuKit 不维护任何选中状态。当容器区域未展开时,顶部和底部行基于屏幕自身的框架站立。ContainerTabs.keyed(id, scope, keys) 为每个键创建一个标签页,而带有 header(...) 的带键列表是一个组,其子标签页在 expandedWhen(...) 成立时从标题下方滑出。ContainerTabs.groups(id, scope, groups, keys) 声明动态添加和移除的组,每组对应一个组键,各自拥有独立的标题和子标签页,顺序由模组指定。若某组的键是另一组键之一,则嵌套在其中。ContainerTabs.frameStaysWhen(scope, screens, when) 在标签页行出现和消失时保持屏幕框架处于原版位置,仅当某行无法容纳时才移动它。参见 Container tabs。
可滚动的槽位网格
SlotScroll.builder() 创建一个高度随其实时行数变化的槽位网格,在有空间时保持完整行数,一旦超出空间则开始滚动,且始终保留至少三行。单元格由槽位库的元素构成,每个槽位对应一个单元格,通过 cells(count, cell) 根据每个单元格的索引和位置生成。narrows() 在滚动时减少一列,以便滚动条适应面板宽度,适用于捆绑场景。对于原版屏幕自带的网格,ScreenFrames.scrollsWhenShort(scope, screens) 会在适应窗口的框架内滚动其行,并在能够完全容纳时让屏幕保持原版的绘制方式。参见 Scroll a slot grid when it doesn't fit。
槽位物品的绘制样式
SlotItemLooks.declare(id, look) 允许模组针对每个槽位和每帧指定槽位物品使用的 ItemAnimation 及其播放进度。数量、耐久度条、高亮、提示框和点击事件仍由原版处理。ItemAnimation.OPEN_LID 可在资源包模型中打开潜影盒或箱子的盖子,posed(...) 可在动画播放期间缩放和移动物品,Transition.of(millis, easing) 则按关键帧控制时间。一种外观还可以用另一个物品堆叠替换当前物品进行绘制。参见 Slot item looks。
SlotDecorations.under(id, decoration) 和 SlotDecorations.over(id, decoration) 在所有容器槽位(无论是否为空)上绘制装饰元素,无需模组自身的 mixin。槽位先绘制背景,然后绘制 under 装饰,接着绘制物品及其数量,最后绘制 over 装饰,因此属于空槽位的物品虚影会显示在下方,而锁扣则显示在上方。所有模组的装饰按 id 顺序绘制。装饰可为鼠标所在槽位部分提供提示框,替代物品自身的提示框。参见 Slot decorations。
位于最底层的面板
Panel.Builder.background() 将面板设为屏幕背景的一部分。屏幕上其他所有内容,包括其他模组的按钮,都会在其上方绘制并保持可点击,而面板仅在其余所有元素拒绝点击后才接收点击或滚轮事件。容器列表的两个面板即以此方式构建。参见 background panel recipe。
共享面板与可移动面板
协同工作的模组可以将它们的元素放入同一个面板中。每个模组通过约定的 id 在其客户端初始化器中使用 SharedPanels.join(id, contribution) 加入该面板,MenuKit 按顺序将所有贡献合并为一个面板。Contribution.Builder.wrapAfter(scope, n) 限制共享面板中每行(垂直面板则为每列)容纳的贡献数量,超出部分自动换行。当仅安装一个模组时,它是一个普通面板。玩家绑定 MenuKit 的“移动面板”按键后即可移动面板:他们可将面板拖到 desired 位置,适用于两种游戏模式或仅限当前模式,MenuKit 会通过面板 id 保存位置。若某个面板的位置是其功能定义的一部分,则可将其标记为 fixed()。Shared panels 和 Moving panels 章节详细说明了这两者。
独立屏幕与屏幕类型
模组的自定义屏幕继承自 MKScreen。元素会根据其所处的屏幕类型进行绘制。ScreenKind.PANEL 采用 Panel 外观,即 MenuKit 的灰色风格;ScreenKind.DEFAULT 采用 Default 外观,即原版暂停菜单的风格。注入到原版屏幕中的面板会从所属屏幕获取类型。独立屏幕使用 super(title, ScreenKind.DEFAULT, panels) 一次性声明其类型,未声明任何类型的屏幕默认为 Panel 屏幕。文本颜色根据其角色(TextRole.HEADING、BODY、MUTED 或 DISABLED)自动调整,确保在两种类型下均清晰可读。Recipes 提供了在暂停菜单外观下构建完整设置屏幕的示例。
布局、标签页与分区
Row 和 Column 仅在面板的首次布局阶段放置元素一次。Flow 是运行时行,而 Flow.spacer() 可将控件固定在任何宽度的设置行的右边缘。Flow.Builder.wrapAfter(n) 限制在换行之前列或行容纳的子项数量,而 keepLast() 保留垂直 Flow 中第一列的最后一批子项。Flow.builder().boxed() 会在行后方绘制卡片背景,从而使它们像原版世界列表那样对齐排列。Tabs 有三种模式:WRAP、SIDE_SCROLL 和 SIDEBAR,选中的标签页由模组自身状态决定。在 SIDEBAR 菜单上调用 tabColumn(false) 可隐藏标签页列,使单页菜单看起来与其带标签页的兄弟菜单一致,而 fitShownTab() 则将 Tabs 的大小调整为当前显示的标签页大小。Section 是可折叠的分组。其他模组可以通过 Tabs.addTo(id, tab) 向命名菜单添加标签页。Concepts 中的 Tabs、Section 和 Layout helpers 章节对它们进行了详细说明。
颜色设置
ColorField 显示色样并在使用时打开对话框,Basic 选项卡包含 16 种染料颜色,Custom 选项卡则提供带有十六进制输入框的完整拾色器。该值为一个整数,格式为 0xRRGGBB;若启用 alpha(true),则为 0xAARRGGBB。更改会立即写入,点击 Done 确认保留,点击 Cancel 或按 Escape 则恢复之前的颜色。在 HUD 上没有对话框,仅显示色样。参见 color setting recipe。
物品设置
ItemPicker 接受一个透镜(lens)。state 按类型保存单个物品,stack 保存单个物品组,stacks 保存一组物品组,而 rule 和 rules 则是各自携带匹配模式的物品组。match(...) 设置物品组的比较方式:仅按物品类型比较(ITEM,默认值)、按精确的物品组比较(EXACT),或按物品及规则设定的至少部分组件进行比较(CONTAINS)。对话框包含 Search 选项卡,对于 EXACT 和 CONTAINS 模式,还包含用于 /give 语法的 Edit 选项卡以编辑组件。在游戏内,rule.matches(stack) 或 ItemMatch.matches(...) 执行比较操作,而 ItemMatch.CODEC 用于保存规则。ItemMatch 是通用代码,因此它同样适用于服务器端。参见 item setting recipe。
来自其他库的小部件
WidgetHost.builder().widget(supplier).size(w, h) 可将任何原版风格的小部件放入面板中。主机随面板及其滚动位置移动小部件,将其裁剪至其边界框内,并传递点击、拖拽、滚轮、按键输入、焦点和叙述事件。如果小部件不是原版的 LayoutElement,则需要一个 place(...) 钩子;如果需要在其下方打开列表,则需要 overflow() 使其绘制在所有元素之上。参见 widget host recipe。
提示框
MKTooltip.hideWhen(...) 可在条件满足时隐藏所有提示框,或仅隐藏物品提示框或其他提示框。wrapWhen、clampWhen 和 avoidCoverWhen 可确保提示框留在屏幕上且不遮挡视线,而配合 scrollBy 使用的 onWheel 允许模组滚动长提示框。除非模组从客户端初始化时注册这些功能,否则它们默认均为关闭状态。Concepts 中的 Claims 章节解释了每个开关的作用。
循环条
HudCycleStrip 在原版快捷栏右侧绘制一个小型快捷栏,显示一组环绕堆叠的物品,并随着持有槽位的变化进行切换。模组编写一个返回快捷栏槽位对应环状物品的源数据,通过 HudCycleStrip.register(...) 注册一次,并在环状物品切换后调用 slide。Cycle strip 提供了完整指南。
槽位
MenuKit 不包含任何槽位代码。需要槽位组、创建槽位或自定义菜单的模组还需依赖 SlotKit。负责在面板中放置槽位的槽位库会通过 SlotClip.shown 报告每个槽位,从而使槽位裁剪到其滚动区域内。SlotKit 的槽位元素已内置此功能。
限制与版本控制
Limits 列出了 MenuKit 未实现的功能及开放缺口。Versioning 解释了版本号所承诺的内容以及为何上下界都至关重要。reference 是 api 包生成的 Javadoc。README 和 docs/ 目录中的所有 Java 示例均在发布前针对上一版本进行编译。
升级指南
升级至 7.5.0
基于 7.4.0 构建的模组仍可照常运行,无需任何更改。使用 7.5.0 中新功能的模组需声明 "menukit": ">=7.5.0 <8.0.0"。Upgrading。
升级至 7.4.0
基于 7.3.0 构建的模组仍可照常运行,但有两个例外。针对 OutsideRegion 的 switch 语句需要新增 LEFT_CENTER 和 RIGHT_CENTER 的 case 分支;自定义元素若绘制超出其自身边界框,当该边界框完全不可见时将不再绘制。那些未指明面板所属游戏模式的表单(例如 Panel.Builder.position(p) 现已变为 in(ModeScope.BOTH, Placement.screen(p)))已被弃用,将在 8.0.0 中移除。原本通过 menukit:panel_raised 为主题化 MenuKit 按钮的资源包,现在应通过 menukit:control_raised 进行主题化。使用 7.4.0 中新功能的模组需声明 "menukit": ">=7.4.0 <8.0.0"。Upgrading 包含了所有替换方案及两项检查说明。
较早版本(从 7.3.0 回溯至 3.0.0)请参阅 Upgrading from older versions。
许可证
MIT。详见 LICENSE。请在 github.com/trevorschoeny/menukit/issues 报告问题。
MenuKit
MenuKit is a UI library for Fabric mods. It has the parts of a Minecraft interface (buttons, sliders, tabs, panels, HUD readouts, and settings controls) and places them on the HUD, on vanilla screens, or on screens a mod opens itself. A mod builds its menus and overlays with it instead of writing vanilla screen code from scratch.
I built it for my own mods, then opened it up so other modders can use it too. If you're a player and another mod asked you to install MenuKit, put it in your mods folder alongside that mod and you're done. On its own it changes very little. Buttons get a pressed look while you hold them down, single chest, barrel, ender chest, and shulker box screens are trimmed to the inventory screen's height so your inventory sits in the same place on all of them, and Controls gets a Move panels key that does nothing until you bind it.
The UI draws on the client. MenuKit also loads on a server, because libraries built on it run server code, but a mod that only draws UI can stay client-only. For slots, there is SlotKit, a separate library built on MenuKit that finds and acts on the slots in any menu and builds real slots and custom menus.
Features
- One set of components for five contexts, which are the HUD, container screens, other vanilla screens, anchors, and standalone screens. An anchor is a rectangle a mod names and supplies on any screen, such as the pause menu's column of buttons.
- Buttons, toggles, checkboxes, radio buttons, sliders, dropdowns (single and multi-select), text fields, labels, info boxes, tooltips, icons, item displays (with an optional colored outline), progress bars, dividers, scroll containers, runtime rows (
Flow, plain or drawn as cards), collapsible sections, tabs, color fields, item pickers, and confirm and alert dialogs.
- Every builder uses the same method names (
at, size, visibleWhen, disabledWhen, tooltip, state, onClick). Every control that shows a value reads and writes a field the mod owns.
- Components are grouped into panels. A panel renders, takes input, and shows or hides as a unit, and one
disabledWhen grays the whole panel.
- A panel's placement is one declaration on the panel, and it can differ by game mode, so a panel can sit somewhere else in creative. The panel sizes itself to fit at any GUI scale, wrapping and scrolling as needed.
- Several mods can add UI to the same screen. Panels that share a region stack in priority order instead of overlapping, in the same order on every launch. Mods that work together can also share one panel, which wraps into a second column past a count the mod sets.
- What a panel covers is inert. An opaque panel takes every point of its rectangle, and a see-through one takes only its controls. A slot or widget under a panel does not highlight, take a click, or show a tooltip.
- Controls have vanilla's click sound, narration, and keyboard focus.
- Each element draws for the kind of screen it is on. Inventories and chests get the Panel look, and Options, Controls, and similar screens get the Default look, which is vanilla's pause-menu look. Text is colored by role (heading, body, muted, or disabled), so it reads on either kind.
- On container screens, a chest and the boxes opened below it can stack in one scrolling list, above an inventory panel that stays put. Other mods' buttons and recipe viewers keep working around both.
- Tabs like the creative inventory's can go on every container screen except the creative inventory itself, one per box the player has open if a mod wants, in groups that slide open under a header.
- A grid of slots scrolls inside its panel when the window is too short for it, and a mod can turn this on for a vanilla chest's own rows.
- A mod can change what a slot's item draws as, such as a shulker box whose lid opens or a different item in its place, timed with its own easing.
- A mod can draw marks under or over the item in any container slot, such as a padlock or a faded ghost of what belongs there, with a tooltip of its own. Several mods' marks on one slot all show.
- When one container screen opens in place of another, the frame moves from the old size and place to the new one instead of jumping.
- Panels can sit behind everything on a screen, so they never block other mods' buttons.
- Players can move panels. A Move panels key, unbound by default, lets them drag any panel a mod hasn't fixed in place, and MenuKit remembers where they put it.
- A color field lets a settings page pick a color, from Minecraft's 16 dye colors or from a full picker with a hex field.
- An item picker holds one item or a list of them as a setting value. Its dialog lists every item and variant the way the creative inventory does, and edits names, counts, enchantments, and other components. One shared matcher compares stacks, and nothing is ever taken from the player's inventory.
- A widget made by another library, such as a config library's own control, can sit inside a panel. It scrolls and clips with the panel, and gets clicks, typed keys, focus, and narration.
- Slots clip to the scroll area they sit in, so a slot half past the edge draws, hovers, and takes clicks only where it shows. Slot libraries report their slots to MenuKit, and SlotKit does.
- Lists that follow a list the mod owns as it changes, with optional add, remove, reorder, search, and sort controls, and a button that opens a menu of actions for a row.
- Text fields cancel an edit with Escape, without closing the screen.
- A cycle strip beside the hotbar is available to any mod that turns a ring of stacks through the held slot. It slides on each turn.
- A status column on the HUD, between the hearts and the hunger bar, holds small icons from several mods, stacked.
- Tooltips can be hidden, docked, wrapped, kept on screen, or scrolled through one shared check.
Works with most resource packs
Menus built with MenuKit follow the resource pack you play with, the way Minecraft's own screens do. A panel's frame, background, and slots come from the pack's container art, buttons, toggles, checkboxes, and dropdowns from the pack's own buttons, and text changes color only where the pack's colors would make it hard to read. Mods built on MenuKit get this with no work of their own, and with no pack installed nothing looks different.
- Dark mode packs, high-resolution packs, and packs that recolor or restyle containers all work. Art too detailed to cut up cleanly, such as a fully painted background, keeps MenuKit's own look.
- In a container list, each container keeps its own art, so a shulker box below a chest looks the way it does on its own screen. Where a pack hides container titles, the boxes get their names back once there's more than one.
- With OptiGUI installed, packs that give each shulker box color its own screen apply per box, so a lime box below a chest gets the lime art.
- Pack authors can theme MenuKit directly with its own sprites:
menukit:panel_raised for panels and the menukit:control_raised set for buttons. Resource packs lists them.
Example
A HUD readout, registered once from the client entry point.
HudPanel.builder("mymod:readout", ModeScope.BOTH)
.region(InsideRegion.CENTER)
.padding(4)
.style(PanelStyle.NONE)
.visibleWhen(() -> Minecraft.getInstance().gui.screen() == null)
.text(0, 0, () -> "Hello from MenuKit")
.build();
The text renders directly below the crosshair during play and disappears while any screen is open.
Getting started
Minecraft 26.3 is unobfuscated, so MenuKit goes on the plain implementation configuration.
repositories {
maven { url 'https://api.modrinth.com/maven' }
}
dependencies {
implementation 'maven.modrinth:menukit:7.5.0+26.3'
}
Declare it in fabric.mod.json with both bounds, separated by a space (why).
"depends": { "menukit": ">=7.5.0 <8.0.0" }
Players install MenuKit separately. The Trevlar mods list it as a required dependency on their Modrinth pages rather than bundling it.
The sources in the repository build as a project of a parent Gradle workspace that is not in the repository, so they don't build on their own yet.
Requirements
- Fabric API, which MenuKit uses for its entry points and events.
Compatibility
- Minecraft 26.3, with Fabric Loader 0.15.0 or newer.
- Required on the client. Optional on the server, unless a mod built on MenuKit needs it there.
- Older releases of Inventory Plus, Keybindery, and a few other Trevlar mods built for earlier MenuKit versions are marked incompatible. Update them alongside MenuKit.
Used by
How to use
This guide follows the order a mod usually meets MenuKit, from a first panel to settings screens. Each part links to the full developer docs.
The public API
The API is every type in a package named api (com.trevlar.menukit.api.*), and the version contract covers those. Every other package is internal, marked @ApiStatus.Internal, and can change in any release. A task that seems to need an internal type is a gap in the API, so open an issue for it.
| Package |
Holds |
api |
MK, the HUD facade, which registers HUD panels and notifications, sends a notification, and reads or sets the recipe book. Declarations, the one freeze that MenuKit and every library built on it declare through. |
api.element |
Every element, and the contexts they render and take input in |
api.layout, api.dialog |
Rows and columns placed once, and confirm, alert, and edit dialogs |
api.panel |
Panel, placement (Placement, PanelPosition and the regions) and ModeScope, the adapters that put panels on vanilla screens and around anchors, the container list (ContainerRegion), container tabs (ContainerTabs), screen frames (ScreenFrames), slot item looks and decorations (SlotItemLooks, SlotDecorations), shared panels (SharedPanels), and MKScreen |
api.hud |
HUD panels, notifications, and the cycle strip |
api.host |
For a library that ships its own screens. It holds the host of a screen's own panels, the hook that routes input to slots drawn in panels, and SlotClip, which clips those slots to a scroll area |
A first panel on the HUD
HudPanel.builder(id, scope) registers a panel on the game window, as in the example above. The scope is the game mode it shows in: ModeScope.SURVIVAL, CREATIVE, or BOTH. It sits on one of nine InsideRegion spots, 4 pixels in from the edges it touches. .offset(dx, dy) nudges it, and .priority(n) orders it among other panels on the same spot. With .region(OutsideRegion.X) a HUD panel sits on that side of the vanilla hotbar instead. HUD panels draw only and take no input. Register them from the client initializer, since registration closes when the client finishes starting. Getting started walks through it, and Recipes has a HUD bar and an icon beside the hotbar.
Panels and elements on a container screen
A Panel holds an ordered list of elements, and panels don't nest. Every element is built with X.builder(), the settings it needs, and build(), and doesn't change after that. A control that shows a value takes state(get, set) over the mod's own field and stores nothing itself. A ScreenPanelAdapter puts the panel on container screens. With no target it shows on every container screen, and .on(InventoryScreen.class) or .onPlayerInventory() narrows it. Panel ids are global, so prefix each one with the mod id. To show a panel only some of the time, give it a visibleWhen rather than removing its adapter. Concepts lists every element and the shared builder methods.
Placement and regions
A panel declares where it sits with Panel.builder(id).in(scope, placement), once for each game mode it shows in: ModeScope.BOTH, or SURVIVAL and CREATIVE apart, so a panel can sit somewhere else in creative. Placement.screen(position) places it against the screen, Placement.around(anchor, position) around an anchor's box, and Placement.at(anchor, dx, dy) at a fixed offset from the box. In a position, PanelPosition.region(OutsideRegion) places it outside the screen's frame along one edge, screenAnchor(InsideRegion) on one of nine screen spots, center() as an overlay on top, pixel(...) at an origin read every frame, and main() as a standalone screen's centered frame. .priority(n) orders panels that share a region (the default is 100), .gap(n) sets the space before a panel, and .offset(dx, dy) nudges one panel after it is placed. A panel wraps to the width its placement leaves and scrolls when it is taller than its room. The Mode scope, Placement, and Region sections of Concepts have the details.
Other screens and anchors
VanillaScreenPanelAdapter puts a panel on a vanilla screen that isn't a container, such as the pause screen or Options, and needs .on(...) or .onAny(). An Anchor is a rectangle a mod names and supplies per screen with Anchor.of(id, screen -> box), and a panel placed with Placement.around(anchor, position) sits around it by the same region rules as a menu frame, put on screen by an AnchorPanelAdapter. A null box means the anchor isn't on that screen. Adapters and anchors are declared at init and can't be added or removed during play. Recipes anchors a row under the pause menu's buttons.
What a panel covers
A shown panel claims the points it covers, so nothing behind it highlights, takes a click, or shows a tooltip. Panels are opaque by default and claim their whole rectangle. .opaque(false) makes a panel see-through, and then only its buttons and other controls block what is behind them. .popover() draws a panel above every ordinary panel on the screen, and a modal dialog still opens above that. The Claims section of Concepts covers the order.
Containers above the inventory
ContainerRegion.section(id, scope) adds a section to a container screen's container list, which stacks the chest's own grid and any boxes opened below it in one scrolling column above the player's inventory. The slots are the menu's own, and the screen's bounds are the rectangle around both panels, so other mods' buttons and recipe viewers go around them. A section's source(...), the placed container or item it shows, gives it that container's art under a resource pack, and its name. ContainerRegion.CONTAINERS, ContainerRegion.INVENTORY, and ContainerRegion.anchor(sectionId) are anchors for a button by the list, by the inventory, or by one box. The Container region section of Concepts covers it, and Recipes builds one.
For boxes the player opens while the screen is up, ContainerRegion.sections(kind, scope, keys) declares a kind of section with one section per key, read every frame. ContainerRegion.fitted(scope, screens) makes the list and the inventory panel one frame, as tall as the shown sections need, and with tabs and a visibleWhen on each section a mod can show one container at a time, the way the creative inventory shows one page. A hidden section's slots are off on the client, and onHiddenLanding(...) runs when a shift-click lands an item in one. See Show one container at a time under tabs.
Tabs on container screens
ContainerTabs.tab(id, scope) puts a tab like the creative inventory's on an edge of a container screen: above or below the container list, or above or below the inventory panel. Every mod's tabs on one edge share one row, ordered by order. MenuKit draws the row, makes room for it, and pages it with arrows when it overflows. Everything else is the mod's, read every frame: onClick, the icon (an item, a sprite, or the mod's own drawing with icon(Icon)), tooltip, visibleWhen, selectedWhen, and disabledWhen. MenuKit keeps no selection. Where the container region isn't up, the top and bottom rows stand on the screen's own frame. ContainerTabs.keyed(id, scope, keys) makes one tab per key, and a keyed list with a header(...) is a group whose sub-tabs slide out from under the header while expandedWhen(...) holds. ContainerTabs.groups(id, scope, groups, keys) declares groups that come and go at runtime, one per group key, each with its own header and sub-tabs, in the order the mod gives. A group whose key is one of another group's keys nests inside it. ContainerTabs.frameStaysWhen(scope, screens, when) keeps the screen's frame at vanilla's place while tab rows come and go, moving it only when a row wouldn't fit. See Container tabs.
A grid of slots that scrolls
SlotScroll.builder() makes a grid of slots that is as tall as its live rows while there is room and scrolls once there isn't, never under three rows. The cells are a slot library's elements, one per slot, made from each cell's index and place with cells(count, cell). narrows() drops a column while it scrolls so the scrollbar fits in the panel's width, for a bundle. For a vanilla screen's own grid, ScreenFrames.scrollsWhenShort(scope, screens) scrolls its rows inside a frame that fits the window, and leaves the screen exactly as vanilla draws it whenever it fits. See Scroll a slot grid when it doesn't fit.
What a slot's item draws as
SlotItemLooks.declare(id, look) lets a mod say, per slot and per frame, which ItemAnimation a slot's item draws with and how far through it is. The count, durability bar, highlight, tooltip, and clicks stay vanilla's. ItemAnimation.OPEN_LID opens a shulker box's or chest's lid in the resource pack's model, posed(...) scales and moves the item as it plays, and Transition.of(millis, easing) times it per key. A look can also draw another stack in the item's place. See Slot item looks.
SlotDecorations.under(id, decoration) and SlotDecorations.over(id, decoration) draw on every container slot, empty or not, without a mixin of the mod's own. A slot draws its background, then the under decorations, then the item and its count, then the over decorations, so a ghost of the item that belongs in an empty slot goes under and a padlock goes over. Every mod's decorations draw, in id order. A decoration can answer a tooltip for the part of the slot under the mouse, in place of the item's own. See Slot decorations.
Panels behind everything
Panel.Builder.background() makes a panel part of the screen's own background. Everything else on the screen, other mods' buttons included, draws over it and stays clickable, and the panel gets a click or the wheel only after everything else declined it. The container list's two panels are built this way. See the background panel recipe.
Shared panels and moving panels
Mods that work together can put their elements in one panel. Each joins it from its client initializer with SharedPanels.join(id, contribution) under an agreed id, and MenuKit builds one panel from every contribution, in order. Contribution.Builder.wrapAfter(scope, n) caps how many contributions the shared panel holds in a row, or in a column for a vertical panel, for a game mode, and the rest wrap. With one mod installed it is an ordinary panel. Players can move panels once they bind MenuKit's Move panels key: they drag a panel where they want it, for both game modes or only the current one, and MenuKit keeps the position by panel id. A mod marks a panel fixed() where its place is part of what it means. The Shared panels and Moving panels sections of Concepts cover both.
Standalone screens and screen kinds
A mod's own screen subclasses MKScreen. Elements draw for the kind of screen they are on. ScreenKind.PANEL is the Panel look, MenuKit's gray, and ScreenKind.DEFAULT is the Default look, vanilla's pause-menu look. Panels injected into vanilla screens take the kind from the screen. A standalone screen declares its kind once, with super(title, ScreenKind.DEFAULT, panels), and one that declares nothing is a Panel screen. Text takes its color from a role (TextRole.HEADING, BODY, MUTED, or DISABLED), so it reads on both kinds. Recipes builds a whole settings screen in the pause-menu look.
Layout, tabs, and sections
Row and Column place elements once, at the panel's first layout pass. Flow is the runtime row, and Flow.spacer() pins a control to the right edge of a settings row at any width. Flow.Builder.wrapAfter(n) caps how many children a column or row holds before it wraps, and keepLast() keeps the last ones in a vertical Flow's first column. Flow.builder().boxed() draws a card behind a row, so a list of them lines up like vanilla's world list. Tabs has three modes, WRAP, SIDE_SCROLL, and SIDEBAR, and the selected tab is the mod's own state. tabColumn(false) on a SIDEBAR menu hides the tab column so a one-page menu looks like its tabbed siblings, and fitShownTab() sizes a Tabs to the tab it shows. Section is a collapsible group. Other mods can add tabs to a named menu with Tabs.addTo(id, tab). The Tabs, Section, and Layout helpers sections of Concepts cover them.
Color settings
ColorField shows a swatch and opens a dialog when clicked, with the 16 dye colors on a Basic tab and a full picker with a hex field on a Custom tab. The value is an int, 0xRRGGBB, or 0xAARRGGBB with alpha(true). Changes are written at once, Done keeps them, and Cancel or Escape puts back the color from before. On the HUD there is no dialog, only the swatch. See the color setting recipe.
Item settings
ItemPicker takes one lens. state holds one item by kind, stack one stack, stacks a list of stacks, and rule and rules stacks that each carry their own match mode. match(...) sets how stacks compare, by the kind of item only (ITEM, the default), by the exact stack (EXACT), or by the item plus at least the components the rule sets (CONTAINS). The dialog has a Search tab and, for EXACT and CONTAINS, an Edit tab for components in /give syntax. In play, rule.matches(stack) or ItemMatch.matches(...) does the comparing, and ItemMatch.CODEC saves a rule. ItemMatch is common code, so it works on a server too. See the item setting recipe.
Widgets from other libraries
WidgetHost.builder().widget(supplier).size(w, h) puts any vanilla-style widget in a panel. The host moves it with the panel and its scroll, clips it to its box, and passes it clicks, drags, the wheel, typed keys, focus, and narration. A widget that isn't a vanilla LayoutElement needs a place(...) hook, and one that opens a list below itself needs overflow() to draw it over everything. See the widget host recipe.
Tooltips
MKTooltip.hideWhen(...) hides every tooltip, or only item tooltips or only the others, while a condition holds. wrapWhen, clampWhen, and avoidCoverWhen keep tooltips on screen and out of the way, and onWheel with scrollBy lets a mod scroll a long one. All of them are off unless a mod registers them from client init. The Claims section of Concepts explains each switch.
The cycle strip
HudCycleStrip draws a small hotbar just right of the vanilla one, showing a ring of stacks that turns through the held slot. A mod writes a source that returns the ring for a hotbar slot, registers it once with HudCycleStrip.register(...), and calls slide after the ring turns. Cycle strip has the full guide.
Slots
MenuKit keeps no slot code. A mod that needs slot groups, created slots, or custom menus depends on SlotKit as well. A slot library that places slots in panels reports each one with SlotClip.shown, so the slot clips to its scroll area. SlotKit's slot elements already do this.
Limits and versioning
Limits lists what MenuKit doesn't do and the open gaps. Versioning explains what a version number promises and why both bounds matter. The reference is the generated javadoc for the api packages. Every Java sample in the README and in docs/ is compiled against the release before it ships.
Upgrading
To 7.5.0
Mods built for 7.4.0 work as before, with nothing to change. A mod that uses anything new in 7.5.0 declares "menukit": ">=7.5.0 <8.0.0". Upgrading.
To 7.4.0
Mods built for 7.3.0 work as before, with two exceptions. A switch over OutsideRegion needs cases for the new LEFT_CENTER and RIGHT_CENTER, and a custom element that draws past its own box isn't drawn while that box is wholly out of view. The forms that don't say which game mode a panel is for, such as Panel.Builder.position(p) (now in(ModeScope.BOTH, Placement.screen(p))), are deprecated and go in 8.0.0. A resource pack that themed MenuKit's buttons through menukit:panel_raised themes them through menukit:control_raised now. A mod that uses anything new in 7.4.0 declares "menukit": ">=7.4.0 <8.0.0". Upgrading has every replacement and both checks.
Older releases, from 7.3.0 back to 3.0.0, are in Upgrading from older versions.
License
MIT. See LICENSE. Report issues at github.com/trevorschoeny/menukit/issues.
评论 0
登录后参与评分
还没有评论,来抢沙发吧