tutorial:screen
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| tutorial:screen [2023/04/21 10:22] – [Notice about the order] solidblock | tutorial:screen [2026/03/01 23:33] (current) – cassiancc | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Creating a screen ====== | ====== Creating a screen ====== | ||
| - | :!: The page is still in progress. | + | A **screen** |
| - | A **screen** is a graphical user interface that extends '' | + | You may use mixins to add into an existing screen a button that goes to your screen. But in many cases, we can implement the '' |
| - | + | ||
| - | You may use mixins to add into an existing screen a button that goes to your screen. But in many cases, we can implement the '' | + | |
| ===== Adding widgets ===== | ===== Adding widgets ===== | ||
| Line 11: | Line 9: | ||
| <code java> | <code java> | ||
| - | @Environment(EnvType.CLIENT) | ||
| public class TutorialScreen extends Screen { | public class TutorialScreen extends Screen { | ||
| protected TutorialScreen() { | protected TutorialScreen() { | ||
| // The parameter is the title of the screen, | // The parameter is the title of the screen, | ||
| // which will be narrated when you enter the screen. | // which will be narrated when you enter the screen. | ||
| - | super(Text.literal(" | + | super(Component.literal(" |
| } | } | ||
| - | public | + | public |
| - | public | + | public |
| @Override | @Override | ||
| protected void init() { | protected void init() { | ||
| - | button1 = ButtonWidget.builder(Text.literal(" | + | button1 = ButtonWidget.builder(Component.literal(" |
| System.out.println(" | System.out.println(" | ||
| }) | }) | ||
| - | .dimensions(width / 2 - 205, 20, 200, 20) | + | .bounds(width / 2 - 205, 20, 200, 20) |
| - | .tooltip(Tooltip.of(Text.literal(" | + | .tooltip(Tooltip.create(Component.literal(" |
| .build(); | .build(); | ||
| button2 = ButtonWidget.builder(Text.literal(" | button2 = ButtonWidget.builder(Text.literal(" | ||
| System.out.println(" | System.out.println(" | ||
| }) | }) | ||
| - | .dimensions(width / 2 + 5, 20, 200, 20) | + | .bounds(width / 2 + 5, 20, 200, 20) |
| - | .tooltip(Tooltip.of(Text.literal(" | + | .tooltip(Tooltip.create(Component.literal(" |
| .build(); | .build(); | ||
| | | ||
| - | | + | |
| - | | + | |
| } | } | ||
| } | } | ||
| Line 50: | Line 47: | ||
| * The screen is resized. Before invoking this, all existing elements are removed. | * The screen is resized. Before invoking this, all existing elements are removed. | ||
| - | You must use add the elements to the screen via using '' | + | You must use add the elements to the screen via using '' |
| - | * '' | + | * '' |
| - | * '' | + | * '' |
| - | * '' | + | * '' |
| - | In the '' | + | In the '' |
| - | For versions before 1.19.3, '' | + | For versions before 1.19.3, '' |
| ==== Can I instantiate widgets in the constructor ==== | ==== Can I instantiate widgets in the constructor ==== | ||
| Line 67: | Line 64: | ||
| @Override | @Override | ||
| protected void init() { | protected void init() { | ||
| - | | + | |
| - | | + | |
| } | } | ||
| </ | </ | ||
| - | This is also OK. Its advantage is, if the widgets have some several states (such as the current selections of '' | + | This is also OK. Its advantage is, if the widgets have some several states (such as the current selections of '' |
| <code java> | <code java> | ||
| Line 80: | Line 77: | ||
| button2.setPosition(width / 2 + 5, 20); | button2.setPosition(width / 2 + 5, 20); | ||
| - | | + | |
| - | | + | |
| } | } | ||
| </ | </ | ||
| Line 89: | Line 86: | ||
| ===== The parent screen ===== | ===== The parent screen ===== | ||
| - | I accessed the screen via another screen, such as the Mod Menu screen, but when I press '' | + | I accessed the screen via another screen, such as the Mod Menu screen, but when I press "Esc" |
| - | Add a parent as a parameter and field, and use it in the '' | + | This is because you did not specify a parent screen, and the '' |
| + | |||
| + | Add a '' | ||
| <code java> | <code java> | ||
| Line 97: | Line 96: | ||
| protected TutorialScreen(Screen parent) { | protected TutorialScreen(Screen parent) { | ||
| - | super(Text.literal(" | + | super(Component.literal(" |
| this.parent = parent; | this.parent = parent; | ||
| } | } | ||
| | | ||
| @Override | @Override | ||
| - | public void close() { | + | public void onClose() { |
| client.setScreen(parent); | client.setScreen(parent); | ||
| } | } | ||
| Line 108: | Line 107: | ||
| ===== Add narrations ===== | ===== Add narrations ===== | ||
| - | 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 '' | + | 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 '' |
| * **Title**: The title of the screen, which is defined in the constructor. When you enter the screen, the title will be automatically narrated. | * **Title**: The title of the screen, which is defined in the constructor. When you enter the screen, the title will be automatically narrated. | ||
| * **Position**: | * **Position**: | ||
| - | * **Hint**: This refers the tooltip of the element you focus on or hovers on. For example, the '' | + | * **Hint**: This refers the tooltip of the element you focus on or hovers on. For example, |
| * **Usage**: In vanilla, the usage is: "//Use mouse cursor or Tab button to select element//" | * **Usage**: In vanilla, the usage is: "//Use mouse cursor or Tab button to select element//" | ||
| Besides the narration of the screen, you can also customize the narration of the element, by overriding '' | Besides the narration of the screen, you can also customize the narration of the element, by overriding '' | ||
| - | In the method of appending narrations, using '' | + | In the method of appending narrations, using '' |
| - | 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, | + | 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, |
| ===== Adding text ===== | ===== Adding text ===== | ||
| - | In '' | + | In '' |
| <code java> | <code java> | ||
| + | // For versions 1.20 below | ||
| @Override | @Override | ||
| - | public void render(MatrixStack | + | public void render(PoseStack |
| super.render(matrices, | super.render(matrices, | ||
| - | | + | |
| + | } | ||
| + | |||
| + | // For versions 1.20 and after | ||
| + | @Override | ||
| + | public void render(GuiGraphics context, int mouseX, int mouseY, float delta) { | ||
| + | super.render(context, | ||
| + | context.drawCenteredString(font, | ||
| } | } | ||
| </ | </ | ||
| Line 133: | Line 140: | ||
| If you're concerned that the text can be pretty long and may exceed the screen limit, you can use '' | If you're concerned that the text can be pretty long and may exceed the screen limit, you can use '' | ||
| <code java> | <code java> | ||
| - | final MultilineText | + | final MultiLineLabel |
| - | multilineText.drawWithShadow(matrices, 10, height / 2, 16, 0xffffff); | + | |
| + | // For versions 1.20 below | ||
| + | multilineText.renderLeftAligned(matrices, 10, height / 2, 16, 0xffffff); | ||
| + | // For versions 1.20 and after | ||
| + | multilineText.renderLeftAligned(context, 10, height / 2, 16, 0xffffff); | ||
| </ | </ | ||
| - | Another alterative is using '' | + | Another alterative is using '' |
| + | |||
| + | ===== Scrolling ===== | ||
| + | The screen does not support scrolling, but you can add widgets that supports scrolling. '' | ||
| + | |||
| + | * '' | ||
| + | * '' | ||
| + | |||
| + | ===== Things to check before finishing ===== | ||
| - | [WIP] | + | 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 " | ||
| + | * 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 " | ||
| + | * whether the behaviors are correct when you resize | ||
| + | * whether the narrations are correct when you use " | ||
tutorial/screen.1682072550.txt.gz · Last modified: 2023/04/21 10:22 by solidblock