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 02:33] – solidblock | tutorial:screen [2023/12/18 01:38] (current) – [Adding text] solidblock | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ===== Creating a screen ===== | + | ====== Creating a screen |
- | :!: The page is still in progress. | + | A **screen** |
- | A screen | + | You may use mixins to add into an existing |
- | Usually you need to use mixins to add a button to that screen into an existing screen. But in most cases, we just implement the '' | + | ===== Adding widgets |
- | + | ||
- | ==== Adding widgets ==== | + | |
A screen should have several " | A screen should have several " | ||
Line 32: | Line 30: | ||
.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) | .dimensions(width / 2 + 5, 20, 200, 20) | ||
Line 39: | Line 37: | ||
| | ||
addDrawableChild(button1); | addDrawableChild(button1); | ||
- | addDrawableChild(button2) | + | addDrawableChild(button2); |
} | } | ||
} | } | ||
</ | </ | ||
- | === The '' | + | ==== The "init" |
The '' | The '' | ||
- | - The screen is created. | + | * The screen is created. |
- | - The screen is resized. Before invoking this, all child 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 |
- | Some users may instantiaze | + | Some users may instantiate |
<code java> | <code java> | ||
- | |||
public ButtonWidget button1 = ButtonWidget.builder(...).build(); | public ButtonWidget button1 = ButtonWidget.builder(...).build(); | ||
public ButtonWidget button2 = ButtonWidget.builder(...).build(); | public ButtonWidget button2 = ButtonWidget.builder(...).build(); | ||
Line 69: | Line 66: | ||
protected void init() { | protected void init() { | ||
addDrawableChild(button1); | addDrawableChild(button1); | ||
- | addDrawableChild(button2) | + | addDrawableChild(button2); |
} | } | ||
</ | </ | ||
- | 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 86: | Line 83: | ||
</ | </ | ||
- | === Notice about the order === | + | ==== Notice about the order ==== |
- | After adding amounts of elements, all of them are added. Some people don't care about the order they are added, because all of the widgets are rendered at the sametime. However, if you select widgets by pressing the '' | + | After adding amounts of elements, all of them can render and be selected. Some people don't care about the order they are added, because all of the widgets are rendered at the sametime. However, if you select widgets by pressing the "Tab" |
+ | |||
+ | ===== The parent screen ===== | ||
+ | I accessed the screen via another screen, such as the Mod Menu screen, but when I press " | ||
+ | |||
+ | This is because you did not specify a parent screen, and the '' | ||
+ | |||
+ | Add a '' | ||
+ | <code java> | ||
+ | |||
+ | private final Screen parent; | ||
+ | |||
+ | protected TutorialScreen(Screen parent) { | ||
+ | super(Text.literal(" | ||
+ | this.parent = parent; | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public void close() { | ||
+ | client.setScreen(parent); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== 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 '' | ||
+ | |||
+ | * **Title**: The title of the screen, which is defined in the constructor. When you enter the screen, the title will be automatically narrated. | ||
+ | * **Position**: | ||
+ | * **Hint**: This refers the tooltip of the element you focus on or hovers on. For example, you may remember about the '' | ||
+ | * **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 '' | ||
+ | |||
+ | 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, | ||
+ | |||
+ | ===== Adding text ===== | ||
+ | In '' | ||
+ | <code java> | ||
+ | // For versions 1.20 below | ||
+ | @Override | ||
+ | public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) { | ||
+ | super.render(matrices, | ||
+ | drawCenteredTextWithShadow(matrices, | ||
+ | } | ||
+ | |||
+ | // For versions 1.20 and after | ||
+ | @Override | ||
+ | public void render(DrawContext context, int mouseX, int mouseY, float delta) { | ||
+ | super.render(context, | ||
+ | context.drawCenteredTextWithShadow(textRenderer, | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | If you're concerned that the text can be pretty long and may exceed the screen limit, you can use '' | ||
+ | <code java> | ||
+ | final MultilineText multilineText = MultilineText.create(textRenderer, | ||
+ | |||
+ | // For versions 1.20 below | ||
+ | multilineText.drawWithShadow(matrices, | ||
+ | // For versions 1.20 and after | ||
+ | multilineText.drawWithShadow(context, | ||
+ | </ | ||
+ | |||
+ | 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.1682044403.txt.gz · Last modified: 2023/04/21 02:33 by solidblock