PopupScreen .java

net.minecraft.client.gui.components.PopupScreen (class): members: BG_BORDER_WITH_SPACING, POPUP_DEFAULT_WIDTH, backgroundWidth, IMAGE_SIZE_X, IMAGE_SIZE_Y.

net.minecraft.client.gui.componentsclass187 linesclientextends Screen6.0 KB

API Surface (12)

void added()
void init()
void repositionElements()
void extractBackground(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a)
Component getNarrationMessage()
void onClose()
PopupScreen.Builder setWidth(final int width)
PopupScreen.Builder setImage(final Identifier image)
PopupScreen.Builder addMessage(final Component message)
PopupScreen.Builder addButton(final Component message, final Consumer<PopupScreen> action)
PopupScreen.Builder onClose(final Runnable onClose)
PopupScreen build()

Fields & Constants (16)

BACKGROUND_SPRITE
SPACING
BG_BORDER_WITH_SPACING
BUTTON_SPACING
IMAGE_SIZE_X
IMAGE_SIZE_Y
POPUP_DEFAULT_WIDTH
backgroundScreen
image
messages
buttons
onClose
contentWidth
layout
backgroundWidth
title

String literals (2)

"popup/background"
"Popup must have at least one button"

Full Source

PopupScreen.java187 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.gui.components;
2
3
import java.util.ArrayList;
4
import java.util.List;
5
import java.util.function.Consumer;
6
import net.fabricmc.api.EnvType;
7
import net.fabricmc.api.Environment;
8
import net.minecraft.ChatFormatting;
9
import net.minecraft.client.gui.GuiGraphicsExtractor;
10
import net.minecraft.client.gui.layouts.FrameLayout;
11
import net.minecraft.client.gui.layouts.LinearLayout;
12
import net.minecraft.client.gui.screens.Screen;
13
import net.minecraft.client.renderer.RenderPipelines;
14
import net.minecraft.network.chat.CommonComponents;
15
import net.minecraft.network.chat.Component;
16
import net.minecraft.resources.Identifier;
17
import org.jspecify.annotations.Nullable;
18
19
@Environment(EnvType.CLIENT)
20
public class PopupScreen extends Screen {
21
	private static final Identifier BACKGROUND_SPRITE = Identifier.withDefaultNamespace("popup/background");
22
	private static final int SPACING = 12;
23
	private static final int BG_BORDER_WITH_SPACING = 18;
24
	private static final int BUTTON_SPACING = 6;
25
	private static final int IMAGE_SIZE_X = 130;
26
	private static final int IMAGE_SIZE_Y = 64;
27
	private static final int POPUP_DEFAULT_WIDTH = 250;
28
	private final @Nullable Screen backgroundScreen;
29
	private final @Nullable Identifier image;
30
	private final List<Component> messages;
31
	private final List<PopupScreen.ButtonOption> buttons;
32
	private final @Nullable Runnable onClose;
33
	private final int contentWidth;
34
	private final LinearLayout layout = LinearLayout.vertical();
35
36
	private PopupScreen(
37
		final @Nullable Screen backgroundScreen,
38
		final int backgroundWidth,
39
		final @Nullable Identifier image,
40
		final Component title,
41
		final List<Component> messages,
42
		final List<PopupScreen.ButtonOption> buttons,
43
		final @Nullable Runnable onClose
44
	) {
45
		super(title);
46
		this.backgroundScreen = backgroundScreen;
47
		this.image = image;
48
		this.messages = messages;
49
		this.buttons = buttons;
50
		this.onClose = onClose;
51
		this.contentWidth = backgroundWidth - 36;
52
	}
53
54
	@Override
55
	public void added() {
56
		super.added();
57
		if (this.backgroundScreen != null) {
58
			this.backgroundScreen.clearFocus();
59
		}
60
	}
61
62
	@Override
63
	protected void init() {
64
		if (this.backgroundScreen != null) {
65
			this.backgroundScreen.init(this.width, this.height);
66
		}
67
68
		this.layout.spacing(12).defaultCellSetting().alignHorizontallyCenter();
69
		this.layout.addChild(new MultiLineTextWidget(this.title.copy().withStyle(ChatFormatting.BOLD), this.font).setMaxWidth(this.contentWidth).setCentered(true));
70
		if (this.image != null) {
71
			this.layout.addChild(ImageWidget.texture(130, 64, this.image, 130, 64));
72
		}
73
74
		this.messages.forEach(message -> this.layout.addChild(new MultiLineTextWidget(message, this.font).setMaxWidth(this.contentWidth).setCentered(true)));
75
		this.layout.addChild(this.buildButtonRow());
76
		this.layout.visitWidgets(x$0 -> this.addRenderableWidget(x$0));
77
		this.repositionElements();
78
	}
79
80
	private LinearLayout buildButtonRow() {
81
		int totalSpacing = 6 * (this.buttons.size() - 1);
82
		int buttonWidth = Math.min((this.contentWidth - totalSpacing) / this.buttons.size(), 150);
83
		LinearLayout row = LinearLayout.horizontal();
84
		row.spacing(6);
85
86
		for (PopupScreen.ButtonOption button : this.buttons) {
87
			row.addChild(Button.builder(button.message(), b -> button.action().accept(this)).width(buttonWidth).build());
88
		}
89
90
		return row;
91
	}
92
93
	@Override
94
	protected void repositionElements() {
95
		if (this.backgroundScreen != null) {
96
			this.backgroundScreen.resize(this.width, this.height);
97
		}
98
99
		this.layout.arrangeElements();
100
		FrameLayout.centerInRectangle(this.layout, this.getRectangle());
101
	}
102
103
	@Override
104
	public void extractBackground(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
105
		if (this.backgroundScreen != null) {
106
			this.backgroundScreen.extractBackground(graphics, mouseX, mouseY, a);
107
			graphics.nextStratum();
108
			this.backgroundScreen.extractRenderState(graphics, -1, -1, a);
109
			graphics.nextStratum();
110
			this.extractTransparentBackground(graphics);
111
		} else {
112
			super.extractBackground(graphics, mouseX, mouseY, a);
113
		}
114
115
		graphics.blitSprite(
116
			RenderPipelines.GUI_TEXTURED, BACKGROUND_SPRITE, this.layout.getX() - 18, this.layout.getY() - 18, this.layout.getWidth() + 36, this.layout.getHeight() + 36
117
		);
118
	}
119
120
	@Override
121
	public Component getNarrationMessage() {
122
		return CommonComponents.joinForNarration(this.title, CommonComponents.joinLines(this.messages));
123
	}
124
125
	@Override
126
	public void onClose() {
127
		if (this.onClose != null) {
128
			this.onClose.run();
129
		}
130
131
		this.minecraft.gui.setScreen(this.backgroundScreen);
132
	}
133
134
	@Environment(EnvType.CLIENT)
135
	public static class Builder {
136
		private final @Nullable Screen backgroundScreen;
137
		private final Component title;
138
		private final List<Component> messages = new ArrayList<>();
139
		private int width = 250;
140
		private @Nullable Identifier image;
141
		private final List<PopupScreen.ButtonOption> buttons = new ArrayList<>();
142
		private @Nullable Runnable onClose = null;
143
144
		public Builder(final @Nullable Screen backgroundScreen, final Component title) {
145
			this.backgroundScreen = backgroundScreen;
146
			this.title = title;
147
		}
148
149
		public PopupScreen.Builder setWidth(final int width) {
150
			this.width = width;
151
			return this;
152
		}
153
154
		public PopupScreen.Builder setImage(final Identifier image) {
155
			this.image = image;
156
			return this;
157
		}
158
159
		public PopupScreen.Builder addMessage(final Component message) {
160
			this.messages.add(message);
161
			return this;
162
		}
163
164
		public PopupScreen.Builder addButton(final Component message, final Consumer<PopupScreen> action) {
165
			this.buttons.add(new PopupScreen.ButtonOption(message, action));
166
			return this;
167
		}
168
169
		public PopupScreen.Builder onClose(final Runnable onClose) {
170
			this.onClose = onClose;
171
			return this;
172
		}
173
174
		public PopupScreen build() {
175
			if (this.buttons.isEmpty()) {
176
				throw new IllegalStateException("Popup must have at least one button");
177
			} else {
178
				return new PopupScreen(this.backgroundScreen, this.width, this.image, this.title, this.messages, List.copyOf(this.buttons), this.onClose);
179
			}
180
		}
181
	}
182
183
	@Environment(EnvType.CLIENT)
184
	private record ButtonOption(Component message, Consumer<PopupScreen> action) {
185
	}
186
}
187

Decompiled Minecraft 26.3 source of PopupScreen (net/minecraft/client/gui/components/PopupScreen.java) with Mojang official mappings, stored verbatim on the site.