DialogScreen .java

net.minecraft.client.gui.screens.dialog.DialogScreen (class, 258 lines): members: makeSureWarningButtonIsInBounds, createTitleWithWarningButton, runAction.

net.minecraft.client.gui.screens.dialogclass258 linesclientextends Dialog>9.2 KB

API Surface (14)

void init()
void populateBodyElements(final LinearLayout layout, final DialogControlSet controlSet, final T dialog, final DialogConnectionAccess connectionAccess)
void updateHeaderAndFooter(final HeaderAndFooterLayout layout, final DialogControlSet controlSet, final T dialog, final DialogConnectionAccess connectionAccess)
void repositionElements()
LayoutElement createTitleWithWarningButton()
void makeSureWarningButtonIsInBounds()
boolean isPauseScreen()
boolean shouldCloseOnEsc()
void onClose()
void runAction(final Optional<ClickEvent> closeAction)
void runAction(final Optional<ClickEvent> closeAction, final DialogAction afterAction)
LayoutElement packControlsIntoColumns(final List<? extends LayoutElement> controls, final int columns)
Screen create(final Minecraft minecraft, final DialogConnectionAccess connectionAccess, final Screen returnScreen)
void updateReturnScreen(final @Nullable Screen newReturnScreen)

Fields & Constants (10)

DISCONNECT
WARNING_BUTTON_SIZE
WARNING_BUTTON_SPRITES
dialog
layout
previousScreen
bodyScroll
warningButton
connectionAccess
onClose

String literals (8)

"menu.custom_screen_info.disconnect"
"dialog/warning_button"
"dialog/warning_button_disabled"
"dialog/warning_button_highlighted"
"menu.custom_screen_info.button_narration"
"menu.custom_screen_info.tooltip"
"menu.custom_screen_info.title"
"menu.custom_screen_info.contents"

Full Source

