User Tools

Site Tools


zh_cn:tutorial:screen

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
zh_cn:tutorial:screen [2023/04/24 08:26] solidblockzh_cn:tutorial:screen [2023/12/18 01:40] (current) – [添加文本] solidblock
Line 1: Line 1:
 ====== 添加屏幕 ====== ====== 添加屏幕 ======
  
-**屏幕**是指的图形用户界面,其类继承了 ''Screen'',允许用户进行交互并实现一些功能。屏幕一个例子是你的模组的配置屏幕。屏幕仅在客户端存在,因此应注解为 ''@Environment(EnvType.CLIENT)''+**屏幕**是指的图形用户界面,其类继承了 ''Screen'',允许用户进行交互并实现一些功能。屏幕一个例子是你的模组的配置屏幕。屏幕仅在客户端存在,因此可将相的类注解为 ''@Environment(EnvType.CLIENT)''
  
 你可以使用 mixin 以在现有的屏幕中,加入你的屏幕的链接。但是很多情况下,我们可以实现 Mod Menu 模组中的 ''ModMenuApi'',这样就能够在模组菜单屏幕中,通过配置按钮来进入屏幕。本文章不会讲述如何实现 ''ModMenuApi'' 你可以使用 mixin 以在现有的屏幕中,加入你的屏幕的链接。但是很多情况下,我们可以实现 Mod Menu 模组中的 ''ModMenuApi'',这样就能够在模组菜单屏幕中,通过配置按钮来进入屏幕。本文章不会讲述如何实现 ''ModMenuApi''
Line 111: Line 111:
  
   * **Title**:屏幕的标题,会在构造函数中指定。当你进入屏幕时,这个标题会被自动复述。   * **Title**:屏幕的标题,会在构造函数中指定。当你进入屏幕时,这个标题会被自动复述。
-  * **Position*:告诉你当前正在选中的部件的位置。在原版中,是“//屏幕控件,第%s个,共%s个//”。此外,如果是在列表中,还会复述以下内容:“//已选中列表的第%s行,共%s行//”。+  * **Position**:告诉你当前正在选中的部件的位置。在原版中,是“//屏幕控件,第%s个,共%s个//”。此外,如果是在列表中,还会复述以下内容:“//已选中列表的第%s行,共%s行//”。
   * **Hint**:这个是指的当前选中或悬浮的元素的提示。例如,你可能会记得在上面的代码中创建 ''ButtonWidget'' 时有个 ''tooltip'' 方法,这个 tooltip 就是在这一部分复述的。   * **Hint**:这个是指的当前选中或悬浮的元素的提示。例如,你可能会记得在上面的代码中创建 ''ButtonWidget'' 时有个 ''tooltip'' 方法,这个 tooltip 就是在这一部分复述的。
   * **Usage**:在原版中,用法为“//使用鼠标指针或Tab键选择屏幕控件//”。   * **Usage**:在原版中,用法为“//使用鼠标指针或Tab键选择屏幕控件//”。
Line 122: Line 122:
  
 ===== 添加文本 ===== ===== 添加文本 =====
-In ''render'' method, you can invoke methods like ''textRenderer.draw''''drawTextWithShadow'' or ''drawCenteredTextWithShadow'' to render a text on the screen.+在 ''render'' 方法中,你可以调用像 ''textRenderer.draw''''drawTextWithShadow'' 或 ''drawCenteredTextWithShadow'' 这样的方法,以在屏幕中渲染文本。
 <code java> <code java>
 +  // 对于 1.20 以下版本
   @Override   @Override
   public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {   public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
     super.render(matrices, mouseX, mouseY, delta);     super.render(matrices, mouseX, mouseY, delta);
-    drawCenteredTextWithShadow(matrices, textRenderer, Text.literal("You must see me"), width / 2, height / 2, 0xffffff);+    drawCenteredTextWithShadow(matrices, textRenderer, Text.literal("你必须看到我"), width / 2, height / 2, 0xffffff); 
 +  } 
 +  
 +  // 对于 1.20 及以上的版本 
 +  @Override 
 +  public void render(DrawContext context, int mouseX, int mouseY, float delta) { 
 +    super.render(context, mouseX, mouseY, delta); 
 +    context.drawCenteredTextWithShadow(textRenderer, Text.literal("你必须看到我"), width / 2, height / 2, 0xffffff);
   }   }
 </code> </code>
  
