User Tools

Site Tools


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
tutorial:screen [2023/04/21 10:49] – [Scrolling] solidblocktutorial:screen [2023/12/18 01:38] (current) – [Adding text] solidblock
Line 1: Line 1:
 ====== Creating a screen ====== ====== Creating a screen ======
  
-A **screen** is a graphical user interface that extends ''Screen'', allowing the user to interact and fulfill some functionalities. One example of a screen is a custom config screen of your mod. Screens only exist in the client, so you must annotate them with ''@Environment(EnvType.CLIENT)''.+A **screen** is a graphical user interface that extends ''Screen'', allowing the user to interact and fulfill some functionalities. One example of a screen is a custom config screen of your mod. Screens only exist in the client, so you can annotate the relavant classes with ''@Environment(EnvType.CLIENT)''.
  
 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 ''ModMenuApi'' of Mod Menu mod, and make it possible to access the screen via the config button in the Mod Menu screen. This article does document how to implement ''ModMenuApi''. 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 ''ModMenuApi'' of Mod Menu mod, and make it possible to access the screen via the config button in the Mod Menu screen. This article does document how to implement ''ModMenuApi''.
Line 124: Line 124:
 In ''render'' method, you can invoke methods like ''textRenderer.draw'', ''drawTextWithShadow'' or ''drawCenteredTextWithShadow'' to render a text on the screen. In ''render'' method, you can invoke methods like ''textRenderer.draw'', ''drawTextWithShadow'' or ''drawCenteredTextWithShadow'' to render a text on the screen.
 <code java> <code java>
 +  // For versions 1.20 below
   @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("You must see me"), width / 2, height / 2, 0xffffff);
 +  }
 +  
 +  // For versions 1.20 and after
 +  @Override
 +  public void render(DrawContext context, int mouseX, int mouseY, float delta) {
 +    super.render(context, mouseX, mouseY, delta);
 +    context.drawCenteredTextWithShadow(textRenderer, Text.literal("You must see me"), width / 2, height / 2, 0xffffff);
   }   }
 </code> </code>
Line 134: Line 142:
 <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("The text is pretty long ".repeat(20)), width - 20);
 +    
 +    // For versions 1.20 below
     multilineText.drawWithShadow(matrices, 10, height / 2, 16, 0xffffff);     multilineText.drawWithShadow(matrices, 10, height / 2, 16, 0xffffff);
 +    // For versions 1.20 and after
 +    multilineText.drawWithShadow(context, 10, height / 2, 16, 0xffffff);
 </code> </code>
  
tutorial/screen.1682074163.txt.gz · Last modified: 2023/04/21 10:49 by solidblock