DialogScreen.java258 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.gui.screens.dialog;
2
3
import java.util.List;
4
import java.util.Optional;
5
import java.util.function.Supplier;
6
import net.fabricmc.api.EnvType;
7
import net.fabricmc.api.Environment;
8
import net.minecraft.client.Minecraft;
9
import net.minecraft.client.gui.components.Button;
10
import net.minecraft.client.gui.components.ImageButton;
11
import net.minecraft.client.gui.components.ScrollableLayout;
12
import net.minecraft.client.gui.components.StringWidget;
13
import net.minecraft.client.gui.components.Tooltip;
14
import net.minecraft.client.gui.components.WidgetSprites;
15
import net.minecraft.client.gui.layouts.GridLayout;
16
import net.minecraft.client.gui.layouts.HeaderAndFooterLayout;
17
import net.minecraft.client.gui.layouts.LayoutElement;
18
import net.minecraft.client.gui.layouts.LinearLayout;
19
import net.minecraft.client.gui.screens.ConfirmScreen;
20
import net.minecraft.client.gui.screens.Screen;
21
import net.minecraft.client.gui.screens.dialog.body.DialogBodyHandlers;
22
import net.minecraft.commands.Commands;
23
import net.minecraft.network.chat.ClickEvent;
24
import net.minecraft.network.chat.CommonComponents;
25
import net.minecraft.network.chat.Component;
26
import net.minecraft.network.chat.ClickEvent.Custom;
27
import net.minecraft.network.chat.ClickEvent.RunCommand;
28
import net.minecraft.network.chat.ClickEvent.ShowDialog;
29
import net.minecraft.resources.Identifier;
30
import net.minecraft.server.dialog.Dialog;
31
import net.minecraft.server.dialog.DialogAction;
32
import net.minecraft.server.dialog.Input;
33
import net.minecraft.server.dialog.body.DialogBody;
34
import org.apache.commons.lang3.mutable.MutableObject;
35
import org.jspecify.annotations.Nullable;
36
37
@Environment(EnvType.CLIENT)
38
public abstract class DialogScreen<T extends Dialog> extends Screen {
39
	public static final Component DISCONNECT = Component.translatable("menu.custom_screen_info.disconnect");
40
	private static final int WARNING_BUTTON_SIZE = 20;
41
	private static final WidgetSprites WARNING_BUTTON_SPRITES = new WidgetSprites(
42
		Identifier.withDefaultNamespace("dialog/warning_button"),
43
		Identifier.withDefaultNamespace("dialog/warning_button_disabled"),
44
		Identifier.withDefaultNamespace("dialog/warning_button_highlighted")
45
	);
46
	private final T dialog;
47
	private final HeaderAndFooterLayout layout = new HeaderAndFooterLayout(this);
48
	private final @Nullable Screen previousScreen;
49
	private @Nullable ScrollableLayout bodyScroll;
50
	private Button warningButton;
51
	private final DialogConnectionAccess connectionAccess;
52
	private Supplier<Optional<ClickEvent>> onClose = DialogControlSet.EMPTY_ACTION;
53
54
	public DialogScreen(final @Nullable Screen previousScreen, final T dialog, final DialogConnectionAccess connectionAccess) {
55
		super(dialog.common().title());
56
		this.dialog = dialog;
57
		this.previousScreen = previousScreen;
58
		this.connectionAccess = connectionAccess;
59
	}
60
61
	@Override
62
	protected final void init() {
63
		super.init();
64
		this.warningButton = this.createWarningButton();
65
		this.warningButton.setTabOrderGroup(-10);
66
		DialogControlSet controlSet = new DialogControlSet(this);
67
		LinearLayout body = LinearLayout.vertical().spacing(10);
68
		body.defaultCellSetting().alignHorizontallyCenter();
69
		this.layout.addToHeader(this.createTitleWithWarningButton());
70
71
		for (DialogBody dialogBody : this.dialog.common().body()) {
72
			LayoutElement bodyElement = DialogBodyHandlers.createBodyElement(this, dialogBody);
73
			if (bodyElement != null) {
74
				body.addChild(bodyElement);
75
			}
76
		}
77
78
		for (Input input : this.dialog.common().inputs()) {
79
			controlSet.addInput(input, body::addChild);
80
		}
81
82
		this.populateBodyElements(body, controlSet, this.dialog, this.connectionAccess);
83
		this.bodyScroll = new ScrollableLayout(this.minecraft, body, this.layout.getContentHeight());
84
		this.layout.addToContents(this.bodyScroll);
85
		this.updateHeaderAndFooter(this.layout, controlSet, this.dialog, this.connectionAccess);
86
		this.onClose = controlSet.bindAction(this.dialog.onCancel());
87
		this.layout.visitWidgets(widget -> {
88
			if (widget != this.warningButton) {
89
				this.addRenderableWidget(widget);
90
			}
91
		});
92
		this.addRenderableWidget(this.warningButton);
93
		this.repositionElements();
94
	}
95
96
	protected void populateBodyElements(
97
		final LinearLayout layout, final DialogControlSet controlSet, final T dialog, final DialogConnectionAccess connectionAccess
98
	) {
99
	}
100
101
	protected void updateHeaderAndFooter(
102
		final HeaderAndFooterLayout layout, final DialogControlSet controlSet, final T dialog, final DialogConnectionAccess connectionAccess
103
	) {
104
	}
105
106
	@Override
107
	protected void repositionElements() {
108
		this.bodyScroll.arrangeElements();
109
		this.bodyScroll.setMaxHeight(this.layout.getContentHeight());
110
		this.layout.arrangeElements();
111
		this.makeSureWarningButtonIsInBounds();
112
	}
113
114
	protected LayoutElement createTitleWithWarningButton() {
115
		LinearLayout layout = LinearLayout.horizontal().spacing(10);
116
		layout.defaultCellSetting().alignHorizontallyCenter().alignVerticallyMiddle();
117
		layout.addChild(new StringWidget(this.title, this.font));
118
		layout.addChild(this.warningButton);
119
		return layout;
120
	}
121
122
	protected void makeSureWarningButtonIsInBounds() {
123
		int x = this.warningButton.getX();
124
		int y = this.warningButton.getY();
125
		if (x < 0 || y < 0 || x > this.width - 20 || y > this.height - 20) {
126
			this.warningButton.setX(Math.max(0, this.width - 40));
127
			this.warningButton.setY(Math.min(5, this.height));
128
		}
129
	}
130
131
	private Button createWarningButton() {
132
		ImageButton result = new ImageButton(
133
			0,
134
			0,
135
			20,
136
			20,
137
			WARNING_BUTTON_SPRITES,
138
			var1x -> this.minecraft.gui.setScreen(DialogScreen.WarningScreen.create(this.minecraft, this.connectionAccess, this)),
139
			Component.translatable("menu.custom_screen_info.button_narration")
140
		);
141
		result.setTooltip(Tooltip.create(Component.translatable("menu.custom_screen_info.tooltip")));
142
		return result;
143
	}
144
145
	@Override
146
	public boolean isPauseScreen() {
147
		return this.dialog.common().pause();
148
	}
149
150
	@Override
151
	public boolean shouldCloseOnEsc() {
152
		return this.dialog.common().canCloseWithEscape();
153
	}
154
155
	@Override
156
	public void onClose() {
157
		this.runAction(this.onClose.get(), DialogAction.CLOSE);
158
	}
159
160
	public void runAction(final Optional<ClickEvent> closeAction) {
161
		this.runAction(closeAction, this.dialog.common().afterAction());
162
	}
163
164
	public void runAction(final Optional<ClickEvent> closeAction, final DialogAction afterAction) {
165
		Screen screenToActivate = switch (afterAction) {
166
			case NONE -> this;
167
			case CLOSE -> this.previousScreen;
168
			case WAIT_FOR_RESPONSE -> new WaitingForResponseScreen(this.previousScreen);
169
		};
170
		if (closeAction.isPresent()) {
171
			this.handleDialogClickEvent(closeAction.get(), screenToActivate);
172
		} else {
173
			this.minecraft.gui.setScreen(screenToActivate);
174
		}
175
	}
176
177
	private void handleDialogClickEvent(final ClickEvent event, final @Nullable Screen activeScreen) {
178
		switch (event) {
179
			case RunCommand(String command):
180
				this.connectionAccess.runCommand(Commands.trimOptionalPrefix(command), activeScreen);
181
				break;
182
			case ShowDialog dialog:
183
				this.connectionAccess.openDialog(dialog.dialog(), activeScreen);
184
				break;
185
			case Custom custom:
186
				this.connectionAccess.sendCustomAction(custom.id(), custom.payload());
187
				this.minecraft.gui.setScreen(activeScreen);
188
				break;
189
			default:
190
				defaultHandleClickEvent(event, this.minecraft, activeScreen);
191
		}
192
	}
193
194
	public @Nullable Screen previousScreen() {
195
		return this.previousScreen;
196
	}
197
198
	protected static LayoutElement packControlsIntoColumns(final List<? extends LayoutElement> controls, final int columns) {
199
		GridLayout gridLayout = new GridLayout();
200
		gridLayout.defaultCellSetting().alignHorizontallyCenter();
201
		gridLayout.columnSpacing(2).rowSpacing(2);
202
		int count = controls.size();
203
		int lastFullRow = count / columns;
204
		int countInFullRows = lastFullRow * columns;
205
206
		for (int i = 0; i < countInFullRows; i++) {
207
			gridLayout.addChild(controls.get(i), i / columns, i % columns);
208
		}
209
210
		if (count != countInFullRows) {
211
			LinearLayout lastRow = LinearLayout.horizontal().spacing(2);
212
			lastRow.defaultCellSetting().alignHorizontallyCenter();
213
214
			for (int i = countInFullRows; i < count; i++) {
215
				lastRow.addChild(controls.get(i));
216
			}
217
218
			gridLayout.addChild(lastRow, lastFullRow, 0, 1, columns);
219
		}
220
221
		return gridLayout;
222
	}
223
224
	@Environment(EnvType.CLIENT)
225
	public static class WarningScreen extends ConfirmScreen {
226
		private final MutableObject<@Nullable Screen> returnScreen;
227
228
		public static Screen create(final Minecraft minecraft, final DialogConnectionAccess connectionAccess, final Screen returnScreen) {
229
			return new DialogScreen.WarningScreen(minecraft, connectionAccess, new MutableObject<>(returnScreen));
230
		}
231
232
		private WarningScreen(final Minecraft minecraft, final DialogConnectionAccess connectionAccess, final MutableObject<Screen> returnScreen) {
233
			super(
234
				disconnect -> {
235
					if (disconnect) {
236
						connectionAccess.disconnect(DialogScreen.DISCONNECT);
237
					} else {
238
						minecraft.gui.setScreen(returnScreen.get());
239
					}
240
				},
241
				Component.translatable("menu.custom_screen_info.title"),
242
				Component.translatable("menu.custom_screen_info.contents"),
243
				CommonComponents.disconnectButtonLabel(minecraft.isLocalServer()),
244
				CommonComponents.GUI_BACK
245
			);
246
			this.returnScreen = returnScreen;
247
		}
248
249
		public @Nullable Screen returnScreen() {
250
			return this.returnScreen.get();
251
		}
252
253
		public void updateReturnScreen(final @Nullable Screen newReturnScreen) {
254
			this.returnScreen.setValue(newReturnScreen);
255
		}
256
	}
257
}
258

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