-If you're concerned that the text can be pretty long and may exceed the screen limit, you can use ''MultilineText'' so it can be wrapped smartly.+如果你担心文本太长超出了屏幕的边界。你可以使用 ''MultilineText'' 这样文本可以恰当地换行。
 <code java> <code java>
-    final MultilineText multilineText = MultilineText.create(textRenderer, Text.literal("The text is pretty long ".repeat(20)), width - 20);+    final MultilineText multilineText = MultilineText.create(textRenderer, Text.literal("这个文本很长 ".repeat(20)), width - 20); 
 +     
 +    // 对于 1.20 以下版本
     multilineText.drawWithShadow(matrices, 10, height / 2, 16, 0xffffff);     multilineText.drawWithShadow(matrices, 10, height / 2, 16, 0xffffff);
 +    // 对于 1.20 及以下的版本
 +    multilineText.drawWithShadow(context, 10, height / 2, 16, 0xffffff);
 </code> </code>
  
-Another alterative is using ''TextWidget'' or ''MultilineTextWidget''. They are by default not selectable or narratable because their ''active'' field is ''false''+另一个选择是使用 ''TextWidget'' 或 ''MultilineTextWidget''。这些部件默认不会被选中也不会被复述,因为其 ''active'' 字段为 ''false''
 ===== 滚动 ===== ===== 滚动 =====
-The screen does not support scrolling, but you can add widgets that supports scrolling. ''<yarn class_350>'' is a class for widgets that contains multiple entries and supports scrolling. However, instead of directly extending it, it is more suitable to extend ''<yarn class_4280>'' or ''<yarn class_4265>'', which already implemented navigation and narration. The difference is: +屏幕不支持滚动,但是你可以添加支持滚动的部件。''<yarn class_350>'' 用于包含多个项并支持滚动的部件。不过,一般不是直接继承它,而是继承 ''<yarn class_4280>'' 或 ''<yarn class_4265>'' 更加合适,这些类实现了导航和复述。二者的区别在于:
- +
-  * ''<yarn class_4280>'' refers to a widget in which you can select a row. In widgets that extends the class, you usually select one entry in the list. Some vanilla examples are biome selection screen in the buffet (single biome) world option, and the language selection screen. +
-  * ''<yarn class_4265>'' refers to a widget where each row has child elements. In widgets that extends this class, you select and interacts the elements in the rows. Like a ''Screen'', the ''<yarn class_4265>.<yarn class_4266>'' should have zero, one, or more child elements.+
  
 +  * ''<yarn class_4280>'' 指你可以选择一行的部件,在继承了此类的部件中,你通常会选择列表中的一个项。例如,在原版中的自选世界(单一生物群系)世界选项中的生物建筑系选择列表,以及语言选择列表。
 +  * ''<yarn class_4265>'' 指每行包含多个子元素的部件。在继承了此类的部件中,你可以选择并交到各行中的元素。就行 ''Screen'' 一样,''<yarn class_4265>.<yarn class_4266>'' 应该有零个、一个或多个子元素。
 ===== 完成之前需要检查的事情 ===== ===== 完成之前需要检查的事情 =====
  
-After finishing your screen, in order to avoid potential issues, please check: +在完成你的屏幕之后,为避免潜在的问题,请检查: 
-  * whether the screen returns to the last screen (parent screen) when you press "Esc" +  * 按下 Esc 时,屏幕是否能够返回上一级屏幕 
-  * whether these classes exist only on client (which means they will not be loaded in the dedicated server) +  * 这些类是否仅存在于客户端(也就是说在专用服务器上不会加载) 
-  * whether elements are focused in the correct order when you press "Tab" to select them +  * 按下 Tab 选择元素时,这些元素被选中的顺序是否正确 
-  * whether the behaviors are correct when you resize +  * 重新调整屏幕尺寸时的行为是否正确 
-  * whether the narrations are correct when you use "Tab" or mouse cursor to select element while narration enabled+  * 启用了复述功能时,按下 Tab 或使用鼠标指针选择元素时的复述是否正确
zh_cn/tutorial/screen.1682324773.txt.gz · Last modified: 2023/04/24 08:26 by solidblock