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

Next revision
Previous revision
zh_cn:tutorial:screen [2023/04/24 08:17] – created 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 108: Line 108:
  
 ===== 添加复述 ===== ===== 添加复述 =====
-By default, when narration is enabled, the screen title and information of the element you hover or focus on will be narrated. If the screen requires extra narrations (for example it has some texts rendered but not added as a widget), you can override ''addScreenNarrations'' or ''addElementNarrations''. The methods take a ''NarrationBuilder'', in which you can use ''add'' method to add narration messages. The narration messages are divided into the following parts (''NarrationPart''):+默认情况下,在复述功能启用时,屏幕的标题以及你鼠标悬浮或选中的元素的信息会被复述。如果屏幕需要额外的复述(例如渲染了一些文本但没有添加为部件),你可以覆盖 ''addScreenNattaions'' 或 ''addElementNarrations''。这些方法接收一个 ''NarrationBuilder'',在这里你可以使用其 ''add'' 方法以添加复述消息。复述的消息分为以下部分(''NarrationPart''):
  
-  * **Title**: The title of the screen, which is defined in the constructor. When you enter the screen, the title will be automatically narrated. +  * **Title**:屏幕的标题,会在构造函数中指定。当你进入屏幕时,这个标题会被自动复述。 
-  * **Position**: Telling you the position of the widget you are focusing on. In vanilla, it is: "//Screen element %s out of %s//". Besides, if in a list widget, the following will also be narrated: "//Selected list row %s out of %s//". +  * **Position**:告诉你当前正在选中的部件的位置。在原版中,是“//屏幕控件,第%s个,共%s//”。此外,如果是在列表中,还会复述以下内容:“//已选中列表的第%s行,共%s//”。 
-  * **Hint**: This refers the tooltip of the element you focus on or hovers on. For example, you may remember about the ''tooltip'' method when you create the ''ButtonWidget'' in the code above. The tooltip is narrated in this part. +  * **Hint**:这个是指的当前选中或悬浮的元素的提示。例如,你可能会记得在上面的代码中创建 ''ButtonWidget'' 时有个 ''tooltip'' 方法,这个 tooltip 就是在这一部分复述的。 
-  * **Usage**: In vanilla, the usage is: "//Use mouse cursor or Tab button to select element//".+  * **Usage**:在原版中,用法为“//使用鼠标指针或Tab键选择屏幕控件//”。
  
-Besides the narration of the screen, you can also customize the narration of the element, by overriding ''appendNarrations'' method of the class of that element. The element is narrated after the narration of the screen.+除了对屏幕的复述之外,你还可以自定义元素的复述,也就是覆盖元素的类的 ''appendNarrations'' 方法,屏幕复述完成后,就会复述元素。
  
-In the method of appending narrations, using ''narrationBuilder.nextMessage()'' can append narrations after the current narrations, instead of replacing existing part of the narration.+在添加复述的方法中,使用 ''narrationBuilder.nextMessage()'' 可以在当前复述消息之后添加复述,而不是替换复述中的已存在的部分。
  
-In some cases, you want repetitive narrations, instead of narrating only once. For example, when loading a level, the percentage of loading is narrated repetitively, telling the user the current loading status. You can call ''narrateScreenIfNarrationEnabled'' in the ''render'' or ''tick'' method. For more details, you may refer to sources of ''LevelLoadingScreen''.+在有些类中,你可能需要重复的复述,而不是只复述一次。例如,在加载世界时,加载的百分比会重复复述,告诉用户当前的加载状态。你可以在 ''render'' 或 ''tick'' 方法中,调用 ''narrateScreenIfNarrationEnabled''。对于更多细节,请参考 ''LevelLoadingScreen'' 的源代码。
  
 ===== 添加文本 ===== ===== 添加文本 =====
-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.1682324240.txt.gz · Last modified: 2023/04/24 08:17 by solidblock